Try to enable show merged file
[TortoiseGit.git] / src / Git / TGitPath.cpp
blobecf5a732e7ae460cad9b03667091b7d5aa67ed96
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "TGitPath.h"
21 #include "UnicodeUtils.h"
22 #include "GitAdminDir.h"
23 #include "PathUtils.h"
24 #include <regex>
25 #include "git.h"
26 #if defined(_MFC_VER)
27 //#include "MessageBox.h"
28 //#include "AppUtils.h"
29 #endif
31 #ifndef ASSERT
32 #define ASSERT()
33 #endif
34 using namespace std;
35 extern CGit g_Git;
37 CTGitPath::CTGitPath(void) :
38 m_bDirectoryKnown(false),
39 m_bIsDirectory(false),
40 m_bIsURL(false),
41 m_bURLKnown(false),
42 m_bHasAdminDirKnown(false),
43 m_bHasAdminDir(false),
44 m_bIsValidOnWindowsKnown(false),
45 m_bIsReadOnly(false),
46 m_bIsAdminDirKnown(false),
47 m_bIsAdminDir(false),
48 m_bExists(false),
49 m_bExistsKnown(false),
50 m_bLastWriteTimeKnown(0),
51 m_lastWriteTime(0),
52 m_customData(NULL),
53 m_bIsSpecialDirectoryKnown(false),
54 m_bIsSpecialDirectory(false)
56 m_Action=0;
59 CTGitPath::~CTGitPath(void)
62 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
63 CTGitPath::CTGitPath(const CString& sUnknownPath) :
64 m_bDirectoryKnown(false),
65 m_bIsDirectory(false),
66 m_bIsURL(false),
67 m_bURLKnown(false),
68 m_bHasAdminDirKnown(false),
69 m_bHasAdminDir(false),
70 m_bIsValidOnWindowsKnown(false),
71 m_bIsReadOnly(false),
72 m_bIsAdminDirKnown(false),
73 m_bIsAdminDir(false),
74 m_bExists(false),
75 m_bExistsKnown(false),
76 m_bLastWriteTimeKnown(0),
77 m_lastWriteTime(0),
78 m_customData(NULL),
79 m_bIsSpecialDirectoryKnown(false),
80 m_bIsSpecialDirectory(false)
82 SetFromUnknown(sUnknownPath);
83 m_Action=0;
84 m_Stage=0;
87 int CTGitPath::ParserAction(BYTE action)
89 //action=action.TrimLeft();
90 //TCHAR c=action.GetAt(0);
91 if(action == 'M')
92 m_Action|= LOGACTIONS_MODIFIED;
93 if(action == 'R')
94 m_Action|= LOGACTIONS_REPLACED;
95 if(action == 'A')
96 m_Action|= LOGACTIONS_ADDED;
97 if(action == 'D')
98 m_Action|= LOGACTIONS_DELETED;
99 if(action == 'U')
100 m_Action|= LOGACTIONS_UNMERGED;
101 if(action == 'K')
102 m_Action|= LOGACTIONS_DELETED;
103 if(action == 'H')
104 m_Action|= LOGACTIONS_CACHE;
105 if(action == 'C' )
106 m_Action|= LOGACTIONS_COPY;
108 return m_Action;
110 void CTGitPath::SetFromGit(const char* pPath)
112 Reset();
113 if (pPath == NULL)
114 return;
115 int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);
116 if (len)
118 len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, m_sFwdslashPath.GetBuffer(len+1), len+1);
119 m_sFwdslashPath.ReleaseBuffer(len-1);
121 SanitizeRootPath(m_sFwdslashPath, true);
124 void CTGitPath::SetFromGit(const char* pPath, bool bIsDirectory)
126 SetFromGit(pPath);
127 m_bDirectoryKnown = true;
128 m_bIsDirectory = bIsDirectory;
131 void CTGitPath::SetFromGit(const TCHAR* pPath, bool bIsDirectory)
133 Reset();
134 if (pPath)
136 m_sFwdslashPath = pPath;
137 SanitizeRootPath(m_sFwdslashPath, true);
139 m_bDirectoryKnown = true;
140 m_bIsDirectory = bIsDirectory;
143 void CTGitPath::SetFromGit(const CString& sPath,CString *oldpath)
145 Reset();
146 m_sFwdslashPath = sPath;
147 SanitizeRootPath(m_sFwdslashPath, true);
148 if(oldpath)
149 m_sOldFwdslashPath = *oldpath;
152 void CTGitPath::SetFromWin(LPCTSTR pPath)
154 Reset();
155 m_sBackslashPath = pPath;
156 SanitizeRootPath(m_sBackslashPath, false);
157 ATLASSERT(m_sBackslashPath.Find('/')<0);
159 void CTGitPath::SetFromWin(const CString& sPath)
161 Reset();
162 m_sBackslashPath = sPath;
163 SanitizeRootPath(m_sBackslashPath, false);
165 void CTGitPath::SetFromWin(const CString& sPath, bool bIsDirectory)
167 Reset();
168 m_sBackslashPath = sPath;
169 m_bIsDirectory = bIsDirectory;
170 m_bDirectoryKnown = true;
171 SanitizeRootPath(m_sBackslashPath, false);
173 void CTGitPath::SetFromUnknown(const CString& sPath)
175 Reset();
176 // Just set whichever path we think is most likely to be used
177 // GitAdminDir admin;
178 // CString p;
179 // if(admin.HasAdminDir(sPath,&p))
180 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
181 // else
182 SetFwdslashPath(sPath);
185 LPCTSTR CTGitPath::GetWinPath() const
187 if(IsEmpty())
189 return _T("");
191 if(m_sBackslashPath.IsEmpty())
193 SetBackslashPath(m_sFwdslashPath);
195 return m_sBackslashPath;
197 // This is a temporary function, to be used during the migration to
198 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
199 const CString& CTGitPath::GetWinPathString() const
201 if(m_sBackslashPath.IsEmpty())
203 SetBackslashPath(m_sFwdslashPath);
205 return m_sBackslashPath;
208 const CString& CTGitPath::GetGitPathString() const
210 if(m_sFwdslashPath.IsEmpty())
212 SetFwdslashPath(m_sBackslashPath);
214 return m_sFwdslashPath;
217 const CString &CTGitPath::GetGitOldPathString() const
219 return m_sOldFwdslashPath;
221 #if 0
222 const char* CTGitPath::GetGitApiPath(apr_pool_t *pool) const
224 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
225 // each call to GetGitApiPath returns a different pointer value.
226 // If you made multiple calls to GetGitApiPath on the same string, only the last
227 // one would give you a valid pointer to an empty string, because each
228 // call would invalidate the previous call's return.
229 if(IsEmpty())
231 return "";
233 if(m_sFwdslashPath.IsEmpty())
235 SetFwdslashPath(m_sBackslashPath);
237 if(m_sUTF8FwdslashPath.IsEmpty())
239 SetUTF8FwdslashPath(m_sFwdslashPath);
241 if (svn_path_is_url(m_sUTF8FwdslashPath))
243 m_sUTF8FwdslashPathEscaped = CPathUtils::PathEscape(m_sUTF8FwdslashPath);
244 m_sUTF8FwdslashPathEscaped.Replace("file:////", "file:///\\");
245 m_sUTF8FwdslashPathEscaped = svn_path_canonicalize(m_sUTF8FwdslashPathEscaped, pool);
246 return m_sUTF8FwdslashPathEscaped;
248 m_sUTF8FwdslashPath = svn_path_canonicalize(m_sUTF8FwdslashPath, pool);
250 return m_sUTF8FwdslashPath;
252 #endif
254 const CString& CTGitPath::GetUIPathString() const
256 if (m_sUIPath.IsEmpty())
258 #if defined(_MFC_VER)
259 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
260 if (IsUrl())
262 m_sUIPath = CPathUtils::PathUnescape(GetGitPathString());
263 m_sUIPath.Replace(_T("file:////"), _T("file:///\\"));
266 else
267 #endif
269 m_sUIPath = GetWinPathString();
272 return m_sUIPath;
275 void CTGitPath::SetFwdslashPath(const CString& sPath) const
277 m_sFwdslashPath = sPath;
278 m_sFwdslashPath.Replace('\\', '/');
280 // We don't leave a trailing /
281 m_sFwdslashPath.TrimRight('/');
283 SanitizeRootPath(m_sFwdslashPath, true);
285 m_sFwdslashPath.Replace(_T("file:////"), _T("file:///\\"));
287 m_sUTF8FwdslashPath.Empty();
290 void CTGitPath::SetBackslashPath(const CString& sPath) const
292 m_sBackslashPath = sPath;
293 m_sBackslashPath.Replace('/', '\\');
294 m_sBackslashPath.TrimRight('\\');
295 SanitizeRootPath(m_sBackslashPath, false);
298 void CTGitPath::SetUTF8FwdslashPath(const CString& sPath) const
300 m_sUTF8FwdslashPath = CUnicodeUtils::GetUTF8(sPath);
303 void CTGitPath::SanitizeRootPath(CString& sPath, bool bIsForwardPath) const
305 // Make sure to add the trailing slash to root paths such as 'C:'
306 if (sPath.GetLength() == 2 && sPath[1] == ':')
308 sPath += (bIsForwardPath) ? _T("/") : _T("\\");
312 bool CTGitPath::IsUrl() const
314 #if 0
315 if (!m_bURLKnown)
317 EnsureFwdslashPathSet();
318 if(m_sUTF8FwdslashPath.IsEmpty())
320 SetUTF8FwdslashPath(m_sFwdslashPath);
322 m_bIsURL = !!svn_path_is_url(m_sUTF8FwdslashPath);
323 m_bURLKnown = true;
325 return m_bIsURL;
326 #endif
327 return false;
330 bool CTGitPath::IsDirectory() const
332 if(!m_bDirectoryKnown)
334 UpdateAttributes();
336 return m_bIsDirectory;
339 bool CTGitPath::Exists() const
341 if (!m_bExistsKnown)
343 UpdateAttributes();
345 return m_bExists;
348 bool CTGitPath::Delete(bool bTrash) const
350 EnsureBackslashPathSet();
351 ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);
352 bool bRet = false;
353 if (Exists())
355 if ((bTrash)||(IsDirectory()))
357 TCHAR * buf = new TCHAR[m_sBackslashPath.GetLength()+2];
358 _tcscpy_s(buf, m_sBackslashPath.GetLength()+2, m_sBackslashPath);
359 buf[m_sBackslashPath.GetLength()] = 0;
360 buf[m_sBackslashPath.GetLength()+1] = 0;
361 SHFILEOPSTRUCT shop = {0};
362 shop.wFunc = FO_DELETE;
363 shop.pFrom = buf;
364 shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
365 if (bTrash)
366 shop.fFlags |= FOF_ALLOWUNDO;
367 bRet = (SHFileOperation(&shop) == 0);
368 delete [] buf;
370 else
372 bRet = !!::DeleteFile(m_sBackslashPath);
375 m_bExists = false;
376 m_bExistsKnown = true;
377 return bRet;
380 __int64 CTGitPath::GetLastWriteTime() const
382 if(!m_bLastWriteTimeKnown)
384 UpdateAttributes();
386 return m_lastWriteTime;
389 bool CTGitPath::IsReadOnly() const
391 if(!m_bLastWriteTimeKnown)
393 UpdateAttributes();
395 return m_bIsReadOnly;
398 void CTGitPath::UpdateAttributes() const
400 EnsureBackslashPathSet();
401 WIN32_FILE_ATTRIBUTE_DATA attribs;
402 if(GetFileAttributesEx(m_sBackslashPath, GetFileExInfoStandard, &attribs))
404 m_bIsDirectory = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
405 m_lastWriteTime = *(__int64*)&attribs.ftLastWriteTime;
406 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
407 m_bExists = true;
409 else
411 DWORD err = GetLastError();
412 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))
414 m_bIsDirectory = false;
415 m_lastWriteTime = 0;
416 m_bExists = false;
418 else
420 m_bIsDirectory = false;
421 m_lastWriteTime = 0;
422 m_bExists = true;
423 return;
426 m_bDirectoryKnown = true;
427 m_bLastWriteTimeKnown = true;
428 m_bExistsKnown = true;
431 CTGitPath CTGitPath::GetSubPath(CTGitPath &root)
433 CTGitPath path;
435 if(GetWinPathString().Left(root.GetWinPathString().GetLength()) == root.GetWinPathString())
437 CString str=GetWinPathString();
438 path.SetFromWin(str.Right(str.GetLength()-root.GetWinPathString().GetLength()-1));
440 return path;
443 void CTGitPath::EnsureBackslashPathSet() const
445 if(m_sBackslashPath.IsEmpty())
447 SetBackslashPath(m_sFwdslashPath);
448 ATLASSERT(IsEmpty() || !m_sBackslashPath.IsEmpty());
451 void CTGitPath::EnsureFwdslashPathSet() const
453 if(m_sFwdslashPath.IsEmpty())
455 SetFwdslashPath(m_sBackslashPath);
456 ATLASSERT(IsEmpty() || !m_sFwdslashPath.IsEmpty());
461 // Reset all the caches
462 void CTGitPath::Reset()
464 m_bDirectoryKnown = false;
465 m_bURLKnown = false;
466 m_bLastWriteTimeKnown = false;
467 m_bHasAdminDirKnown = false;
468 m_bIsValidOnWindowsKnown = false;
469 m_bIsAdminDirKnown = false;
470 m_bExistsKnown = false;
471 m_bIsSpecialDirectoryKnown = false;
472 m_bIsSpecialDirectory = false;
474 m_sBackslashPath.Empty();
475 m_sFwdslashPath.Empty();
476 m_sUTF8FwdslashPath.Empty();
477 this->m_Action=0;
478 this->m_StatAdd=_T("");
479 this->m_StatDel=_T("");
480 ATLASSERT(IsEmpty());
483 CTGitPath CTGitPath::GetDirectory() const
485 if ((IsDirectory())||(!Exists()))
487 return *this;
489 return GetContainingDirectory();
492 CTGitPath CTGitPath::GetContainingDirectory() const
494 EnsureBackslashPathSet();
496 CString sDirName = m_sBackslashPath.Left(m_sBackslashPath.ReverseFind('\\'));
497 if(sDirName.GetLength() == 2 && sDirName[1] == ':')
499 // This is a root directory, which needs a trailing slash
500 sDirName += '\\';
501 if(sDirName == m_sBackslashPath)
503 // We were clearly provided with a root path to start with - we should return nothing now
504 sDirName.Empty();
507 if(sDirName.GetLength() == 1 && sDirName[0] == '\\')
509 // We have an UNC path and we already are the root
510 sDirName.Empty();
512 CTGitPath retVal;
513 retVal.SetFromWin(sDirName);
514 return retVal;
517 CString CTGitPath::GetRootPathString() const
519 EnsureBackslashPathSet();
520 CString workingPath = m_sBackslashPath;
521 LPTSTR pPath = workingPath.GetBuffer(MAX_PATH); // MAX_PATH ok here.
522 ATLVERIFY(::PathStripToRoot(pPath));
523 workingPath.ReleaseBuffer();
524 return workingPath;
528 CString CTGitPath::GetFilename() const
530 ATLASSERT(!IsDirectory());
531 return GetFileOrDirectoryName();
534 CString CTGitPath::GetFileOrDirectoryName() const
536 EnsureBackslashPathSet();
537 return m_sBackslashPath.Mid(m_sBackslashPath.ReverseFind('\\')+1);
540 CString CTGitPath::GetUIFileOrDirectoryName() const
542 GetUIPathString();
543 return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);
546 CString CTGitPath::GetFileExtension() const
548 if(!IsDirectory())
550 EnsureBackslashPathSet();
551 int dotPos = m_sBackslashPath.ReverseFind('.');
552 int slashPos = m_sBackslashPath.ReverseFind('\\');
553 if (dotPos > slashPos)
554 return m_sBackslashPath.Mid(dotPos);
556 return CString();
558 CString CTGitPath::GetBaseFilename() const
560 int dot;
561 CString filename=GetFilename();
562 dot = filename.ReverseFind(_T('.'));
563 if(dot>0)
564 return filename.Left(dot);
565 else
566 return filename;
569 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)
571 int length = sP1.GetLength();
572 if(length != sP2.GetLength())
574 // Different lengths
575 return false;
577 // We work from the end of the strings, because path differences
578 // are more likely to occur at the far end of a string
579 LPCTSTR pP1Start = sP1;
580 LPCTSTR pP1 = pP1Start+(length-1);
581 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
582 while(length-- > 0)
584 if(_totlower(*pP1--) != _totlower(*pP2--))
586 return false;
589 return true;
592 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)
594 int length = sP1.GetLength();
595 if(length != sP2.GetLength())
597 // Different lengths
598 return false;
600 // We work from the end of the strings, because path differences
601 // are more likely to occur at the far end of a string
602 LPCTSTR pP1Start = sP1;
603 LPCTSTR pP1 = pP1Start+(length-1);
604 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
605 while(length-- > 0)
607 if((*pP1--) != (*pP2--))
609 return false;
612 return true;
615 bool CTGitPath::IsEmpty() const
617 // Check the backward slash path first, since the chance that this
618 // one is set is higher. In case of a 'false' return value it's a little
619 // bit faster.
620 return m_sBackslashPath.IsEmpty() && m_sFwdslashPath.IsEmpty();
623 // Test if both paths refer to the same item
624 // Ignores case and slash direction
625 bool CTGitPath::IsEquivalentTo(const CTGitPath& rhs) const
627 // Try and find a slash direction which avoids having to convert
628 // both filenames
629 if(!m_sBackslashPath.IsEmpty())
631 // *We've* got a \ path - make sure that the RHS also has a \ path
632 rhs.EnsureBackslashPathSet();
633 return ArePathStringsEqualWithCase(m_sBackslashPath, rhs.m_sBackslashPath);
635 else
637 // Assume we've got a fwdslash path and make sure that the RHS has one
638 rhs.EnsureFwdslashPathSet();
639 return ArePathStringsEqualWithCase(m_sFwdslashPath, rhs.m_sFwdslashPath);
643 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath& rhs) const
645 // Try and find a slash direction which avoids having to convert
646 // both filenames
647 if(!m_sBackslashPath.IsEmpty())
649 // *We've* got a \ path - make sure that the RHS also has a \ path
650 rhs.EnsureBackslashPathSet();
651 return ArePathStringsEqual(m_sBackslashPath, rhs.m_sBackslashPath);
653 else
655 // Assume we've got a fwdslash path and make sure that the RHS has one
656 rhs.EnsureFwdslashPathSet();
657 return ArePathStringsEqual(m_sFwdslashPath, rhs.m_sFwdslashPath);
661 bool CTGitPath::IsAncestorOf(const CTGitPath& possibleDescendant) const
663 possibleDescendant.EnsureBackslashPathSet();
664 EnsureBackslashPathSet();
666 bool bPathStringsEqual = ArePathStringsEqual(m_sBackslashPath, possibleDescendant.m_sBackslashPath.Left(m_sBackslashPath.GetLength()));
667 if (m_sBackslashPath.GetLength() >= possibleDescendant.GetWinPathString().GetLength())
669 return bPathStringsEqual;
672 return (bPathStringsEqual &&
673 ((possibleDescendant.m_sBackslashPath[m_sBackslashPath.GetLength()] == '\\')||
674 (m_sBackslashPath.GetLength()==3 && m_sBackslashPath[1]==':')));
677 // Get a string representing the file path, optionally with a base
678 // section stripped off the front.
679 CString CTGitPath::GetDisplayString(const CTGitPath* pOptionalBasePath /* = NULL*/) const
681 EnsureFwdslashPathSet();
682 if(pOptionalBasePath != NULL)
684 // Find the length of the base-path without having to do an 'ensure' on it
685 int baseLength = max(pOptionalBasePath->m_sBackslashPath.GetLength(), pOptionalBasePath->m_sFwdslashPath.GetLength());
687 // Now, chop that baseLength of the front of the path
688 return m_sFwdslashPath.Mid(baseLength).TrimLeft('/');
690 return m_sFwdslashPath;
693 int CTGitPath::Compare(const CTGitPath& left, const CTGitPath& right)
695 left.EnsureBackslashPathSet();
696 right.EnsureBackslashPathSet();
697 return left.m_sBackslashPath.CompareNoCase(right.m_sBackslashPath);
700 bool operator<(const CTGitPath& left, const CTGitPath& right)
702 return CTGitPath::Compare(left, right) < 0;
705 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right)
707 return left.IsEquivalentTo(right);
710 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right)
712 if (left.IsAdminDir() && right.IsAdminDir())
714 CTGitPath l = left;
715 CTGitPath r = right;
718 l = l.GetContainingDirectory();
719 } while(l.HasAdminDir());
722 r = r.GetContainingDirectory();
723 } while(r.HasAdminDir());
724 return l.GetContainingDirectory().IsEquivalentTo(r.GetContainingDirectory());
726 return left.GetDirectory().IsEquivalentTo(right.GetDirectory());
729 bool CTGitPath::CheckChild(const CTGitPath &parent, const CTGitPath& child)
731 return parent.IsAncestorOf(child);
734 void CTGitPath::AppendRawString(const CString& sAppend)
736 EnsureFwdslashPathSet();
737 CString strCopy = m_sFwdslashPath += sAppend;
738 SetFromUnknown(strCopy);
741 void CTGitPath::AppendPathString(const CString& sAppend)
743 EnsureBackslashPathSet();
744 CString cleanAppend(sAppend);
745 cleanAppend.Replace('/', '\\');
746 cleanAppend.TrimLeft('\\');
747 m_sBackslashPath.TrimRight('\\');
748 CString strCopy = m_sBackslashPath + _T("\\") + cleanAppend;
749 SetFromWin(strCopy);
752 bool CTGitPath::HasAdminDir() const
754 if (m_bHasAdminDirKnown)
755 return m_bHasAdminDir;
757 EnsureBackslashPathSet();
758 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
759 m_bHasAdminDirKnown = true;
760 return m_bHasAdminDir;
763 bool CTGitPath::HasSubmodules() const
765 return !g_GitAdminDir.GetSuperProjectRoot(GetWinPathString()).IsEmpty();
768 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const
770 if (m_bHasAdminDirKnown)
772 if (ProjectTopDir)
773 *ProjectTopDir = m_sProjectRoot;
774 return m_bHasAdminDir;
777 EnsureBackslashPathSet();
778 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
779 m_bHasAdminDirKnown = true;
780 if (ProjectTopDir)
781 *ProjectTopDir = m_sProjectRoot;
782 return m_bHasAdminDir;
785 bool CTGitPath::IsAdminDir() const
787 if (m_bIsAdminDirKnown)
788 return m_bIsAdminDir;
790 EnsureBackslashPathSet();
791 m_bIsAdminDir = g_GitAdminDir.IsAdminDirPath(m_sBackslashPath);
792 m_bIsAdminDirKnown = true;
793 return m_bIsAdminDir;
796 bool CTGitPath::IsValidOnWindows() const
798 if (m_bIsValidOnWindowsKnown)
799 return m_bIsValidOnWindows;
801 m_bIsValidOnWindows = false;
802 EnsureBackslashPathSet();
803 CString sMatch = m_sBackslashPath + _T("\r\n");
804 wstring sPattern;
805 // the 'file://' URL is just a normal windows path:
806 if (sMatch.Left(7).CompareNoCase(_T("file:\\\\"))==0)
808 sMatch = sMatch.Mid(7);
809 sMatch.TrimLeft(_T("\\"));
810 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
812 else if (IsUrl())
814 sPattern = _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
816 else
818 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
823 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
824 tr1::wsmatch match;
826 wstring rmatch = wstring((LPCTSTR)sMatch);
827 if (tr1::regex_match(rmatch, match, rx))
829 if (wstring(match[0]).compare(sMatch)==0)
830 m_bIsValidOnWindows = true;
832 if (m_bIsValidOnWindows)
834 // now check for illegal filenames
835 tr1::wregex rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
836 rmatch = m_sBackslashPath;
837 if (tr1::regex_search(rmatch, rx2, tr1::regex_constants::match_default))
838 m_bIsValidOnWindows = false;
841 catch (exception) {}
843 m_bIsValidOnWindowsKnown = true;
844 return m_bIsValidOnWindows;
847 bool CTGitPath::IsSpecialDirectory() const
849 if (m_bIsSpecialDirectoryKnown)
850 return m_bIsSpecialDirectory;
852 static LPCTSTR specialDirectories[]
853 = { _T("trunk"), _T("tags"), _T("branches") };
855 for (int i=0 ; i<(sizeof(specialDirectories) / sizeof(specialDirectories[0])) ; ++i)
857 CString name = GetFileOrDirectoryName();
858 if (0 == name.CompareNoCase(specialDirectories[i]))
860 m_bIsSpecialDirectory = true;
861 break;
865 m_bIsSpecialDirectoryKnown = true;
867 return m_bIsSpecialDirectory;
870 //////////////////////////////////////////////////////////////////////////
872 CTGitPathList::CTGitPathList()
877 // A constructor which allows a path list to be easily built which one initial entry in
878 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)
880 AddPath(firstEntry);
882 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool staged)
884 int pos=0;
885 CString one;
886 CTGitPath path;
887 CString part;
888 while(pos>=0 && pos<out.size())
890 one.Empty();
891 path.Reset();
893 g_Git.StringAppend(&one,&out[pos],CP_ACP);
894 int tabstart=0;
895 path.m_Action=path.ParserAction(out[pos]);
896 one.Tokenize(_T("\t"),tabstart);
898 if(tabstart>=0)
899 path.SetFromGit(one.Right(one.GetLength()-tabstart));
901 tabstart=0;
903 part=one.Tokenize(_T(" "),tabstart); //Tag
905 part=one.Tokenize(_T(" "),tabstart); //Mode
907 part=one.Tokenize(_T(" "),tabstart); //Hash
909 part=one.Tokenize(_T("\t"),tabstart); //Stage
911 path.m_Stage=_ttol(part);
913 this->AddPath(path);
915 pos=out.findNextString(pos);
917 return pos;
919 int CTGitPathList::FillUnRev(int action,CTGitPathList *list)
921 int pos=0;
922 this->Clear();
923 CTGitPath path;
925 int count;
926 if(list==NULL)
927 count=1;
928 else
929 count=list->GetCount();
930 for(int i=0;i<count;i++)
932 CString cmd;
933 pos=0;
935 CString ignored;
936 if(action & CTGitPath::LOGACTIONS_IGNORE)
937 ignored= _T(" --ignored");
939 if(list==NULL)
941 cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");
942 cmd+=ignored;
945 else
946 { cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z %s-- \"%s\""),
947 ignored,
948 (*list)[i].GetWinPathString());
951 BYTE_VECTOR out;
952 out.clear();
953 g_Git.Run(cmd,&out);
955 pos=0;
956 CString one;
957 while( pos>=0 && pos<out.size())
959 one.Empty();
960 g_Git.StringAppend(&one,&out[pos],CP_ACP);
961 if(!one.IsEmpty())
963 //SetFromGit will clear all status
964 path.SetFromGit(one);
965 path.m_Action=action;
966 AddPath(path);
968 pos=out.findNextString(pos);
972 return 0;
974 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log)
976 this->Clear();
977 int pos=0;
978 //BYTE *p=&log[0];
979 //CString one;
980 CTGitPath path;
981 m_Action=0;
982 while( pos>=0 && pos<log.size())
984 //one=log.Tokenize(_T("\n"),pos);
985 path.Reset();
986 if(log[pos]=='\n')
987 pos++;
989 if(log[pos]==':')
991 bool merged=false;
992 if(log[pos+1] ==';')
994 merged=true;
996 int end=log.find(0,pos);
997 int actionstart=-1;
998 int numfile=1;
999 int file1=-1,file2=-1;
1000 if( end>0 )
1002 actionstart=log.find(' ',end-6);
1003 pos=actionstart;
1005 if( actionstart>0 )
1007 actionstart++;
1009 file1 = log.find(0,actionstart);
1010 if( file1>=0 )
1012 file1++;
1013 pos=file1;
1015 if( log[actionstart] == 'C' || log[actionstart] == 'R' )
1017 file2=file1;
1018 numfile=2;
1019 file1 = log.find(0,file1);
1020 if(file1>=0 )
1022 file1++;
1023 pos=file1;
1029 CString pathname1;
1030 CString pathname2;
1032 if( file1>=0 )
1033 g_Git.StringAppend(&pathname1,&log[file1],CP_ACP);
1034 if( file2>=0 )
1035 g_Git.StringAppend(&pathname2,&log[file2],CP_ACP);
1037 CTGitPath *GitPath=LookForGitPath(pathname1);
1039 if(GitPath)
1041 this->m_Action|=GitPath->ParserAction( log[actionstart] );
1043 }else
1045 int ac=path.ParserAction(log[actionstart] );
1047 path.SetFromGit(pathname1,&pathname2);
1048 path.m_Action=ac;
1049 //action must be set after setfromgit. SetFromGit will clear all status.
1050 this->m_Action|=ac;
1051 AddPath(path);
1055 }else
1057 int tabstart=0;
1058 path.Reset();
1059 CString StatAdd;
1060 CString StatDel;
1061 CString file1;
1062 CString file2;
1064 tabstart=log.find('\t',pos);
1065 if(tabstart >=0)
1067 log[tabstart]=0;
1068 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);
1069 pos=tabstart+1;
1072 tabstart=log.find('\t',pos);
1073 if(tabstart >=0)
1075 log[tabstart]=0;
1077 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);
1078 pos=tabstart+1;
1081 if(log[pos] == 0) //rename
1083 pos++;
1084 g_Git.StringAppend(&file2,&log[pos],CP_ACP);
1085 int sec=log.find(0,pos);
1086 if(sec>=0)
1088 sec++;
1089 g_Git.StringAppend(&file1,&log[sec],CP_ACP);
1091 pos=sec;
1093 }else
1095 g_Git.StringAppend(&file1,&log[pos],CP_ACP);
1097 path.SetFromGit(file1,&file2);
1099 CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());
1100 if(GitPath)
1102 GitPath->m_StatAdd=StatAdd;
1103 GitPath->m_StatDel=StatDel;
1104 }else
1106 //path.SetFromGit(pathname);
1107 path.m_StatAdd=StatAdd;
1108 path.m_StatDel=StatDel;
1109 AddPath(path);
1113 pos=log.findNextString(pos);
1115 return pos;
1118 void CTGitPathList::AddPath(const CTGitPath& newPath)
1120 m_paths.push_back(newPath);
1121 m_commonBaseDirectory.Reset();
1123 int CTGitPathList::GetCount() const
1125 return (int)m_paths.size();
1127 void CTGitPathList::Clear()
1129 m_paths.clear();
1130 m_commonBaseDirectory.Reset();
1133 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const
1135 ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());
1136 return m_paths[index];
1139 bool CTGitPathList::AreAllPathsFiles() const
1141 // Look through the vector for any directories - if we find them, return false
1142 return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();
1146 #if defined(_MFC_VER)
1148 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)
1150 Clear();
1153 CString strLine;
1154 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
1156 // for every selected file/folder
1157 CTGitPath path;
1158 while (file.ReadString(strLine))
1160 path.SetFromUnknown(strLine);
1161 AddPath(path);
1163 file.Close();
1165 catch (CFileException* pE)
1167 TRACE("CFileException loading target file list\n");
1168 TCHAR error[10000] = {0};
1169 pE->GetErrorMessage(error, 10000);
1170 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1171 pE->Delete();
1172 return false;
1174 return true;
1177 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const
1181 if (bANSI)
1183 CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
1184 PathVector::const_iterator it;
1185 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1187 CStringA line = CStringA(it->GetGitPathString()) + '\n';
1188 file.Write(line, line.GetLength());
1190 file.Close();
1192 else
1194 CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);
1195 PathVector::const_iterator it;
1196 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1198 file.WriteString(it->GetGitPathString()+_T("\n"));
1200 file.Close();
1203 catch (CFileException* pE)
1205 TRACE("CFileException in writing temp file\n");
1206 pE->Delete();
1207 return false;
1209 return true;
1213 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)
1215 int pos = 0;
1216 CString temp;
1217 for(;;)
1219 temp = sPathString.Tokenize(_T("*"),pos);
1220 if(temp.IsEmpty())
1222 break;
1224 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));
1228 CString CTGitPathList::CreateAsteriskSeparatedString() const
1230 CString sRet;
1231 PathVector::const_iterator it;
1232 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1234 if (!sRet.IsEmpty())
1235 sRet += _T("*");
1236 sRet += it->GetWinPathString();
1238 return sRet;
1240 #endif // _MFC_VER
1242 bool
1243 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1245 // Check if all the paths are files and in the same directory
1246 PathVector::const_iterator it;
1247 m_commonBaseDirectory.Reset();
1248 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1250 if(it->IsDirectory())
1252 return false;
1254 const CTGitPath& baseDirectory = it->GetDirectory();
1255 if(m_commonBaseDirectory.IsEmpty())
1257 m_commonBaseDirectory = baseDirectory;
1259 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1261 // Different path
1262 m_commonBaseDirectory.Reset();
1263 return false;
1266 return true;
1269 CTGitPath CTGitPathList::GetCommonDirectory() const
1271 if (m_commonBaseDirectory.IsEmpty())
1273 PathVector::const_iterator it;
1274 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1276 const CTGitPath& baseDirectory = it->GetDirectory();
1277 if(m_commonBaseDirectory.IsEmpty())
1279 m_commonBaseDirectory = baseDirectory;
1281 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1283 // Different path
1284 m_commonBaseDirectory.Reset();
1285 break;
1289 // since we only checked strings, not paths,
1290 // we have to make sure now that we really return a *path* here
1291 PathVector::const_iterator iter;
1292 for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)
1294 if (!m_commonBaseDirectory.IsAncestorOf(*iter))
1296 m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();
1297 break;
1300 return m_commonBaseDirectory;
1303 CTGitPath CTGitPathList::GetCommonRoot() const
1305 PathVector::const_iterator it;
1306 CString sRoot, sTempRoot;
1307 bool bEqual = true;
1309 if (GetCount() == 1)
1310 return m_paths[0];
1312 int backSlashPos = 0;
1313 int searchStartPos = 0;
1314 while (bEqual)
1316 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1318 if (backSlashPos == 0)
1320 backSlashPos = it->GetWinPathString().Find('\\', searchStartPos+1);
1321 if ((backSlashPos < 0)&&(searchStartPos != it->GetWinPathString().GetLength()))
1322 backSlashPos = it->GetWinPathString().GetLength();
1324 else if (it->GetWinPathString().Find('\\', searchStartPos+1) != backSlashPos)
1326 if (it->GetWinPathString().Find('\\', searchStartPos+1) < 0)
1328 if (it->GetWinPathString().GetLength() != backSlashPos)
1330 bEqual = false;
1331 break;
1334 else
1336 bEqual = false;
1337 break;
1340 if (backSlashPos < 0)
1342 bEqual = false;
1343 break;
1346 if (bEqual == false)
1348 if (searchStartPos)
1349 sRoot = m_paths[0].GetWinPathString().Left(searchStartPos+1);
1351 else
1353 searchStartPos = backSlashPos;
1355 backSlashPos = 0;
1358 return CTGitPath(sRoot.TrimRight('\\'));
1361 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)
1363 std::sort(m_paths.begin(), m_paths.end());
1364 if (bReverse)
1365 std::reverse(m_paths.begin(), m_paths.end());
1368 void CTGitPathList::DeleteAllFiles(bool bTrash)
1370 PathVector::const_iterator it;
1371 if (bTrash)
1373 SortByPathname();
1374 CString sPaths;
1375 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1377 if ((it->Exists())&&(!it->IsDirectory()))
1379 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1380 sPaths += it->GetWinPath();
1381 sPaths += '\0';
1384 sPaths += '\0';
1385 sPaths += '\0';
1386 SHFILEOPSTRUCT shop = {0};
1387 shop.wFunc = FO_DELETE;
1388 shop.pFrom = (LPCTSTR)sPaths;
1389 shop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
1390 SHFileOperation(&shop);
1392 else
1394 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1396 if (!it->IsDirectory())
1398 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1399 ::DeleteFile(it->GetWinPath());
1403 Clear();
1406 void CTGitPathList::RemoveDuplicates()
1408 SortByPathname();
1409 // Remove the duplicates
1410 // (Unique moves them to the end of the vector, then erase chops them off)
1411 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());
1414 void CTGitPathList::RemoveAdminPaths()
1416 PathVector::iterator it;
1417 for(it = m_paths.begin(); it != m_paths.end(); )
1419 if (it->IsAdminDir())
1421 m_paths.erase(it);
1422 it = m_paths.begin();
1424 else
1425 ++it;
1429 void CTGitPathList::RemovePath(const CTGitPath& path)
1431 PathVector::iterator it;
1432 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1434 if (it->IsEquivalentTo(path))
1436 m_paths.erase(it);
1437 return;
1442 void CTGitPathList::RemoveItem(CTGitPath & path)
1444 PathVector::iterator it;
1445 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1447 if (it->GetGitPathString()==path.GetGitPathString())
1449 m_paths.erase(it);
1450 return;
1454 void CTGitPathList::RemoveChildren()
1456 SortByPathname();
1457 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());
1460 bool CTGitPathList::IsEqual(const CTGitPathList& list)
1462 if (list.GetCount() != GetCount())
1463 return false;
1464 for (int i=0; i<list.GetCount(); ++i)
1466 if (!list[i].IsEquivalentTo(m_paths[i]))
1467 return false;
1469 return true;
1472 //////////////////////////////////////////////////////////////////////////
1473 #if 0
1474 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const
1476 apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));
1478 for(int nItem = 0; nItem < GetCount(); nItem++)
1480 const char * target = m_paths[nItem].GetGitApiPath(pool);
1481 (*((const char **) apr_array_push (targets))) = target;
1484 return targets;
1486 #endif
1487 //////////////////////////////////////////////////////////////////////////
1489 #if 0
1490 #if defined(_DEBUG)
1491 // Some test cases for these classes
1492 static class CTGitPathTests
1494 public:
1495 CTGitPathTests()
1497 apr_initialize();
1498 pool = svn_pool_create(NULL);
1499 GetDirectoryTest();
1500 AdminDirTest();
1501 SortTest();
1502 RawAppendTest();
1503 PathAppendTest();
1504 RemoveDuplicatesTest();
1505 RemoveChildrenTest();
1506 ContainingDirectoryTest();
1507 AncestorTest();
1508 SubversionPathTest();
1509 GetCommonRootTest();
1510 #if defined(_MFC_VER)
1511 ValidPathAndUrlTest();
1512 ListLoadingTest();
1513 #endif
1514 apr_terminate();
1517 private:
1518 // apr_pool_t * pool;
1519 void GetDirectoryTest()
1521 // Bit tricky, this test, because we need to know something about the file
1522 // layout on the machine which is running the test
1523 TCHAR winDir[MAX_PATH+1];
1524 GetWindowsDirectory(winDir, MAX_PATH);
1525 CString sWinDir(winDir);
1527 CTGitPath testPath;
1528 // This is a file which we know will always be there
1529 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));
1530 ATLASSERT(!testPath.IsDirectory());
1531 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1532 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);
1534 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1535 // but we know it must be different to the directory itself
1536 testPath.SetFromUnknown(sWinDir);
1537 ATLASSERT(testPath.IsDirectory());
1538 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1539 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);
1540 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());
1542 // Try a root path
1543 testPath.SetFromUnknown(_T("C:\\"));
1544 ATLASSERT(testPath.IsDirectory());
1545 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1546 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1547 // Try a root UNC path
1548 testPath.SetFromUnknown(_T("\\MYSTATION"));
1549 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1552 void AdminDirTest()
1554 CTGitPath testPath;
1555 testPath.SetFromUnknown(_T("c:\\.svndir"));
1556 ATLASSERT(!testPath.IsAdminDir());
1557 testPath.SetFromUnknown(_T("c:\\test.svn"));
1558 ATLASSERT(!testPath.IsAdminDir());
1559 testPath.SetFromUnknown(_T("c:\\.svn"));
1560 ATLASSERT(testPath.IsAdminDir());
1561 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));
1562 ATLASSERT(!testPath.IsAdminDir());
1563 testPath.SetFromUnknown(_T("c:\\.svn\\test"));
1564 ATLASSERT(testPath.IsAdminDir());
1566 CTGitPathList pathList;
1567 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));
1568 pathList.AddPath(CTGitPath(_T("c:\\.svn")));
1569 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1570 pathList.AddPath(CTGitPath(_T("c:\\test")));
1571 pathList.RemoveAdminPaths();
1572 ATLASSERT(pathList.GetCount()==2);
1573 pathList.Clear();
1574 pathList.AddPath(CTGitPath(_T("c:\\test")));
1575 pathList.RemoveAdminPaths();
1576 ATLASSERT(pathList.GetCount()==1);
1579 void SortTest()
1581 CTGitPathList testList;
1582 CTGitPath testPath;
1583 testPath.SetFromUnknown(_T("c:/Z"));
1584 testList.AddPath(testPath);
1585 testPath.SetFromUnknown(_T("c:/B"));
1586 testList.AddPath(testPath);
1587 testPath.SetFromUnknown(_T("c:\\a"));
1588 testList.AddPath(testPath);
1589 testPath.SetFromUnknown(_T("c:/Test"));
1590 testList.AddPath(testPath);
1592 testList.SortByPathname();
1594 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));
1595 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));
1596 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));
1597 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));
1600 void RawAppendTest()
1602 CTGitPath testPath(_T("c:/test/"));
1603 testPath.AppendRawString(_T("/Hello"));
1604 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1606 testPath.AppendRawString(_T("\\T2"));
1607 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1609 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1610 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1611 testBasePath.AppendRawString(testFilePath.GetFileExtension());
1612 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1615 void PathAppendTest()
1617 CTGitPath testPath(_T("c:/test/"));
1618 testPath.AppendPathString(_T("/Hello"));
1619 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1621 testPath.AppendPathString(_T("T2"));
1622 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1624 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1625 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1626 // You wouldn't want to do this in real life - you'd use append-raw
1627 testBasePath.AppendPathString(testFilePath.GetFileExtension());
1628 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1631 void RemoveDuplicatesTest()
1633 CTGitPathList list;
1634 list.AddPath(CTGitPath(_T("Z")));
1635 list.AddPath(CTGitPath(_T("A")));
1636 list.AddPath(CTGitPath(_T("E")));
1637 list.AddPath(CTGitPath(_T("E")));
1639 ATLASSERT(list[2].IsEquivalentTo(list[3]));
1640 ATLASSERT(list[2]==list[3]);
1642 ATLASSERT(list.GetCount() == 4);
1644 list.RemoveDuplicates();
1646 ATLASSERT(list.GetCount() == 3);
1648 ATLASSERT(list[0].GetWinPathString() == _T("A"));
1649 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);
1650 ATLASSERT(list[2].GetWinPathString() == _T("Z"));
1653 void RemoveChildrenTest()
1655 CTGitPathList list;
1656 list.AddPath(CTGitPath(_T("c:\\test")));
1657 list.AddPath(CTGitPath(_T("c:\\test\\file")));
1658 list.AddPath(CTGitPath(_T("c:\\testfile")));
1659 list.AddPath(CTGitPath(_T("c:\\parent")));
1660 list.AddPath(CTGitPath(_T("c:\\parent\\child")));
1661 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1662 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1664 ATLASSERT(list.GetCount() == 7);
1666 list.RemoveChildren();
1668 ATLTRACE("count = %d\n", list.GetCount());
1669 ATLASSERT(list.GetCount() == 3);
1671 list.SortByPathname();
1673 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1674 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1675 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1678 #if defined(_MFC_VER)
1679 void ListLoadingTest()
1681 TCHAR buf[MAX_PATH];
1682 GetCurrentDirectory(MAX_PATH, buf);
1683 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1684 CTGitPathList testList;
1685 testList.LoadFromAsteriskSeparatedString(sPathList);
1687 ATLASSERT(testList.GetCount() == 3);
1688 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));
1689 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1690 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));
1692 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));
1693 testList.Clear();
1694 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1695 testList.LoadFromAsteriskSeparatedString(sPathList);
1696 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1698 #endif
1700 void ContainingDirectoryTest()
1703 CTGitPath testPath;
1704 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1705 CTGitPath dir;
1706 dir = testPath.GetContainingDirectory();
1707 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1708 dir = dir.GetContainingDirectory();
1709 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));
1710 dir = dir.GetContainingDirectory();
1711 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));
1712 dir = dir.GetContainingDirectory();
1713 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));
1714 dir = dir.GetContainingDirectory();
1715 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));
1716 dir = dir.GetContainingDirectory();
1717 ATLASSERT(dir.IsEmpty());
1718 ATLASSERT(dir.GetWinPathString() == _T(""));
1721 void AncestorTest()
1723 CTGitPath testPath;
1724 testPath.SetFromWin(_T("c:\\windows"));
1725 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1726 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1727 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1728 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1729 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1732 void SubversionPathTest()
1734 CTGitPath testPath;
1735 testPath.SetFromWin(_T("c:\\"));
1736 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);
1737 testPath.SetFromWin(_T("c:\\folder"));
1738 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);
1739 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1740 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);
1741 testPath.SetFromUnknown(_T("http://testing/"));
1742 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);
1743 testPath.SetFromGit(NULL);
1744 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);
1745 #if defined(_MFC_VER)
1746 testPath.SetFromUnknown(_T("http://testing again"));
1747 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1748 testPath.SetFromUnknown(_T("http://testing%20again"));
1749 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1750 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1751 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1752 #endif
1755 void GetCommonRootTest()
1757 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));
1758 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));
1759 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1761 CTGitPathList list;
1762 list.AddPath(pathA);
1763 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1764 list.AddPath(pathB);
1765 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1766 list.AddPath(pathC);
1767 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1768 #ifdef _MFC_VER
1769 list.Clear();
1770 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");
1771 list.LoadFromAsteriskSeparatedString(sPathList);
1772 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1774 list.Clear();
1775 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");
1776 list.LoadFromAsteriskSeparatedString(sPathList);
1777 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1779 list.Clear();
1780 sPathList = _T("c:\\windows\\*c:\\windows");
1781 list.LoadFromAsteriskSeparatedString(sPathList);
1782 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1784 list.Clear();
1785 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");
1786 list.LoadFromAsteriskSeparatedString(sPathList);
1787 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1789 list.Clear();
1790 sPathList = _T("c:\\windowsdummy*c:\\windows");
1791 list.LoadFromAsteriskSeparatedString(sPathList);
1792 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1793 #endif
1796 void ValidPathAndUrlTest()
1798 CTGitPath testPath;
1799 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1800 ATLASSERT(testPath.IsValidOnWindows());
1801 testPath.SetFromWin(_T("c:\\"));
1802 ATLASSERT(testPath.IsValidOnWindows());
1803 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1804 ATLASSERT(testPath.IsValidOnWindows());
1805 testPath.SetFromWin(_T("c"));
1806 ATLASSERT(testPath.IsValidOnWindows());
1807 testPath.SetFromWin(_T("c:\\test folder\\file"));
1808 ATLASSERT(testPath.IsValidOnWindows());
1809 testPath.SetFromWin(_T("c:\\folder\\"));
1810 ATLASSERT(testPath.IsValidOnWindows());
1811 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1812 ATLASSERT(testPath.IsValidOnWindows());
1813 testPath.SetFromWin(_T("c:\\.svn"));
1814 ATLASSERT(testPath.IsValidOnWindows());
1815 testPath.SetFromWin(_T("c:\\com\\file"));
1816 ATLASSERT(testPath.IsValidOnWindows());
1817 testPath.SetFromWin(_T("c:\\test\\conf"));
1818 ATLASSERT(testPath.IsValidOnWindows());
1819 testPath.SetFromWin(_T("c:\\LPT"));
1820 ATLASSERT(testPath.IsValidOnWindows());
1821 testPath.SetFromWin(_T("c:\\test\\LPT"));
1822 ATLASSERT(testPath.IsValidOnWindows());
1823 testPath.SetFromWin(_T("c:\\com1test"));
1824 ATLASSERT(testPath.IsValidOnWindows());
1825 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1826 ATLASSERT(testPath.IsValidOnWindows());
1828 testPath.SetFromWin(_T("\\\\Share\\filename"));
1829 ATLASSERT(testPath.IsValidOnWindows());
1830 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));
1831 ATLASSERT(testPath.IsValidOnWindows());
1832 testPath.SetFromWin(_T("\\\\Share\\.svn"));
1833 ATLASSERT(testPath.IsValidOnWindows());
1835 // now the negative tests
1836 testPath.SetFromWin(_T("c:\\test:folder"));
1837 ATLASSERT(!testPath.IsValidOnWindows());
1838 testPath.SetFromWin(_T("c:\\file<name"));
1839 ATLASSERT(!testPath.IsValidOnWindows());
1840 testPath.SetFromWin(_T("c:\\something*else"));
1841 ATLASSERT(!testPath.IsValidOnWindows());
1842 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));
1843 ATLASSERT(!testPath.IsValidOnWindows());
1844 testPath.SetFromWin(_T("c:\\ext.>ension"));
1845 ATLASSERT(!testPath.IsValidOnWindows());
1846 testPath.SetFromWin(_T("c:\\com1\\filename"));
1847 ATLASSERT(!testPath.IsValidOnWindows());
1848 testPath.SetFromWin(_T("c:\\com1"));
1849 ATLASSERT(!testPath.IsValidOnWindows());
1850 testPath.SetFromWin(_T("c:\\com1\\AuX"));
1851 ATLASSERT(!testPath.IsValidOnWindows());
1853 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1854 ATLASSERT(!testPath.IsValidOnWindows());
1855 testPath.SetFromWin(_T("\\\\Share\\prn"));
1856 ATLASSERT(!testPath.IsValidOnWindows());
1857 testPath.SetFromWin(_T("\\\\Share\\NUL"));
1858 ATLASSERT(!testPath.IsValidOnWindows());
1860 // now come some URL tests
1861 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));
1862 ATLASSERT(testPath.IsValidOnWindows());
1863 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1864 ATLASSERT(testPath.IsValidOnWindows());
1865 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1866 ATLASSERT(testPath.IsValidOnWindows());
1867 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1868 ATLASSERT(testPath.IsValidOnWindows());
1869 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));
1870 ATLASSERT(testPath.IsValidOnWindows());
1871 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1872 ATLASSERT(testPath.IsValidOnWindows());
1873 // and some negative URL tests
1874 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1875 ATLASSERT(!testPath.IsValidOnWindows());
1876 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1877 ATLASSERT(!testPath.IsValidOnWindows());
1878 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1879 ATLASSERT(!testPath.IsValidOnWindows());
1880 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
1881 ATLASSERT(!testPath.IsValidOnWindows());
1882 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
1883 ATLASSERT(!testPath.IsValidOnWindows());
1887 } TGitPathTestobject;
1888 #endif
1889 #endif
1891 CTGitPath * CTGitPathList::LookForGitPath(CString path)
1893 int i=0;
1894 for(i=0;i<this->GetCount();i++)
1896 if((*this)[i].GetGitPathString() == path )
1897 return (CTGitPath*)&(*this)[i];
1899 return NULL;
1901 CString CTGitPath::GetActionName(int action)
1903 if(action & CTGitPath::LOGACTIONS_UNMERGED)
1904 return _T("Conflict");
1905 if(action & CTGitPath::LOGACTIONS_ADDED)
1906 return _T("Added");
1907 if(action & CTGitPath::LOGACTIONS_DELETED)
1908 return _T("Deleted");
1909 if(action & CTGitPath::LOGACTIONS_MODIFIED)
1910 return _T("Modified");
1911 if(action & CTGitPath::LOGACTIONS_REPLACED)
1912 return _T("Rename");
1913 if(action & CTGitPath::LOGACTIONS_COPY)
1914 return _T("Copy");
1915 if(action & CTGitPath::LOGACTIONS_REBASE_EDIT)
1916 return _T("Edit");
1917 if(action & CTGitPath::LOGACTIONS_REBASE_SQUASH)
1918 return _T("Squash");
1919 if(action & CTGitPath::LOGACTIONS_REBASE_PICK)
1920 return _T("Pick");
1921 if(action & CTGitPath::LOGACTIONS_REBASE_SKIP)
1922 return _T("Skip");
1924 return _T("Unknown");
1926 CString CTGitPath::GetActionName()
1928 return GetActionName(m_Action);
1931 int CTGitPathList::GetAction()
1933 return m_Action;