Avoid creation of intermediates
[TortoiseGit.git] / src / Git / TGitPath.h
blob5217ed78bd108fb5a58f8d4146709c22b1039061
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - 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 = nullptr);
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 = nullptr) 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;
252 * Checks if the path or URL is valid on Windows.
253 * A path is valid if conforms to the specs in the windows API.
254 * An URL is valid if the path checked out from it is valid
255 * on windows. That means an URL which is valid according to the WWW specs
256 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
257 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
258 * this function would return \c false for that URL).
260 bool IsValidOnWindows() const;
262 CString GetAbbreviatedRename();
264 private:
265 // All these functions are const, and all the data
266 // is mutable, in order that the hidden caching operations
267 // can be carried out on a const CTGitPath object, which is what's
268 // likely to be passed between functions
269 // The public 'SetFromxxx' functions are not const, and so the proper
270 // const-correctness semantics are preserved
271 void SetFwdslashPath(const CString& sPath) const;
272 void SetBackslashPath(const CString& sPath) const;
273 void SetUTF8FwdslashPath(const CString& sPath) const;
274 void EnsureBackslashPathSet() const;
275 void EnsureFwdslashPathSet() const;
277 public:
279 * Checks if two path strings are equal. No conversion of slashes is done!
280 * \remark for slash-independent comparison, use IsEquivalentTo()
282 static bool ArePathStringsEqual(const CString& sP1, const CString& sP2);
283 static bool ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2);
285 private:
287 * Adds the required trailing slash to local root paths such as 'C:'
289 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
291 void UpdateAttributes() const;
293 bool HasStashDir(const CString& adminDirPath) const;
295 private:
296 mutable CString m_sBackslashPath;
297 mutable CString m_sLongBackslashPath;
298 mutable CString m_sFwdslashPath;
299 mutable CString m_sUIPath;
300 mutable CStringA m_sUTF8FwdslashPath;
301 mutable CStringA m_sUTF8FwdslashPathEscaped;
302 mutable CString m_sProjectRoot;
304 //used for rename case
305 mutable CString m_sOldFwdslashPath;
307 // Have we yet determined if this is a directory or not?
308 mutable bool m_bDirectoryKnown;
309 mutable bool m_bIsDirectory;
310 mutable bool m_bLastWriteTimeKnown;
311 mutable bool m_bURLKnown;
312 mutable __int64 m_lastWriteTime;
313 mutable __int64 m_fileSize;
314 mutable bool m_bIsReadOnly;
315 mutable bool m_bHasAdminDirKnown;
316 mutable bool m_bHasAdminDir;
317 mutable bool m_bIsValidOnWindowsKnown;
318 mutable bool m_bIsValidOnWindows;
319 mutable bool m_bIsAdminDirKnown;
320 mutable bool m_bIsAdminDir;
321 mutable bool m_bIsWCRootKnown;
322 mutable bool m_bIsWCRoot;
323 mutable bool m_bExists;
324 mutable bool m_bExistsKnown;
325 mutable bool m_bIsSpecialDirectoryKnown;
326 mutable bool m_bIsSpecialDirectory;
328 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
331 * Compares two paths and return true if left is earlier in sort order than right
332 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
334 bool operator<(const CTGitPath& left, const CTGitPath& right);
337 //////////////////////////////////////////////////////////////////////////
340 * \ingroup Utils
341 * This class represents a list of paths
343 class CTGitPathList
345 public:
346 CTGitPathList();
347 // A constructor which allows a path list to be easily built with one initial entry in
348 explicit CTGitPathList(const CTGitPath& firstEntry);
349 int m_Action;
351 public:
352 void AddPath(const CTGitPath& newPath);
353 bool LoadFromFile(const CTGitPath& filename);
354 bool WriteToFile(const CString& sFilename, bool bANSI = false) const;
355 const CTGitPath* LookForGitPath(const CString& path);
356 int ParserFromLog(BYTE_VECTOR &log, bool parseDeletes = false);
357 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
358 int FillUnRev(unsigned int Action, CTGitPathList *list = nullptr, CString *err = nullptr);
359 int FillBasedOnIndexFlags(unsigned short flag, unsigned short flagextended, CTGitPathList* list = nullptr);
360 int GetAction();
362 * Load from the path argument string, when the 'path' parameter is used
363 * This is a list of paths, with '*' between them
365 void LoadFromAsteriskSeparatedString(const CString& sPathString);
366 CString CreateAsteriskSeparatedString() const;
368 int GetCount() const;
369 bool IsEmpty() const;
370 void Clear();
371 const CTGitPath& operator[](INT_PTR index) const;
372 bool AreAllPathsFiles() const;
373 bool AreAllPathsFilesInOneDirectory() const;
374 CTGitPath GetCommonDirectory() const;
375 CTGitPath GetCommonRoot() const;
376 void SortByPathname(bool bReverse = false);
378 * Delete all the files in the list, then clear the list.
379 * \param bTrash if true, the items are deleted using the Windows trash bin
380 * \param bShowErrorUI if true, show error dialog box when error occurs.
382 void DeleteAllFiles(bool bTrash, bool bFilesOnly = true, bool bShowErrorUI = false);
383 static bool DeleteViaShell(LPCTSTR path, bool useTrashbin, bool bShowErrorUI);
384 /** Remove duplicate entries from the list (sorts the list as a side-effect */
385 void RemoveDuplicates();
386 /** Removes all paths which are on or in a git admin directory */
387 void RemoveAdminPaths();
388 void RemovePath(const CTGitPath& path);
389 void RemoveItem(CTGitPath &path);
391 * Removes all child items and leaves only the top folders. Useful if you
392 * create the list to remove them (i.e. if you remove a parent folder, the
393 * child files and folders don't have to be deleted anymore)
395 void RemoveChildren();
397 /** Checks if two CTGitPathLists are the same */
398 bool IsEqual(const CTGitPathList& list);
400 typedef std::vector<CTGitPath> PathVector;
401 PathVector m_paths;
402 // If the list contains just files in one directory, then
403 // this contains the directory name
404 mutable CTGitPath m_commonBaseDirectory;