Fixed issue #151: need "stash pop" function and clear HasAdminDir HasGitSVN HashSubmo...
[TortoiseGit.git] / src / Git / TGitPath.h
blob07528fc20bcac58960cee46c8618e9a26ee93877
1 #pragma once
2 #include "gittype.h"
4 class CTGitPath
6 public:
7 CTGitPath(void);
8 ~CTGitPath(void);
9 CTGitPath(const CString& sUnknownPath);
10 int m_Stage;
11 public:
12 enum
14 LOGACTIONS_ADDED = 0x00000001,
15 LOGACTIONS_MODIFIED = 0x00000002,
16 LOGACTIONS_REPLACED = 0x00000004,
17 LOGACTIONS_DELETED = 0x00000008,
18 LOGACTIONS_UNMERGED = 0x00000010,
19 LOGACTIONS_CACHE = 0x00000020,
20 LOGACTIONS_COPY = 0x00000040,
21 LOGACTIONS_MERGED = 0x00000080,
22 LOGACTIONS_FORWORD = 0x00000100,
23 LOGACTIONS_UNVER = 0x80000000,
24 LOGACTIONS_IGNORE = 0x40000000,
25 //LOGACTIONS_CONFLICT = 0x20000000,
27 // For log filter only
28 LOGACTIONS_HIDE = 0x20000000,
29 LOGACTIONS_GRAY = 0x10000000,
31 // For Rebase only
32 LOGACTIONS_REBASE_CURRENT = 0x08000000,
33 LOGACTIONS_REBASE_PICK = 0x04000000,
34 LOGACTIONS_REBASE_SQUASH = 0x02000000,
35 LOGACTIONS_REBASE_EDIT = 0x01000000,
36 LOGACTIONS_REBASE_DONE = 0x00800000,
37 LOGACTIONS_REBASE_SKIP = 0x00400000,
38 LOGACTIONS_REBASE_MASK = 0x0FC00000,
39 LOGACTIONS_REBASE_MODE_MASK=0x07C00000,
43 CString m_StatAdd;
44 CString m_StatDel;
45 int m_Action;
46 bool m_Checked;
47 int ParserAction(BYTE action);
48 CString GetActionName();
49 static CString GetActionName(int action);
50 /**
51 * Set the path as an UTF8 string with forward slashes
53 void SetFromGit(const char* pPath);
54 void SetFromGit(const char* pPath, bool bIsDirectory);
55 void SetFromGit(const TCHAR* pPath, bool bIsDirectory);
56 void SetFromGit(const CString& sPath,CString *OldPath=NULL);
58 /**
59 * Set the path as UNICODE with backslashes
61 void SetFromWin(LPCTSTR pPath);
62 void SetFromWin(const CString& sPath);
63 void SetFromWin(const CString& sPath, bool bIsDirectory);
64 /**
65 * Set the path from an unknown source.
67 void SetFromUnknown(const CString& sPath);
68 /**
69 * Returns the path in Windows format, i.e. with backslashes
71 LPCTSTR GetWinPath() const;
72 /**
73 * Returns the path in Windows format, i.e. with backslashes
75 const CString& GetWinPathString() const;
76 /**
77 * Returns the path with forward slashes.
79 const CString& GetGitPathString() const;
81 const CString& GetGitOldPathString() const;
82 /**
83 * Returns the path completely prepared to be fed the the Git APIs
84 * It will be in UTF8, with URLs escaped, if necessary
86 // const char* GetGitApiPath(apr_pool_t *pool) const;
87 /**
88 * Returns the path for showing in an UI.
90 * URL's are returned with forward slashes, unescaped if necessary
91 * Paths are returned with backward slashes
93 const CString& GetUIPathString() const;
94 /**
95 * Checks if the path is an URL.
97 bool IsUrl() const;
98 /**
99 * Returns true if the path points to a directory
101 bool IsDirectory() const;
103 CTGitPath GetSubPath(CTGitPath &root);
106 * Returns the directory. If the path points to a directory, then the path
107 * is returned unchanged. If the path points to a file, the path to the
108 * parent directory is returned.
110 CTGitPath GetDirectory() const;
112 * Returns the the directory which contains the item the path refers to.
113 * If the path is a directory, then this returns the directory above it.
114 * If the path is to a file, then this returns the directory which contains the path
115 * parent directory is returned.
117 CTGitPath GetContainingDirectory() const;
119 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
121 CString GetRootPathString() const;
123 * Returns the filename part of the full path.
124 * \remark don't call this for directories.
126 CString GetFilename() const;
127 CString GetBaseFilename() const;
129 * Returns the item's name without the full path.
131 CString GetFileOrDirectoryName() const;
133 * Returns the item's name without the full path, unescaped if necessary.
135 CString GetUIFileOrDirectoryName() const;
137 * Returns the file extension, including the dot.
138 * \remark Returns an empty string for directories
140 CString GetFileExtension() const;
142 bool IsEmpty() const;
143 void Reset();
145 * Checks if two paths are equal. The slashes are taken care of.
147 bool IsEquivalentTo(const CTGitPath& rhs) const;
148 bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;
149 bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}
152 * Checks if \c possibleDescendant is a child of this path.
154 bool IsAncestorOf(const CTGitPath& possibleDescendant) const;
156 * Get a string representing the file path, optionally with a base
157 * section stripped off the front
158 * Returns a string with fwdslash paths
160 CString GetDisplayString(const CTGitPath* pOptionalBasePath = NULL) const;
162 * Compares two paths. Slash format is irrelevant.
164 static int Compare(const CTGitPath& left, const CTGitPath& right);
166 /** As PredLeftLessThanRight, but for checking if paths are equivalent
168 static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);
170 /** Checks if the left path is pointing to the same working copy path as the right.
171 * The same wc path means the paths are equivalent once all the admin dir path parts
172 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
173 * paths to crawl.
175 static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);
177 static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);
180 * appends a string to this path.
181 *\remark - missing slashes are not added - this is just a string concatenation, but with
182 * preservation of the proper caching behavior.
183 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
185 void AppendRawString(const CString& sAppend);
188 * appends a part of a path to this path.
189 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
192 void AppendPathString(const CString& sAppend);
195 * Get the file modification time - returns zero for files which don't exist
196 * Returns a FILETIME structure cast to an __int64, for easy comparisons
198 __int64 GetLastWriteTime() const;
200 bool IsReadOnly() const;
203 * Checks if the path really exists.
205 bool Exists() const;
208 * Deletes the file/folder
209 * \param bTrash if true, uses the Windows trash bin when deleting.
211 bool Delete(bool bTrash) const;
214 * Checks if a Subversion admin directory is present. For files, the check
215 * is done in the same directory. For folders, it checks if the folder itself
216 * contains an admin directory.
218 bool HasAdminDir() const;
219 bool HasAdminDir(CString *ProjectTopDir) const;
220 bool HasSubmodules() const;
221 bool HasGitSVNDir() const;
222 bool HasStashDir() const;
224 int GetAdminDirMask() const;
227 * Checks if the path point to or below a Subversion admin directory (.Git).
229 bool IsAdminDir() const;
231 void SetCustomData(LPARAM lp) {m_customData = lp;}
232 LPARAM GetCustomData() {return m_customData;}
235 * Checks if the path or URL is valid on Windows.
236 * A path is valid if conforms to the specs in the windows API.
237 * An URL is valid if the path checked out from it is valid
238 * on windows. That means an URL which is valid according to the WWW specs
239 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
240 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
241 * this function would return \c false for that URL).
243 bool IsValidOnWindows() const;
246 * Checks to see if the path or URL represents one of the special directories
247 * (branches, tags, or trunk).
249 bool IsSpecialDirectory() const;
250 private:
251 // All these functions are const, and all the data
252 // is mutable, in order that the hidden caching operations
253 // can be carried out on a const CTGitPath object, which is what's
254 // likely to be passed between functions
255 // The public 'SetFromxxx' functions are not const, and so the proper
256 // const-correctness semantics are preserved
257 void SetFwdslashPath(const CString& sPath) const;
258 void SetBackslashPath(const CString& sPath) const;
259 void SetUTF8FwdslashPath(const CString& sPath) const;
260 void EnsureBackslashPathSet() const;
261 void EnsureFwdslashPathSet() const;
263 * Checks if two path strings are equal. No conversion of slashes is done!
264 * \remark for slash-independent comparison, use IsEquivalentTo()
266 static bool ArePathStringsEqual(const CString& sP1, const CString& sP2);
267 static bool ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2);
270 * Adds the required trailing slash to local root paths such as 'C:'
272 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
274 void UpdateAttributes() const;
278 private:
279 mutable CString m_sBackslashPath;
280 mutable CString m_sFwdslashPath;
281 mutable CString m_sUIPath;
282 mutable CStringA m_sUTF8FwdslashPath;
283 mutable CStringA m_sUTF8FwdslashPathEscaped;
284 mutable CString m_sProjectRoot;
286 //used for rename case
287 mutable CString m_sOldBackslashPath;
288 mutable CString m_sOldFwdslashPath;
290 // Have we yet determined if this is a directory or not?
291 mutable bool m_bDirectoryKnown;
292 mutable bool m_bIsDirectory;
293 mutable bool m_bLastWriteTimeKnown;
294 mutable bool m_bURLKnown;
295 mutable bool m_bIsURL;
296 mutable __int64 m_lastWriteTime;
297 mutable bool m_bIsReadOnly;
298 mutable bool m_bHasAdminDirKnown;
299 mutable bool m_bHasAdminDir;
300 mutable bool m_bIsValidOnWindowsKnown;
301 mutable bool m_bIsValidOnWindows;
302 mutable bool m_bIsAdminDirKnown;
303 mutable bool m_bIsAdminDir;
304 mutable bool m_bExists;
305 mutable bool m_bExistsKnown;
306 mutable LPARAM m_customData;
307 mutable bool m_bIsSpecialDirectoryKnown;
308 mutable bool m_bIsSpecialDirectory;
310 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
313 * Compares two paths and return true if left is earlier in sort order than right
314 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
316 bool operator<(const CTGitPath& left, const CTGitPath& right);
319 //////////////////////////////////////////////////////////////////////////
322 * \ingroup Utils
323 * This class represents a list of paths
325 class CTGitPathList
327 public:
328 CTGitPathList();
329 // A constructor which allows a path list to be easily built with one initial entry in
330 explicit CTGitPathList(const CTGitPath& firstEntry);
331 int m_Action;
333 public:
334 void AddPath(const CTGitPath& newPath);
335 bool LoadFromFile(const CTGitPath& filename);
336 bool WriteToFile(const CString& sFilename, bool bANSI = false) const;
337 CTGitPath * LookForGitPath(CString path);
338 int ParserFromLog(BYTE_VECTOR &log);
339 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
340 int FillUnRev(int Action,CTGitPathList *list=NULL);
341 int GetAction();
343 * Load from the path argument string, when the 'path' parameter is used
344 * This is a list of paths, with '*' between them
346 void LoadFromAsteriskSeparatedString(const CString& sPathString);
347 CString CreateAsteriskSeparatedString() const;
349 int GetCount() const;
350 void Clear();
351 const CTGitPath& operator[](INT_PTR index) const;
352 bool AreAllPathsFiles() const;
353 bool AreAllPathsFilesInOneDirectory() const;
354 CTGitPath GetCommonDirectory() const;
355 CTGitPath GetCommonRoot() const;
356 void SortByPathname(bool bReverse = false);
357 /**
358 * Delete all the files in the list, then clear the list.
359 * \param bTrash if true, the items are deleted using the Windows trash bin
361 void DeleteAllFiles(bool bTrash);
362 /** Remove duplicate entries from the list (sorts the list as a side-effect */
363 void RemoveDuplicates();
364 /** Removes all paths which are on or in a Subversion admin directory */
365 void RemoveAdminPaths();
366 void RemovePath(const CTGitPath& path);
367 void RemoveItem(CTGitPath &path);
368 /**
369 * Removes all child items and leaves only the top folders. Useful if you
370 * create the list to remove them (i.e. if you remove a parent folder, the
371 * child files and folders don't have to be deleted anymore)
373 void RemoveChildren();
375 /** Checks if two CTGitPathLists are the same */
376 bool IsEqual(const CTGitPathList& list);
378 /** Convert into the Git API parameter format */
379 // apr_array_header_t * MakePathArray (apr_pool_t *pool) const;
381 private:
382 typedef std::vector<CTGitPath> PathVector;
383 PathVector m_paths;
384 // If the list contains just files in one directory, then
385 // this contains the directory name
386 mutable CTGitPath m_commonBaseDirectory;