Extend static functions in CAppUtils with a window handle parameter
[TortoiseGit.git] / src / Git / TGitPath.h
blobc91365751f75f087e74248a810dab43cbde8740d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2018 - 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 #ifdef GMOCK_INCLUDE_GMOCK_GMOCK_H_
32 virtual ~CTGitPath(void);
33 #else
34 ~CTGitPath(void);
35 #endif
36 CTGitPath(const CString& sUnknownPath);
37 CTGitPath(const CString& sUnknownPath, bool bIsDirectory);
38 int m_Stage;
39 int m_ParentNo;
41 enum Actions : unsigned int
43 LOGACTIONS_ADDED = 0x00000001,
44 LOGACTIONS_MODIFIED = 0x00000002,
45 LOGACTIONS_REPLACED = 0x00000004,
46 LOGACTIONS_DELETED = 0x00000008,
47 LOGACTIONS_UNMERGED = 0x00000010,
48 LOGACTIONS_COPY = 0x00000040,
49 LOGACTIONS_MERGED = 0x00000080,
50 LOGACTIONS_ASSUMEVALID = 0x00000200,
51 LOGACTIONS_SKIPWORKTREE = 0x00000400,
52 LOGACTIONS_MISSING = 0x00001000,
53 LOGACTIONS_UNVER = 0x80000000,
54 LOGACTIONS_IGNORE = 0x40000000,
56 // For log filter only
57 LOGACTIONS_HIDE = 0x20000000,
58 LOGACTIONS_GRAY = 0x10000000,
61 CString m_StatAdd;
62 CString m_StatDel;
63 unsigned int m_Action;
64 bool m_Checked;
65 unsigned int ParserAction(BYTE action);
66 unsigned int ParserAction(git_delta_t action);
67 CString GetActionName() const;
68 static CString GetActionName(unsigned int action);
69 /**
70 * Set the path as an UTF8 string with forward slashes
72 void SetFromGit(const char* pPath);
73 void SetFromGit(const char* pPath, bool bIsDirectory);
74 void SetFromGit(const TCHAR* pPath, bool bIsDirectory);
75 void SetFromGit(const CString& sPath, CString* oldPath = nullptr, int* bIsDirectory = nullptr);
77 /**
78 * Set the path as UNICODE with backslashes
80 void SetFromWin(LPCTSTR pPath);
81 void SetFromWin(const CString& sPath);
82 void SetFromWin(LPCTSTR pPath, bool bIsDirectory);
83 void SetFromWin(const CString& sPath, bool bIsDirectory);
84 /**
85 * Set the path from an unknown source.
87 void SetFromUnknown(const CString& sPath);
88 /**
89 * Returns the path in Windows format, i.e. with backslashes
91 LPCTSTR GetWinPath() const;
92 /**
93 * Returns the path in Windows format, i.e. with backslashes
95 const CString& GetWinPathString() const;
96 /**
97 * Returns the path with forward slashes.
99 const CString& GetGitPathString() const;
101 const CString& GetGitOldPathString() const;
104 * Returns the path for showing in an UI.
106 * URL's are returned with forward slashes, unescaped if necessary
107 * Paths are returned with backward slashes
109 const CString& GetUIPathString() const;
111 * Returns true if the path points to a directory
113 bool IsDirectory() const;
115 CTGitPath GetSubPath(const CTGitPath &root);
118 * Returns the directory. If the path points to a directory, then the path
119 * is returned unchanged. If the path points to a file, the path to the
120 * parent directory is returned.
122 CTGitPath GetDirectory() const;
124 * Returns the the directory which contains the item the path refers to.
125 * If the path is a directory, then this returns the directory above it.
126 * If the path is to a file, then this returns the directory which contains the path
127 * parent directory is returned.
129 CTGitPath GetContainingDirectory() const;
131 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
133 CString GetRootPathString() const;
135 * Returns the filename part of the full path.
136 * \remark don't call this for directories.
138 CString GetFilename() const;
139 CString GetBaseFilename() const;
141 * Returns the item's name without the full path.
143 CString GetFileOrDirectoryName() const;
145 * Returns the item's name without the full path, unescaped if necessary.
147 CString GetUIFileOrDirectoryName() const;
149 * Returns the file extension, including the dot.
150 * \remark Returns an empty string for directories
152 CString GetFileExtension() const;
154 void UpdateCase();
156 bool IsEmpty() const;
157 void Reset();
159 * Checks if two paths are equal. The slashes are taken care of.
161 bool IsEquivalentTo(const CTGitPath& rhs) const;
162 bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;
163 bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}
166 * Checks if \c possibleDescendant is a child of this path.
168 bool IsAncestorOf(const CTGitPath& possibleDescendant) const;
170 * Get a string representing the file path, optionally with a base
171 * section stripped off the front
172 * Returns a string with fwdslash paths
174 CString GetDisplayString(const CTGitPath* pOptionalBasePath = nullptr) const;
176 * Compares two paths. Slash format is irrelevant.
178 static int Compare(const CTGitPath& left, const CTGitPath& right);
180 /** As PredLeftLessThanRight, but for checking if paths are equivalent
182 static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);
184 /** Checks if the left path is pointing to the same working copy path as the right.
185 * The same wc path means the paths are equivalent once all the admin dir path parts
186 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
187 * paths to crawl.
189 static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);
191 static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);
194 * appends a string to this path.
195 *\remark - missing slashes are not added - this is just a string concatenation, but with
196 * preservation of the proper caching behavior.
197 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
199 void AppendRawString(const CString& sAppend);
202 * appends a part of a path to this path.
203 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
206 void AppendPathString(const CString& sAppend);
209 * Get the file modification time - returns zero for files which don't exist
211 __int64 GetLastWriteTime(bool force = false) const;
214 * Get the file size. Returns zero for directories or files that don't exist.
216 __int64 GetFileSize() const;
218 bool IsReadOnly() const;
221 * Checks if the path really exists.
223 bool Exists() const;
226 * Deletes the file/folder
227 * \param bTrash if true, uses the Windows trash bin when deleting.
229 bool Delete(bool bTrash, bool bShowErrorUI) const;
232 * Checks if a git admin directory is present. For files, the check
233 * is done in the same directory. For folders, it checks if the folder itself
234 * contains an admin directory.
236 bool HasAdminDir(CString* projectTopDir = nullptr, bool force = false) const;
237 bool HasSubmodules() const;
238 bool HasGitSVNDir() const;
239 bool IsBisectActive() const;
240 bool IsMergeActive() const;
241 bool HasStashDir() const;
242 bool HasRebaseApply() const;
244 bool IsWCRoot() const;
246 int GetAdminDirMask() const;
249 * Checks if the path point to or below a git admin directory (.Git).
251 bool IsAdminDir() const;
254 * Checks if the path or URL is valid on Windows.
255 * A path is valid if conforms to the specs in the windows API.
256 * An URL is valid if the path checked out from it is valid
257 * on windows. That means an URL which is valid according to the WWW specs
258 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
259 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
260 * this function would return \c false for that URL).
262 bool IsValidOnWindows() const;
264 CString GetAbbreviatedRename() 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 EnsureBackslashPathSet() const;
276 void EnsureFwdslashPathSet() const;
278 public:
280 * Marks a path as a file by unsetting the cached IsDirectory status
281 * Used while diffing commits where a submodule changed to a file
283 void UnsetDirectoryStatus() { m_bIsDirectory = false; }
285 private:
287 * Adds the required trailing slash to local root paths such as 'C:'
289 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
291 #ifdef GMOCK_INCLUDE_GMOCK_GMOCK_H_
292 protected:
293 virtual void UpdateAttributes() const;
294 private:
295 #else
296 void UpdateAttributes() const;
297 #endif
299 bool HasStashDir(const CString& adminDirPath) const;
301 private:
302 mutable CString m_sBackslashPath;
303 mutable CString m_sLongBackslashPath;
304 mutable CString m_sFwdslashPath;
305 mutable CString m_sUIPath;
306 mutable CString m_sProjectRoot;
308 //used for rename case
309 mutable CString m_sOldFwdslashPath;
311 // Have we yet determined if this is a directory or not?
312 mutable bool m_bDirectoryKnown;
313 mutable bool m_bIsDirectory;
314 mutable bool m_bLastWriteTimeKnown;
315 mutable __int64 m_lastWriteTime;
316 mutable __int64 m_fileSize;
317 mutable bool m_bIsReadOnly;
318 mutable bool m_bHasAdminDirKnown;
319 mutable bool m_bHasAdminDir;
320 mutable bool m_bIsValidOnWindowsKnown;
321 mutable bool m_bIsValidOnWindows;
322 mutable bool m_bIsAdminDirKnown;
323 mutable bool m_bIsAdminDir;
324 mutable bool m_bIsWCRootKnown;
325 mutable bool m_bIsWCRoot;
326 mutable bool m_bExists;
327 mutable bool m_bExistsKnown;
329 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
332 * Compares two paths and return true if left is earlier in sort order than right
333 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
335 bool operator<(const CTGitPath& left, const CTGitPath& right);
338 //////////////////////////////////////////////////////////////////////////
341 * \ingroup Utils
342 * This class represents a list of paths
344 class CTGitPathList
346 public:
347 CTGitPathList();
348 // A constructor which allows a path list to be easily built with one initial entry in
349 explicit CTGitPathList(const CTGitPath& firstEntry);
350 unsigned int m_Action;
352 public:
353 void AddPath(const CTGitPath& newPath);
354 bool LoadFromFile(const CTGitPath& filename);
355 bool WriteToFile(const CString& sFilename, bool bANSI = false) const;
356 const CTGitPath* LookForGitPath(const CString& path);
357 int ParserFromLog(BYTE_VECTOR &log, bool parseDeletes = false);
358 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
359 int FillUnRev(unsigned int Action, const CTGitPathList* filterlist = nullptr, CString* err = nullptr);
360 int FillBasedOnIndexFlags(unsigned short flag, unsigned short flagextended, const CTGitPathList* filterlist = nullptr);
361 unsigned int GetAction();
363 * Load from the path argument string, when the 'path' parameter is used
364 * This is a list of paths, with '*' between them
366 void LoadFromAsteriskSeparatedString(const CString& sPathString);
367 CString CreateAsteriskSeparatedString() const;
369 int GetCount() const;
370 bool IsEmpty() const;
371 void Clear();
372 const CTGitPath& operator[](INT_PTR index) const;
373 bool AreAllPathsFiles() const;
374 bool AreAllPathsFilesInOneDirectory() const;
376 * returns the directory which all items have in common.
377 * if not all paths are in the same directory, then
378 * an empty path is returned
380 CTGitPath GetCommonDirectory() const;
382 * returns the root path of all paths in the list.
383 * only returns an empty path if not all paths are on
384 * the same drive/root.
386 CTGitPath GetCommonRoot() const;
387 void SortByPathname(bool bReverse = false);
389 * Delete all the files in the list, then clear the list.
390 * \param bTrash if true, the items are deleted using the Windows trash bin
391 * \param bShowErrorUI if true, show error dialog box when error occurs.
393 void DeleteAllFiles(bool bTrash, bool bFilesOnly = true, bool bShowErrorUI = false);
394 static bool DeleteViaShell(LPCTSTR path, bool useTrashbin, bool bShowErrorUI);
395 /** Remove duplicate entries from the list (sorts the list as a side-effect */
396 void RemoveDuplicates();
397 /** Removes all paths which are on or in a git admin directory */
398 void RemoveAdminPaths();
399 void RemovePath(const CTGitPath& path);
400 void RemoveItem(CTGitPath &path);
402 * Removes all child items and leaves only the top folders. Useful if you
403 * create the list to remove them (i.e. if you remove a parent folder, the
404 * child files and folders don't have to be deleted anymore)
406 void RemoveChildren();
408 /** Checks if two CTGitPathLists are the same */
409 bool IsEqual(const CTGitPathList& list);
411 typedef std::vector<CTGitPath> PathVector;
412 PathVector m_paths;
413 // If the list contains just files in one directory, then
414 // this contains the directory name
415 mutable CTGitPath m_commonBaseDirectory;