Revert "Fix resource leak: Close the event handle in the destructor"
[TortoiseGit.git] / src / Git / TGitPath.h
blob243870d4113706ef3dcb156333675041aedc03f5
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - 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_CACHE = 0x00000020,
46 LOGACTIONS_COPY = 0x00000040,
47 LOGACTIONS_MERGED = 0x00000080,
48 LOGACTIONS_FORWORD = 0x00000100,
49 LOGACTIONS_ASSUMEVALID = 0x00000200,
50 LOGACTIONS_SKIPWORKTREE = 0x00000400,
51 LOGACTIONS_UNVER = 0x80000000,
52 LOGACTIONS_IGNORE = 0x40000000,
53 //LOGACTIONS_CONFLICT = 0x20000000,
55 // For log filter only
56 LOGACTIONS_HIDE = 0x20000000,
57 LOGACTIONS_GRAY = 0x10000000,
59 #pragma warning(pop)
61 CString m_StatAdd;
62 CString m_StatDel;
63 unsigned int m_Action;
64 bool m_Checked;
65 int ParserAction(BYTE action);
66 CString GetActionName();
67 static CString GetActionName(int action);
68 /**
69 * Set the path as an UTF8 string with forward slashes
71 void SetFromGit(const char* pPath);
72 void SetFromGit(const char* pPath, bool bIsDirectory);
73 void SetFromGit(const TCHAR* pPath, bool bIsDirectory);
74 void SetFromGit(const CString& sPath,CString *OldPath=NULL);
76 /**
77 * Set the path as UNICODE with backslashes
79 void SetFromWin(LPCTSTR pPath);
80 void SetFromWin(const CString& sPath);
81 void SetFromWin(LPCTSTR pPath, bool bIsDirectory);
82 void SetFromWin(const CString& sPath, bool bIsDirectory);
83 /**
84 * Set the path from an unknown source.
86 void SetFromUnknown(const CString& sPath);
87 /**
88 * Returns the path in Windows format, i.e. with backslashes
90 LPCTSTR GetWinPath() const;
91 /**
92 * Returns the path in Windows format, i.e. with backslashes
94 const CString& GetWinPathString() const;
95 /**
96 * Returns the path with forward slashes.
98 const CString& GetGitPathString() const;
100 const CString& GetGitOldPathString() const;
102 * Returns the path completely prepared to be fed the the Git APIs
103 * It will be in UTF8, with URLs escaped, if necessary
105 // const char* GetGitApiPath(apr_pool_t *pool) const;
107 * Returns the path for showing in an UI.
109 * URL's are returned with forward slashes, unescaped if necessary
110 * Paths are returned with backward slashes
112 const CString& GetUIPathString() const;
114 * Returns true if the path points to a directory
116 bool IsDirectory() const;
118 CTGitPath GetSubPath(const CTGitPath &root);
121 * Returns the directory. If the path points to a directory, then the path
122 * is returned unchanged. If the path points to a file, the path to the
123 * parent directory is returned.
125 CTGitPath GetDirectory() const;
127 * Returns the the directory which contains the item the path refers to.
128 * If the path is a directory, then this returns the directory above it.
129 * If the path is to a file, then this returns the directory which contains the path
130 * parent directory is returned.
132 CTGitPath GetContainingDirectory() const;
134 * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType
136 CString GetRootPathString() const;
138 * Returns the filename part of the full path.
139 * \remark don't call this for directories.
141 CString GetFilename() const;
142 CString GetBaseFilename() const;
144 * Returns the item's name without the full path.
146 CString GetFileOrDirectoryName() const;
148 * Returns the item's name without the full path, unescaped if necessary.
150 CString GetUIFileOrDirectoryName() const;
152 * Returns the file extension, including the dot.
153 * \remark Returns an empty string for directories
155 CString GetFileExtension() const;
157 bool IsEmpty() const;
158 void Reset();
160 * Checks if two paths are equal. The slashes are taken care of.
162 bool IsEquivalentTo(const CTGitPath& rhs) const;
163 bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;
164 bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}
167 * Checks if \c possibleDescendant is a child of this path.
169 bool IsAncestorOf(const CTGitPath& possibleDescendant) const;
171 * Get a string representing the file path, optionally with a base
172 * section stripped off the front
173 * Returns a string with fwdslash paths
175 CString GetDisplayString(const CTGitPath* pOptionalBasePath = NULL) const;
177 * Compares two paths. Slash format is irrelevant.
179 static int Compare(const CTGitPath& left, const CTGitPath& right);
181 /** As PredLeftLessThanRight, but for checking if paths are equivalent
183 static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);
185 /** Checks if the left path is pointing to the same working copy path as the right.
186 * The same wc path means the paths are equivalent once all the admin dir path parts
187 * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'
188 * paths to crawl.
190 static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);
192 static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);
195 * appends a string to this path.
196 *\remark - missing slashes are not added - this is just a string concatenation, but with
197 * preservation of the proper caching behavior.
198 * If you want to join a file- or directory-name onto the path, you should use AppendPathString
200 void AppendRawString(const CString& sAppend);
203 * appends a part of a path to this path.
204 *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example
207 void AppendPathString(const CString& sAppend);
210 * Get the file modification time - returns zero for files which don't exist
211 * Returns a FILETIME structure cast to an __int64, for easy comparisons
213 __int64 GetLastWriteTime() const;
216 * Get the file size. Returns zero for directories or files that don't exist.
218 __int64 GetFileSize() const;
220 bool IsReadOnly() const;
223 * Checks if the path really exists.
225 bool Exists() const;
228 * Deletes the file/folder
229 * \param bTrash if true, uses the Windows trash bin when deleting.
231 bool Delete(bool bTrash) const;
234 * Checks if a git admin directory is present. For files, the check
235 * is done in the same directory. For folders, it checks if the folder itself
236 * contains an admin directory.
238 bool HasAdminDir() const;
239 bool HasAdminDir(CString *ProjectTopDir) const;
240 bool HasSubmodules() const;
241 bool HasGitSVNDir() const;
242 bool IsBisectActive() const;
243 bool IsMergeActive() const;
244 bool HasStashDir() const;
245 bool HasRebaseApply() const;
247 bool IsWCRoot() const;
249 int GetAdminDirMask() const;
252 * Checks if the path point to or below a git admin directory (.Git).
254 bool IsAdminDir() const;
256 void SetCustomData(LPARAM lp) {m_customData = lp;}
257 LPARAM GetCustomData() const {return m_customData;}
260 * Checks if the path or URL is valid on Windows.
261 * A path is valid if conforms to the specs in the windows API.
262 * An URL is valid if the path checked out from it is valid
263 * on windows. That means an URL which is valid according to the WWW specs
264 * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name
265 * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so
266 * this function would return \c false for that URL).
268 bool IsValidOnWindows() const;
270 private:
271 // All these functions are const, and all the data
272 // is mutable, in order that the hidden caching operations
273 // can be carried out on a const CTGitPath object, which is what's
274 // likely to be passed between functions
275 // The public 'SetFromxxx' functions are not const, and so the proper
276 // const-correctness semantics are preserved
277 void SetFwdslashPath(const CString& sPath) const;
278 void SetBackslashPath(const CString& sPath) const;
279 void SetUTF8FwdslashPath(const CString& sPath) const;
280 void EnsureBackslashPathSet() const;
281 void EnsureFwdslashPathSet() const;
283 * Checks if two path strings are equal. No conversion of slashes is done!
284 * \remark for slash-independent comparison, use IsEquivalentTo()
286 static bool ArePathStringsEqual(const CString& sP1, const CString& sP2);
287 static bool ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2);
290 * Adds the required trailing slash to local root paths such as 'C:'
292 void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;
294 void UpdateAttributes() const;
298 private:
299 mutable CString m_sBackslashPath;
300 mutable CString m_sLongBackslashPath;
301 mutable CString m_sFwdslashPath;
302 mutable CString m_sUIPath;
303 mutable CStringA m_sUTF8FwdslashPath;
304 mutable CStringA m_sUTF8FwdslashPathEscaped;
305 mutable CString m_sProjectRoot;
307 //used for rename case
308 mutable CString m_sOldFwdslashPath;
310 // Have we yet determined if this is a directory or not?
311 mutable bool m_bDirectoryKnown;
312 mutable bool m_bIsDirectory;
313 mutable bool m_bLastWriteTimeKnown;
314 mutable bool m_bURLKnown;
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;
328 mutable LPARAM m_customData;
329 mutable bool m_bIsSpecialDirectoryKnown;
330 mutable bool m_bIsSpecialDirectory;
332 friend bool operator<(const CTGitPath& left, const CTGitPath& right);
335 * Compares two paths and return true if left is earlier in sort order than right
336 * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)
338 bool operator<(const CTGitPath& left, const CTGitPath& right);
341 //////////////////////////////////////////////////////////////////////////
344 * \ingroup Utils
345 * This class represents a list of paths
347 class CTGitPathList
349 public:
350 CTGitPathList();
351 // A constructor which allows a path list to be easily built with one initial entry in
352 explicit CTGitPathList(const CTGitPath& firstEntry);
353 int m_Action;
355 public:
356 void AddPath(const CTGitPath& newPath);
357 bool LoadFromFile(const CTGitPath& filename);
358 bool WriteToFile(const CString& sFilename, bool bANSI = false) const;
359 CTGitPath * LookForGitPath(CString path);
360 int ParserFromLog(BYTE_VECTOR &log, bool parseDeletes = false);
361 int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);
362 int FillUnRev(unsigned int Action, CTGitPathList *list = nullptr, CString *err = nullptr);
363 int FillBasedOnIndexFlags(unsigned short flag, CTGitPathList* list = nullptr);
364 int GetAction();
366 * Load from the path argument string, when the 'path' parameter is used
367 * This is a list of paths, with '*' between them
369 void LoadFromAsteriskSeparatedString(const CString& sPathString);
370 CString CreateAsteriskSeparatedString() const;
372 int GetCount() const;
373 bool IsEmpty() const;
374 void Clear();
375 const CTGitPath& operator[](INT_PTR index) const;
376 bool AreAllPathsFiles() const;
377 bool AreAllPathsFilesInOneDirectory() const;
378 CTGitPath GetCommonDirectory() const;
379 CTGitPath GetCommonRoot() const;
380 void SortByPathname(bool bReverse = false);
382 * Delete all the files in the list, then clear the list.
383 * \param bTrash if true, the items are deleted using the Windows trash bin
385 void DeleteAllFiles(bool bTrash, bool bFilesOnly = true);
386 static bool DeleteViaShell(LPCTSTR path, bool useTrashbin);
387 /** Remove duplicate entries from the list (sorts the list as a side-effect */
388 void RemoveDuplicates();
389 /** Removes all paths which are on or in a git admin directory */
390 void RemoveAdminPaths();
391 void RemovePath(const CTGitPath& path);
392 void RemoveItem(CTGitPath &path);
394 * Removes all child items and leaves only the top folders. Useful if you
395 * create the list to remove them (i.e. if you remove a parent folder, the
396 * child files and folders don't have to be deleted anymore)
398 void RemoveChildren();
400 /** Checks if two CTGitPathLists are the same */
401 bool IsEqual(const CTGitPathList& list);
403 /** Convert into the Git API parameter format */
404 // apr_array_header_t * MakePathArray (apr_pool_t *pool) const;
406 typedef std::vector<CTGitPath> PathVector;
407 PathVector m_paths;
408 // If the list contains just files in one directory, then
409 // this contains the directory name
410 mutable CTGitPath m_commonBaseDirectory;