Adjust test cases to latest Git 2.34 changes
[TortoiseGit.git] / src / Git / TGitPath.h
blob343ec744d4bc08e50a13311aea415fd27f5d0128
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2021 - 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();
31 #ifdef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_H_
32 virtual ~CTGitPath();
33 #else
34 ~CTGitPath();
35 #endif
36 CTGitPath(const CString& sUnknownPath);
37 CTGitPath(const CString& sUnknownPath, bool bIsDirectory);
38 int m_Stage;
39 int m_ParentNo;
41 enum class StagingStatus
43 DontCare,
44 TotallyStaged,
45 PartiallyStaged,
46 TotallyUnstaged
49 enum Actions : unsigned int
51 LOGACTIONS_ADDED = 0x00000001,
52 LOGACTIONS_MODIFIED = 0x00000002,
53 LOGACTIONS_REPLACED = 0x00000004,
54 LOGACTIONS_DELETED = 0x00000008,
55 LOGACTIONS_UNMERGED = 0x00000010,
56 LOGACTIONS_COPY = 0x00000040,
57 LOGACTIONS_MERGED = 0x00000080,
58 LOGACTIONS_ASSUMEVALID = 0x00000200,
59 LOGACTIONS_SKIPWORKTREE = 0x00000400,
60 LOGACTIONS_MISSING = 0x00001000,
61 LOGACTIONS_UNVER = 0x80000000,
62 LOGACTIONS_IGNORE = 0x40000000,
64 // For log filter only
65 LOGACTIONS_HIDE = 0x20000000,
66 LOGACTIONS_GRAY = 0x10000000,
69 CString m_StatAdd;
70 CString m_StatDel;
71 StagingStatus m_stagingStatus;
72 #ifdef TGIT_LFS
73 CString m_LFSLockOwner;
74 #endif
75 unsigned int m_Action;
76 bool m_Checked;
77 unsigned int ParserAction(BYTE action);
78 unsigned int ParserAction(git_delta_t action);
79 CString GetActionName() const;
80 static CString GetActionName(unsigned int action);
81 /**
82 * Set the path as an UTF8 string with forward slashes
84 void SetFromGit(const char* pPath);
85 void SetFromGit(const char* pPath, bool bIsDirectory);
86 void SetFromGit(const wchar_t* pPath, bool bIsDirectory);
87 void SetFromGit(const CString& sPath, CString* oldPath = nullptr, int* bIsDirectory = nullptr);
89 /**
90 * Set the path as UNICODE with backslashes
92 void SetFromWin(LPCWSTR pPath);
93 void SetFromWin(const CString& sPath);
94 void SetFromWin(LPCWSTR pPath, bool bIsDirectory);
95 void SetFromWin(const CString& sPath, bool bIsDirectory);
96 /**
97 * Set the path from an unknown source.
99 void SetFromUnknown(const CString& sPath);
101 * Returns the path in Windows format, i.e. with backslashes
103 LPCWSTR GetWinPath() const;
105 * Returns the path in Windows format, i.e. with backslashes
107 const CString& GetWinPathString() const;
109 * Returns the path with forward slashes.
111 const CString& GetGitPathString() const;
113 const CString& GetGitOldPathString() const;
116 * Returns the path for showing in an UI.
118 * URL's are returned with forward slashes, unescaped if necessary
119 * Paths are returned with backward slashes
121 const CString& GetUIPathString() const;
123 * Returns true if the path points to a directory
125 bool IsDirectory() const;
127 CTGitPath GetSubPath(const CTGitPath &root) const;
130 * Returns the directory. If the path points to a directory, then the path
131 * is returned unchanged. If the path points to a file, the path to the
132 * parent directory is returned.
134 CTGitPath GetDirectory() const;
136 * Returns the the directory which contains the item the path refers to.
137 * If the path is a directory, then this returns the directory above it.
138 * If the path is to a file, then this returns the directory which contains the path
139 * parent directory is returned.
141 CTGitPath GetContainingDirectory() const;
143 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
145 CString GetRootPathString() const;
147 * Returns the filename part of the full path.
148 * \remark don't call this for directories.
150 CString GetFilename() const;
151 CString GetBaseFilename() const;
153 * Returns the item's name without the full path.
155 CString GetFileOrDirectoryName() const;
157 * Returns the item's name without the full path, unescaped if necessary.
159 CString GetUIFileOrDirectoryName() const;
161 * Returns the file extension, including the dot.
162 * \remark Returns an empty string for directories
164 CString GetFileExtension() const;
166 void UpdateCase();
168 bool IsEmpty() const;
169 void Reset();
171 * Checks if two paths are equal. The slashes are taken care of.
173 bool IsEquivalentTo(const CTGitPath& rhs) const;
174 bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;
175 bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}
178 * Checks if \c possibleDescendant is a child of this path.
180 bool IsAncestorOf(const CTGitPath& possibleDescendant) const;
182 * Get a string representing the file path, optionally with a base
183 * section stripped off the front
184 * Returns a string with fwdslash paths
186 CString GetDisplayString(const CTGitPath* pOptionalBasePath = nullptr) const;
188 * Compares two paths. Slash format is irrelevant.
190 static int Compare(const CTGitPath& left, const CTGitPath& right);
192 /** As PredLeftLessThanRight, but for checking if paths are equivalent
194 static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);
196 /** Checks if the left path is pointing to the same working copy path as the right.
197 * The same wc path means the paths are equivalent once all the admin dir path parts
198 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
199 * paths to crawl.
201 static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);
203 static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);
206 * appends a string to this path.
207 *\remark - missing slashes are not added - this is just a string concatenation, but with
208 * preservation of the proper caching behavior.
209 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
211 void AppendRawString(const CString& sAppend);
214 * appends a part of a path to this path.
215 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
218 void AppendPathString(const CString& sAppend);
221 * Get the file modification time - returns zero for files which don't exist
223 __int64 GetLastWriteTime(bool force = false) const;
226 * Get the file size. Returns zero for directories or files that don't exist.
228 __int64 GetFileSize() const;
230 bool IsReadOnly() const;
233 * Checks if the path really exists.
235 bool Exists() const;
238 * Deletes the file/folder
239 * \param bTrash if true, uses the Windows trash bin when deleting.
241 bool Delete(bool bTrash, bool bShowErrorUI) const;
244 * Checks if a git admin directory is present. For files, the check
245 * is done in the same directory. For folders, it checks if the folder itself
246 * contains an admin directory.
248 bool HasAdminDir(CString* projectTopDir = nullptr, bool force = false) const;
249 bool HasSubmodules() const;
250 bool HasGitSVNDir() const;
251 bool IsBisectActive() const;
252 bool IsMergeActive() const;
253 bool HasStashDir() const;
254 bool HasRebaseApply() const;
256 bool IsWCRoot() const;
258 int GetAdminDirMask() const;
260 bool IsRegisteredSubmoduleOfParentProject(CString* parentProjectRoot = nullptr) const;
263 * Checks if the path point to or below a git admin directory (.Git).
265 bool IsAdminDir() const;
268 * Checks if the path or URL is valid on Windows.
269 * A path is valid if conforms to the specs in the windows API.
270 * An URL is valid if the path checked out from it is valid
271 * on windows. That means an URL which is valid according to the WWW specs
272 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
273 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
274 * this function would return \c false for that URL).
276 bool IsValidOnWindows() const;
278 CString GetAbbreviatedRename() const;
280 private:
281 // All these functions are const, and all the data
282 // is mutable, in order that the hidden caching operations
283 // can be carried out on a const CTGitPath object, which is what's
284 // likely to be passed between functions
285 // The public 'SetFromxxx' functions are not const, and so the proper
286 // const-correctness semantics are preserved
287 void SetFwdslashPath(const CString& sPath) const;
288 void SetBackslashPath(const CString& sPath) const;
289 void EnsureBackslashPathSet() const;
290 void EnsureFwdslashPathSet() const;
292 public:
294 * Marks a path as a file by unsetting the cached IsDirectory status
295 * Used while diffing commits where a submodule changed to a file
297 void UnsetDirectoryStatus() { m_bIsDirectory = false; }
299 * Marks a path as a directory by setting the cached IsDirectory status
300 * Used while diffing commits where a file changed to a submodule
302 void SetDirectoryStatus() { m_bIsDirectory = true; }
304 private:
306 * Adds the required trailing slash to local root paths such as 'C:'
308 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
310 #ifdef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_H_
311 protected:
312 virtual void UpdateAttributes() const;
313 private:
314 #else
315 void UpdateAttributes() const;
316 #endif
318 bool HasStashDir(const CString& adminDirPath) const;
320 private:
321 mutable CString m_sBackslashPath;
322 mutable CString m_sLongBackslashPath;
323 mutable CString m_sFwdslashPath;
324 mutable CString m_sUIPath;
325 mutable CString m_sProjectRoot;
327 //used for rename case
328 mutable CString m_sOldFwdslashPath;
330 // Have we yet determined if this is a directory or not?
331 mutable bool m_bDirectoryKnown;
332 mutable bool m_bIsDirectory;
333 mutable bool m_bLastWriteTimeKnown;
334 mutable __int64 m_lastWriteTime;
335 mutable __int64 m_fileSize;
336 mutable bool m_bIsReadOnly;
337 mutable bool m_bHasAdminDirKnown;
338 mutable bool m_bHasAdminDir;
339 mutable bool m_bIsValidOnWindowsKnown;
340 mutable bool m_bIsValidOnWindows;
341 mutable bool m_bIsAdminDirKnown;
342 mutable bool m_bIsAdminDir;
343 mutable bool m_bIsWCRootKnown;
344 mutable bool m_bIsWCRoot;
345 mutable bool m_bExists;
346 mutable bool m_bExistsKnown;
348 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
351 * Compares two paths and return true if left is earlier in sort order than right
352 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
354 bool operator<(const CTGitPath& left, const CTGitPath& right);
357 //////////////////////////////////////////////////////////////////////////
360 * \ingroup Utils
361 * This class represents a list of paths
363 class CTGitPathList
365 public:
366 CTGitPathList();
367 // A constructor which allows a path list to be easily built with one initial entry in
368 explicit CTGitPathList(const CTGitPath& firstEntry);
369 unsigned int m_Action;
371 public:
372 void AddPath(const CTGitPath& newPath);
373 bool LoadFromFile(const CTGitPath& filename);
374 bool WriteToFile(const CString& sFilename, bool bUTF8 = false) const;
375 const CTGitPath* LookForGitPath(const CString& path) const;
376 int ParserFromLog(BYTE_VECTOR &log, bool parseDeletes = false);
377 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
378 void UpdateStagingStatusFromPath(const CString& path, CTGitPath::StagingStatus status);
379 int FillUnRev(unsigned int Action, const CTGitPathList* filterlist = nullptr, CString* err = nullptr);
380 #ifdef TGIT_LFS
381 int FillLFSLocks(unsigned int action, CString* err = nullptr);
382 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_
383 private:
384 #endif
385 int ParserFromLFSLocks(unsigned int action, const CString& output, CString* err = nullptr);
386 #endif
387 public:
388 int FillBasedOnIndexFlags(unsigned short flag, unsigned short flagextended, const CTGitPathList* filterlist = nullptr);
389 unsigned int GetAction();
391 * Load from the path argument string, when the 'path' parameter is used
392 * This is a list of paths, with '*' between them
394 void LoadFromAsteriskSeparatedString(const CString& sPathString);
395 CString CreateAsteriskSeparatedString() const;
397 int GetCount() const;
398 bool IsEmpty() const;
399 void Clear();
400 const CTGitPath& operator[](INT_PTR index) const;
401 bool AreAllPathsFiles() const;
402 bool AreAllPathsDirectories() const;
403 bool AreAllPathsFilesInOneDirectory() const;
404 bool IsAnyAncestorOf(const CTGitPath& possibleDescendant) const;
407 * returns the directory which all items have in common.
408 * if not all paths are in the same directory, then
409 * an empty path is returned
411 CTGitPath GetCommonDirectory() const;
413 * returns the root path of all paths in the list.
414 * only returns an empty path if not all paths are on
415 * the same drive/root.
417 CTGitPath GetCommonRoot() const;
418 void SortByPathname(bool bReverse = false);
420 * Delete all the files in the list, then clear the list.
421 * \param bTrash if true, the items are deleted using the Windows trash bin
422 * \param bShowErrorUI if true, show error dialog box when error occurs.
424 void DeleteAllFiles(bool bTrash, bool bFilesOnly = true, bool bShowErrorUI = false);
425 static bool DeleteViaShell(LPCWSTR path, bool useTrashbin, bool bShowErrorUI);
426 /** Remove duplicate entries from the list (sorts the list as a side-effect */
427 void RemoveDuplicates();
428 /** Removes all paths which are on or in a git admin directory */
429 void RemoveAdminPaths();
430 void RemovePath(const CTGitPath& path);
431 void RemoveItem(const CTGitPath& path);
433 * Removes all child items and leaves only the top folders. Useful if you
434 * create the list to remove them (i.e. if you remove a parent folder, the
435 * child files and folders don't have to be deleted anymore)
437 void RemoveChildren();
439 /** Checks if two CTGitPathLists are the same */
440 bool IsEqual(const CTGitPathList& list);
442 using PathVector = std::vector<CTGitPath>;
443 PathVector m_paths;
444 // If the list contains just files in one directory, then
445 // this contains the directory name
446 mutable CTGitPath m_commonBaseDirectory;