Performance optimization
[TortoiseGit.git] / src / Git / TGitPath.h
blob185d3480c8d2277c7409fa16e1068e047fadefda
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - 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_COPY = 0x00000040,
46 LOGACTIONS_MERGED = 0x00000080,
47 LOGACTIONS_ASSUMEVALID = 0x00000200,
48 LOGACTIONS_SKIPWORKTREE = 0x00000400,
49 LOGACTIONS_MISSING = 0x00001000,
50 LOGACTIONS_UNVER = 0x80000000,
51 LOGACTIONS_IGNORE = 0x40000000,
53 // For log filter only
54 LOGACTIONS_HIDE = 0x20000000,
55 LOGACTIONS_GRAY = 0x10000000,
57 #pragma warning(pop)
59 CString m_StatAdd;
60 CString m_StatDel;
61 unsigned int m_Action;
62 bool m_Checked;
63 int ParserAction(BYTE action);
64 int ParserAction(git_delta_t action);
65 CString GetActionName();
66 static CString GetActionName(int action);
67 /**
68 * Set the path as an UTF8 string with forward slashes
70 void SetFromGit(const char* pPath);
71 void SetFromGit(const char* pPath, bool bIsDirectory);
72 void SetFromGit(const TCHAR* pPath, bool bIsDirectory);
73 void SetFromGit(const CString& sPath,CString *OldPath=NULL);
75 /**
76 * Set the path as UNICODE with backslashes
78 void SetFromWin(LPCTSTR pPath);
79 void SetFromWin(const CString& sPath);
80 void SetFromWin(LPCTSTR pPath, bool bIsDirectory);
81 void SetFromWin(const CString& sPath, bool bIsDirectory);
82 /**
83 * Set the path from an unknown source.
85 void SetFromUnknown(const CString& sPath);
86 /**
87 * Returns the path in Windows format, i.e. with backslashes
89 LPCTSTR GetWinPath() const;
90 /**
91 * Returns the path in Windows format, i.e. with backslashes
93 const CString& GetWinPathString() const;
94 /**
95 * Returns the path with forward slashes.
97 const CString& GetGitPathString() const;
99 const CString& GetGitOldPathString() const;
102 * Returns the path for showing in an UI.
104 * URL's are returned with forward slashes, unescaped if necessary
105 * Paths are returned with backward slashes
107 const CString& GetUIPathString() const;
109 * Returns true if the path points to a directory
111 bool IsDirectory() const;
113 CTGitPath GetSubPath(const CTGitPath &root);
116 * Returns the directory. If the path points to a directory, then the path
117 * is returned unchanged. If the path points to a file, the path to the
118 * parent directory is returned.
120 CTGitPath GetDirectory() const;
122 * Returns the the directory which contains the item the path refers to.
123 * If the path is a directory, then this returns the directory above it.
124 * If the path is to a file, then this returns the directory which contains the path
125 * parent directory is returned.
127 CTGitPath GetContainingDirectory() const;
129 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
131 CString GetRootPathString() const;
133 * Returns the filename part of the full path.
134 * \remark don't call this for directories.
136 CString GetFilename() const;
137 CString GetBaseFilename() const;
139 * Returns the item's name without the full path.
141 CString GetFileOrDirectoryName() const;
143 * Returns the item's name without the full path, unescaped if necessary.
145 CString GetUIFileOrDirectoryName() const;
147 * Returns the file extension, including the dot.
148 * \remark Returns an empty string for directories
150 CString GetFileExtension() const;
152 bool IsEmpty() const;
153 void Reset();
155 * Checks if two paths are equal. The slashes are taken care of.
157 bool IsEquivalentTo(const CTGitPath& rhs) const;
158 bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;
159 bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}
162 * Checks if \c possibleDescendant is a child of this path.
164 bool IsAncestorOf(const CTGitPath& possibleDescendant) const;
166 * Get a string representing the file path, optionally with a base
167 * section stripped off the front
168 * Returns a string with fwdslash paths
170 CString GetDisplayString(const CTGitPath* pOptionalBasePath = NULL) const;
172 * Compares two paths. Slash format is irrelevant.
174 static int Compare(const CTGitPath& left, const CTGitPath& right);
176 /** As PredLeftLessThanRight, but for checking if paths are equivalent
178 static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);
180 /** Checks if the left path is pointing to the same working copy path as the right.
181 * The same wc path means the paths are equivalent once all the admin dir path parts
182 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
183 * paths to crawl.
185 static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);
187 static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);
190 * appends a string to this path.
191 *\remark - missing slashes are not added - this is just a string concatenation, but with
192 * preservation of the proper caching behavior.
193 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
195 void AppendRawString(const CString& sAppend);
198 * appends a part of a path to this path.
199 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
202 void AppendPathString(const CString& sAppend);
205 * Get the file modification time - returns zero for files which don't exist
206 * Returns a FILETIME structure cast to an __int64, for easy comparisons
208 __int64 GetLastWriteTime() const;
211 * Get the file size. Returns zero for directories or files that don't exist.
213 __int64 GetFileSize() const;
215 bool IsReadOnly() const;
218 * Checks if the path really exists.
220 bool Exists() const;
223 * Deletes the file/folder
224 * \param bTrash if true, uses the Windows trash bin when deleting.
226 bool Delete(bool bTrash, bool bShowErrorUI) const;
229 * Checks if a git admin directory is present. For files, the check
230 * is done in the same directory. For folders, it checks if the folder itself
231 * contains an admin directory.
233 bool HasAdminDir() const;
234 bool HasAdminDir(CString *ProjectTopDir) const;
235 bool HasSubmodules() const;
236 bool HasGitSVNDir() const;
237 bool IsBisectActive() const;
238 bool IsMergeActive() const;
239 bool HasStashDir() const;
240 bool HasRebaseApply() const;
242 bool IsWCRoot() const;
244 int GetAdminDirMask() const;
247 * Checks if the path point to or below a git admin directory (.Git).
249 bool IsAdminDir() const;
251 void SetCustomData(LPARAM lp) {m_customData = lp;}
252 LPARAM GetCustomData() const {return m_customData;}
255 * Checks if the path or URL is valid on Windows.
256 * A path is valid if conforms to the specs in the windows API.
257 * An URL is valid if the path checked out from it is valid
258 * on windows. That means an URL which is valid according to the WWW specs
259 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
260 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
261 * this function would return \c false for that URL).
263 bool IsValidOnWindows() const;
265 private:
266 // All these functions are const, and all the data
267 // is mutable, in order that the hidden caching operations
268 // can be carried out on a const CTGitPath object, which is what's
269 // likely to be passed between functions
270 // The public 'SetFromxxx' functions are not const, and so the proper
271 // const-correctness semantics are preserved
272 void SetFwdslashPath(const CString& sPath) const;
273 void SetBackslashPath(const CString& sPath) const;
274 void SetUTF8FwdslashPath(const CString& sPath) const;
275 void EnsureBackslashPathSet() const;
276 void EnsureFwdslashPathSet() const;
278 * Checks if two path strings are equal. No conversion of slashes is done!
279 * \remark for slash-independent comparison, use IsEquivalentTo()
281 static bool ArePathStringsEqual(const CString& sP1, const CString& sP2);
282 static bool ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2);
285 * Adds the required trailing slash to local root paths such as 'C:'
287 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
289 void UpdateAttributes() const;
293 private:
294 mutable CString m_sBackslashPath;
295 mutable CString m_sLongBackslashPath;
296 mutable CString m_sFwdslashPath;
297 mutable CString m_sUIPath;
298 mutable CStringA m_sUTF8FwdslashPath;
299 mutable CStringA m_sUTF8FwdslashPathEscaped;
300 mutable CString m_sProjectRoot;
302 //used for rename case
303 mutable CString m_sOldFwdslashPath;
305 // Have we yet determined if this is a directory or not?
306 mutable bool m_bDirectoryKnown;
307 mutable bool m_bIsDirectory;
308 mutable bool m_bLastWriteTimeKnown;
309 mutable bool m_bURLKnown;
310 mutable __int64 m_lastWriteTime;
311 mutable __int64 m_fileSize;
312 mutable bool m_bIsReadOnly;
313 mutable bool m_bHasAdminDirKnown;
314 mutable bool m_bHasAdminDir;
315 mutable bool m_bIsValidOnWindowsKnown;
316 mutable bool m_bIsValidOnWindows;
317 mutable bool m_bIsAdminDirKnown;
318 mutable bool m_bIsAdminDir;
319 mutable bool m_bIsWCRootKnown;
320 mutable bool m_bIsWCRoot;
321 mutable bool m_bExists;
322 mutable bool m_bExistsKnown;
323 mutable LPARAM m_customData;
324 mutable bool m_bIsSpecialDirectoryKnown;
325 mutable bool m_bIsSpecialDirectory;
327 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
330 * Compares two paths and return true if left is earlier in sort order than right
331 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
333 bool operator<(const CTGitPath& left, const CTGitPath& right);
336 //////////////////////////////////////////////////////////////////////////
339 * \ingroup Utils
340 * This class represents a list of paths
342 class CTGitPathList
344 public:
345 CTGitPathList();
346 // A constructor which allows a path list to be easily built with one initial entry in
347 explicit CTGitPathList(const CTGitPath& firstEntry);
348 int m_Action;
350 public:
351 void AddPath(const CTGitPath& newPath);
352 bool LoadFromFile(const CTGitPath& filename);
353 bool WriteToFile(const CString& sFilename, bool bANSI = false) const;
354 const CTGitPath* LookForGitPath(const CString& path);
355 int ParserFromLog(BYTE_VECTOR &log, bool parseDeletes = false);
356 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
357 int FillUnRev(unsigned int Action, CTGitPathList *list = nullptr, CString *err = nullptr);
358 int FillBasedOnIndexFlags(unsigned short flag, CTGitPathList* list = nullptr);
359 int GetAction();
361 * Load from the path argument string, when the 'path' parameter is used
362 * This is a list of paths, with '*' between them
364 void LoadFromAsteriskSeparatedString(const CString& sPathString);
365 CString CreateAsteriskSeparatedString() const;
367 int GetCount() const;
368 bool IsEmpty() const;
369 void Clear();
370 const CTGitPath& operator[](INT_PTR index) const;
371 bool AreAllPathsFiles() const;
372 bool AreAllPathsFilesInOneDirectory() const;
373 CTGitPath GetCommonDirectory() const;
374 CTGitPath GetCommonRoot() const;
375 void SortByPathname(bool bReverse = false);
377 * Delete all the files in the list, then clear the list.
378 * \param bTrash if true, the items are deleted using the Windows trash bin
379 * \param bShowErrorUI if true, show error dialog box when error occurs.
381 void DeleteAllFiles(bool bTrash, bool bFilesOnly = true, bool bShowErrorUI = false);
382 static bool DeleteViaShell(LPCTSTR path, bool useTrashbin, bool bShowErrorUI);
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;