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.
24 #define PARENT_MASK 0xFFFFFF
25 #define MERGE_MASK (0x1000000)
32 CTGitPath(const CString
& sUnknownPath
);
37 #pragma warning(disable: 4480) // nonstandard extension used: specifying underlying type for enum 'enum'
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_UNVER
= 0x80000000,
50 LOGACTIONS_IGNORE
= 0x40000000,
52 // For log filter only
53 LOGACTIONS_HIDE
= 0x20000000,
54 LOGACTIONS_GRAY
= 0x10000000,
60 unsigned int m_Action
;
62 int ParserAction(BYTE action
);
63 CString
GetActionName();
64 static CString
GetActionName(int action
);
66 * Set the path as an UTF8 string with forward slashes
68 void SetFromGit(const char* pPath
);
69 void SetFromGit(const char* pPath
, bool bIsDirectory
);
70 void SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
);
71 void SetFromGit(const CString
& sPath
,CString
*OldPath
=NULL
);
74 * Set the path as UNICODE with backslashes
76 void SetFromWin(LPCTSTR pPath
);
77 void SetFromWin(const CString
& sPath
);
78 void SetFromWin(LPCTSTR pPath
, bool bIsDirectory
);
79 void SetFromWin(const CString
& sPath
, bool bIsDirectory
);
81 * Set the path from an unknown source.
83 void SetFromUnknown(const CString
& sPath
);
85 * Returns the path in Windows format, i.e. with backslashes
87 LPCTSTR
GetWinPath() const;
89 * Returns the path in Windows format, i.e. with backslashes
91 const CString
& GetWinPathString() const;
93 * Returns the path with forward slashes.
95 const CString
& GetGitPathString() const;
97 const CString
& GetGitOldPathString() const;
100 * Returns the path for showing in an UI.
102 * URL's are returned with forward slashes, unescaped if necessary
103 * Paths are returned with backward slashes
105 const CString
& GetUIPathString() const;
107 * Returns true if the path points to a directory
109 bool IsDirectory() const;
111 CTGitPath
GetSubPath(const CTGitPath
&root
);
114 * Returns the directory. If the path points to a directory, then the path
115 * is returned unchanged. If the path points to a file, the path to the
116 * parent directory is returned.
118 CTGitPath
GetDirectory() const;
120 * Returns the the directory which contains the item the path refers to.
121 * If the path is a directory, then this returns the directory above it.
122 * If the path is to a file, then this returns the directory which contains the path
123 * parent directory is returned.
125 CTGitPath
GetContainingDirectory() const;
127 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
129 CString
GetRootPathString() const;
131 * Returns the filename part of the full path.
132 * \remark don't call this for directories.
134 CString
GetFilename() const;
135 CString
GetBaseFilename() const;
137 * Returns the item's name without the full path.
139 CString
GetFileOrDirectoryName() const;
141 * Returns the item's name without the full path, unescaped if necessary.
143 CString
GetUIFileOrDirectoryName() const;
145 * Returns the file extension, including the dot.
146 * \remark Returns an empty string for directories
148 CString
GetFileExtension() const;
150 bool IsEmpty() const;
153 * Checks if two paths are equal. The slashes are taken care of.
155 bool IsEquivalentTo(const CTGitPath
& rhs
) const;
156 bool IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const;
157 bool operator==(const CTGitPath
& x
) const {return IsEquivalentTo(x
);}
160 * Checks if \c possibleDescendant is a child of this path.
162 bool IsAncestorOf(const CTGitPath
& possibleDescendant
) const;
164 * Get a string representing the file path, optionally with a base
165 * section stripped off the front
166 * Returns a string with fwdslash paths
168 CString
GetDisplayString(const CTGitPath
* pOptionalBasePath
= NULL
) const;
170 * Compares two paths. Slash format is irrelevant.
172 static int Compare(const CTGitPath
& left
, const CTGitPath
& right
);
174 /** As PredLeftLessThanRight, but for checking if paths are equivalent
176 static bool PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
);
178 /** Checks if the left path is pointing to the same working copy path as the right.
179 * The same wc path means the paths are equivalent once all the admin dir path parts
180 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
183 static bool PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
);
185 static bool CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
);
188 * appends a string to this path.
189 *\remark - missing slashes are not added - this is just a string concatenation, but with
190 * preservation of the proper caching behavior.
191 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
193 void AppendRawString(const CString
& sAppend
);
196 * appends a part of a path to this path.
197 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
200 void AppendPathString(const CString
& sAppend
);
203 * Get the file modification time - returns zero for files which don't exist
204 * Returns a FILETIME structure cast to an __int64, for easy comparisons
206 __int64
GetLastWriteTime() const;
209 * Get the file size. Returns zero for directories or files that don't exist.
211 __int64
GetFileSize() const;
213 bool IsReadOnly() const;
216 * Checks if the path really exists.
221 * Deletes the file/folder
222 * \param bTrash if true, uses the Windows trash bin when deleting.
224 bool Delete(bool bTrash
) const;
227 * Checks if a git admin directory is present. For files, the check
228 * is done in the same directory. For folders, it checks if the folder itself
229 * contains an admin directory.
231 bool HasAdminDir() const;
232 bool HasAdminDir(CString
*ProjectTopDir
) const;
233 bool HasSubmodules() const;
234 bool HasGitSVNDir() const;
235 bool IsBisectActive() const;
236 bool IsMergeActive() const;
237 bool HasStashDir() const;
238 bool HasRebaseApply() const;
240 bool IsWCRoot() const;
242 int GetAdminDirMask() const;
245 * Checks if the path point to or below a git admin directory (.Git).
247 bool IsAdminDir() const;
249 void SetCustomData(LPARAM lp
) {m_customData
= lp
;}
250 LPARAM
GetCustomData() const {return m_customData
;}
253 * Checks if the path or URL is valid on Windows.
254 * A path is valid if conforms to the specs in the windows API.
255 * An URL is valid if the path checked out from it is valid
256 * on windows. That means an URL which is valid according to the WWW specs
257 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
258 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
259 * this function would return \c false for that URL).
261 bool IsValidOnWindows() const;
264 // All these functions are const, and all the data
265 // is mutable, in order that the hidden caching operations
266 // can be carried out on a const CTGitPath object, which is what's
267 // likely to be passed between functions
268 // The public 'SetFromxxx' functions are not const, and so the proper
269 // const-correctness semantics are preserved
270 void SetFwdslashPath(const CString
& sPath
) const;
271 void SetBackslashPath(const CString
& sPath
) const;
272 void SetUTF8FwdslashPath(const CString
& sPath
) const;
273 void EnsureBackslashPathSet() const;
274 void EnsureFwdslashPathSet() const;
276 * Checks if two path strings are equal. No conversion of slashes is done!
277 * \remark for slash-independent comparison, use IsEquivalentTo()
279 static bool ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
);
280 static bool ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
);
283 * Adds the required trailing slash to local root paths such as 'C:'
285 void SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const;
287 void UpdateAttributes() const;
292 mutable CString m_sBackslashPath
;
293 mutable CString m_sLongBackslashPath
;
294 mutable CString m_sFwdslashPath
;
295 mutable CString m_sUIPath
;
296 mutable CStringA m_sUTF8FwdslashPath
;
297 mutable CStringA m_sUTF8FwdslashPathEscaped
;
298 mutable CString m_sProjectRoot
;
300 //used for rename case
301 mutable CString m_sOldFwdslashPath
;
303 // Have we yet determined if this is a directory or not?
304 mutable bool m_bDirectoryKnown
;
305 mutable bool m_bIsDirectory
;
306 mutable bool m_bLastWriteTimeKnown
;
307 mutable bool m_bURLKnown
;
308 mutable __int64 m_lastWriteTime
;
309 mutable __int64 m_fileSize
;
310 mutable bool m_bIsReadOnly
;
311 mutable bool m_bHasAdminDirKnown
;
312 mutable bool m_bHasAdminDir
;
313 mutable bool m_bIsValidOnWindowsKnown
;
314 mutable bool m_bIsValidOnWindows
;
315 mutable bool m_bIsAdminDirKnown
;
316 mutable bool m_bIsAdminDir
;
317 mutable bool m_bIsWCRootKnown
;
318 mutable bool m_bIsWCRoot
;
319 mutable bool m_bExists
;
320 mutable bool m_bExistsKnown
;
321 mutable LPARAM m_customData
;
322 mutable bool m_bIsSpecialDirectoryKnown
;
323 mutable bool m_bIsSpecialDirectory
;
325 friend bool operator<(const CTGitPath
& left
, const CTGitPath
& right
);
328 * Compares two paths and return true if left is earlier in sort order than right
329 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
331 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
);
334 //////////////////////////////////////////////////////////////////////////
338 * This class represents a list of paths
344 // A constructor which allows a path list to be easily built with one initial entry in
345 explicit CTGitPathList(const CTGitPath
& firstEntry
);
349 void AddPath(const CTGitPath
& newPath
);
350 bool LoadFromFile(const CTGitPath
& filename
);
351 bool WriteToFile(const CString
& sFilename
, bool bANSI
= false) const;
352 CTGitPath
* LookForGitPath(CString path
);
353 int ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
= false);
354 int ParserFromLsFile(BYTE_VECTOR
&out
,bool staged
=true);
355 int FillUnRev(unsigned int Action
, CTGitPathList
*list
= nullptr, CString
*err
= nullptr);
356 int FillBasedOnIndexFlags(unsigned short flag
, CTGitPathList
* list
= nullptr);
359 * Load from the path argument string, when the 'path' parameter is used
360 * This is a list of paths, with '*' between them
362 void LoadFromAsteriskSeparatedString(const CString
& sPathString
);
363 CString
CreateAsteriskSeparatedString() const;
365 int GetCount() const;
366 bool IsEmpty() const;
368 const CTGitPath
& operator[](INT_PTR index
) const;
369 bool AreAllPathsFiles() const;
370 bool AreAllPathsFilesInOneDirectory() const;
371 CTGitPath
GetCommonDirectory() const;
372 CTGitPath
GetCommonRoot() const;
373 void SortByPathname(bool bReverse
= false);
375 * Delete all the files in the list, then clear the list.
376 * \param bTrash if true, the items are deleted using the Windows trash bin
378 void DeleteAllFiles(bool bTrash
, bool bFilesOnly
= true);
379 static bool DeleteViaShell(LPCTSTR path
, bool useTrashbin
);
380 /** Remove duplicate entries from the list (sorts the list as a side-effect */
381 void RemoveDuplicates();
382 /** Removes all paths which are on or in a git admin directory */
383 void RemoveAdminPaths();
384 void RemovePath(const CTGitPath
& path
);
385 void RemoveItem(CTGitPath
&path
);
387 * Removes all child items and leaves only the top folders. Useful if you
388 * create the list to remove them (i.e. if you remove a parent folder, the
389 * child files and folders don't have to be deleted anymore)
391 void RemoveChildren();
393 /** Checks if two CTGitPathLists are the same */
394 bool IsEqual(const CTGitPathList
& list
);
396 typedef std::vector
<CTGitPath
> PathVector
;
398 // If the list contains just files in one directory, then
399 // this contains the directory name
400 mutable CTGitPath m_commonBaseDirectory
;