OSDN Git Service

Fix Compare with preview version crash problem at log dialog.
[tortoisegit/TortoiseGitJp.git] / src / Git / TGitPath.h
1 #pragma once\r
2 #include "gittype.h"\r
3 \r
4 class CTGitPath\r
5 {\r
6 public:\r
7         CTGitPath(void);\r
8         ~CTGitPath(void);\r
9         CTGitPath(const CString& sUnknownPath);\r
10         int m_Stage;\r
11 public:\r
12         enum\r
13         {       \r
14                 LOGACTIONS_ADDED        = 0x00000001,\r
15                 LOGACTIONS_MODIFIED     = 0x00000002,\r
16                 LOGACTIONS_REPLACED     = 0x00000004,\r
17                 LOGACTIONS_DELETED      = 0x00000008,\r
18                 LOGACTIONS_UNMERGED = 0x00000010,\r
19                 LOGACTIONS_CACHE        = 0x00000020,\r
20                 LOGACTIONS_UNVER        = 0x80000000,\r
21                 LOGACTIONS_IGNORE       = 0x40000000,\r
22                 //LOGACTIONS_CONFLICT = 0x20000000,\r
23         };\r
24 \r
25         CString m_StatAdd;\r
26         CString m_StatDel;\r
27         int             m_Action;\r
28         bool    m_Checked;\r
29         int     ParserAction(BYTE action);\r
30         CString GetActionName();\r
31         /**\r
32          * Set the path as an UTF8 string with forward slashes\r
33          */\r
34         void SetFromGit(const char* pPath);\r
35         void SetFromGit(const char* pPath, bool bIsDirectory);\r
36         void SetFromGit(const CString& sPath,CString *OldPath=NULL);\r
37         \r
38         /**\r
39          * Set the path as UNICODE with backslashes\r
40          */\r
41         void SetFromWin(LPCTSTR pPath);\r
42         void SetFromWin(const CString& sPath);\r
43         void SetFromWin(const CString& sPath, bool bIsDirectory);\r
44         /**\r
45          * Set the path from an unknown source.\r
46          */\r
47         void SetFromUnknown(const CString& sPath);\r
48         /**\r
49          * Returns the path in Windows format, i.e. with backslashes\r
50          */\r
51         LPCTSTR GetWinPath() const;\r
52         /**\r
53          * Returns the path in Windows format, i.e. with backslashes\r
54          */\r
55         const CString& GetWinPathString() const;\r
56         /**\r
57          * Returns the path with forward slashes.\r
58          */\r
59         const CString& GetGitPathString() const;\r
60 \r
61         const CString& GetGitOldPathString() const;\r
62         /**\r
63          * Returns the path completely prepared to be fed the the Git APIs\r
64          * It will be in UTF8, with URLs escaped, if necessary\r
65          */\r
66 //      const char* GetGitApiPath(apr_pool_t *pool) const;\r
67         /**\r
68          * Returns the path for showing in an UI.\r
69          *\r
70          * URL's are returned with forward slashes, unescaped if necessary\r
71          * Paths are returned with backward slashes\r
72          */\r
73         const CString& GetUIPathString() const;\r
74         /**\r
75          * Checks if the path is an URL.\r
76          */\r
77         bool IsUrl() const;\r
78         /**\r
79          * Returns true if the path points to a directory\r
80          */\r
81         bool IsDirectory() const;\r
82         /**\r
83          * Returns the directory. If the path points to a directory, then the path\r
84          * is returned unchanged. If the path points to a file, the path to the \r
85          * parent directory is returned.\r
86          */\r
87         CTGitPath GetDirectory() const;\r
88         /**\r
89         * Returns the the directory which contains the item the path refers to.\r
90         * If the path is a directory, then this returns the directory above it.\r
91         * If the path is to a file, then this returns the directory which contains the path\r
92         * parent directory is returned.\r
93         */\r
94         CTGitPath GetContainingDirectory() const;\r
95         /**\r
96          * Get the 'root path' (e.g. "c:\") - Used to pass to GetDriveType \r
97          */\r
98         CString GetRootPathString() const;\r
99         /**\r
100          * Returns the filename part of the full path.\r
101          * \remark don't call this for directories.\r
102          */\r
103         CString GetFilename() const;\r
104         CString GetBaseFilename() const;\r
105         /**\r
106          * Returns the item's name without the full path.\r
107          */\r
108         CString GetFileOrDirectoryName() const;\r
109         /**\r
110          * Returns the item's name without the full path, unescaped if necessary.\r
111          */\r
112         CString GetUIFileOrDirectoryName() const;\r
113         /**\r
114          * Returns the file extension, including the dot.\r
115          * \remark Returns an empty string for directories\r
116          */\r
117         CString GetFileExtension() const;\r
118 \r
119         bool IsEmpty() const;\r
120         void Reset();\r
121         /**\r
122          * Checks if two paths are equal. The slashes are taken care of.\r
123          */\r
124         bool IsEquivalentTo(const CTGitPath& rhs) const;\r
125         bool IsEquivalentToWithoutCase(const CTGitPath& rhs) const;\r
126         bool operator==(const CTGitPath& x) const {return IsEquivalentTo(x);}\r
127         \r
128         /**\r
129          * Checks if \c possibleDescendant is a child of this path.\r
130          */\r
131         bool IsAncestorOf(const CTGitPath& possibleDescendant) const;\r
132         /**\r
133          * Get a string representing the file path, optionally with a base \r
134          * section stripped off the front\r
135          * Returns a string with fwdslash paths \r
136          */\r
137         CString GetDisplayString(const CTGitPath* pOptionalBasePath = NULL) const;\r
138         /**\r
139          * Compares two paths. Slash format is irrelevant.\r
140          */\r
141         static int Compare(const CTGitPath& left, const CTGitPath& right);\r
142 \r
143         /** As PredLeftLessThanRight, but for checking if paths are equivalent \r
144          */\r
145         static bool PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right);\r
146 \r
147         /** Checks if the left path is pointing to the same working copy path as the right.\r
148          * The same wc path means the paths are equivalent once all the admin dir path parts\r
149          * are removed. This is used in the TGitCache crawler to filter out all the 'duplicate'\r
150          * paths to crawl.\r
151          */\r
152         static bool PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right);\r
153         \r
154         static bool CheckChild(const CTGitPath &parent, const CTGitPath& child);\r
155 \r
156         /**\r
157          * appends a string to this path. \r
158          *\remark - missing slashes are not added - this is just a string concatenation, but with\r
159          * preservation of the proper caching behavior.\r
160          * If you want to join a file- or directory-name onto the path, you should use AppendPathString\r
161          */\r
162         void AppendRawString(const CString& sAppend);\r
163 \r
164         /**\r
165         * appends a part of a path to this path. \r
166         *\remark - missing slashes are dealt with properly. Don't use this to append a file extension, for example\r
167         * \r
168         */\r
169         void AppendPathString(const CString& sAppend);\r
170 \r
171         /**\r
172          * Get the file modification time - returns zero for files which don't exist\r
173          * Returns a FILETIME structure cast to an __int64, for easy comparisons\r
174          */\r
175         __int64 GetLastWriteTime() const;\r
176         \r
177         bool IsReadOnly() const;\r
178         \r
179         /**\r
180          * Checks if the path really exists.\r
181          */\r
182         bool Exists() const;\r
183         \r
184         /**\r
185          * Deletes the file/folder\r
186          * \param bTrash if true, uses the Windows trash bin when deleting.\r
187          */\r
188         bool Delete(bool bTrash) const;\r
189 \r
190         /**\r
191          * Checks if a Subversion admin directory is present. For files, the check\r
192          * is done in the same directory. For folders, it checks if the folder itself\r
193          * contains an admin directory.\r
194          */\r
195         bool HasAdminDir() const;\r
196         \r
197         /**\r
198          * Checks if the path point to or below a Subversion admin directory (.Git).\r
199          */\r
200         bool IsAdminDir() const;\r
201 \r
202         void SetCustomData(LPARAM lp) {m_customData = lp;}\r
203         LPARAM GetCustomData() {return m_customData;}\r
204 \r
205         /**\r
206          * Checks if the path or URL is valid on Windows.\r
207          * A path is valid if conforms to the specs in the windows API.\r
208          * An URL is valid if the path checked out from it is valid\r
209          * on windows. That means an URL which is valid according to the WWW specs\r
210          * isn't necessarily valid as a windows path (e.g. http://myserver.com/repos/file:name \r
211          * is a valid URL, but the path is illegal on windows ("file:name" is illegal), so\r
212          * this function would return \c false for that URL).\r
213          */\r
214         bool IsValidOnWindows() const;\r
215 \r
216         /**\r
217          * Checks to see if the path or URL represents one of the special directories\r
218          * (branches, tags, or trunk).\r
219          */\r
220         bool IsSpecialDirectory() const;\r
221 private:\r
222         // All these functions are const, and all the data\r
223         // is mutable, in order that the hidden caching operations\r
224         // can be carried out on a const CTGitPath object, which is what's \r
225         // likely to be passed between functions\r
226         // The public 'SetFromxxx' functions are not const, and so the proper \r
227         // const-correctness semantics are preserved\r
228         void SetFwdslashPath(const CString& sPath) const;\r
229         void SetBackslashPath(const CString& sPath) const;\r
230         void SetUTF8FwdslashPath(const CString& sPath) const;\r
231         void EnsureBackslashPathSet() const;\r
232         void EnsureFwdslashPathSet() const;\r
233         /**\r
234          * Checks if two path strings are equal. No conversion of slashes is done!\r
235          * \remark for slash-independent comparison, use IsEquivalentTo()\r
236          */\r
237         static bool ArePathStringsEqual(const CString& sP1, const CString& sP2);\r
238         static bool ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2);\r
239         \r
240         /**\r
241          * Adds the required trailing slash to local root paths such as 'C:'\r
242          */\r
243         void SanitizeRootPath(CString& sPath, bool bIsForwardPath) const;\r
244 \r
245         void UpdateAttributes() const;\r
246         \r
247 \r
248 \r
249 private:\r
250         mutable CString m_sBackslashPath;\r
251         mutable CString m_sFwdslashPath;\r
252         mutable CString m_sUIPath;\r
253         mutable CStringA m_sUTF8FwdslashPath;\r
254         mutable CStringA m_sUTF8FwdslashPathEscaped;\r
255 \r
256         //used for rename case\r
257         mutable CString m_sOldBackslashPath;\r
258         mutable CString m_sOldFwdslashPath;\r
259         \r
260         // Have we yet determined if this is a directory or not?\r
261         mutable bool m_bDirectoryKnown;\r
262         mutable bool m_bIsDirectory;\r
263         mutable bool m_bLastWriteTimeKnown;\r
264         mutable bool m_bURLKnown;\r
265         mutable bool m_bIsURL;\r
266         mutable __int64 m_lastWriteTime;\r
267         mutable bool m_bIsReadOnly;\r
268         mutable bool m_bHasAdminDirKnown;\r
269         mutable bool m_bHasAdminDir;\r
270         mutable bool m_bIsValidOnWindowsKnown;\r
271         mutable bool m_bIsValidOnWindows;\r
272         mutable bool m_bIsAdminDirKnown;\r
273         mutable bool m_bIsAdminDir;\r
274         mutable bool m_bExists;\r
275         mutable bool m_bExistsKnown;\r
276         mutable LPARAM m_customData;\r
277         mutable bool m_bIsSpecialDirectoryKnown;\r
278         mutable bool m_bIsSpecialDirectory;\r
279 \r
280         friend bool operator<(const CTGitPath& left, const CTGitPath& right);\r
281 };\r
282 /**\r
283  * Compares two paths and return true if left is earlier in sort order than right\r
284  * (Uses CTGitPath::Compare logic, but is suitable for std::sort and similar)\r
285  */\r
286  bool operator<(const CTGitPath& left, const CTGitPath& right);\r
287 \r
288 \r
289 //////////////////////////////////////////////////////////////////////////\r
290 \r
291 /**\r
292  * \ingroup Utils\r
293  * This class represents a list of paths\r
294  */\r
295 class CTGitPathList \r
296 {\r
297 public:\r
298         CTGitPathList();\r
299         // A constructor which allows a path list to be easily built with one initial entry in\r
300         explicit CTGitPathList(const CTGitPath& firstEntry);\r
301         int m_Action;\r
302 \r
303 public:\r
304         void AddPath(const CTGitPath& newPath);\r
305         bool LoadFromFile(const CTGitPath& filename);\r
306         bool WriteToFile(const CString& sFilename, bool bANSI = false) const;\r
307         CTGitPath * LookForGitPath(CString path);\r
308         int     ParserFromLog(BYTE_VECTOR &log);\r
309         int ParserFromLsFile(BYTE_VECTOR &out,bool staged=true);\r
310         int FillUnRev(int Action,CTGitPathList *list=NULL);\r
311         int GetAction();\r
312         /**\r
313          * Load from the path argument string, when the 'path' parameter is used\r
314          * This is a list of paths, with '*' between them\r
315          */\r
316         void LoadFromAsteriskSeparatedString(const CString& sPathString);\r
317         CString CreateAsteriskSeparatedString() const;\r
318 \r
319         int GetCount() const;\r
320         void Clear();\r
321         const CTGitPath& operator[](INT_PTR index) const;\r
322         bool AreAllPathsFiles() const;\r
323         bool AreAllPathsFilesInOneDirectory() const;\r
324         CTGitPath GetCommonDirectory() const;\r
325         CTGitPath GetCommonRoot() const;\r
326         void SortByPathname(bool bReverse = false);\r
327         /** \r
328          * Delete all the files in the list, then clear the list.\r
329          * \param bTrash if true, the items are deleted using the Windows trash bin\r
330          */\r
331         void DeleteAllFiles(bool bTrash);\r
332         /** Remove duplicate entries from the list (sorts the list as a side-effect */\r
333         void RemoveDuplicates();\r
334         /** Removes all paths which are on or in a Subversion admin directory */\r
335         void RemoveAdminPaths();\r
336         void RemovePath(const CTGitPath& path);\r
337         void RemoveItem(CTGitPath &path);\r
338         /** \r
339          * Removes all child items and leaves only the top folders. Useful if you\r
340          * create the list to remove them (i.e. if you remove a parent folder, the\r
341          * child files and folders don't have to be deleted anymore)\r
342          */\r
343         void RemoveChildren();\r
344 \r
345         /** Checks if two CTGitPathLists are the same */\r
346         bool IsEqual(const CTGitPathList& list);\r
347 \r
348         /** Convert into the Git API parameter format */\r
349 //      apr_array_header_t * MakePathArray (apr_pool_t *pool) const;\r
350 \r
351 private:\r
352         typedef std::vector<CTGitPath> PathVector;\r
353         PathVector m_paths;\r
354         // If the list contains just files in one directory, then\r
355         // this contains the directory name\r
356         mutable CTGitPath m_commonBaseDirectory;\r
357 };\r