OSDN Git Service

9bdbed4164ee7e189d956b72d416404e643c6294
[tortoisegit/TortoiseGitJp.git] / src / Git / TGitPath.cpp
1 // TortoiseGit - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseGit\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "TGitPath.h"\r
21 #include "UnicodeUtils.h"\r
22 #include "GitAdminDir.h"\r
23 #include "PathUtils.h"\r
24 #include <regex>\r
25 #include "git.h"\r
26 #if defined(_MFC_VER)\r
27 //#include "MessageBox.h"\r
28 //#include "AppUtils.h"\r
29 #endif\r
30 \r
31 #ifndef ASSERT\r
32 #define ASSERT()\r
33 #endif\r
34 using namespace std;\r
35 extern CGit g_Git;\r
36 \r
37 CTGitPath::CTGitPath(void) :\r
38         m_bDirectoryKnown(false),\r
39         m_bIsDirectory(false),\r
40         m_bIsURL(false),\r
41         m_bURLKnown(false),\r
42         m_bHasAdminDirKnown(false),\r
43         m_bHasAdminDir(false),\r
44         m_bIsValidOnWindowsKnown(false),\r
45         m_bIsReadOnly(false),\r
46         m_bIsAdminDirKnown(false),\r
47         m_bIsAdminDir(false),\r
48         m_bExists(false),\r
49         m_bExistsKnown(false),\r
50         m_bLastWriteTimeKnown(0),\r
51         m_lastWriteTime(0),\r
52         m_customData(NULL),\r
53         m_bIsSpecialDirectoryKnown(false),\r
54         m_bIsSpecialDirectory(false)\r
55 {\r
56         m_Action=0;\r
57 }\r
58 \r
59 CTGitPath::~CTGitPath(void)\r
60 {\r
61 }\r
62 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)\r
63 CTGitPath::CTGitPath(const CString& sUnknownPath) :\r
64         m_bDirectoryKnown(false),\r
65         m_bIsDirectory(false),\r
66         m_bIsURL(false),\r
67         m_bURLKnown(false),\r
68         m_bHasAdminDirKnown(false),\r
69         m_bHasAdminDir(false),\r
70         m_bIsValidOnWindowsKnown(false),\r
71         m_bIsReadOnly(false),\r
72         m_bIsAdminDirKnown(false),\r
73         m_bIsAdminDir(false),\r
74         m_bExists(false),\r
75         m_bExistsKnown(false),\r
76         m_bLastWriteTimeKnown(0),\r
77         m_lastWriteTime(0),\r
78         m_customData(NULL),\r
79         m_bIsSpecialDirectoryKnown(false),\r
80         m_bIsSpecialDirectory(false)\r
81 {\r
82         SetFromUnknown(sUnknownPath);\r
83         m_Action=0;\r
84         m_Stage=0;\r
85 }\r
86 \r
87 int CTGitPath::ParserAction(BYTE action)\r
88 {\r
89         //action=action.TrimLeft();\r
90         //TCHAR c=action.GetAt(0);\r
91         if(action == 'M')\r
92                 m_Action|= LOGACTIONS_MODIFIED;\r
93         if(action == 'R')\r
94                 m_Action|= LOGACTIONS_REPLACED;\r
95         if(action == 'A')\r
96                 m_Action|= LOGACTIONS_ADDED;\r
97         if(action == 'D')\r
98                 m_Action|= LOGACTIONS_DELETED;\r
99         if(action == 'U')\r
100                 m_Action|= LOGACTIONS_UNMERGED;\r
101         if(action == 'K')\r
102                 m_Action|= LOGACTIONS_DELETED;\r
103         if(action == 'H')\r
104                 m_Action|= LOGACTIONS_CACHE;\r
105         if(action == 'C' )\r
106                 m_Action|= LOGACTIONS_COPY;\r
107 \r
108         return m_Action;\r
109 }\r
110 void CTGitPath::SetFromGit(const char* pPath)\r
111 {\r
112         Reset();\r
113         if (pPath == NULL)\r
114                 return;\r
115         int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);\r
116         if (len)\r
117         {\r
118                 len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, m_sFwdslashPath.GetBuffer(len+1), len+1);\r
119                 m_sFwdslashPath.ReleaseBuffer(len-1);\r
120         }\r
121         SanitizeRootPath(m_sFwdslashPath, true);\r
122 }\r
123 \r
124 void CTGitPath::SetFromGit(const char* pPath, bool bIsDirectory)\r
125 {\r
126         SetFromGit(pPath);\r
127         m_bDirectoryKnown = true;\r
128         m_bIsDirectory = bIsDirectory;\r
129 }\r
130 \r
131 void CTGitPath::SetFromGit(const TCHAR* pPath, bool bIsDirectory)\r
132 {\r
133         Reset();\r
134         if (pPath)\r
135         {\r
136                 m_sFwdslashPath = pPath;\r
137                 SanitizeRootPath(m_sFwdslashPath, true);\r
138         }\r
139         m_bDirectoryKnown = true;\r
140         m_bIsDirectory = bIsDirectory;\r
141 }\r
142 \r
143 void CTGitPath::SetFromGit(const CString& sPath,CString *oldpath)\r
144 {\r
145         Reset();\r
146         m_sFwdslashPath = sPath;\r
147         SanitizeRootPath(m_sFwdslashPath, true);\r
148         if(oldpath)\r
149                 m_sOldFwdslashPath = *oldpath;\r
150 }\r
151 \r
152 void CTGitPath::SetFromWin(LPCTSTR pPath)\r
153 {\r
154         Reset();\r
155         m_sBackslashPath = pPath;\r
156         SanitizeRootPath(m_sBackslashPath, false);\r
157         ATLASSERT(m_sBackslashPath.Find('/')<0);\r
158 }\r
159 void CTGitPath::SetFromWin(const CString& sPath)\r
160 {\r
161         Reset();\r
162         m_sBackslashPath = sPath;\r
163         SanitizeRootPath(m_sBackslashPath, false);\r
164 }\r
165 void CTGitPath::SetFromWin(const CString& sPath, bool bIsDirectory)\r
166 {\r
167         Reset();\r
168         m_sBackslashPath = sPath;\r
169         m_bIsDirectory = bIsDirectory;\r
170         m_bDirectoryKnown = true;\r
171         SanitizeRootPath(m_sBackslashPath, false);\r
172 }\r
173 void CTGitPath::SetFromUnknown(const CString& sPath)\r
174 {\r
175         Reset();\r
176         // Just set whichever path we think is most likely to be used\r
177 //      GitAdminDir admin;\r
178 //      CString p;\r
179 //      if(admin.HasAdminDir(sPath,&p))\r
180 //              SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));\r
181 //      else\r
182                 SetFwdslashPath(sPath);\r
183 }\r
184 \r
185 LPCTSTR CTGitPath::GetWinPath() const\r
186 {\r
187         if(IsEmpty())\r
188         {\r
189                 return _T("");\r
190         }\r
191         if(m_sBackslashPath.IsEmpty())\r
192         {\r
193                 SetBackslashPath(m_sFwdslashPath);\r
194         }\r
195         return m_sBackslashPath;\r
196 }\r
197 // This is a temporary function, to be used during the migration to \r
198 // the path class.  Ultimately, functions consuming paths should take a CTGitPath&, not a CString\r
199 const CString& CTGitPath::GetWinPathString() const\r
200 {\r
201         if(m_sBackslashPath.IsEmpty())\r
202         {\r
203                 SetBackslashPath(m_sFwdslashPath);\r
204         }\r
205         return m_sBackslashPath;\r
206 }\r
207 \r
208 const CString& CTGitPath::GetGitPathString() const\r
209 {\r
210         if(m_sFwdslashPath.IsEmpty())\r
211         {\r
212                 SetFwdslashPath(m_sBackslashPath);\r
213         }\r
214         return m_sFwdslashPath;\r
215 }\r
216 \r
217 const CString &CTGitPath::GetGitOldPathString() const\r
218 {\r
219         return m_sOldFwdslashPath;\r
220 }\r
221 #if 0\r
222 const char* CTGitPath::GetGitApiPath(apr_pool_t *pool) const\r
223 {\r
224         // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby\r
225         // each call to GetGitApiPath returns a different pointer value.\r
226         // If you made multiple calls to GetGitApiPath on the same string, only the last\r
227         // one would give you a valid pointer to an empty string, because each \r
228         // call would invalidate the previous call's return. \r
229         if(IsEmpty())\r
230         {\r
231                 return "";\r
232         }\r
233         if(m_sFwdslashPath.IsEmpty())\r
234         {\r
235                 SetFwdslashPath(m_sBackslashPath);\r
236         }\r
237         if(m_sUTF8FwdslashPath.IsEmpty())\r
238         {\r
239                 SetUTF8FwdslashPath(m_sFwdslashPath);\r
240         }\r
241         if (svn_path_is_url(m_sUTF8FwdslashPath))\r
242         {\r
243                 m_sUTF8FwdslashPathEscaped = CPathUtils::PathEscape(m_sUTF8FwdslashPath);\r
244                 m_sUTF8FwdslashPathEscaped.Replace("file:////", "file:///\\");\r
245                 m_sUTF8FwdslashPathEscaped = svn_path_canonicalize(m_sUTF8FwdslashPathEscaped, pool);\r
246                 return m_sUTF8FwdslashPathEscaped;\r
247         }\r
248         m_sUTF8FwdslashPath = svn_path_canonicalize(m_sUTF8FwdslashPath, pool);\r
249 \r
250         return m_sUTF8FwdslashPath;\r
251 }\r
252 #endif\r
253 \r
254 const CString& CTGitPath::GetUIPathString() const\r
255 {\r
256         if (m_sUIPath.IsEmpty())\r
257         {\r
258 #if defined(_MFC_VER)\r
259                 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only\r
260                 if (IsUrl())\r
261                 {\r
262                         m_sUIPath = CPathUtils::PathUnescape(GetGitPathString());\r
263                         m_sUIPath.Replace(_T("file:////"), _T("file:///\\"));\r
264 \r
265                 }\r
266                 else\r
267 #endif \r
268                 {\r
269                         m_sUIPath = GetWinPathString();\r
270                 }\r
271         }\r
272         return m_sUIPath;\r
273 }\r
274 \r
275 void CTGitPath::SetFwdslashPath(const CString& sPath) const\r
276 {\r
277         m_sFwdslashPath = sPath;\r
278         m_sFwdslashPath.Replace('\\', '/');\r
279 \r
280         // We don't leave a trailing /\r
281         m_sFwdslashPath.TrimRight('/'); \r
282 \r
283         SanitizeRootPath(m_sFwdslashPath, true);\r
284 \r
285         m_sFwdslashPath.Replace(_T("file:////"), _T("file:///\\"));\r
286 \r
287         m_sUTF8FwdslashPath.Empty();\r
288 }\r
289 \r
290 void CTGitPath::SetBackslashPath(const CString& sPath) const\r
291 {\r
292         m_sBackslashPath = sPath;\r
293         m_sBackslashPath.Replace('/', '\\');\r
294         m_sBackslashPath.TrimRight('\\');\r
295         SanitizeRootPath(m_sBackslashPath, false);\r
296 }\r
297 \r
298 void CTGitPath::SetUTF8FwdslashPath(const CString& sPath) const\r
299 {\r
300         m_sUTF8FwdslashPath = CUnicodeUtils::GetUTF8(sPath);\r
301 }\r
302 \r
303 void CTGitPath::SanitizeRootPath(CString& sPath, bool bIsForwardPath) const\r
304 {\r
305         // Make sure to add the trailing slash to root paths such as 'C:'\r
306         if (sPath.GetLength() == 2 && sPath[1] == ':')\r
307         {\r
308                 sPath += (bIsForwardPath) ? _T("/") : _T("\\");\r
309         }\r
310 }\r
311 \r
312 bool CTGitPath::IsUrl() const\r
313 {\r
314 #if 0\r
315         if (!m_bURLKnown)\r
316         {\r
317                 EnsureFwdslashPathSet();\r
318                 if(m_sUTF8FwdslashPath.IsEmpty())\r
319                 {\r
320                         SetUTF8FwdslashPath(m_sFwdslashPath);\r
321                 }\r
322                 m_bIsURL = !!svn_path_is_url(m_sUTF8FwdslashPath);\r
323                 m_bURLKnown = true;\r
324         }\r
325         return m_bIsURL;\r
326 #endif \r
327         return false;\r
328 }\r
329 \r
330 bool CTGitPath::IsDirectory() const\r
331 {\r
332         if(!m_bDirectoryKnown)\r
333         {\r
334                 UpdateAttributes();\r
335         }\r
336         return m_bIsDirectory;\r
337 }\r
338 \r
339 bool CTGitPath::Exists() const\r
340 {\r
341         if (!m_bExistsKnown)\r
342         {\r
343                 UpdateAttributes();\r
344         }\r
345         return m_bExists;\r
346 }\r
347 \r
348 bool CTGitPath::Delete(bool bTrash) const\r
349 {\r
350         EnsureBackslashPathSet();\r
351         ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);\r
352         bool bRet = false;\r
353         if (Exists())\r
354         {\r
355                 if ((bTrash)||(IsDirectory()))\r
356                 {\r
357                         TCHAR * buf = new TCHAR[m_sBackslashPath.GetLength()+2];\r
358                         _tcscpy_s(buf, m_sBackslashPath.GetLength()+2, m_sBackslashPath);\r
359                         buf[m_sBackslashPath.GetLength()] = 0;\r
360                         buf[m_sBackslashPath.GetLength()+1] = 0;\r
361                         SHFILEOPSTRUCT shop = {0};\r
362                         shop.wFunc = FO_DELETE;\r
363                         shop.pFrom = buf;\r
364                         shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;\r
365                         if (bTrash)\r
366                                 shop.fFlags |= FOF_ALLOWUNDO;\r
367                         bRet = (SHFileOperation(&shop) == 0);\r
368                         delete [] buf;\r
369                 }\r
370                 else\r
371                 {\r
372                         bRet = !!::DeleteFile(m_sBackslashPath);\r
373                 }\r
374         }\r
375         m_bExists = false;\r
376         m_bExistsKnown = true;\r
377         return bRet;\r
378 }\r
379 \r
380 __int64  CTGitPath::GetLastWriteTime() const\r
381 {\r
382         if(!m_bLastWriteTimeKnown)\r
383         {\r
384                 UpdateAttributes();\r
385         }\r
386         return m_lastWriteTime;\r
387 }\r
388 \r
389 bool CTGitPath::IsReadOnly() const\r
390 {\r
391         if(!m_bLastWriteTimeKnown)\r
392         {\r
393                 UpdateAttributes();\r
394         }\r
395         return m_bIsReadOnly;\r
396 }\r
397 \r
398 void CTGitPath::UpdateAttributes() const\r
399 {\r
400         EnsureBackslashPathSet();\r
401         WIN32_FILE_ATTRIBUTE_DATA attribs;\r
402         if(GetFileAttributesEx(m_sBackslashPath, GetFileExInfoStandard, &attribs))\r
403         {\r
404                 m_bIsDirectory = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);\r
405                 m_lastWriteTime = *(__int64*)&attribs.ftLastWriteTime;\r
406                 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);\r
407                 m_bExists = true;\r
408         }\r
409         else\r
410         {\r
411                 DWORD err = GetLastError();\r
412                 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))\r
413                 {\r
414                         m_bIsDirectory = false;\r
415                         m_lastWriteTime = 0;\r
416                         m_bExists = false;\r
417                 }\r
418                 else\r
419                 {\r
420                         m_bIsDirectory = false;\r
421                         m_lastWriteTime = 0;\r
422                         m_bExists = true;\r
423                         return;\r
424                 }\r
425         }\r
426         m_bDirectoryKnown = true;\r
427         m_bLastWriteTimeKnown = true;\r
428         m_bExistsKnown = true;\r
429 }\r
430 \r
431 CTGitPath CTGitPath::GetSubPath(CTGitPath &root)\r
432 {\r
433         CTGitPath path;\r
434         \r
435         if(GetWinPathString().Left(root.GetWinPathString().GetLength()) == root.GetWinPathString())\r
436         {\r
437                 CString str=GetWinPathString();\r
438                 path.SetFromWin(str.Right(str.GetLength()-root.GetWinPathString().GetLength()-1));\r
439         }\r
440         return path;\r
441 }\r
442 \r
443 void CTGitPath::EnsureBackslashPathSet() const\r
444 {\r
445         if(m_sBackslashPath.IsEmpty())\r
446         {\r
447                 SetBackslashPath(m_sFwdslashPath);\r
448                 ATLASSERT(IsEmpty() || !m_sBackslashPath.IsEmpty());\r
449         }\r
450 }\r
451 void CTGitPath::EnsureFwdslashPathSet() const\r
452 {\r
453         if(m_sFwdslashPath.IsEmpty())\r
454         {\r
455                 SetFwdslashPath(m_sBackslashPath);\r
456                 ATLASSERT(IsEmpty() || !m_sFwdslashPath.IsEmpty());\r
457         }\r
458 }\r
459 \r
460 \r
461 // Reset all the caches\r
462 void CTGitPath::Reset()\r
463 {\r
464         m_bDirectoryKnown = false;\r
465         m_bURLKnown = false;\r
466         m_bLastWriteTimeKnown = false;\r
467         m_bHasAdminDirKnown = false;\r
468         m_bIsValidOnWindowsKnown = false;\r
469         m_bIsAdminDirKnown = false;\r
470         m_bExistsKnown = false;\r
471         m_bIsSpecialDirectoryKnown = false;\r
472         m_bIsSpecialDirectory = false;\r
473 \r
474         m_sBackslashPath.Empty();\r
475         m_sFwdslashPath.Empty();\r
476         m_sUTF8FwdslashPath.Empty();\r
477         this->m_Action=0;\r
478         this->m_StatAdd=_T("");\r
479         this->m_StatDel=_T("");\r
480         ATLASSERT(IsEmpty());\r
481 }\r
482 \r
483 CTGitPath CTGitPath::GetDirectory() const\r
484 {\r
485         if ((IsDirectory())||(!Exists()))\r
486         {\r
487                 return *this;\r
488         }\r
489         return GetContainingDirectory();\r
490 }\r
491 \r
492 CTGitPath CTGitPath::GetContainingDirectory() const\r
493 {\r
494         EnsureBackslashPathSet();\r
495 \r
496         CString sDirName = m_sBackslashPath.Left(m_sBackslashPath.ReverseFind('\\'));\r
497         if(sDirName.GetLength() == 2 && sDirName[1] == ':')\r
498         {\r
499                 // This is a root directory, which needs a trailing slash\r
500                 sDirName += '\\';\r
501                 if(sDirName == m_sBackslashPath)\r
502                 {\r
503                         // We were clearly provided with a root path to start with - we should return nothing now\r
504                         sDirName.Empty();\r
505                 }\r
506         }\r
507         if(sDirName.GetLength() == 1 && sDirName[0] == '\\')\r
508         {\r
509                 // We have an UNC path and we already are the root\r
510                 sDirName.Empty();\r
511         }\r
512         CTGitPath retVal;\r
513         retVal.SetFromWin(sDirName);\r
514         return retVal;\r
515 }\r
516 \r
517 CString CTGitPath::GetRootPathString() const\r
518 {\r
519         EnsureBackslashPathSet();\r
520         CString workingPath = m_sBackslashPath;\r
521         LPTSTR pPath = workingPath.GetBuffer(MAX_PATH);         // MAX_PATH ok here.\r
522         ATLVERIFY(::PathStripToRoot(pPath));\r
523         workingPath.ReleaseBuffer();\r
524         return workingPath;\r
525 }\r
526 \r
527 \r
528 CString CTGitPath::GetFilename() const\r
529 {\r
530         ATLASSERT(!IsDirectory());\r
531         return GetFileOrDirectoryName();\r
532 }\r
533 \r
534 CString CTGitPath::GetFileOrDirectoryName() const\r
535 {\r
536         EnsureBackslashPathSet();\r
537         return m_sBackslashPath.Mid(m_sBackslashPath.ReverseFind('\\')+1);\r
538 }\r
539 \r
540 CString CTGitPath::GetUIFileOrDirectoryName() const\r
541 {\r
542         GetUIPathString();\r
543         return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);\r
544 }\r
545 \r
546 CString CTGitPath::GetFileExtension() const\r
547 {\r
548         if(!IsDirectory())\r
549         {\r
550                 EnsureBackslashPathSet();\r
551                 int dotPos = m_sBackslashPath.ReverseFind('.');\r
552                 int slashPos = m_sBackslashPath.ReverseFind('\\');\r
553                 if (dotPos > slashPos)\r
554                         return m_sBackslashPath.Mid(dotPos);\r
555         }\r
556         return CString();\r
557 }\r
558 CString CTGitPath::GetBaseFilename() const\r
559 {\r
560         int dot;\r
561         CString filename=GetFilename();\r
562         dot = filename.ReverseFind(_T('.'));\r
563         if(dot>0)\r
564                 return filename.Left(dot);\r
565         else\r
566                 return filename;\r
567 }\r
568 \r
569 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)\r
570 {\r
571         int length = sP1.GetLength();\r
572         if(length != sP2.GetLength())\r
573         {\r
574                 // Different lengths\r
575                 return false;\r
576         }\r
577         // We work from the end of the strings, because path differences\r
578         // are more likely to occur at the far end of a string\r
579         LPCTSTR pP1Start = sP1;\r
580         LPCTSTR pP1 = pP1Start+(length-1);\r
581         LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);\r
582         while(length-- > 0)\r
583         {\r
584                 if(_totlower(*pP1--) != _totlower(*pP2--))\r
585                 {\r
586                         return false;\r
587                 }\r
588         }\r
589         return true;\r
590 }\r
591 \r
592 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)\r
593 {\r
594         int length = sP1.GetLength();\r
595         if(length != sP2.GetLength())\r
596         {\r
597                 // Different lengths\r
598                 return false;\r
599         }\r
600         // We work from the end of the strings, because path differences\r
601         // are more likely to occur at the far end of a string\r
602         LPCTSTR pP1Start = sP1;\r
603         LPCTSTR pP1 = pP1Start+(length-1);\r
604         LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);\r
605         while(length-- > 0)\r
606         {\r
607                 if((*pP1--) != (*pP2--))\r
608                 {\r
609                         return false;\r
610                 }\r
611         }\r
612         return true;\r
613 }\r
614 \r
615 bool CTGitPath::IsEmpty() const\r
616 {\r
617         // Check the backward slash path first, since the chance that this\r
618         // one is set is higher. In case of a 'false' return value it's a little\r
619         // bit faster.\r
620         return m_sBackslashPath.IsEmpty() && m_sFwdslashPath.IsEmpty();\r
621 }\r
622 \r
623 // Test if both paths refer to the same item\r
624 // Ignores case and slash direction\r
625 bool CTGitPath::IsEquivalentTo(const CTGitPath& rhs) const\r
626 {\r
627         // Try and find a slash direction which avoids having to convert\r
628         // both filenames\r
629         if(!m_sBackslashPath.IsEmpty())\r
630         {\r
631                 // *We've* got a \ path - make sure that the RHS also has a \ path\r
632                 rhs.EnsureBackslashPathSet();\r
633                 return ArePathStringsEqualWithCase(m_sBackslashPath, rhs.m_sBackslashPath);\r
634         }\r
635         else\r
636         {\r
637                 // Assume we've got a fwdslash path and make sure that the RHS has one\r
638                 rhs.EnsureFwdslashPathSet();\r
639                 return ArePathStringsEqualWithCase(m_sFwdslashPath, rhs.m_sFwdslashPath);\r
640         }\r
641 }\r
642 \r
643 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath& rhs) const\r
644 {\r
645         // Try and find a slash direction which avoids having to convert\r
646         // both filenames\r
647         if(!m_sBackslashPath.IsEmpty())\r
648         {\r
649                 // *We've* got a \ path - make sure that the RHS also has a \ path\r
650                 rhs.EnsureBackslashPathSet();\r
651                 return ArePathStringsEqual(m_sBackslashPath, rhs.m_sBackslashPath);\r
652         }\r
653         else\r
654         {\r
655                 // Assume we've got a fwdslash path and make sure that the RHS has one\r
656                 rhs.EnsureFwdslashPathSet();\r
657                 return ArePathStringsEqual(m_sFwdslashPath, rhs.m_sFwdslashPath);\r
658         }\r
659 }\r
660 \r
661 bool CTGitPath::IsAncestorOf(const CTGitPath& possibleDescendant) const\r
662 {\r
663         possibleDescendant.EnsureBackslashPathSet();\r
664         EnsureBackslashPathSet();\r
665 \r
666         bool bPathStringsEqual = ArePathStringsEqual(m_sBackslashPath, possibleDescendant.m_sBackslashPath.Left(m_sBackslashPath.GetLength()));\r
667         if (m_sBackslashPath.GetLength() >= possibleDescendant.GetWinPathString().GetLength())\r
668         {\r
669                 return bPathStringsEqual;               \r
670         }\r
671         \r
672         return (bPathStringsEqual && \r
673                         ((possibleDescendant.m_sBackslashPath[m_sBackslashPath.GetLength()] == '\\')||\r
674                         (m_sBackslashPath.GetLength()==3 && m_sBackslashPath[1]==':')));\r
675 }\r
676 \r
677 // Get a string representing the file path, optionally with a base \r
678 // section stripped off the front.\r
679 CString CTGitPath::GetDisplayString(const CTGitPath* pOptionalBasePath /* = NULL*/) const\r
680 {\r
681         EnsureFwdslashPathSet();\r
682         if(pOptionalBasePath != NULL)\r
683         {\r
684                 // Find the length of the base-path without having to do an 'ensure' on it\r
685                 int baseLength = max(pOptionalBasePath->m_sBackslashPath.GetLength(), pOptionalBasePath->m_sFwdslashPath.GetLength());\r
686 \r
687                 // Now, chop that baseLength of the front of the path\r
688                 return m_sFwdslashPath.Mid(baseLength).TrimLeft('/');\r
689         }\r
690         return m_sFwdslashPath;\r
691 }\r
692 \r
693 int CTGitPath::Compare(const CTGitPath& left, const CTGitPath& right)\r
694 {\r
695         left.EnsureBackslashPathSet();\r
696         right.EnsureBackslashPathSet();\r
697         return left.m_sBackslashPath.CompareNoCase(right.m_sBackslashPath);\r
698 }\r
699 \r
700 bool operator<(const CTGitPath& left, const CTGitPath& right)\r
701 {\r
702         return CTGitPath::Compare(left, right) < 0;\r
703 }\r
704 \r
705 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right)\r
706 {\r
707         return left.IsEquivalentTo(right);\r
708 }\r
709 \r
710 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right)\r
711 {\r
712         if (left.IsAdminDir() && right.IsAdminDir())\r
713         {\r
714                 CTGitPath l = left;\r
715                 CTGitPath r = right;\r
716                 do \r
717                 {\r
718                         l = l.GetContainingDirectory();\r
719                 } while(l.HasAdminDir());\r
720                 do \r
721                 {\r
722                         r = r.GetContainingDirectory();\r
723                 } while(r.HasAdminDir());\r
724                 return l.GetContainingDirectory().IsEquivalentTo(r.GetContainingDirectory());\r
725         }\r
726         return left.GetDirectory().IsEquivalentTo(right.GetDirectory());\r
727 }\r
728 \r
729 bool CTGitPath::CheckChild(const CTGitPath &parent, const CTGitPath& child)\r
730 {\r
731         return parent.IsAncestorOf(child);\r
732 }\r
733 \r
734 void CTGitPath::AppendRawString(const CString& sAppend)\r
735 {\r
736         EnsureFwdslashPathSet();\r
737         CString strCopy = m_sFwdslashPath += sAppend;\r
738         SetFromUnknown(strCopy);\r
739 }\r
740 \r
741 void CTGitPath::AppendPathString(const CString& sAppend)\r
742 {\r
743         EnsureBackslashPathSet();\r
744         CString cleanAppend(sAppend);\r
745         cleanAppend.Replace('/', '\\');\r
746         cleanAppend.TrimLeft('\\');\r
747         m_sBackslashPath.TrimRight('\\');\r
748         CString strCopy = m_sBackslashPath + _T("\\") + cleanAppend;\r
749         SetFromWin(strCopy);\r
750 }\r
751 \r
752 bool CTGitPath::HasAdminDir() const\r
753 {\r
754         if (m_bHasAdminDirKnown)\r
755                 return m_bHasAdminDir;\r
756 \r
757         EnsureBackslashPathSet();\r
758         m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);\r
759         m_bHasAdminDirKnown = true;\r
760         return m_bHasAdminDir;\r
761 }\r
762 \r
763 bool CTGitPath::HasSubmodules() const\r
764 {\r
765         return !g_GitAdminDir.GetSuperProjectRoot(GetWinPathString()).IsEmpty();\r
766 }\r
767 \r
768 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const\r
769 {\r
770         if (m_bHasAdminDirKnown)\r
771         {\r
772                 if (ProjectTopDir)\r
773                         *ProjectTopDir = m_sProjectRoot;\r
774                 return m_bHasAdminDir;\r
775         }\r
776 \r
777         EnsureBackslashPathSet();\r
778         m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);\r
779         m_bHasAdminDirKnown = true;\r
780         if (ProjectTopDir)\r
781                 *ProjectTopDir = m_sProjectRoot;\r
782         return m_bHasAdminDir;\r
783 }\r
784 \r
785 bool CTGitPath::IsAdminDir() const\r
786 {\r
787         if (m_bIsAdminDirKnown)\r
788                 return m_bIsAdminDir;\r
789         \r
790         EnsureBackslashPathSet();\r
791         m_bIsAdminDir = g_GitAdminDir.IsAdminDirPath(m_sBackslashPath);\r
792         m_bIsAdminDirKnown = true;\r
793         return m_bIsAdminDir;\r
794 }\r
795 \r
796 bool CTGitPath::IsValidOnWindows() const\r
797 {\r
798         if (m_bIsValidOnWindowsKnown)\r
799                 return m_bIsValidOnWindows;\r
800 \r
801         m_bIsValidOnWindows = false;\r
802         EnsureBackslashPathSet();\r
803         CString sMatch = m_sBackslashPath + _T("\r\n");\r
804         wstring sPattern;\r
805         // the 'file://' URL is just a normal windows path:\r
806         if (sMatch.Left(7).CompareNoCase(_T("file:\\\\"))==0)\r
807         {\r
808                 sMatch = sMatch.Mid(7);\r
809                 sMatch.TrimLeft(_T("\\"));\r
810                 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");\r
811         }\r
812         else if (IsUrl())\r
813         {\r
814                 sPattern = _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");\r
815         }\r
816         else\r
817         {\r
818                 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");\r
819         }\r
820 \r
821         try\r
822         {\r
823                 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);\r
824                 tr1::wsmatch match;\r
825 \r
826                 wstring rmatch = wstring((LPCTSTR)sMatch);\r
827                 if (tr1::regex_match(rmatch, match, rx))\r
828                 {\r
829                         if (wstring(match[0]).compare(sMatch)==0)\r
830                                 m_bIsValidOnWindows = true;\r
831                 }\r
832                 if (m_bIsValidOnWindows)\r
833                 {\r
834                         // now check for illegal filenames\r
835                         tr1::wregex rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);\r
836                         rmatch = m_sBackslashPath;\r
837                         if (tr1::regex_search(rmatch, rx2, tr1::regex_constants::match_default))\r
838                                 m_bIsValidOnWindows = false;\r
839                 }\r
840         }\r
841         catch (exception) {}\r
842 \r
843         m_bIsValidOnWindowsKnown = true;\r
844         return m_bIsValidOnWindows;\r
845 }\r
846 \r
847 bool CTGitPath::IsSpecialDirectory() const\r
848 {\r
849         if (m_bIsSpecialDirectoryKnown)\r
850                 return m_bIsSpecialDirectory;\r
851 \r
852         static LPCTSTR specialDirectories[]\r
853                 = { _T("trunk"), _T("tags"), _T("branches") };\r
854 \r
855         for (int i=0 ; i<(sizeof(specialDirectories) / sizeof(specialDirectories[0])) ; ++i)\r
856         {\r
857                 CString name = GetFileOrDirectoryName();\r
858                 if (0 == name.CompareNoCase(specialDirectories[i]))\r
859                 {\r
860                         m_bIsSpecialDirectory = true;\r
861                         break;\r
862                 }\r
863         }\r
864 \r
865         m_bIsSpecialDirectoryKnown = true;\r
866 \r
867         return m_bIsSpecialDirectory;\r
868 }\r
869 \r
870 //////////////////////////////////////////////////////////////////////////\r
871 \r
872 CTGitPathList::CTGitPathList()\r
873 {\r
874 \r
875 }\r
876 \r
877 // A constructor which allows a path list to be easily built which one initial entry in\r
878 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)\r
879 {\r
880         AddPath(firstEntry);\r
881 }\r
882 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool staged)\r
883 {\r
884         int pos=0;\r
885         CString one;\r
886         CTGitPath path;\r
887         CString part;\r
888         while(pos>=0 && pos<out.size())\r
889         {\r
890                 one.Empty();\r
891                 path.Reset();\r
892 \r
893                 g_Git.StringAppend(&one,&out[pos],CP_ACP);\r
894                 int tabstart=0;\r
895                 path.m_Action=path.ParserAction(out[pos]);\r
896                 one.Tokenize(_T("\t"),tabstart); \r
897 \r
898                 if(tabstart>=0)\r
899                         path.SetFromGit(one.Right(one.GetLength()-tabstart));\r
900 \r
901                 tabstart=0;\r
902 \r
903                 part=one.Tokenize(_T(" "),tabstart); //Tag\r
904 \r
905                 part=one.Tokenize(_T(" "),tabstart); //Mode\r
906                 \r
907                 part=one.Tokenize(_T(" "),tabstart); //Hash\r
908 \r
909                 part=one.Tokenize(_T("\t"),tabstart); //Stage\r
910 \r
911                 path.m_Stage=_ttol(part);\r
912 \r
913                 this->AddPath(path);\r
914 \r
915                 pos=out.findNextString(pos);\r
916         }\r
917         return pos;\r
918 }\r
919 int CTGitPathList::FillUnRev(int action,CTGitPathList *list)\r
920 {\r
921         int pos=0;\r
922         this->Clear();\r
923         CTGitPath path;\r
924 \r
925         int count;\r
926         if(list==NULL)\r
927                 count=1;\r
928         else\r
929                 count=list->GetCount();\r
930         for(int i=0;i<count;i++)\r
931         {       \r
932                 CString cmd;\r
933                 pos=0;\r
934                 \r
935                 CString ignored;\r
936                 if(action & CTGitPath::LOGACTIONS_IGNORE)\r
937                         ignored= _T(" --ignored");\r
938                 \r
939                 if(list==NULL)\r
940                 {\r
941                         cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");\r
942                         cmd+=ignored;\r
943                         \r
944                 }\r
945                 else\r
946                 {       cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z %s-- \"%s\""),\r
947                                         ignored,\r
948                                         (*list)[i].GetWinPathString());\r
949                 }\r
950 \r
951                 BYTE_VECTOR out;\r
952                 out.clear();\r
953                 g_Git.Run(cmd,&out);\r
954                 \r
955                 pos=0;\r
956                 CString one;\r
957                 while( pos>=0 && pos<out.size())\r
958                 {\r
959                         one.Empty();\r
960                         g_Git.StringAppend(&one,&out[pos],CP_ACP);\r
961                         if(!one.IsEmpty())\r
962                         {\r
963                                 //SetFromGit will clear all status\r
964                                 path.SetFromGit(one);\r
965                                 path.m_Action=action;\r
966                                 AddPath(path);\r
967                         }\r
968                         pos=out.findNextString(pos);\r
969                 }\r
970 \r
971         }\r
972         return 0;\r
973 }\r
974 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log)\r
975 {\r
976         this->Clear();\r
977         int pos=0;\r
978         //BYTE *p=&log[0];\r
979         //CString one;\r
980         CTGitPath path;\r
981         m_Action=0;\r
982         while( pos>=0 && pos<log.size())\r
983         {\r
984                 //one=log.Tokenize(_T("\n"),pos);\r
985                 path.Reset();\r
986                 if(log[pos]=='\n')\r
987                         pos++;\r
988 \r
989                 if(log[pos]==':')\r
990                 {\r
991                         bool merged=false;\r
992                         if(log[pos+1] ==':')\r
993                         {\r
994                                 merged=true;\r
995                         }\r
996                         int end=log.find(0,pos);\r
997                         int actionstart=-1;\r
998                         int numfile=1;\r
999                         int file1=-1,file2=-1;\r
1000                         if( end>0 )\r
1001                         {\r
1002                                 actionstart=log.find(' ',end-6);\r
1003                                 pos=actionstart;\r
1004                         }\r
1005                         if( actionstart>0 )\r
1006                         {\r
1007                                 actionstart++;\r
1008 \r
1009                                 file1 = log.find(0,actionstart);\r
1010                                 if( file1>=0 )\r
1011                                 {\r
1012                                         file1++;\r
1013                                         pos=file1;\r
1014                                 }\r
1015                                 if( log[actionstart] == 'C' || log[actionstart] == 'R' )\r
1016                                 {\r
1017                                         file2=file1;\r
1018                                         numfile=2;\r
1019                                         file1 = log.find(0,file1);\r
1020                                         if(file1>=0 )\r
1021                                         {\r
1022                                                 file1++;\r
1023                                                 pos=file1;\r
1024                                         }\r
1025 \r
1026                                 }\r
1027                         }\r
1028                         \r
1029                         CString pathname1;\r
1030                         CString pathname2;\r
1031 \r
1032                         if( file1>=0 )\r
1033                                 g_Git.StringAppend(&pathname1,&log[file1],CP_ACP);\r
1034                         if( file2>=0 )\r
1035                                 g_Git.StringAppend(&pathname2,&log[file2],CP_ACP);\r
1036 \r
1037                         CTGitPath *GitPath=LookForGitPath(pathname1);\r
1038 \r
1039                         if(GitPath)\r
1040                         {\r
1041                                 GitPath->ParserAction( log[actionstart] );      \r
1042                                 \r
1043                                 if(merged)\r
1044                                 {\r
1045                                         GitPath->m_Action |= CTGitPath::LOGACTIONS_MERGED;\r
1046                                         GitPath->m_Action &= ~CTGitPath::LOGACTIONS_FORWORD;\r
1047                                 }\r
1048                                 m_Action |=GitPath->m_Action;\r
1049 \r
1050                         }else\r
1051                         {       \r
1052                                 int ac=path.ParserAction(log[actionstart] );\r
1053                                 ac |= merged?CTGitPath::LOGACTIONS_MERGED:0;    \r
1054 \r
1055                                 path.SetFromGit(pathname1,&pathname2);\r
1056                                 path.m_Action=ac;\r
1057                                         //action must be set after setfromgit. SetFromGit will clear all status. \r
1058                                 this->m_Action|=ac;\r
1059                                 \r
1060                                 AddPath(path);\r
1061                                 \r
1062                         }\r
1063         \r
1064                 }else\r
1065                 {                       \r
1066                         int tabstart=0;\r
1067                         path.Reset();\r
1068                         CString StatAdd;\r
1069                         CString StatDel;\r
1070                         CString file1;\r
1071                         CString file2;\r
1072 \r
1073                         tabstart=log.find('\t',pos);\r
1074                         if(tabstart >=0)\r
1075                         {\r
1076                                 log[tabstart]=0;\r
1077                                 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);\r
1078                                 pos=tabstart+1;\r
1079                         }\r
1080 \r
1081                         tabstart=log.find('\t',pos);\r
1082                         if(tabstart >=0)\r
1083                         {\r
1084                                 log[tabstart]=0;\r
1085                                 \r
1086                                 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);\r
1087                                 pos=tabstart+1;\r
1088                         }\r
1089                         \r
1090                         if(log[pos] == 0) //rename\r
1091                         {\r
1092                                 pos++;\r
1093                                 g_Git.StringAppend(&file2,&log[pos],CP_ACP);\r
1094                                 int sec=log.find(0,pos);\r
1095                                 if(sec>=0)\r
1096                                 {\r
1097                                         sec++;\r
1098                                         g_Git.StringAppend(&file1,&log[sec],CP_ACP);\r
1099                                 }\r
1100                                 pos=sec;\r
1101 \r
1102                         }else\r
1103                         {\r
1104                                 g_Git.StringAppend(&file1,&log[pos],CP_ACP);\r
1105                         }\r
1106                         path.SetFromGit(file1,&file2);\r
1107         \r
1108                         CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());\r
1109                         if(GitPath)\r
1110                         {\r
1111                                 GitPath->m_StatAdd=StatAdd;\r
1112                                 GitPath->m_StatDel=StatDel;\r
1113                         }else\r
1114                         {\r
1115                                 //path.SetFromGit(pathname);\r
1116                                 path.m_StatAdd=StatAdd;\r
1117                                 path.m_StatDel=StatDel;\r
1118                                 path.m_Action |= CTGitPath::LOGACTIONS_FORWORD;\r
1119                                 AddPath(path);\r
1120                         }\r
1121 \r
1122                 }\r
1123                 pos=log.findNextString(pos);\r
1124         }\r
1125         return pos;\r
1126 }\r
1127 \r
1128 void CTGitPathList::AddPath(const CTGitPath& newPath)\r
1129 {\r
1130         m_paths.push_back(newPath);\r
1131         m_commonBaseDirectory.Reset();\r
1132 }\r
1133 int CTGitPathList::GetCount() const\r
1134 {\r
1135         return (int)m_paths.size();\r
1136 }\r
1137 void CTGitPathList::Clear()\r
1138 {\r
1139         m_paths.clear();\r
1140         m_commonBaseDirectory.Reset();\r
1141 }\r
1142 \r
1143 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const\r
1144 {\r
1145         ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());\r
1146         return m_paths[index];\r
1147 }\r
1148 \r
1149 bool CTGitPathList::AreAllPathsFiles() const\r
1150 {\r
1151         // Look through the vector for any directories - if we find them, return false\r
1152         return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();\r
1153 }\r
1154 \r
1155 \r
1156 #if defined(_MFC_VER)\r
1157 \r
1158 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)\r
1159 {\r
1160         Clear();\r
1161         try\r
1162         {\r
1163                 CString strLine;\r
1164                 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);\r
1165 \r
1166                 // for every selected file/folder\r
1167                 CTGitPath path;\r
1168                 while (file.ReadString(strLine))\r
1169                 {\r
1170                         path.SetFromUnknown(strLine);\r
1171                         AddPath(path);\r
1172                 }\r
1173                 file.Close();\r
1174         }\r
1175         catch (CFileException* pE)\r
1176         {\r
1177                 TRACE("CFileException loading target file list\n");\r
1178                 TCHAR error[10000] = {0};\r
1179                 pE->GetErrorMessage(error, 10000);\r
1180 //              CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);\r
1181                 pE->Delete();\r
1182                 return false;\r
1183         }\r
1184         return true;\r
1185 }\r
1186 \r
1187 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const\r
1188 {\r
1189         try\r
1190         {\r
1191                 if (bANSI)\r
1192                 {\r
1193                         CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);\r
1194                         PathVector::const_iterator it;\r
1195                         for(it = m_paths.begin(); it != m_paths.end(); ++it)\r
1196                         {\r
1197                                 CStringA line = CStringA(it->GetGitPathString()) + '\n';\r
1198                                 file.Write(line, line.GetLength());\r
1199                         } \r
1200                         file.Close();\r
1201                 }\r
1202                 else\r
1203                 {\r
1204                         CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);\r
1205                         PathVector::const_iterator it;\r
1206                         for(it = m_paths.begin(); it != m_paths.end(); ++it)\r
1207                         {\r
1208                                 file.WriteString(it->GetGitPathString()+_T("\n"));\r
1209                         } \r
1210                         file.Close();\r
1211                 }\r
1212         }\r
1213         catch (CFileException* pE)\r
1214         {\r
1215                 TRACE("CFileException in writing temp file\n");\r
1216                 pE->Delete();\r
1217                 return false;\r
1218         }\r
1219         return true;\r
1220 }\r
1221 \r
1222 \r
1223 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)\r
1224 {\r
1225         int pos = 0;\r
1226         CString temp;\r
1227         for(;;)\r
1228         {\r
1229                 temp = sPathString.Tokenize(_T("*"),pos);\r
1230                 if(temp.IsEmpty())\r
1231                 {\r
1232                         break;\r
1233                 }\r
1234                 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));\r
1235         } \r
1236 }\r
1237 \r
1238 CString CTGitPathList::CreateAsteriskSeparatedString() const\r
1239 {\r
1240         CString sRet;\r
1241         PathVector::const_iterator it;\r
1242         for(it = m_paths.begin(); it != m_paths.end(); ++it)\r
1243         {\r
1244                 if (!sRet.IsEmpty())\r
1245                         sRet += _T("*");\r
1246                 sRet += it->GetWinPathString();\r
1247         }\r
1248         return sRet;\r
1249 }\r
1250 #endif // _MFC_VER\r
1251 \r
1252 bool \r
1253 CTGitPathList::AreAllPathsFilesInOneDirectory() const\r
1254 {\r
1255         // Check if all the paths are files and in the same directory\r
1256         PathVector::const_iterator it;\r
1257         m_commonBaseDirectory.Reset();\r
1258         for(it = m_paths.begin(); it != m_paths.end(); ++it)\r
1259         {\r
1260                 if(it->IsDirectory())\r
1261                 {\r
1262                         return false;\r
1263                 }\r
1264                 const CTGitPath& baseDirectory = it->GetDirectory();\r
1265                 if(m_commonBaseDirectory.IsEmpty())\r
1266                 {\r
1267                         m_commonBaseDirectory = baseDirectory;\r
1268                 }\r
1269                 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))\r
1270                 {\r
1271                         // Different path\r
1272                         m_commonBaseDirectory.Reset();\r
1273                         return false;\r
1274                 }\r
1275         }\r
1276         return true;\r
1277 }\r
1278 \r
1279 CTGitPath CTGitPathList::GetCommonDirectory() const\r
1280 {\r
1281         if (m_commonBaseDirectory.IsEmpty())\r
1282         {\r
1283                 PathVector::const_iterator it;\r
1284                 for(it = m_paths.begin(); it != m_paths.end(); ++it)\r
1285                 {\r
1286                         const CTGitPath& baseDirectory = it->GetDirectory();\r
1287                         if(m_commonBaseDirectory.IsEmpty())\r
1288                         {\r
1289                                 m_commonBaseDirectory = baseDirectory;\r
1290                         }\r
1291                         else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))\r
1292                         {\r
1293                                 // Different path\r
1294                                 m_commonBaseDirectory.Reset();\r
1295                                 break;\r
1296                         }\r
1297                 }\r
1298         }\r
1299         // since we only checked strings, not paths,\r
1300         // we have to make sure now that we really return a *path* here\r
1301         PathVector::const_iterator iter;\r
1302         for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)\r
1303         {\r
1304                 if (!m_commonBaseDirectory.IsAncestorOf(*iter))\r
1305                 {\r
1306                         m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();\r
1307                         break;\r
1308                 }\r
1309         }       \r
1310         return m_commonBaseDirectory;\r
1311 }\r
1312 \r
1313 CTGitPath CTGitPathList::GetCommonRoot() const\r
1314 {\r
1315         PathVector::const_iterator it;\r
1316         CString sRoot, sTempRoot;\r
1317         bool bEqual = true;\r
1318 \r
1319         if (GetCount() == 1)\r
1320                 return m_paths[0];\r
1321 \r
1322         int backSlashPos = 0;\r
1323         int searchStartPos = 0;\r
1324         while (bEqual)\r
1325         {\r
1326                 for (it = m_paths.begin(); it != m_paths.end(); ++it)\r
1327                 {\r
1328                         if (backSlashPos == 0)\r
1329                         {\r
1330                                 backSlashPos = it->GetWinPathString().Find('\\', searchStartPos+1);\r
1331                                 if ((backSlashPos < 0)&&(searchStartPos != it->GetWinPathString().GetLength()))\r
1332                                         backSlashPos = it->GetWinPathString().GetLength();\r
1333                         }\r
1334                         else if (it->GetWinPathString().Find('\\', searchStartPos+1) != backSlashPos)\r
1335                         {\r
1336                                 if (it->GetWinPathString().Find('\\', searchStartPos+1) < 0)\r
1337                                 {\r
1338                                         if (it->GetWinPathString().GetLength() != backSlashPos)\r
1339                                         {\r
1340                                                 bEqual = false;\r
1341                                                 break;\r
1342                                         }\r
1343                                 }\r
1344                                 else\r
1345                                 {\r
1346                                         bEqual = false;\r
1347                                         break;\r
1348                                 }\r
1349                         }\r
1350                         if (backSlashPos < 0)\r
1351                         {\r
1352                                 bEqual = false;\r
1353                                 break;\r
1354                         }\r
1355                 }\r
1356                 if (bEqual == false)\r
1357                 {\r
1358                         if (searchStartPos)\r
1359                                 sRoot = m_paths[0].GetWinPathString().Left(searchStartPos+1);\r
1360                 }\r
1361                 else\r
1362                 {\r
1363                         searchStartPos = backSlashPos;\r
1364                 }\r
1365                 backSlashPos = 0;\r
1366         }\r
1367 \r
1368         return CTGitPath(sRoot.TrimRight('\\'));\r
1369 }\r
1370 \r
1371 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)\r
1372 {\r
1373         std::sort(m_paths.begin(), m_paths.end());\r
1374         if (bReverse)\r
1375                 std::reverse(m_paths.begin(), m_paths.end());\r
1376 }\r
1377 \r
1378 void CTGitPathList::DeleteAllFiles(bool bTrash)\r
1379 {\r
1380         PathVector::const_iterator it;\r
1381         if (bTrash)\r
1382         {\r
1383                 SortByPathname();\r
1384                 CString sPaths;\r
1385                 for (it = m_paths.begin(); it != m_paths.end(); ++it)\r
1386                 {\r
1387                         if ((it->Exists())&&(!it->IsDirectory()))\r
1388                         {\r
1389                                 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);\r
1390                                 sPaths += it->GetWinPath();\r
1391                                 sPaths += '\0';\r
1392                         }\r
1393                 }\r
1394                 sPaths += '\0';\r
1395                 sPaths += '\0';\r
1396                 SHFILEOPSTRUCT shop = {0};\r
1397                 shop.wFunc = FO_DELETE;\r
1398                 shop.pFrom = (LPCTSTR)sPaths;\r
1399                 shop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;\r
1400                 SHFileOperation(&shop);\r
1401         }\r
1402         else\r
1403         {\r
1404                 for (it = m_paths.begin(); it != m_paths.end(); ++it)\r
1405                 {\r
1406                         if (!it->IsDirectory())\r
1407                         {\r
1408                                 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);\r
1409                                 ::DeleteFile(it->GetWinPath());\r
1410                         }\r
1411                 }\r
1412         }\r
1413         Clear();\r
1414 }\r
1415 \r
1416 void CTGitPathList::RemoveDuplicates()\r
1417 {\r
1418         SortByPathname();\r
1419         // Remove the duplicates\r
1420         // (Unique moves them to the end of the vector, then erase chops them off)\r
1421         m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());\r
1422 }\r
1423 \r
1424 void CTGitPathList::RemoveAdminPaths()\r
1425 {\r
1426         PathVector::iterator it;\r
1427         for(it = m_paths.begin(); it != m_paths.end(); )\r
1428         {\r
1429                 if (it->IsAdminDir())\r
1430                 {\r
1431                         m_paths.erase(it);\r
1432                         it = m_paths.begin();\r
1433                 }\r
1434                 else\r
1435                         ++it;\r
1436         }\r
1437 }\r
1438 \r
1439 void CTGitPathList::RemovePath(const CTGitPath& path)\r
1440 {\r
1441         PathVector::iterator it;\r
1442         for(it = m_paths.begin(); it != m_paths.end(); ++it)\r
1443         {\r
1444                 if (it->IsEquivalentTo(path))\r
1445                 {\r
1446                         m_paths.erase(it);\r
1447                         return;\r
1448                 }\r
1449         }\r
1450 }\r
1451 \r
1452 void CTGitPathList::RemoveItem(CTGitPath & path)\r
1453 {\r
1454         PathVector::iterator it;\r
1455         for(it = m_paths.begin(); it != m_paths.end(); ++it)\r
1456         {\r
1457                 if (it->GetGitPathString()==path.GetGitPathString())\r
1458                 {\r
1459                         m_paths.erase(it);\r
1460                         return;\r
1461                 }\r
1462         }\r
1463 }\r
1464 void CTGitPathList::RemoveChildren()\r
1465 {\r
1466         SortByPathname();\r
1467         m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());\r
1468 }\r
1469 \r
1470 bool CTGitPathList::IsEqual(const CTGitPathList& list)\r
1471 {\r
1472         if (list.GetCount() != GetCount())\r
1473                 return false;\r
1474         for (int i=0; i<list.GetCount(); ++i)\r
1475         {\r
1476                 if (!list[i].IsEquivalentTo(m_paths[i]))\r
1477                         return false;\r
1478         }\r
1479         return true;\r
1480 }\r
1481 \r
1482 //////////////////////////////////////////////////////////////////////////\r
1483 #if 0\r
1484 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const\r
1485 {\r
1486         apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));\r
1487 \r
1488         for(int nItem = 0; nItem < GetCount(); nItem++)\r
1489         {\r
1490                 const char * target = m_paths[nItem].GetGitApiPath(pool);\r
1491                 (*((const char **) apr_array_push (targets))) = target;\r
1492         }\r
1493 \r
1494         return targets;\r
1495 }\r
1496 #endif\r
1497 //////////////////////////////////////////////////////////////////////////\r
1498 \r
1499 #if 0\r
1500 #if defined(_DEBUG)\r
1501 // Some test cases for these classes\r
1502 static class CTGitPathTests\r
1503 {\r
1504 public:\r
1505         CTGitPathTests()\r
1506         {\r
1507                 apr_initialize();\r
1508                 pool = svn_pool_create(NULL);\r
1509                 GetDirectoryTest();\r
1510                 AdminDirTest();\r
1511                 SortTest();\r
1512                 RawAppendTest();\r
1513                 PathAppendTest();\r
1514                 RemoveDuplicatesTest();\r
1515                 RemoveChildrenTest();\r
1516                 ContainingDirectoryTest();\r
1517                 AncestorTest();\r
1518                 SubversionPathTest();\r
1519                 GetCommonRootTest();\r
1520 #if defined(_MFC_VER)\r
1521                 ValidPathAndUrlTest();\r
1522                 ListLoadingTest();\r
1523 #endif\r
1524                 apr_terminate();\r
1525         }\r
1526 \r
1527 private:\r
1528 //      apr_pool_t * pool;\r
1529         void GetDirectoryTest()\r
1530         {\r
1531                 // Bit tricky, this test, because we need to know something about the file\r
1532                 // layout on the machine which is running the test\r
1533                 TCHAR winDir[MAX_PATH+1];\r
1534                 GetWindowsDirectory(winDir, MAX_PATH);\r
1535                 CString sWinDir(winDir);\r
1536 \r
1537                 CTGitPath testPath;\r
1538                 // This is a file which we know will always be there\r
1539                 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));\r
1540                 ATLASSERT(!testPath.IsDirectory());\r
1541                 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);\r
1542                 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);\r
1543 \r
1544                 // Now do the test on the win directory itself - It's hard to be sure about the containing directory\r
1545                 // but we know it must be different to the directory itself\r
1546                 testPath.SetFromUnknown(sWinDir);\r
1547                 ATLASSERT(testPath.IsDirectory());\r
1548                 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);\r
1549                 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);\r
1550                 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());\r
1551 \r
1552                 // Try a root path\r
1553                 testPath.SetFromUnknown(_T("C:\\"));\r
1554                 ATLASSERT(testPath.IsDirectory());\r
1555                 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);\r
1556                 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());\r
1557                 // Try a root UNC path\r
1558                 testPath.SetFromUnknown(_T("\\MYSTATION"));\r
1559                 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());\r
1560         }\r
1561 \r
1562         void AdminDirTest()\r
1563         {\r
1564                 CTGitPath testPath;\r
1565                 testPath.SetFromUnknown(_T("c:\\.svndir"));\r
1566                 ATLASSERT(!testPath.IsAdminDir());\r
1567                 testPath.SetFromUnknown(_T("c:\\test.svn"));\r
1568                 ATLASSERT(!testPath.IsAdminDir());\r
1569                 testPath.SetFromUnknown(_T("c:\\.svn"));\r
1570                 ATLASSERT(testPath.IsAdminDir());\r
1571                 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));\r
1572                 ATLASSERT(!testPath.IsAdminDir());\r
1573                 testPath.SetFromUnknown(_T("c:\\.svn\\test"));\r
1574                 ATLASSERT(testPath.IsAdminDir());\r
1575                 \r
1576                 CTGitPathList pathList;\r
1577                 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));\r
1578                 pathList.AddPath(CTGitPath(_T("c:\\.svn")));\r
1579                 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));\r
1580                 pathList.AddPath(CTGitPath(_T("c:\\test")));\r
1581                 pathList.RemoveAdminPaths();\r
1582                 ATLASSERT(pathList.GetCount()==2);\r
1583                 pathList.Clear();\r
1584                 pathList.AddPath(CTGitPath(_T("c:\\test")));\r
1585                 pathList.RemoveAdminPaths();\r
1586                 ATLASSERT(pathList.GetCount()==1);\r
1587         }\r
1588         \r
1589         void SortTest()\r
1590         {\r
1591                 CTGitPathList testList;\r
1592                 CTGitPath testPath;\r
1593                 testPath.SetFromUnknown(_T("c:/Z"));\r
1594                 testList.AddPath(testPath);\r
1595                 testPath.SetFromUnknown(_T("c:/B"));\r
1596                 testList.AddPath(testPath);\r
1597                 testPath.SetFromUnknown(_T("c:\\a"));\r
1598                 testList.AddPath(testPath);\r
1599                 testPath.SetFromUnknown(_T("c:/Test"));\r
1600                 testList.AddPath(testPath);\r
1601 \r
1602                 testList.SortByPathname();\r
1603 \r
1604                 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));\r
1605                 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));\r
1606                 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));\r
1607                 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));\r
1608         }\r
1609 \r
1610         void RawAppendTest()\r
1611         {\r
1612                 CTGitPath testPath(_T("c:/test/"));\r
1613                 testPath.AppendRawString(_T("/Hello"));\r
1614                 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));\r
1615 \r
1616                 testPath.AppendRawString(_T("\\T2"));\r
1617                 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));\r
1618 \r
1619                 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));\r
1620                 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));\r
1621                 testBasePath.AppendRawString(testFilePath.GetFileExtension());\r
1622                 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));\r
1623         }\r
1624 \r
1625         void PathAppendTest()\r
1626         {\r
1627                 CTGitPath testPath(_T("c:/test/"));\r
1628                 testPath.AppendPathString(_T("/Hello"));\r
1629                 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));\r
1630 \r
1631                 testPath.AppendPathString(_T("T2"));\r
1632                 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));\r
1633 \r
1634                 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));\r
1635                 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));\r
1636                 // You wouldn't want to do this in real life - you'd use append-raw\r
1637                 testBasePath.AppendPathString(testFilePath.GetFileExtension());\r
1638                 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));\r
1639         }\r
1640 \r
1641         void RemoveDuplicatesTest()\r
1642         {\r
1643                 CTGitPathList list;\r
1644                 list.AddPath(CTGitPath(_T("Z")));\r
1645                 list.AddPath(CTGitPath(_T("A")));\r
1646                 list.AddPath(CTGitPath(_T("E")));\r
1647                 list.AddPath(CTGitPath(_T("E")));\r
1648 \r
1649                 ATLASSERT(list[2].IsEquivalentTo(list[3]));\r
1650                 ATLASSERT(list[2]==list[3]);\r
1651                 \r
1652                 ATLASSERT(list.GetCount() == 4);\r
1653 \r
1654                 list.RemoveDuplicates();\r
1655 \r
1656                 ATLASSERT(list.GetCount() == 3);\r
1657 \r
1658                 ATLASSERT(list[0].GetWinPathString() == _T("A"));\r
1659                 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);\r
1660                 ATLASSERT(list[2].GetWinPathString() == _T("Z"));\r
1661         }\r
1662         \r
1663         void RemoveChildrenTest()\r
1664         {\r
1665                 CTGitPathList list;\r
1666                 list.AddPath(CTGitPath(_T("c:\\test")));\r
1667                 list.AddPath(CTGitPath(_T("c:\\test\\file")));\r
1668                 list.AddPath(CTGitPath(_T("c:\\testfile")));\r
1669                 list.AddPath(CTGitPath(_T("c:\\parent")));\r
1670                 list.AddPath(CTGitPath(_T("c:\\parent\\child")));\r
1671                 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));\r
1672                 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));\r
1673                 \r
1674                 ATLASSERT(list.GetCount() == 7);\r
1675 \r
1676                 list.RemoveChildren();\r
1677                 \r
1678                 ATLTRACE("count = %d\n", list.GetCount());\r
1679                 ATLASSERT(list.GetCount() == 3);\r
1680 \r
1681                 list.SortByPathname();\r
1682 \r
1683                 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);\r
1684                 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);\r
1685                 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);\r
1686         }\r
1687 \r
1688 #if defined(_MFC_VER)\r
1689         void ListLoadingTest()\r
1690         {\r
1691                 TCHAR buf[MAX_PATH];\r
1692                 GetCurrentDirectory(MAX_PATH, buf);\r
1693                 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));\r
1694                 CTGitPathList testList;\r
1695                 testList.LoadFromAsteriskSeparatedString(sPathList);\r
1696 \r
1697                 ATLASSERT(testList.GetCount() == 3);\r
1698                 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));\r
1699                 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));\r
1700                 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));\r
1701                 \r
1702                 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));\r
1703                 testList.Clear();\r
1704                 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");\r
1705                 testList.LoadFromAsteriskSeparatedString(sPathList);\r
1706                 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));\r
1707         }\r
1708 #endif \r
1709 \r
1710         void ContainingDirectoryTest()\r
1711         {\r
1712 \r
1713                 CTGitPath testPath;\r
1714                 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));\r
1715                 CTGitPath dir;\r
1716                 dir = testPath.GetContainingDirectory();\r
1717                 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));\r
1718                 dir = dir.GetContainingDirectory();\r
1719                 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));\r
1720                 dir = dir.GetContainingDirectory();\r
1721                 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));\r
1722                 dir = dir.GetContainingDirectory();\r
1723                 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));\r
1724                 dir = dir.GetContainingDirectory();\r
1725                 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));\r
1726                 dir = dir.GetContainingDirectory();\r
1727                 ATLASSERT(dir.IsEmpty());\r
1728                 ATLASSERT(dir.GetWinPathString() == _T(""));\r
1729         }\r
1730         \r
1731         void AncestorTest()\r
1732         {\r
1733                 CTGitPath testPath;\r
1734                 testPath.SetFromWin(_T("c:\\windows"));\r
1735                 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);\r
1736                 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));\r
1737                 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);\r
1738                 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));\r
1739                 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));\r
1740         }\r
1741 \r
1742         void SubversionPathTest()\r
1743         {\r
1744                 CTGitPath testPath;\r
1745                 testPath.SetFromWin(_T("c:\\"));\r
1746                 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);\r
1747                 testPath.SetFromWin(_T("c:\\folder"));\r
1748                 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);\r
1749                 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));\r
1750                 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);\r
1751                 testPath.SetFromUnknown(_T("http://testing/"));\r
1752                 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);\r
1753                 testPath.SetFromGit(NULL);\r
1754                 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);\r
1755 #if defined(_MFC_VER)\r
1756                 testPath.SetFromUnknown(_T("http://testing again"));\r
1757                 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);\r
1758                 testPath.SetFromUnknown(_T("http://testing%20again"));\r
1759                 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);\r
1760                 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));\r
1761                 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);          \r
1762 #endif\r
1763         }\r
1764 \r
1765         void GetCommonRootTest()\r
1766         {\r
1767                 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));\r
1768                 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));\r
1769                 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));\r
1770                 \r
1771                 CTGitPathList list;\r
1772                 list.AddPath(pathA);\r
1773                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);\r
1774                 list.AddPath(pathB);\r
1775                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);\r
1776                 list.AddPath(pathC);\r
1777                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);\r
1778 #ifdef _MFC_VER\r
1779                 list.Clear();\r
1780                 CString sPathList = _T("D:\\Development\\StExBar\\StExBar\\src\\setup\\Setup64.wxs*D:\\Development\\StExBar\\StExBar\\src\\setup\\Setup.wxs*D:\\Development\\StExBar\\SKTimeStamp\\src\\setup\\Setup.wxs*D:\\Development\\StExBar\\SKTimeStamp\\src\\setup\\Setup64.wxs");\r
1781                 list.LoadFromAsteriskSeparatedString(sPathList);\r
1782                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);\r
1783 \r
1784                 list.Clear();\r
1785                 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");\r
1786                 list.LoadFromAsteriskSeparatedString(sPathList);\r
1787                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);\r
1788 \r
1789                 list.Clear();\r
1790                 sPathList = _T("c:\\windows\\*c:\\windows");\r
1791                 list.LoadFromAsteriskSeparatedString(sPathList);\r
1792                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);\r
1793 \r
1794                 list.Clear();\r
1795                 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");\r
1796                 list.LoadFromAsteriskSeparatedString(sPathList);\r
1797                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);\r
1798 \r
1799                 list.Clear();\r
1800                 sPathList = _T("c:\\windowsdummy*c:\\windows");\r
1801                 list.LoadFromAsteriskSeparatedString(sPathList);\r
1802                 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);\r
1803 #endif\r
1804         }\r
1805         \r
1806         void ValidPathAndUrlTest()\r
1807         {\r
1808                 CTGitPath testPath;\r
1809                 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));\r
1810                 ATLASSERT(testPath.IsValidOnWindows());\r
1811                 testPath.SetFromWin(_T("c:\\"));\r
1812                 ATLASSERT(testPath.IsValidOnWindows());\r
1813                 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));\r
1814                 ATLASSERT(testPath.IsValidOnWindows());\r
1815                 testPath.SetFromWin(_T("c"));\r
1816                 ATLASSERT(testPath.IsValidOnWindows());\r
1817                 testPath.SetFromWin(_T("c:\\test folder\\file"));\r
1818                 ATLASSERT(testPath.IsValidOnWindows());\r
1819                 testPath.SetFromWin(_T("c:\\folder\\"));\r
1820                 ATLASSERT(testPath.IsValidOnWindows());\r
1821                 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));\r
1822                 ATLASSERT(testPath.IsValidOnWindows());\r
1823                 testPath.SetFromWin(_T("c:\\.svn"));\r
1824                 ATLASSERT(testPath.IsValidOnWindows());\r
1825                 testPath.SetFromWin(_T("c:\\com\\file"));\r
1826                 ATLASSERT(testPath.IsValidOnWindows());\r
1827                 testPath.SetFromWin(_T("c:\\test\\conf"));\r
1828                 ATLASSERT(testPath.IsValidOnWindows());\r
1829                 testPath.SetFromWin(_T("c:\\LPT"));\r
1830                 ATLASSERT(testPath.IsValidOnWindows());\r
1831                 testPath.SetFromWin(_T("c:\\test\\LPT"));\r
1832                 ATLASSERT(testPath.IsValidOnWindows());\r
1833                 testPath.SetFromWin(_T("c:\\com1test"));\r
1834                 ATLASSERT(testPath.IsValidOnWindows());\r
1835                 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));\r
1836                 ATLASSERT(testPath.IsValidOnWindows());\r
1837 \r
1838                 testPath.SetFromWin(_T("\\\\Share\\filename"));\r
1839                 ATLASSERT(testPath.IsValidOnWindows());\r
1840                 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));\r
1841                 ATLASSERT(testPath.IsValidOnWindows());\r
1842                 testPath.SetFromWin(_T("\\\\Share\\.svn"));\r
1843                 ATLASSERT(testPath.IsValidOnWindows());\r
1844 \r
1845                 // now the negative tests\r
1846                 testPath.SetFromWin(_T("c:\\test:folder"));\r
1847                 ATLASSERT(!testPath.IsValidOnWindows());\r
1848                 testPath.SetFromWin(_T("c:\\file<name"));\r
1849                 ATLASSERT(!testPath.IsValidOnWindows());\r
1850                 testPath.SetFromWin(_T("c:\\something*else"));\r
1851                 ATLASSERT(!testPath.IsValidOnWindows());\r
1852                 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));\r
1853                 ATLASSERT(!testPath.IsValidOnWindows());\r
1854                 testPath.SetFromWin(_T("c:\\ext.>ension"));\r
1855                 ATLASSERT(!testPath.IsValidOnWindows());\r
1856                 testPath.SetFromWin(_T("c:\\com1\\filename"));\r
1857                 ATLASSERT(!testPath.IsValidOnWindows());\r
1858                 testPath.SetFromWin(_T("c:\\com1"));\r
1859                 ATLASSERT(!testPath.IsValidOnWindows());\r
1860                 testPath.SetFromWin(_T("c:\\com1\\AuX"));\r
1861                 ATLASSERT(!testPath.IsValidOnWindows());\r
1862 \r
1863                 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));\r
1864                 ATLASSERT(!testPath.IsValidOnWindows());\r
1865                 testPath.SetFromWin(_T("\\\\Share\\prn"));\r
1866                 ATLASSERT(!testPath.IsValidOnWindows());\r
1867                 testPath.SetFromWin(_T("\\\\Share\\NUL"));\r
1868                 ATLASSERT(!testPath.IsValidOnWindows());\r
1869                 \r
1870                 // now come some URL tests\r
1871                 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));\r
1872                 ATLASSERT(testPath.IsValidOnWindows());\r
1873                 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));\r
1874                 ATLASSERT(testPath.IsValidOnWindows());\r
1875                 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));\r
1876                 ATLASSERT(testPath.IsValidOnWindows());\r
1877                 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));\r
1878                 ATLASSERT(testPath.IsValidOnWindows());\r
1879                 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));\r
1880                 ATLASSERT(testPath.IsValidOnWindows());\r
1881                 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));\r
1882                 ATLASSERT(testPath.IsValidOnWindows());\r
1883                 // and some negative URL tests\r
1884                 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));\r
1885                 ATLASSERT(!testPath.IsValidOnWindows());\r
1886                 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));\r
1887                 ATLASSERT(!testPath.IsValidOnWindows());\r
1888                 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));\r
1889                 ATLASSERT(!testPath.IsValidOnWindows());\r
1890                 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));\r
1891                 ATLASSERT(!testPath.IsValidOnWindows());\r
1892                 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));\r
1893                 ATLASSERT(!testPath.IsValidOnWindows());\r
1894                 \r
1895         }\r
1896 \r
1897 } TGitPathTestobject;\r
1898 #endif\r
1899 #endif\r
1900 \r
1901 CTGitPath * CTGitPathList::LookForGitPath(CString path)\r
1902 {\r
1903         int i=0;\r
1904         for(i=0;i<this->GetCount();i++)\r
1905         {\r
1906                 if((*this)[i].GetGitPathString() == path )\r
1907                         return (CTGitPath*)&(*this)[i];\r
1908         }\r
1909         return NULL;\r
1910 }\r
1911 CString CTGitPath::GetActionName(int action)\r
1912 {\r
1913         if(action  & CTGitPath::LOGACTIONS_UNMERGED)\r
1914                 return _T("Conflict");\r
1915         if(action  & CTGitPath::LOGACTIONS_ADDED)\r
1916                 return _T("Added");\r
1917         if(action  & CTGitPath::LOGACTIONS_DELETED)\r
1918                 return _T("Deleted");\r
1919         if(action  & CTGitPath::LOGACTIONS_MERGED )\r
1920                 return _T("Merged");\r
1921 \r
1922         if(action  & CTGitPath::LOGACTIONS_MODIFIED)\r
1923                 return _T("Modified");\r
1924         if(action  & CTGitPath::LOGACTIONS_REPLACED)\r
1925                 return _T("Rename");\r
1926         if(action  & CTGitPath::LOGACTIONS_COPY)\r
1927                 return _T("Copy");\r
1928 \r
1929         if(action  & CTGitPath::LOGACTIONS_FORWORD )\r
1930                 return _T("Forward");\r
1931 \r
1932         if(action & CTGitPath::LOGACTIONS_REBASE_EDIT)\r
1933                 return _T("Edit");\r
1934         if(action & CTGitPath::LOGACTIONS_REBASE_SQUASH)\r
1935                 return _T("Squash");\r
1936         if(action & CTGitPath::LOGACTIONS_REBASE_PICK)\r
1937                 return _T("Pick");\r
1938         if(action & CTGitPath::LOGACTIONS_REBASE_SKIP)\r
1939                 return _T("Skip");\r
1940 \r
1941         return _T("Unknown");\r
1942 }\r
1943 CString CTGitPath::GetActionName()\r
1944 {\r
1945         return GetActionName(m_Action);\r
1946 }\r
1947 \r
1948 int CTGitPathList::GetAction()\r
1949 {\r
1950         return m_Action;\r
1951 }\r
1952 \r