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.
21 #include "UnicodeUtils.h"
22 #include "GitAdminDir.h"
23 #include "PathUtils.h"
27 //#include "MessageBox.h"
28 //#include "AppUtils.h"
37 CTGitPath::CTGitPath(void) :
38 m_bDirectoryKnown(false),
39 m_bIsDirectory(false),
42 m_bHasAdminDirKnown(false),
43 m_bHasAdminDir(false),
44 m_bIsValidOnWindowsKnown(false),
46 m_bIsAdminDirKnown(false),
49 m_bExistsKnown(false),
50 m_bLastWriteTimeKnown(0),
53 m_bIsSpecialDirectoryKnown(false),
54 m_bIsSpecialDirectory(false)
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),
68 m_bHasAdminDirKnown(false),
69 m_bHasAdminDir(false),
70 m_bIsValidOnWindowsKnown(false),
72 m_bIsAdminDirKnown(false),
75 m_bExistsKnown(false),
76 m_bLastWriteTimeKnown(0),
79 m_bIsSpecialDirectoryKnown(false),
80 m_bIsSpecialDirectory(false)
82 SetFromUnknown(sUnknownPath
);
87 int CTGitPath::ParserAction(BYTE action
)
89 //action=action.TrimLeft();
90 //TCHAR c=action.GetAt(0);
92 m_Action
|= LOGACTIONS_MODIFIED
;
94 m_Action
|= LOGACTIONS_REPLACED
;
96 m_Action
|= LOGACTIONS_ADDED
;
98 m_Action
|= LOGACTIONS_DELETED
;
100 m_Action
|= LOGACTIONS_UNMERGED
;
102 m_Action
|= LOGACTIONS_DELETED
;
104 m_Action
|= LOGACTIONS_CACHE
;
106 m_Action
|= LOGACTIONS_COPY
;
110 void CTGitPath::SetFromGit(const char* pPath
)
115 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, NULL
, 0);
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
)
127 m_bDirectoryKnown
= true;
128 m_bIsDirectory
= bIsDirectory
;
131 void CTGitPath::SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
)
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
)
146 m_sFwdslashPath
= sPath
;
147 SanitizeRootPath(m_sFwdslashPath
, true);
149 m_sOldFwdslashPath
= *oldpath
;
152 void CTGitPath::SetFromWin(LPCTSTR pPath
)
155 m_sBackslashPath
= pPath
;
156 SanitizeRootPath(m_sBackslashPath
, false);
157 ATLASSERT(m_sBackslashPath
.Find('/')<0);
159 void CTGitPath::SetFromWin(const CString
& sPath
)
162 m_sBackslashPath
= sPath
;
163 SanitizeRootPath(m_sBackslashPath
, false);
165 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
168 m_sBackslashPath
= sPath
;
169 m_bIsDirectory
= bIsDirectory
;
170 m_bDirectoryKnown
= true;
171 SanitizeRootPath(m_sBackslashPath
, false);
173 void CTGitPath::SetFromUnknown(const CString
& sPath
)
176 // Just set whichever path we think is most likely to be used
177 // GitAdminDir admin;
179 // if(admin.HasAdminDir(sPath,&p))
180 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
182 SetFwdslashPath(sPath
);
185 LPCTSTR
CTGitPath::GetWinPath() const
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
;
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.
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
;
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
262 m_sUIPath
= CPathUtils::PathUnescape(GetGitPathString());
263 m_sUIPath
.Replace(_T("file:////"), _T("file:///\\"));
269 m_sUIPath
= GetWinPathString();
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
317 EnsureFwdslashPathSet();
318 if(m_sUTF8FwdslashPath
.IsEmpty())
320 SetUTF8FwdslashPath(m_sFwdslashPath
);
322 m_bIsURL
= !!svn_path_is_url(m_sUTF8FwdslashPath
);
330 bool CTGitPath::IsDirectory() const
332 if(!m_bDirectoryKnown
)
336 return m_bIsDirectory
;
339 bool CTGitPath::Exists() const
348 bool CTGitPath::Delete(bool bTrash
) const
350 EnsureBackslashPathSet();
351 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
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
;
364 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
;
366 shop
.fFlags
|= FOF_ALLOWUNDO
;
367 bRet
= (SHFileOperation(&shop
) == 0);
372 bRet
= !!::DeleteFile(m_sBackslashPath
);
376 m_bExistsKnown
= true;
380 __int64
CTGitPath::GetLastWriteTime() const
382 if(!m_bLastWriteTimeKnown
)
386 return m_lastWriteTime
;
389 bool CTGitPath::IsReadOnly() const
391 if(!m_bLastWriteTimeKnown
)
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
);
411 DWORD err
= GetLastError();
412 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
414 m_bIsDirectory
= false;
420 m_bIsDirectory
= false;
426 m_bDirectoryKnown
= true;
427 m_bLastWriteTimeKnown
= true;
428 m_bExistsKnown
= true;
431 CTGitPath
CTGitPath::GetSubPath(CTGitPath
&root
)
435 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
437 CString str
=GetWinPathString();
438 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
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;
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();
478 this->m_StatAdd
=_T("");
479 this->m_StatDel
=_T("");
480 ATLASSERT(IsEmpty());
483 CTGitPath
CTGitPath::GetDirectory() const
485 if ((IsDirectory())||(!Exists()))
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
501 if(sDirName
== m_sBackslashPath
)
503 // We were clearly provided with a root path to start with - we should return nothing now
507 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
509 // We have an UNC path and we already are the root
513 retVal
.SetFromWin(sDirName
);
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();
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
543 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
546 CString
CTGitPath::GetFileExtension() const
550 EnsureBackslashPathSet();
551 int dotPos
= m_sBackslashPath
.ReverseFind('.');
552 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
553 if (dotPos
> slashPos
)
554 return m_sBackslashPath
.Mid(dotPos
);
558 CString
CTGitPath::GetBaseFilename() const
561 CString filename
=GetFilename();
562 dot
= filename
.ReverseFind(_T('.'));
564 return filename
.Left(dot
);
569 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
571 int length
= sP1
.GetLength();
572 if(length
!= sP2
.GetLength())
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);
584 if(_totlower(*pP1
--) != _totlower(*pP2
--))
592 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
594 int length
= sP1
.GetLength();
595 if(length
!= sP2
.GetLength())
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);
607 if((*pP1
--) != (*pP2
--))
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
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
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
);
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
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
);
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())
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
;
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
)
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;
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");
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]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
814 sPattern
= _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
818 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
823 tr1::wregex
rx(sPattern
, tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
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;
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;
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
)
882 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool staged
)
888 while(pos
>=0 && pos
<out
.size())
893 g_Git
.StringAppend(&one
,&out
[pos
],CP_ACP
);
895 path
.m_Action
=path
.ParserAction(out
[pos
]);
896 one
.Tokenize(_T("\t"),tabstart
);
899 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
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
);
915 pos
=out
.findNextString(pos
);
919 int CTGitPathList::FillUnRev(int action
,CTGitPathList
*list
)
929 count
=list
->GetCount();
930 for(int i
=0;i
<count
;i
++)
936 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
937 ignored
= _T(" --ignored");
941 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
946 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z %s-- \"%s\""),
948 (*list
)[i
].GetWinPathString());
957 while( pos
>=0 && pos
<out
.size())
960 g_Git
.StringAppend(&one
,&out
[pos
],CP_ACP
);
963 //SetFromGit will clear all status
964 path
.SetFromGit(one
);
965 path
.m_Action
=action
;
968 pos
=out
.findNextString(pos
);
974 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
)
982 while( pos
>=0 && pos
<log
.size())
984 //one=log.Tokenize(_T("\n"),pos);
996 int end
=log
.find(0,pos
);
999 int file1
=-1,file2
=-1;
1002 actionstart
=log
.find(' ',end
-6);
1009 file1
= log
.find(0,actionstart
);
1015 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1019 file1
= log
.find(0,file1
);
1033 g_Git
.StringAppend(&pathname1
,&log
[file1
],CP_ACP
);
1035 g_Git
.StringAppend(&pathname2
,&log
[file2
],CP_ACP
);
1037 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1041 this->m_Action
|=GitPath
->ParserAction( log
[actionstart
] );
1045 int ac
=path
.ParserAction(log
[actionstart
] );
1047 path
.SetFromGit(pathname1
,&pathname2
);
1049 //action must be set after setfromgit. SetFromGit will clear all status.
1064 tabstart
=log
.find('\t',pos
);
1068 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1072 tabstart
=log
.find('\t',pos
);
1077 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1081 if(log
[pos
] == 0) //rename
1084 g_Git
.StringAppend(&file2
,&log
[pos
],CP_ACP
);
1085 int sec
=log
.find(0,pos
);
1089 g_Git
.StringAppend(&file1
,&log
[sec
],CP_ACP
);
1095 g_Git
.StringAppend(&file1
,&log
[pos
],CP_ACP
);
1097 path
.SetFromGit(file1
,&file2
);
1099 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1102 GitPath
->m_StatAdd
=StatAdd
;
1103 GitPath
->m_StatDel
=StatDel
;
1106 //path.SetFromGit(pathname);
1107 path
.m_StatAdd
=StatAdd
;
1108 path
.m_StatDel
=StatDel
;
1113 pos
=log
.findNextString(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()
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
)
1154 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1156 // for every selected file/folder
1158 while (file
.ReadString(strLine
))
1160 path
.SetFromUnknown(strLine
);
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);
1177 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
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());
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"));
1203 catch (CFileException
* pE
)
1205 TRACE("CFileException in writing temp file\n");
1213 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1219 temp
= sPathString
.Tokenize(_T("*"),pos
);
1224 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1228 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1231 PathVector::const_iterator it
;
1232 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1234 if (!sRet
.IsEmpty())
1236 sRet
+= it
->GetWinPathString();
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())
1254 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1255 if(m_commonBaseDirectory
.IsEmpty())
1257 m_commonBaseDirectory
= baseDirectory
;
1259 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1262 m_commonBaseDirectory
.Reset();
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
))
1284 m_commonBaseDirectory
.Reset();
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();
1300 return m_commonBaseDirectory
;
1303 CTGitPath
CTGitPathList::GetCommonRoot() const
1305 PathVector::const_iterator it
;
1306 CString sRoot
, sTempRoot
;
1309 if (GetCount() == 1)
1312 int backSlashPos
= 0;
1313 int searchStartPos
= 0;
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
)
1340 if (backSlashPos
< 0)
1346 if (bEqual
== false)
1349 sRoot
= m_paths
[0].GetWinPathString().Left(searchStartPos
+1);
1353 searchStartPos
= backSlashPos
;
1358 return CTGitPath(sRoot
.TrimRight('\\'));
1361 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1363 std::sort(m_paths
.begin(), m_paths
.end());
1365 std::reverse(m_paths
.begin(), m_paths
.end());
1368 void CTGitPathList::DeleteAllFiles(bool bTrash
)
1370 PathVector::const_iterator it
;
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();
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
);
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());
1406 void CTGitPathList::RemoveDuplicates()
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())
1422 it
= m_paths
.begin();
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
))
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())
1454 void CTGitPathList::RemoveChildren()
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())
1464 for (int i
=0; i
<list
.GetCount(); ++i
)
1466 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1472 //////////////////////////////////////////////////////////////////////////
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
;
1487 //////////////////////////////////////////////////////////////////////////
1491 // Some test cases for these classes
1492 static class CTGitPathTests
1498 pool
= svn_pool_create(NULL
);
1504 RemoveDuplicatesTest();
1505 RemoveChildrenTest();
1506 ContainingDirectoryTest();
1508 SubversionPathTest();
1509 GetCommonRootTest();
1510 #if defined(_MFC_VER)
1511 ValidPathAndUrlTest();
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
);
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());
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());
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);
1574 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1575 pathList
.RemoveAdminPaths();
1576 ATLASSERT(pathList
.GetCount()==1);
1581 CTGitPathList testList
;
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()
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()
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(""));
1694 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1695 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1696 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1700 void ContainingDirectoryTest()
1704 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
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(""));
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()
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);
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"));
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);
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);
1775 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1776 list
.LoadFromAsteriskSeparatedString(sPathList
);
1777 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1780 sPathList
= _T("c:\\windows\\*c:\\windows");
1781 list
.LoadFromAsteriskSeparatedString(sPathList
);
1782 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1785 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1786 list
.LoadFromAsteriskSeparatedString(sPathList
);
1787 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1790 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1791 list
.LoadFromAsteriskSeparatedString(sPathList
);
1792 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1796 void ValidPathAndUrlTest()
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
;
1891 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
1894 for(i
=0;i
<this->GetCount();i
++)
1896 if((*this)[i
].GetGitPathString() == path
)
1897 return (CTGitPath
*)&(*this)[i
];
1901 CString
CTGitPath::GetActionName(int action
)
1903 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
1904 return _T("Conflict");
1905 if(action
& CTGitPath::LOGACTIONS_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
)
1915 if(action
& CTGitPath::LOGACTIONS_REBASE_EDIT
)
1917 if(action
& CTGitPath::LOGACTIONS_REBASE_SQUASH
)
1918 return _T("Squash");
1919 if(action
& CTGitPath::LOGACTIONS_REBASE_PICK
)
1921 if(action
& CTGitPath::LOGACTIONS_REBASE_SKIP
)
1924 return _T("Unknown");
1926 CString
CTGitPath::GetActionName()
1928 return GetActionName(m_Action
);
1931 int CTGitPathList::GetAction()