allow to detect active bisect session
[TortoiseGit.git] / src / Git / TGitPath.h
blob9997ae50a66a98bcdbd71d72c41398ca26006f97
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include "gittype.h"
23 #define PARENT_MASK 0xFFFFFF
24 #define MERGE_MASK (0x1000000)
26 class CTGitPath
28 public:
29 CTGitPath(void);
30 ~CTGitPath(void);
31 CTGitPath(const CString& sUnknownPath);
32 int m_Stage;
33 int m_ParentNo;
34 public:
35 enum : unsigned int
37 LOGACTIONS_ADDED = 0x00000001,
38 LOGACTIONS_MODIFIED = 0x00000002,
39 LOGACTIONS_REPLACED = 0x00000004,
40 LOGACTIONS_DELETED = 0x00000008,
41 LOGACTIONS_UNMERGED = 0x00000010,
42 LOGACTIONS_CACHE = 0x00000020,
43 LOGACTIONS_COPY = 0x00000040,
44 LOGACTIONS_MERGED = 0x00000080,
45 LOGACTIONS_FORWORD = 0x00000100,
46 LOGACTIONS_UNVER = 0x80000000,
47 LOGACTIONS_IGNORE = 0x40000000,
48 //LOGACTIONS_CONFLICT = 0x20000000,
50 // For log filter only
51 LOGACTIONS_HIDE = 0x20000000,
52 LOGACTIONS_GRAY = 0x10000000,
54 // For Rebase only
55 LOGACTIONS_REBASE_CURRENT = 0x08000000,
56 LOGACTIONS_REBASE_PICK = 0x04000000,
57 LOGACTIONS_REBASE_SQUASH = 0x02000000,
58 LOGACTIONS_REBASE_EDIT = 0x01000000,
59 LOGACTIONS_REBASE_DONE = 0x00800000,
60 LOGACTIONS_REBASE_SKIP = 0x00400000,
61 LOGACTIONS_REBASE_MASK = 0x0FC00000,
62 LOGACTIONS_REBASE_MODE_MASK=0x07C00000,
66 CString m_StatAdd;
67 CString m_StatDel;
68 unsigned int m_Action;
69 bool m_Checked;
70 int ParserAction(BYTE action);
71 CString GetActionName();
72 static CString GetActionName(int action);
73 /**
74 * Set the path as an UTF8 string with forward slashes
76 void SetFromGit(const char* pPath);
77 void SetFromGit(const char* pPath, bool bIsDirectory);
78 void SetFromGit(const TCHAR* pPath, bool bIsDirectory);
79 void SetFromGit(const CString& sPath,CString *OldPath=NULL);
81 /**
82 * Set the path as UNICODE with backslashes
84 void SetFromWin(LPCTSTR pPath);
85 void SetFromWin(const CString& sPath);
86 void SetFromWin(const CString& sPath, bool bIsDirectory);
87 /**
88 * Set the path from an unknown source.
90 void SetFromUnknown(const CString& sPath);
91 /**
92 * Returns the path in Windows format, i.e. with backslashes
94 LPCTSTR GetWinPath() const;
95 /**
96 * Returns the path in Windows format, i.e. with backslashes
98 const CString& GetWinPathString() const;
99 /**
100 * Returns the path with forward slashes.
102 const CString& GetGitPathString() const;
104 const CString& GetGitOldPathString() const;
106 * Returns the path completely prepared to be fed the the Git APIs
107 * It will be in UTF8, with URLs escaped, if necessary
109 // const char* GetGitApiPath(apr_pool_t *pool) const;
111 * Returns the path for showing in an UI.
113 * URL's are returned with forward slashes, unescaped if necessary
114 * Paths are returned with backward slashes
116 const CString& GetUIPathString() const;
118 * Checks if the path is an URL.
120 bool IsUrl() const;
122 * Returns true if the path points to a directory
124 bool IsDirectory() const;
126 CTGitPath GetSubPath(const CTGitPath &root);
129 * Returns the directory. If the path points to a directory, then the path
130 * is returned unchanged. If the path points to a file, the path to the
131 * parent directory is returned.
133 CTGitPath GetDirectory() const;
135 * Returns the the directory which contains the item the path refers to.
136 * If the path is a directory, then this returns the directory above it.
137 * If the path is to a file, then this returns the directory which contains the path
138 * parent directory is returned.
140 CTGitPath GetContainingDirectory() const;
142 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
144 CString GetRootPathString() const;
146 * Returns the filename part of the full path.
147 * \remark don't call this for directories.
149 CString GetFilename() const;
150 CString GetBaseFilename() const;
152 * Returns the item's name without the full path.
154 CString GetFileOrDirectoryName() const;
156 * Returns the item's name without the full path, unescaped if necessary.
158 CString GetUIFileOrDirectoryName() const;
160 * Returns the file extension, including the dot.
161 * \remark Returns an empty string for directories
163 CString GetFileExtension() const;
165 bool IsEmpty() const;
166 void Reset();
168 * Checks if two paths are equal. The slashes are taken care of.
170 bool IsEquivalentTo(const CTGitPath& rhs) const;
171 bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;
172 bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}
175 * Checks if \c possibleDescendant is a child of this path.
177 bool IsAncestorOf(const CTGitPath& possibleDescendant) const;
179 * Get a string representing the file path, optionally with a base
180 * section stripped off the front
181 * Returns a string with fwdslash paths
183 CString GetDisplayString(const CTGitPath* pOptionalBasePath = NULL) const;
185 * Compares two paths. Slash format is irrelevant.
187 static int Compare(const CTGitPath& left, const CTGitPath& right);
189 /** As PredLeftLessThanRight, but for checking if paths are equivalent
191 static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);
193 /** Checks if the left path is pointing to the same working copy path as the right.
194 * The same wc path means the paths are equivalent once all the admin dir path parts
195 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
196 * paths to crawl.
198 static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);
200 static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);
203 * appends a string to this path.
204 *\remark - missing slashes are not added - this is just a string concatenation, but with
205 * preservation of the proper caching behavior.
206 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
208 void AppendRawString(const CString& sAppend);
211 * appends a part of a path to this path.
212 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
215 void AppendPathString(const CString& sAppend);
218 * Get the file modification time - returns zero for files which don't exist
219 * Returns a FILETIME structure cast to an __int64, for easy comparisons
221 __int64 GetLastWriteTime() const;
223 bool IsReadOnly() const;
226 * Checks if the path really exists.
228 bool Exists() const;
231 * Deletes the file/folder
232 * \param bTrash if true, uses the Windows trash bin when deleting.
234 bool Delete(bool bTrash) const;
237 * Checks if a Subversion admin directory is present. For files, the check
238 * is done in the same directory. For folders, it checks if the folder itself
239 * contains an admin directory.
241 bool HasAdminDir() const;
242 bool HasAdminDir(CString *ProjectTopDir) const;
243 bool HasSubmodules() const;
244 bool HasGitSVNDir() const;
245 bool IsBisectActive() const;
246 bool HasStashDir() const;
247 bool HasRebaseApply() const;
249 bool IsWCRoot() const;
251 int GetAdminDirMask() const;
254 * Checks if the path point to or below a Subversion admin directory (.Git).
256 bool IsAdminDir() const;
258 void SetCustomData(LPARAM lp) {m_customData = lp;}
259 LPARAM GetCustomData() {return m_customData;}
262 * Checks if the path or URL is valid on Windows.
263 * A path is valid if conforms to the specs in the windows API.
264 * An URL is valid if the path checked out from it is valid
265 * on windows. That means an URL which is valid according to the WWW specs
266 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
267 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
268 * this function would return \c false for that URL).
270 bool IsValidOnWindows() const;
273 * Checks to see if the path or URL represents one of the special directories
274 * (branches, tags, or trunk).
276 bool IsSpecialDirectory() const;
277 private:
278 // All these functions are const, and all the data
279 // is mutable, in order that the hidden caching operations
280 // can be carried out on a const CTGitPath object, which is what's
281 // likely to be passed between functions
282 // The public 'SetFromxxx' functions are not const, and so the proper
283 // const-correctness semantics are preserved
284 void SetFwdslashPath(const CString& sPath) const;
285 void SetBackslashPath(const CString& sPath) const;
286 void SetUTF8FwdslashPath(const CString& sPath) const;
287 void EnsureBackslashPathSet() const;
288 void EnsureFwdslashPathSet() const;
290 * Checks if two path strings are equal. No conversion of slashes is done!
291 * \remark for slash-independent comparison, use IsEquivalentTo()
293 static bool ArePathStringsEqual(const CString& sP1, const CString& sP2);
294 static bool ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2);
297 * Adds the required trailing slash to local root paths such as 'C:'
299 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
301 void UpdateAttributes() const;
305 private:
306 mutable CString m_sBackslashPath;
307 mutable CString m_sFwdslashPath;
308 mutable CString m_sUIPath;
309 mutable CStringA m_sUTF8FwdslashPath;
310 mutable CStringA m_sUTF8FwdslashPathEscaped;
311 mutable CString m_sProjectRoot;
313 //used for rename case
314 mutable CString m_sOldBackslashPath;
315 mutable CString m_sOldFwdslashPath;
317 // Have we yet determined if this is a directory or not?
318 mutable bool m_bDirectoryKnown;
319 mutable bool m_bIsDirectory;
320 mutable bool m_bLastWriteTimeKnown;
321 mutable bool m_bURLKnown;
322 mutable bool m_bIsURL;
323 mutable __int64 m_lastWriteTime;
324 mutable bool m_bIsReadOnly;
325 mutable bool m_bHasAdminDirKnown;
326 mutable bool m_bHasAdminDir;
327 mutable bool m_bIsValidOnWindowsKnown;
328 mutable bool m_bIsValidOnWindows;
329 mutable bool m_bIsAdminDirKnown;
330 mutable bool m_bIsAdminDir;
331 mutable bool m_bIsWCRootKnown;
332 mutable bool m_bIsWCRoot;
333 mutable bool m_bExists;
334 mutable bool m_bExistsKnown;
335 mutable LPARAM m_customData;
336 mutable bool m_bIsSpecialDirectoryKnown;
337 mutable bool m_bIsSpecialDirectory;
339 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
342 * Compares two paths and return true if left is earlier in sort order than right
343 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
345 bool operator<(const CTGitPath& left, const CTGitPath& right);
348 //////////////////////////////////////////////////////////////////////////
351 * \ingroup Utils
352 * This class represents a list of paths
354 class CTGitPathList
356 public:
357 CTGitPathList();
358 // A constructor which allows a path list to be easily built with one initial entry in
359 explicit CTGitPathList(const CTGitPath& firstEntry);
360 int m_Action;
362 public:
363 void AddPath(const CTGitPath& newPath);
364 bool LoadFromFile(const CTGitPath& filename);
365 bool WriteToFile(const CString& sFilename, bool bANSI = false) const;
366 CTGitPath * LookForGitPath(CString path);
367 int ParserFromLog(BYTE_VECTOR &log, bool parseDeletes = false);
368 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
369 int FillUnRev(unsigned int Action,CTGitPathList *list=NULL);
370 int GetAction();
372 * Load from the path argument string, when the 'path' parameter is used
373 * This is a list of paths, with '*' between them
375 void LoadFromAsteriskSeparatedString(const CString& sPathString);
376 CString CreateAsteriskSeparatedString() const;
378 int GetCount() const;
379 void Clear();
380 const CTGitPath& operator[](INT_PTR index) const;
381 bool AreAllPathsFiles() const;
382 bool AreAllPathsFilesInOneDirectory() const;
383 CTGitPath GetCommonDirectory() const;
384 CTGitPath GetCommonRoot() const;
385 void SortByPathname(bool bReverse = false);
387 * Delete all the files in the list, then clear the list.
388 * \param bTrash if true, the items are deleted using the Windows trash bin
390 void DeleteAllFiles(bool bTrash);
391 /** Remove duplicate entries from the list (sorts the list as a side-effect */
392 void RemoveDuplicates();
393 /** Removes all paths which are on or in a Subversion admin directory */
394 void RemoveAdminPaths();
395 void RemovePath(const CTGitPath& path);
396 void RemoveItem(CTGitPath &path);
398 * Removes all child items and leaves only the top folders. Useful if you
399 * create the list to remove them (i.e. if you remove a parent folder, the
400 * child files and folders don't have to be deleted anymore)
402 void RemoveChildren();
404 /** Checks if two CTGitPathLists are the same */
405 bool IsEqual(const CTGitPathList& list);
407 /** Convert into the Git API parameter format */
408 // apr_array_header_t * MakePathArray (apr_pool_t *pool) const;
410 typedef std::vector<CTGitPath> PathVector;
411 PathVector m_paths;
412 // If the list contains just files in one directory, then
413 // this contains the directory name
414 mutable CTGitPath m_commonBaseDirectory;