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 GitPath
->ParserAction( log
[actionstart
] );
1042 GitPath
->m_Action
|=merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1043 m_Action
|=GitPath
->m_Action
;
1047 int ac
=path
.ParserAction(log
[actionstart
] );
1048 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1050 path
.SetFromGit(pathname1
,&pathname2
);
1052 //action must be set after setfromgit. SetFromGit will clear all status.
1068 tabstart
=log
.find('\t',pos
);
1072 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1076 tabstart
=log
.find('\t',pos
);
1081 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1085 if(log
[pos
] == 0) //rename
1088 g_Git
.StringAppend(&file2
,&log
[pos
],CP_ACP
);
1089 int sec
=log
.find(0,pos
);
1093 g_Git
.StringAppend(&file1
,&log
[sec
],CP_ACP
);
1099 g_Git
.StringAppend(&file1
,&log
[pos
],CP_ACP
);
1101 path
.SetFromGit(file1
,&file2
);
1103 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1106 GitPath
->m_StatAdd
=StatAdd
;
1107 GitPath
->m_StatDel
=StatDel
;
1110 //path.SetFromGit(pathname);
1111 path
.m_StatAdd
=StatAdd
;
1112 path
.m_StatDel
=StatDel
;
1117 pos
=log
.findNextString(pos
);
1122 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1124 m_paths
.push_back(newPath
);
1125 m_commonBaseDirectory
.Reset();
1127 int CTGitPathList::GetCount() const
1129 return (int)m_paths
.size();
1131 void CTGitPathList::Clear()
1134 m_commonBaseDirectory
.Reset();
1137 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1139 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1140 return m_paths
[index
];
1143 bool CTGitPathList::AreAllPathsFiles() const
1145 // Look through the vector for any directories - if we find them, return false
1146 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1150 #if defined(_MFC_VER)
1152 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1158 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1160 // for every selected file/folder
1162 while (file
.ReadString(strLine
))
1164 path
.SetFromUnknown(strLine
);
1169 catch (CFileException
* pE
)
1171 TRACE("CFileException loading target file list\n");
1172 TCHAR error
[10000] = {0};
1173 pE
->GetErrorMessage(error
, 10000);
1174 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1181 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1187 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1188 PathVector::const_iterator it
;
1189 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1191 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1192 file
.Write(line
, line
.GetLength());
1198 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1199 PathVector::const_iterator it
;
1200 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1202 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1207 catch (CFileException
* pE
)
1209 TRACE("CFileException in writing temp file\n");
1217 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1223 temp
= sPathString
.Tokenize(_T("*"),pos
);
1228 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1232 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1235 PathVector::const_iterator it
;
1236 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1238 if (!sRet
.IsEmpty())
1240 sRet
+= it
->GetWinPathString();
1247 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1249 // Check if all the paths are files and in the same directory
1250 PathVector::const_iterator it
;
1251 m_commonBaseDirectory
.Reset();
1252 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1254 if(it
->IsDirectory())
1258 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1259 if(m_commonBaseDirectory
.IsEmpty())
1261 m_commonBaseDirectory
= baseDirectory
;
1263 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1266 m_commonBaseDirectory
.Reset();
1273 CTGitPath
CTGitPathList::GetCommonDirectory() const
1275 if (m_commonBaseDirectory
.IsEmpty())
1277 PathVector::const_iterator it
;
1278 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1280 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1281 if(m_commonBaseDirectory
.IsEmpty())
1283 m_commonBaseDirectory
= baseDirectory
;
1285 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1288 m_commonBaseDirectory
.Reset();
1293 // since we only checked strings, not paths,
1294 // we have to make sure now that we really return a *path* here
1295 PathVector::const_iterator iter
;
1296 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1298 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1300 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1304 return m_commonBaseDirectory
;
1307 CTGitPath
CTGitPathList::GetCommonRoot() const
1309 PathVector::const_iterator it
;
1310 CString sRoot
, sTempRoot
;
1313 if (GetCount() == 1)
1316 int backSlashPos
= 0;
1317 int searchStartPos
= 0;
1320 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1322 if (backSlashPos
== 0)
1324 backSlashPos
= it
->GetWinPathString().Find('\\', searchStartPos
+1);
1325 if ((backSlashPos
< 0)&&(searchStartPos
!= it
->GetWinPathString().GetLength()))
1326 backSlashPos
= it
->GetWinPathString().GetLength();
1328 else if (it
->GetWinPathString().Find('\\', searchStartPos
+1) != backSlashPos
)
1330 if (it
->GetWinPathString().Find('\\', searchStartPos
+1) < 0)
1332 if (it
->GetWinPathString().GetLength() != backSlashPos
)
1344 if (backSlashPos
< 0)
1350 if (bEqual
== false)
1353 sRoot
= m_paths
[0].GetWinPathString().Left(searchStartPos
+1);
1357 searchStartPos
= backSlashPos
;
1362 return CTGitPath(sRoot
.TrimRight('\\'));
1365 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1367 std::sort(m_paths
.begin(), m_paths
.end());
1369 std::reverse(m_paths
.begin(), m_paths
.end());
1372 void CTGitPathList::DeleteAllFiles(bool bTrash
)
1374 PathVector::const_iterator it
;
1379 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1381 if ((it
->Exists())&&(!it
->IsDirectory()))
1383 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1384 sPaths
+= it
->GetWinPath();
1390 SHFILEOPSTRUCT shop
= {0};
1391 shop
.wFunc
= FO_DELETE
;
1392 shop
.pFrom
= (LPCTSTR
)sPaths
;
1393 shop
.fFlags
= FOF_ALLOWUNDO
|FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
;
1394 SHFileOperation(&shop
);
1398 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1400 if (!it
->IsDirectory())
1402 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1403 ::DeleteFile(it
->GetWinPath());
1410 void CTGitPathList::RemoveDuplicates()
1413 // Remove the duplicates
1414 // (Unique moves them to the end of the vector, then erase chops them off)
1415 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1418 void CTGitPathList::RemoveAdminPaths()
1420 PathVector::iterator it
;
1421 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1423 if (it
->IsAdminDir())
1426 it
= m_paths
.begin();
1433 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1435 PathVector::iterator it
;
1436 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1438 if (it
->IsEquivalentTo(path
))
1446 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1448 PathVector::iterator it
;
1449 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1451 if (it
->GetGitPathString()==path
.GetGitPathString())
1458 void CTGitPathList::RemoveChildren()
1461 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1464 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1466 if (list
.GetCount() != GetCount())
1468 for (int i
=0; i
<list
.GetCount(); ++i
)
1470 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1476 //////////////////////////////////////////////////////////////////////////
1478 apr_array_header_t
* CTGitPathList::MakePathArray (apr_pool_t
*pool
) const
1480 apr_array_header_t
*targets
= apr_array_make (pool
, GetCount(), sizeof(const char *));
1482 for(int nItem
= 0; nItem
< GetCount(); nItem
++)
1484 const char * target
= m_paths
[nItem
].GetGitApiPath(pool
);
1485 (*((const char **) apr_array_push (targets
))) = target
;
1491 //////////////////////////////////////////////////////////////////////////
1495 // Some test cases for these classes
1496 static class CTGitPathTests
1502 pool
= svn_pool_create(NULL
);
1508 RemoveDuplicatesTest();
1509 RemoveChildrenTest();
1510 ContainingDirectoryTest();
1512 SubversionPathTest();
1513 GetCommonRootTest();
1514 #if defined(_MFC_VER)
1515 ValidPathAndUrlTest();
1522 // apr_pool_t * pool;
1523 void GetDirectoryTest()
1525 // Bit tricky, this test, because we need to know something about the file
1526 // layout on the machine which is running the test
1527 TCHAR winDir
[MAX_PATH
+1];
1528 GetWindowsDirectory(winDir
, MAX_PATH
);
1529 CString
sWinDir(winDir
);
1532 // This is a file which we know will always be there
1533 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1534 ATLASSERT(!testPath
.IsDirectory());
1535 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1536 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1538 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1539 // but we know it must be different to the directory itself
1540 testPath
.SetFromUnknown(sWinDir
);
1541 ATLASSERT(testPath
.IsDirectory());
1542 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1543 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1544 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1547 testPath
.SetFromUnknown(_T("C:\\"));
1548 ATLASSERT(testPath
.IsDirectory());
1549 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1550 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1551 // Try a root UNC path
1552 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1553 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1559 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1560 ATLASSERT(!testPath
.IsAdminDir());
1561 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1562 ATLASSERT(!testPath
.IsAdminDir());
1563 testPath
.SetFromUnknown(_T("c:\\.svn"));
1564 ATLASSERT(testPath
.IsAdminDir());
1565 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1566 ATLASSERT(!testPath
.IsAdminDir());
1567 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1568 ATLASSERT(testPath
.IsAdminDir());
1570 CTGitPathList pathList
;
1571 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1572 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1573 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1574 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1575 pathList
.RemoveAdminPaths();
1576 ATLASSERT(pathList
.GetCount()==2);
1578 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1579 pathList
.RemoveAdminPaths();
1580 ATLASSERT(pathList
.GetCount()==1);
1585 CTGitPathList testList
;
1587 testPath
.SetFromUnknown(_T("c:/Z"));
1588 testList
.AddPath(testPath
);
1589 testPath
.SetFromUnknown(_T("c:/B"));
1590 testList
.AddPath(testPath
);
1591 testPath
.SetFromUnknown(_T("c:\\a"));
1592 testList
.AddPath(testPath
);
1593 testPath
.SetFromUnknown(_T("c:/Test"));
1594 testList
.AddPath(testPath
);
1596 testList
.SortByPathname();
1598 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1599 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1600 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1601 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1604 void RawAppendTest()
1606 CTGitPath
testPath(_T("c:/test/"));
1607 testPath
.AppendRawString(_T("/Hello"));
1608 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1610 testPath
.AppendRawString(_T("\\T2"));
1611 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1613 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1614 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1615 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1616 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1619 void PathAppendTest()
1621 CTGitPath
testPath(_T("c:/test/"));
1622 testPath
.AppendPathString(_T("/Hello"));
1623 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1625 testPath
.AppendPathString(_T("T2"));
1626 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1628 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1629 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1630 // You wouldn't want to do this in real life - you'd use append-raw
1631 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1632 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1635 void RemoveDuplicatesTest()
1638 list
.AddPath(CTGitPath(_T("Z")));
1639 list
.AddPath(CTGitPath(_T("A")));
1640 list
.AddPath(CTGitPath(_T("E")));
1641 list
.AddPath(CTGitPath(_T("E")));
1643 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1644 ATLASSERT(list
[2]==list
[3]);
1646 ATLASSERT(list
.GetCount() == 4);
1648 list
.RemoveDuplicates();
1650 ATLASSERT(list
.GetCount() == 3);
1652 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1653 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1654 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1657 void RemoveChildrenTest()
1660 list
.AddPath(CTGitPath(_T("c:\\test")));
1661 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1662 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1663 list
.AddPath(CTGitPath(_T("c:\\parent")));
1664 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1665 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1666 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1668 ATLASSERT(list
.GetCount() == 7);
1670 list
.RemoveChildren();
1672 ATLTRACE("count = %d\n", list
.GetCount());
1673 ATLASSERT(list
.GetCount() == 3);
1675 list
.SortByPathname();
1677 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1678 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1679 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1682 #if defined(_MFC_VER)
1683 void ListLoadingTest()
1685 TCHAR buf
[MAX_PATH
];
1686 GetCurrentDirectory(MAX_PATH
, buf
);
1687 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1688 CTGitPathList testList
;
1689 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1691 ATLASSERT(testList
.GetCount() == 3);
1692 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1693 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1694 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1696 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1698 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1699 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1700 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1704 void ContainingDirectoryTest()
1708 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1710 dir
= testPath
.GetContainingDirectory();
1711 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1712 dir
= dir
.GetContainingDirectory();
1713 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1714 dir
= dir
.GetContainingDirectory();
1715 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1716 dir
= dir
.GetContainingDirectory();
1717 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1718 dir
= dir
.GetContainingDirectory();
1719 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1720 dir
= dir
.GetContainingDirectory();
1721 ATLASSERT(dir
.IsEmpty());
1722 ATLASSERT(dir
.GetWinPathString() == _T(""));
1728 testPath
.SetFromWin(_T("c:\\windows"));
1729 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1730 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1731 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1732 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1733 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1736 void SubversionPathTest()
1739 testPath
.SetFromWin(_T("c:\\"));
1740 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1741 testPath
.SetFromWin(_T("c:\\folder"));
1742 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1743 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1744 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1745 testPath
.SetFromUnknown(_T("http://testing/"));
1746 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1747 testPath
.SetFromGit(NULL
);
1748 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1749 #if defined(_MFC_VER)
1750 testPath
.SetFromUnknown(_T("http://testing again"));
1751 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1752 testPath
.SetFromUnknown(_T("http://testing%20again"));
1753 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1754 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1755 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1759 void GetCommonRootTest()
1761 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1762 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1763 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1766 list
.AddPath(pathA
);
1767 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1768 list
.AddPath(pathB
);
1769 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1770 list
.AddPath(pathC
);
1771 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1774 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");
1775 list
.LoadFromAsteriskSeparatedString(sPathList
);
1776 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1779 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1780 list
.LoadFromAsteriskSeparatedString(sPathList
);
1781 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1784 sPathList
= _T("c:\\windows\\*c:\\windows");
1785 list
.LoadFromAsteriskSeparatedString(sPathList
);
1786 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1789 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1790 list
.LoadFromAsteriskSeparatedString(sPathList
);
1791 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1794 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1795 list
.LoadFromAsteriskSeparatedString(sPathList
);
1796 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1800 void ValidPathAndUrlTest()
1803 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1804 ATLASSERT(testPath
.IsValidOnWindows());
1805 testPath
.SetFromWin(_T("c:\\"));
1806 ATLASSERT(testPath
.IsValidOnWindows());
1807 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1808 ATLASSERT(testPath
.IsValidOnWindows());
1809 testPath
.SetFromWin(_T("c"));
1810 ATLASSERT(testPath
.IsValidOnWindows());
1811 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1812 ATLASSERT(testPath
.IsValidOnWindows());
1813 testPath
.SetFromWin(_T("c:\\folder\\"));
1814 ATLASSERT(testPath
.IsValidOnWindows());
1815 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1816 ATLASSERT(testPath
.IsValidOnWindows());
1817 testPath
.SetFromWin(_T("c:\\.svn"));
1818 ATLASSERT(testPath
.IsValidOnWindows());
1819 testPath
.SetFromWin(_T("c:\\com\\file"));
1820 ATLASSERT(testPath
.IsValidOnWindows());
1821 testPath
.SetFromWin(_T("c:\\test\\conf"));
1822 ATLASSERT(testPath
.IsValidOnWindows());
1823 testPath
.SetFromWin(_T("c:\\LPT"));
1824 ATLASSERT(testPath
.IsValidOnWindows());
1825 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1826 ATLASSERT(testPath
.IsValidOnWindows());
1827 testPath
.SetFromWin(_T("c:\\com1test"));
1828 ATLASSERT(testPath
.IsValidOnWindows());
1829 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1830 ATLASSERT(testPath
.IsValidOnWindows());
1832 testPath
.SetFromWin(_T("\\\\Share\\filename"));
1833 ATLASSERT(testPath
.IsValidOnWindows());
1834 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
1835 ATLASSERT(testPath
.IsValidOnWindows());
1836 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
1837 ATLASSERT(testPath
.IsValidOnWindows());
1839 // now the negative tests
1840 testPath
.SetFromWin(_T("c:\\test:folder"));
1841 ATLASSERT(!testPath
.IsValidOnWindows());
1842 testPath
.SetFromWin(_T("c:\\file<name"));
1843 ATLASSERT(!testPath
.IsValidOnWindows());
1844 testPath
.SetFromWin(_T("c:\\something*else"));
1845 ATLASSERT(!testPath
.IsValidOnWindows());
1846 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
1847 ATLASSERT(!testPath
.IsValidOnWindows());
1848 testPath
.SetFromWin(_T("c:\\ext.>ension"));
1849 ATLASSERT(!testPath
.IsValidOnWindows());
1850 testPath
.SetFromWin(_T("c:\\com1\\filename"));
1851 ATLASSERT(!testPath
.IsValidOnWindows());
1852 testPath
.SetFromWin(_T("c:\\com1"));
1853 ATLASSERT(!testPath
.IsValidOnWindows());
1854 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
1855 ATLASSERT(!testPath
.IsValidOnWindows());
1857 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1858 ATLASSERT(!testPath
.IsValidOnWindows());
1859 testPath
.SetFromWin(_T("\\\\Share\\prn"));
1860 ATLASSERT(!testPath
.IsValidOnWindows());
1861 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
1862 ATLASSERT(!testPath
.IsValidOnWindows());
1864 // now come some URL tests
1865 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
1866 ATLASSERT(testPath
.IsValidOnWindows());
1867 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1868 ATLASSERT(testPath
.IsValidOnWindows());
1869 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1870 ATLASSERT(testPath
.IsValidOnWindows());
1871 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1872 ATLASSERT(testPath
.IsValidOnWindows());
1873 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
1874 ATLASSERT(testPath
.IsValidOnWindows());
1875 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1876 ATLASSERT(testPath
.IsValidOnWindows());
1877 // and some negative URL tests
1878 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1879 ATLASSERT(!testPath
.IsValidOnWindows());
1880 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1881 ATLASSERT(!testPath
.IsValidOnWindows());
1882 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1883 ATLASSERT(!testPath
.IsValidOnWindows());
1884 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
1885 ATLASSERT(!testPath
.IsValidOnWindows());
1886 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
1887 ATLASSERT(!testPath
.IsValidOnWindows());
1891 } TGitPathTestobject
;
1895 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
1898 for(i
=0;i
<this->GetCount();i
++)
1900 if((*this)[i
].GetGitPathString() == path
)
1901 return (CTGitPath
*)&(*this)[i
];
1905 CString
CTGitPath::GetActionName(int action
)
1907 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
1908 return _T("Conflict");
1909 if(action
& CTGitPath::LOGACTIONS_ADDED
)
1911 if(action
& CTGitPath::LOGACTIONS_DELETED
)
1912 return _T("Deleted");
1913 if(action
& CTGitPath::LOGACTIONS_MERGED
)
1914 return _T("Merged");
1916 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
1917 return _T("Modified");
1918 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
1919 return _T("Rename");
1920 if(action
& CTGitPath::LOGACTIONS_COPY
)
1922 if(action
& CTGitPath::LOGACTIONS_REBASE_EDIT
)
1924 if(action
& CTGitPath::LOGACTIONS_REBASE_SQUASH
)
1925 return _T("Squash");
1926 if(action
& CTGitPath::LOGACTIONS_REBASE_PICK
)
1928 if(action
& CTGitPath::LOGACTIONS_REBASE_SKIP
)
1931 return _T("Unknown");
1933 CString
CTGitPath::GetActionName()
1935 return GetActionName(m_Action
);
1938 int CTGitPathList::GetAction()