Fix reverting to parent for renamed file
[TortoiseGit.git] / src / Git / TGitPath.h
blob0e38710ebdbb181bd140cea6e6bd4a82eb5c35bb
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
4 // Copyright (C) 2003-2008, 2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #pragma once
22 #include "gittype.h"
24 #define PARENT_MASK 0xFFFFFF
25 #define MERGE_MASK (0x1000000)
27 class CTGitPath
29 public:
30 CTGitPath(void);
31 ~CTGitPath(void);
32 CTGitPath(const CString& sUnknownPath);
33 int m_Stage;
34 int m_ParentNo;
35 public:
36 #pragma warning(push)
37 #pragma warning(disable: 4480) // nonstandard extension used: specifying underlying type for enum 'enum'
38 enum : unsigned int
40 LOGACTIONS_ADDED = 0x00000001,
41 LOGACTIONS_MODIFIED = 0x00000002,
42 LOGACTIONS_REPLACED = 0x00000004,
43 LOGACTIONS_DELETED = 0x00000008,
44 LOGACTIONS_UNMERGED = 0x00000010,
45 LOGACTIONS_CACHE = 0x00000020,
46 LOGACTIONS_COPY = 0x00000040,
47 LOGACTIONS_MERGED = 0x00000080,
48 LOGACTIONS_FORWORD = 0x00000100,
49 LOGACTIONS_ASSUMEVALID = 0x00000200,
50 LOGACTIONS_SKIPWORKTREE = 0x00000400,
51 LOGACTIONS_UNVER = 0x80000000,
52 LOGACTIONS_IGNORE = 0x40000000,
53 //LOGACTIONS_CONFLICT = 0x20000000,
55 // For log filter only
56 LOGACTIONS_HIDE = 0x20000000,
57 LOGACTIONS_GRAY = 0x10000000,
59 #pragma warning(pop)
61 CString m_StatAdd;
62 CString m_StatDel;
63 unsigned int m_Action;
64 bool m_Checked;
65 int ParserAction(BYTE action);
66 CString GetActionName();
67 static CString GetActionName(int action);
68 /**
69 * Set the path as an UTF8 string with forward slashes
71 void SetFromGit(const char* pPath);
72 void SetFromGit(const char* pPath, bool bIsDirectory);
73 void SetFromGit(const TCHAR* pPath, bool bIsDirectory);
74 void SetFromGit(const CString& sPath,CString *OldPath=NULL);
76 /**
77 * Set the path as UNICODE with backslashes
79 void SetFromWin(LPCTSTR pPath);
80 void SetFromWin(const CString& sPath);
81 void SetFromWin(LPCTSTR pPath, bool bIsDirectory);
82 void SetFromWin(const CString& sPath, bool bIsDirectory);
83 /**
84 * Set the path from an unknown source.
86 void SetFromUnknown(const CString& sPath);
87 /**
88 * Returns the path in Windows format, i.e. with backslashes
90 LPCTSTR GetWinPath() const;
91 /**
92 * Returns the path in Windows format, i.e. with backslashes
94 const CString& GetWinPathString() const;
95 /**
96 * Returns the path with forward slashes.
98 const CString& GetGitPathString() const;
100 const CString& GetGitOldPathString() const;
103 * Returns the path for showing in an UI.
105 * URL's are returned with forward slashes, unescaped if necessary
106 * Paths are returned with backward slashes
108 const CString& GetUIPathString() const;
110 * Returns true if the path points to a directory
112 bool IsDirectory() const;
114 CTGitPath GetSubPath(const CTGitPath &root);
117 * Returns the directory. If the path points to a directory, then the path
118 * is returned unchanged. If the path points to a file, the path to the
119 * parent directory is returned.
121 CTGitPath GetDirectory() const;
123 * Returns the the directory which contains the item the path refers to.
124 * If the path is a directory, then this returns the directory above it.
125 * If the path is to a file, then this returns the directory which contains the path
126 * parent directory is returned.
128 CTGitPath GetContainingDirectory() const;
130 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
132 CString GetRootPathString() const;
134 * Returns the filename part of the full path.
135 * \remark don't call this for directories.
137 CString GetFilename() const;
138 CString GetBaseFilename() const;
140 * Returns the item's name without the full path.
142 CString GetFileOrDirectoryName() const;
144 * Returns the item's name without the full path, unescaped if necessary.
146 CString GetUIFileOrDirectoryName() const;
148 * Returns the file extension, including the dot.
149 * \remark Returns an empty string for directories
151 CString GetFileExtension() const;
153 bool IsEmpty() const;
154 void Reset();
156 * Checks if two paths are equal. The slashes are taken care of.
158 bool IsEquivalentTo(const CTGitPath& rhs) const;
159 bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;
160 bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}
163 * Checks if \c possibleDescendant is a child of this path.
165 bool IsAncestorOf(const CTGitPath& possibleDescendant) const;
167 * Get a string representing the file path, optionally with a base
168 * section stripped off the front
169 * Returns a string with fwdslash paths
171 CString GetDisplayString(const CTGitPath* pOptionalBasePath = NULL) const;
173 * Compares two paths. Slash format is irrelevant.
175 static int Compare(const CTGitPath& left, const CTGitPath& right);
177 /** As PredLeftLessThanRight, but for checking if paths are equivalent
179 static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);
181 /** Checks if the left path is pointing to the same working copy path as the right.
182 * The same wc path means the paths are equivalent once all the admin dir path parts
183 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
184 * paths to crawl.
186 static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);
188 static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);
191 * appends a string to this path.
192 *\remark - missing slashes are not added - this is just a string concatenation, but with
193 * preservation of the proper caching behavior.
194 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
196 void AppendRawString(const CString& sAppend);
199 * appends a part of a path to this path.
200 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
203 void AppendPathString(const CString& sAppend);
206 * Get the file modification time - returns zero for files which don't exist
207 * Returns a FILETIME structure cast to an __int64, for easy comparisons
209 __int64 GetLastWriteTime() const;
212 * Get the file size. Returns zero for directories or files that don't exist.
214 __int64 GetFileSize() const;
216 bool IsReadOnly() const;
219 * Checks if the path really exists.
221 bool Exists() const;
224 * Deletes the file/folder
225 * \param bTrash if true, uses the Windows trash bin when deleting.
227 bool Delete(bool bTrash) const;
230 * Checks if a git admin directory is present. For files, the check
231 * is done in the same directory. For folders, it checks if the folder itself
232 * contains an admin directory.
234 bool HasAdminDir() const;
235 bool HasAdminDir(CString *ProjectTopDir) const;
236 bool HasSubmodules() const;
237 bool HasGitSVNDir() const;
238 bool IsBisectActive() const;
239 bool IsMergeActive() const;
240 bool HasStashDir() const;
241 bool HasRebaseApply() const;
243 bool IsWCRoot() const;
245 int GetAdminDirMask() const;
248 * Checks if the path point to or below a git admin directory (.Git).
250 bool IsAdminDir() const;
252 void SetCustomData(LPARAM lp) {m_customData = lp;}
253 LPARAM GetCustomData() const {return m_customData;}
256 * Checks if the path or URL is valid on Windows.
257 * A path is valid if conforms to the specs in the windows API.
258 * An URL is valid if the path checked out from it is valid
259 * on windows. That means an URL which is valid according to the WWW specs
260 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
261 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
262 * this function would return \c false for that URL).
264 bool IsValidOnWindows() const;
266 private:
267 // All these functions are const, and all the data
268 // is mutable, in order that the hidden caching operations
269 // can be carried out on a const CTGitPath object, which is what's
270 // likely to be passed between functions
271 // The public 'SetFromxxx' functions are not const, and so the proper
272 // const-correctness semantics are preserved
273 void SetFwdslashPath(const CString& sPath) const;
274 void SetBackslashPath(const CString& sPath) const;
275 void SetUTF8FwdslashPath(const CString& sPath) const;
276 void EnsureBackslashPathSet() const;
277 void EnsureFwdslashPathSet() const;
279 * Checks if two path strings are equal. No conversion of slashes is done!
280 * \remark for slash-independent comparison, use IsEquivalentTo()
282 static bool ArePathStringsEqual(const CString& sP1, const CString& sP2);
283 static bool ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2);
286 * Adds the required trailing slash to local root paths such as 'C:'
288 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
290 void UpdateAttributes() const;
294 private:
295 mutable CString m_sBackslashPath;
296 mutable CString m_sLongBackslashPath;
297 mutable CString m_sFwdslashPath;
298 mutable CString m_sUIPath;
299 mutable CStringA m_sUTF8FwdslashPath;
300 mutable CStringA m_sUTF8FwdslashPathEscaped;
301 mutable CString m_sProjectRoot;
303 //used for rename case
304 mutable CString m_sOldFwdslashPath;
306 // Have we yet determined if this is a directory or not?
307 mutable bool m_bDirectoryKnown;
308 mutable bool m_bIsDirectory;
309 mutable bool m_bLastWriteTimeKnown;
310 mutable bool m_bURLKnown;
311 mutable __int64 m_lastWriteTime;
312 mutable __int64 m_fileSize;
313 mutable bool m_bIsReadOnly;
314 mutable bool m_bHasAdminDirKnown;
315 mutable bool m_bHasAdminDir;
316 mutable bool m_bIsValidOnWindowsKnown;
317 mutable bool m_bIsValidOnWindows;
318 mutable bool m_bIsAdminDirKnown;
319 mutable bool m_bIsAdminDir;
320 mutable bool m_bIsWCRootKnown;
321 mutable bool m_bIsWCRoot;
322 mutable bool m_bExists;
323 mutable bool m_bExistsKnown;
324 mutable LPARAM m_customData;
325 mutable bool m_bIsSpecialDirectoryKnown;
326 mutable bool m_bIsSpecialDirectory;
328 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
331 * Compares two paths and return true if left is earlier in sort order than right
332 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
334 bool operator<(const CTGitPath& left, const CTGitPath& right);
337 //////////////////////////////////////////////////////////////////////////
340 * \ingroup Utils
341 * This class represents a list of paths
343 class CTGitPathList
345 public:
346 CTGitPathList();
347 // A constructor which allows a path list to be easily built with one initial entry in
348 explicit CTGitPathList(const CTGitPath& firstEntry);
349 int m_Action;
351 public:
352 void AddPath(const CTGitPath& newPath);
353 bool LoadFromFile(const CTGitPath& filename);
354 bool WriteToFile(const CString& sFilename, bool bANSI = false) const;
355 CTGitPath * LookForGitPath(CString path);
356 int ParserFromLog(BYTE_VECTOR &log, bool parseDeletes = false);
357 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
358 int FillUnRev(unsigned int Action, CTGitPathList *list = nullptr, CString *err = nullptr);
359 int FillBasedOnIndexFlags(unsigned short flag, CTGitPathList* list = nullptr);
360 int GetAction();
362 * Load from the path argument string, when the 'path' parameter is used
363 * This is a list of paths, with '*' between them
365 void LoadFromAsteriskSeparatedString(const CString& sPathString);
366 CString CreateAsteriskSeparatedString() const;
368 int GetCount() const;
369 bool IsEmpty() const;
370 void Clear();
371 const CTGitPath& operator[](INT_PTR index) const;
372 bool AreAllPathsFiles() const;
373 bool AreAllPathsFilesInOneDirectory() const;
374 CTGitPath GetCommonDirectory() const;
375 CTGitPath GetCommonRoot() const;
376 void SortByPathname(bool bReverse = false);
378 * Delete all the files in the list, then clear the list.
379 * \param bTrash if true, the items are deleted using the Windows trash bin
381 void DeleteAllFiles(bool bTrash, bool bFilesOnly = true);
382 static bool DeleteViaShell(LPCTSTR path, bool useTrashbin);
383 /** Remove duplicate entries from the list (sorts the list as a side-effect */
384 void RemoveDuplicates();
385 /** Removes all paths which are on or in a git admin directory */
386 void RemoveAdminPaths();
387 void RemovePath(const CTGitPath& path);
388 void RemoveItem(CTGitPath &path);
390 * Removes all child items and leaves only the top folders. Useful if you
391 * create the list to remove them (i.e. if you remove a parent folder, the
392 * child files and folders don't have to be deleted anymore)
394 void RemoveChildren();
396 /** Checks if two CTGitPathLists are the same */
397 bool IsEqual(const CTGitPathList& list);
399 typedef std::vector<CTGitPath> PathVector;
400 PathVector m_paths;
401 // If the list contains just files in one directory, then
402 // this contains the directory name
403 mutable CTGitPath m_commonBaseDirectory;