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.
24 #define PARENT_MASK 0xFFFFFF
25 #define MERGE_MASK (0x1000000)
31 #ifdef GMOCK_INCLUDE_GMOCK_GMOCK_H_
32 virtual ~CTGitPath(void);
36 CTGitPath(const CString
& sUnknownPath
);
37 CTGitPath(const CString
& sUnknownPath
, bool bIsDirectory
);
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,
63 unsigned int m_Action
;
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
);
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);
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
);
85 * Set the path from an unknown source.
87 void SetFromUnknown(const CString
& sPath
);
89 * Returns the path in Windows format, i.e. with backslashes
91 LPCTSTR
GetWinPath() const;
93 * Returns the path in Windows format, i.e. with backslashes
95 const CString
& GetWinPathString() const;
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;
156 bool IsEmpty() const;
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'
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.
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;
248 bool IsRegisteredSubmoduleOfParentProject(CString
* parentProjectRoot
= nullptr) const;
251 * Checks if the path point to or below a git admin directory (.Git).
253 bool IsAdminDir() const;
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 CString
GetAbbreviatedRename() const;
269 // All these functions are const, and all the data
270 // is mutable, in order that the hidden caching operations
271 // can be carried out on a const CTGitPath object, which is what's
272 // likely to be passed between functions
273 // The public 'SetFromxxx' functions are not const, and so the proper
274 // const-correctness semantics are preserved
275 void SetFwdslashPath(const CString
& sPath
) const;
276 void SetBackslashPath(const CString
& sPath
) const;
277 void EnsureBackslashPathSet() const;
278 void EnsureFwdslashPathSet() const;
282 * Marks a path as a file by unsetting the cached IsDirectory status
283 * Used while diffing commits where a submodule changed to a file
285 void UnsetDirectoryStatus() { m_bIsDirectory
= false; }
289 * Adds the required trailing slash to local root paths such as 'C:'
291 void SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const;
293 #ifdef GMOCK_INCLUDE_GMOCK_GMOCK_H_
295 virtual void UpdateAttributes() const;
298 void UpdateAttributes() const;
301 bool HasStashDir(const CString
& adminDirPath
) const;
304 mutable CString m_sBackslashPath
;
305 mutable CString m_sLongBackslashPath
;
306 mutable CString m_sFwdslashPath
;
307 mutable CString m_sUIPath
;
308 mutable CString m_sProjectRoot
;
310 //used for rename case
311 mutable CString m_sOldFwdslashPath
;
313 // Have we yet determined if this is a directory or not?
314 mutable bool m_bDirectoryKnown
;
315 mutable bool m_bIsDirectory
;
316 mutable bool m_bLastWriteTimeKnown
;
317 mutable __int64 m_lastWriteTime
;
318 mutable __int64 m_fileSize
;
319 mutable bool m_bIsReadOnly
;
320 mutable bool m_bHasAdminDirKnown
;
321 mutable bool m_bHasAdminDir
;
322 mutable bool m_bIsValidOnWindowsKnown
;
323 mutable bool m_bIsValidOnWindows
;
324 mutable bool m_bIsAdminDirKnown
;
325 mutable bool m_bIsAdminDir
;
326 mutable bool m_bIsWCRootKnown
;
327 mutable bool m_bIsWCRoot
;
328 mutable bool m_bExists
;
329 mutable bool m_bExistsKnown
;
331 friend bool operator<(const CTGitPath
& left
, const CTGitPath
& right
);
334 * Compares two paths and return true if left is earlier in sort order than right
335 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
337 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
);
340 //////////////////////////////////////////////////////////////////////////
344 * This class represents a list of paths
350 // A constructor which allows a path list to be easily built with one initial entry in
351 explicit CTGitPathList(const CTGitPath
& firstEntry
);
352 unsigned int m_Action
;
355 void AddPath(const CTGitPath
& newPath
);
356 bool LoadFromFile(const CTGitPath
& filename
);
357 bool WriteToFile(const CString
& sFilename
, bool bANSI
= false) const;
358 const CTGitPath
* LookForGitPath(const CString
& path
);
359 int ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
= false);
360 int ParserFromLsFile(BYTE_VECTOR
&out
,bool staged
=true);
361 int FillUnRev(unsigned int Action
, const CTGitPathList
* filterlist
= nullptr, CString
* err
= nullptr);
362 int FillBasedOnIndexFlags(unsigned short flag
, unsigned short flagextended
, const CTGitPathList
* filterlist
= nullptr);
363 unsigned int GetAction();
365 * Load from the path argument string, when the 'path' parameter is used
366 * This is a list of paths, with '*' between them
368 void LoadFromAsteriskSeparatedString(const CString
& sPathString
);
369 CString
CreateAsteriskSeparatedString() const;
371 int GetCount() const;
372 bool IsEmpty() const;
374 const CTGitPath
& operator[](INT_PTR index
) const;
375 bool AreAllPathsFiles() const;
376 bool AreAllPathsFilesInOneDirectory() const;
378 * returns the directory which all items have in common.
379 * if not all paths are in the same directory, then
380 * an empty path is returned
382 CTGitPath
GetCommonDirectory() const;
384 * returns the root path of all paths in the list.
385 * only returns an empty path if not all paths are on
386 * the same drive/root.
388 CTGitPath
GetCommonRoot() const;
389 void SortByPathname(bool bReverse
= false);
391 * Delete all the files in the list, then clear the list.
392 * \param bTrash if true, the items are deleted using the Windows trash bin
393 * \param bShowErrorUI if true, show error dialog box when error occurs.
395 void DeleteAllFiles(bool bTrash
, bool bFilesOnly
= true, bool bShowErrorUI
= false);
396 static bool DeleteViaShell(LPCTSTR path
, bool useTrashbin
, bool bShowErrorUI
);
397 /** Remove duplicate entries from the list (sorts the list as a side-effect */
398 void RemoveDuplicates();
399 /** Removes all paths which are on or in a git admin directory */
400 void RemoveAdminPaths();
401 void RemovePath(const CTGitPath
& path
);
402 void RemoveItem(CTGitPath
&path
);
404 * Removes all child items and leaves only the top folders. Useful if you
405 * create the list to remove them (i.e. if you remove a parent folder, the
406 * child files and folders don't have to be deleted anymore)
408 void RemoveChildren();
410 /** Checks if two CTGitPathLists are the same */
411 bool IsEqual(const CTGitPathList
& list
);
413 typedef std::vector
<CTGitPath
> PathVector
;
415 // If the list contains just files in one directory, then
416 // this contains the directory name
417 mutable CTGitPath m_commonBaseDirectory
;