1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
4 // Copyright (C) 2010-2012 Sven Strickroth <email@cs-ware.de>
5 // Copyright (C) 2003-2008 - TortoiseSVN
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "UnicodeUtils.h"
24 #include "GitAdminDir.h"
25 #include "PathUtils.h"
31 //#include "MessageBox.h"
32 //#include "AppUtils.h"
41 CTGitPath::CTGitPath(void)
42 : m_bDirectoryKnown(false)
43 , m_bIsDirectory(false)
46 , m_bHasAdminDirKnown(false)
47 , m_bHasAdminDir(false)
48 , m_bIsValidOnWindowsKnown(false)
49 , m_bIsValidOnWindows(false)
50 , m_bIsReadOnly(false)
51 , m_bIsAdminDirKnown(false)
52 , m_bIsAdminDir(false)
54 , m_bExistsKnown(false)
55 , m_bLastWriteTimeKnown(0)
58 , m_bIsSpecialDirectoryKnown(false)
59 , m_bIsSpecialDirectory(false)
60 , m_bIsWCRootKnown(false)
67 CTGitPath::~CTGitPath(void)
70 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
71 CTGitPath::CTGitPath(const CString
& sUnknownPath
) :
72 m_bDirectoryKnown(false)
73 , m_bIsDirectory(false)
76 , m_bHasAdminDirKnown(false)
77 , m_bHasAdminDir(false)
78 , m_bIsValidOnWindowsKnown(false)
79 , m_bIsValidOnWindows(false)
80 , m_bIsReadOnly(false)
81 , m_bIsAdminDirKnown(false)
82 , m_bIsAdminDir(false)
84 , m_bExistsKnown(false)
85 , m_bLastWriteTimeKnown(0)
88 , m_bIsSpecialDirectoryKnown(false)
89 , m_bIsSpecialDirectory(false)
90 , m_bIsWCRootKnown(false)
93 SetFromUnknown(sUnknownPath
);
99 int CTGitPath::ParserAction(BYTE action
)
101 //action=action.TrimLeft();
102 //TCHAR c=action.GetAt(0);
104 m_Action
|= LOGACTIONS_MODIFIED
;
106 m_Action
|= LOGACTIONS_REPLACED
;
108 m_Action
|= LOGACTIONS_ADDED
;
110 m_Action
|= LOGACTIONS_DELETED
;
112 m_Action
|= LOGACTIONS_UNMERGED
;
114 m_Action
|= LOGACTIONS_DELETED
;
116 m_Action
|= LOGACTIONS_CACHE
;
118 m_Action
|= LOGACTIONS_COPY
;
122 void CTGitPath::SetFromGit(const char* pPath
)
127 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, NULL
, 0);
130 len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, m_sFwdslashPath
.GetBuffer(len
+1), len
+1);
131 m_sFwdslashPath
.ReleaseBuffer(len
-1);
133 SanitizeRootPath(m_sFwdslashPath
, true);
136 void CTGitPath::SetFromGit(const char* pPath
, bool bIsDirectory
)
139 m_bDirectoryKnown
= true;
140 m_bIsDirectory
= bIsDirectory
;
143 void CTGitPath::SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
)
148 m_sFwdslashPath
= pPath
;
149 SanitizeRootPath(m_sFwdslashPath
, true);
151 m_bDirectoryKnown
= true;
152 m_bIsDirectory
= bIsDirectory
;
155 void CTGitPath::SetFromGit(const CString
& sPath
,CString
*oldpath
)
158 m_sFwdslashPath
= sPath
;
159 SanitizeRootPath(m_sFwdslashPath
, true);
161 m_sOldFwdslashPath
= *oldpath
;
164 void CTGitPath::SetFromWin(LPCTSTR pPath
)
167 m_sBackslashPath
= pPath
;
168 SanitizeRootPath(m_sBackslashPath
, false);
169 ATLASSERT(m_sBackslashPath
.Find('/')<0);
171 void CTGitPath::SetFromWin(const CString
& sPath
)
174 m_sBackslashPath
= sPath
;
175 SanitizeRootPath(m_sBackslashPath
, false);
177 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
180 m_sBackslashPath
= sPath
;
181 m_bIsDirectory
= bIsDirectory
;
182 m_bDirectoryKnown
= true;
183 SanitizeRootPath(m_sBackslashPath
, false);
185 void CTGitPath::SetFromUnknown(const CString
& sPath
)
188 // Just set whichever path we think is most likely to be used
189 // GitAdminDir admin;
191 // if(admin.HasAdminDir(sPath,&p))
192 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
194 SetFwdslashPath(sPath
);
197 LPCTSTR
CTGitPath::GetWinPath() const
203 if(m_sBackslashPath
.IsEmpty())
205 SetBackslashPath(m_sFwdslashPath
);
207 return m_sBackslashPath
;
209 // This is a temporary function, to be used during the migration to
210 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
211 const CString
& CTGitPath::GetWinPathString() const
213 if(m_sBackslashPath
.IsEmpty())
215 SetBackslashPath(m_sFwdslashPath
);
217 return m_sBackslashPath
;
220 const CString
& CTGitPath::GetGitPathString() const
222 if(m_sFwdslashPath
.IsEmpty())
224 SetFwdslashPath(m_sBackslashPath
);
226 return m_sFwdslashPath
;
229 const CString
&CTGitPath::GetGitOldPathString() const
231 return m_sOldFwdslashPath
;
234 const char* CTGitPath::GetGitApiPath(apr_pool_t
*pool
) const
236 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
237 // each call to GetGitApiPath returns a different pointer value.
238 // If you made multiple calls to GetGitApiPath on the same string, only the last
239 // one would give you a valid pointer to an empty string, because each
240 // call would invalidate the previous call's return.
245 if(m_sFwdslashPath
.IsEmpty())
247 SetFwdslashPath(m_sBackslashPath
);
249 if(m_sUTF8FwdslashPath
.IsEmpty())
251 SetUTF8FwdslashPath(m_sFwdslashPath
);
253 if (svn_path_is_url(m_sUTF8FwdslashPath
))
255 m_sUTF8FwdslashPathEscaped
= CPathUtils::PathEscape(m_sUTF8FwdslashPath
);
256 m_sUTF8FwdslashPathEscaped
.Replace("file:////", "file:///\\");
257 m_sUTF8FwdslashPathEscaped
= svn_path_canonicalize(m_sUTF8FwdslashPathEscaped
, pool
);
258 return m_sUTF8FwdslashPathEscaped
;
260 m_sUTF8FwdslashPath
= svn_path_canonicalize(m_sUTF8FwdslashPath
, pool
);
262 return m_sUTF8FwdslashPath
;
266 const CString
& CTGitPath::GetUIPathString() const
268 if (m_sUIPath
.IsEmpty())
270 #if defined(_MFC_VER)
271 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
274 m_sUIPath
= CPathUtils::PathUnescape(GetGitPathString());
275 m_sUIPath
.Replace(_T("file:////"), _T("file:///\\"));
281 m_sUIPath
= GetWinPathString();
287 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
289 m_sFwdslashPath
= sPath
;
290 m_sFwdslashPath
.Replace('\\', '/');
292 // We don't leave a trailing /
293 m_sFwdslashPath
.TrimRight('/');
295 SanitizeRootPath(m_sFwdslashPath
, true);
297 m_sFwdslashPath
.Replace(_T("file:////"), _T("file:///\\"));
299 m_sUTF8FwdslashPath
.Empty();
302 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
304 m_sBackslashPath
= sPath
;
305 m_sBackslashPath
.Replace('/', '\\');
306 m_sBackslashPath
.TrimRight('\\');
307 SanitizeRootPath(m_sBackslashPath
, false);
310 void CTGitPath::SetUTF8FwdslashPath(const CString
& sPath
) const
312 m_sUTF8FwdslashPath
= CUnicodeUtils::GetUTF8(sPath
);
315 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
317 // Make sure to add the trailing slash to root paths such as 'C:'
318 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
320 sPath
+= (bIsForwardPath
) ? _T("/") : _T("\\");
324 bool CTGitPath::IsUrl() const
329 EnsureFwdslashPathSet();
330 if(m_sUTF8FwdslashPath
.IsEmpty())
332 SetUTF8FwdslashPath(m_sFwdslashPath
);
334 m_bIsURL
= !!svn_path_is_url(m_sUTF8FwdslashPath
);
342 bool CTGitPath::IsDirectory() const
344 if(!m_bDirectoryKnown
)
348 return m_bIsDirectory
;
351 bool CTGitPath::Exists() const
360 bool CTGitPath::Delete(bool bTrash
) const
362 EnsureBackslashPathSet();
363 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
367 if ((bTrash
)||(IsDirectory()))
369 TCHAR
* buf
= new TCHAR
[m_sBackslashPath
.GetLength()+2];
370 _tcscpy_s(buf
, m_sBackslashPath
.GetLength()+2, m_sBackslashPath
);
371 buf
[m_sBackslashPath
.GetLength()] = 0;
372 buf
[m_sBackslashPath
.GetLength()+1] = 0;
373 SHFILEOPSTRUCT shop
= {0};
374 shop
.wFunc
= FO_DELETE
;
376 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
;
378 shop
.fFlags
|= FOF_ALLOWUNDO
;
379 bRet
= (SHFileOperation(&shop
) == 0);
384 bRet
= !!::DeleteFile(m_sBackslashPath
);
388 m_bExistsKnown
= true;
392 __int64
CTGitPath::GetLastWriteTime() const
394 if(!m_bLastWriteTimeKnown
)
398 return m_lastWriteTime
;
401 bool CTGitPath::IsReadOnly() const
403 if(!m_bLastWriteTimeKnown
)
407 return m_bIsReadOnly
;
410 void CTGitPath::UpdateAttributes() const
412 EnsureBackslashPathSet();
413 WIN32_FILE_ATTRIBUTE_DATA attribs
;
414 if(GetFileAttributesEx(m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
416 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
417 m_lastWriteTime
= *(__int64
*)&attribs
.ftLastWriteTime
;
418 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
423 DWORD err
= GetLastError();
424 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
426 m_bIsDirectory
= false;
432 m_bIsDirectory
= false;
438 m_bDirectoryKnown
= true;
439 m_bLastWriteTimeKnown
= true;
440 m_bExistsKnown
= true;
443 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
447 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
449 CString str
=GetWinPathString();
450 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
455 void CTGitPath::EnsureBackslashPathSet() const
457 if(m_sBackslashPath
.IsEmpty())
459 SetBackslashPath(m_sFwdslashPath
);
460 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
463 void CTGitPath::EnsureFwdslashPathSet() const
465 if(m_sFwdslashPath
.IsEmpty())
467 SetFwdslashPath(m_sBackslashPath
);
468 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
473 // Reset all the caches
474 void CTGitPath::Reset()
476 m_bDirectoryKnown
= false;
478 m_bLastWriteTimeKnown
= false;
479 m_bHasAdminDirKnown
= false;
480 m_bIsValidOnWindowsKnown
= false;
481 m_bIsAdminDirKnown
= false;
482 m_bExistsKnown
= false;
483 m_bIsSpecialDirectoryKnown
= false;
484 m_bIsSpecialDirectory
= false;
486 m_sBackslashPath
.Empty();
487 m_sFwdslashPath
.Empty();
488 m_sUTF8FwdslashPath
.Empty();
490 this->m_StatAdd
=_T("");
491 this->m_StatDel
=_T("");
493 ATLASSERT(IsEmpty());
496 CTGitPath
CTGitPath::GetDirectory() const
498 if ((IsDirectory())||(!Exists()))
502 return GetContainingDirectory();
505 CTGitPath
CTGitPath::GetContainingDirectory() const
507 EnsureBackslashPathSet();
509 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
510 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
512 // This is a root directory, which needs a trailing slash
514 if(sDirName
== m_sBackslashPath
)
516 // We were clearly provided with a root path to start with - we should return nothing now
520 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
522 // We have an UNC path and we already are the root
526 retVal
.SetFromWin(sDirName
);
530 CString
CTGitPath::GetRootPathString() const
532 EnsureBackslashPathSet();
533 CString workingPath
= m_sBackslashPath
;
534 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
535 ATLVERIFY(::PathStripToRoot(pPath
));
536 workingPath
.ReleaseBuffer();
541 CString
CTGitPath::GetFilename() const
543 //ATLASSERT(!IsDirectory());
544 return GetFileOrDirectoryName();
547 CString
CTGitPath::GetFileOrDirectoryName() const
549 EnsureBackslashPathSet();
550 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
553 CString
CTGitPath::GetUIFileOrDirectoryName() const
556 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
559 CString
CTGitPath::GetFileExtension() const
563 EnsureBackslashPathSet();
564 int dotPos
= m_sBackslashPath
.ReverseFind('.');
565 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
566 if (dotPos
> slashPos
)
567 return m_sBackslashPath
.Mid(dotPos
);
571 CString
CTGitPath::GetBaseFilename() const
574 CString filename
=GetFilename();
575 dot
= filename
.ReverseFind(_T('.'));
577 return filename
.Left(dot
);
582 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
584 int length
= sP1
.GetLength();
585 if(length
!= sP2
.GetLength())
590 // We work from the end of the strings, because path differences
591 // are more likely to occur at the far end of a string
592 LPCTSTR pP1Start
= sP1
;
593 LPCTSTR pP1
= pP1Start
+(length
-1);
594 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
597 if(_totlower(*pP1
--) != _totlower(*pP2
--))
605 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
607 int length
= sP1
.GetLength();
608 if(length
!= sP2
.GetLength())
613 // We work from the end of the strings, because path differences
614 // are more likely to occur at the far end of a string
615 LPCTSTR pP1Start
= sP1
;
616 LPCTSTR pP1
= pP1Start
+(length
-1);
617 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
620 if((*pP1
--) != (*pP2
--))
628 bool CTGitPath::IsEmpty() const
630 // Check the backward slash path first, since the chance that this
631 // one is set is higher. In case of a 'false' return value it's a little
633 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
636 // Test if both paths refer to the same item
637 // Ignores case and slash direction
638 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
640 // Try and find a slash direction which avoids having to convert
642 if(!m_sBackslashPath
.IsEmpty())
644 // *We've* got a \ path - make sure that the RHS also has a \ path
645 rhs
.EnsureBackslashPathSet();
646 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
650 // Assume we've got a fwdslash path and make sure that the RHS has one
651 rhs
.EnsureFwdslashPathSet();
652 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
656 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
658 // Try and find a slash direction which avoids having to convert
660 if(!m_sBackslashPath
.IsEmpty())
662 // *We've* got a \ path - make sure that the RHS also has a \ path
663 rhs
.EnsureBackslashPathSet();
664 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
668 // Assume we've got a fwdslash path and make sure that the RHS has one
669 rhs
.EnsureFwdslashPathSet();
670 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
674 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
676 possibleDescendant
.EnsureBackslashPathSet();
677 EnsureBackslashPathSet();
679 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
680 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
682 return bPathStringsEqual
;
685 return (bPathStringsEqual
&&
686 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
687 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
690 // Get a string representing the file path, optionally with a base
691 // section stripped off the front.
692 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
694 EnsureFwdslashPathSet();
695 if(pOptionalBasePath
!= NULL
)
697 // Find the length of the base-path without having to do an 'ensure' on it
698 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
700 // Now, chop that baseLength of the front of the path
701 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
703 return m_sFwdslashPath
;
706 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
708 left
.EnsureBackslashPathSet();
709 right
.EnsureBackslashPathSet();
710 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
713 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
715 return CTGitPath::Compare(left
, right
) < 0;
718 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
720 return left
.IsEquivalentTo(right
);
723 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
725 if (left
.IsAdminDir() && right
.IsAdminDir())
731 l
= l
.GetContainingDirectory();
732 } while(l
.HasAdminDir());
735 r
= r
.GetContainingDirectory();
736 } while(r
.HasAdminDir());
737 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
739 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
742 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
744 return parent
.IsAncestorOf(child
);
747 void CTGitPath::AppendRawString(const CString
& sAppend
)
749 EnsureFwdslashPathSet();
750 CString strCopy
= m_sFwdslashPath
+= sAppend
;
751 SetFromUnknown(strCopy
);
754 void CTGitPath::AppendPathString(const CString
& sAppend
)
756 EnsureBackslashPathSet();
757 CString
cleanAppend(sAppend
);
758 cleanAppend
.Replace('/', '\\');
759 cleanAppend
.TrimLeft('\\');
760 m_sBackslashPath
.TrimRight('\\');
761 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
765 bool CTGitPath::IsWCRoot() const
767 if (m_bIsWCRootKnown
)
770 m_bIsWCRootKnown
= true;
773 CString topDirectory
;
774 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
779 if (IsEquivalentToWithoutCase(topDirectory
))
787 bool CTGitPath::HasAdminDir() const
789 if (m_bHasAdminDirKnown
)
790 return m_bHasAdminDir
;
792 EnsureBackslashPathSet();
793 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
794 m_bHasAdminDirKnown
= true;
795 return m_bHasAdminDir
;
798 bool CTGitPath::HasSubmodules() const
802 CString path
= m_sProjectRoot
;
803 path
+= _T("\\.gitmodules");
804 if( PathFileExists(path
) )
810 int CTGitPath::GetAdminDirMask() const
814 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
819 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
820 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
824 status
|= ITEMIS_FOLDERINGIT
;
826 status
|= ITEMIS_WCROOT
;
830 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
832 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
833 status
|= ITEMIS_BISECT
;
835 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
836 status
|= ITEMIS_STASH
;
838 if (PathFileExists(dotGitPath
+ _T("svn")))
839 status
|= ITEMIS_GITSVN
;
841 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
842 status
|= ITEMIS_SUBMODULECONTAINER
;
847 bool CTGitPath::HasStashDir() const
850 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
856 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
858 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
860 bool CTGitPath::HasGitSVNDir() const
863 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
869 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
871 return !!PathFileExists(dotGitPath
+ _T("svn"));
873 bool CTGitPath::IsBisectActive() const
876 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
882 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
884 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
886 bool CTGitPath::IsMergeActive() const
889 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
895 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
897 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
899 bool CTGitPath::HasRebaseApply() const
902 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
908 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
910 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
913 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
915 if (m_bHasAdminDirKnown
)
918 *ProjectTopDir
= m_sProjectRoot
;
919 return m_bHasAdminDir
;
922 EnsureBackslashPathSet();
923 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
924 m_bHasAdminDirKnown
= true;
926 *ProjectTopDir
= m_sProjectRoot
;
927 return m_bHasAdminDir
;
930 bool CTGitPath::IsAdminDir() const
932 if (m_bIsAdminDirKnown
)
933 return m_bIsAdminDir
;
935 EnsureBackslashPathSet();
936 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
937 m_bIsAdminDirKnown
= true;
938 return m_bIsAdminDir
;
941 bool CTGitPath::IsValidOnWindows() const
943 if (m_bIsValidOnWindowsKnown
)
944 return m_bIsValidOnWindows
;
946 m_bIsValidOnWindows
= false;
947 EnsureBackslashPathSet();
948 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
950 // the 'file://' URL is just a normal windows path:
951 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
953 sMatch
= sMatch
.Mid(7);
954 sMatch
.TrimLeft(_T("\\"));
955 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
959 sPattern
= _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
963 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
968 tr1::wregex
rx(sPattern
, tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
971 wstring rmatch
= wstring((LPCTSTR
)sMatch
);
972 if (tr1::regex_match(rmatch
, match
, rx
))
974 if (wstring(match
[0]).compare(sMatch
)==0)
975 m_bIsValidOnWindows
= true;
977 if (m_bIsValidOnWindows
)
979 // now check for illegal filenames
980 tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
981 rmatch
= m_sBackslashPath
;
982 if (tr1::regex_search(rmatch
, rx2
, tr1::regex_constants::match_default
))
983 m_bIsValidOnWindows
= false;
988 m_bIsValidOnWindowsKnown
= true;
989 return m_bIsValidOnWindows
;
992 bool CTGitPath::IsSpecialDirectory() const
994 if (m_bIsSpecialDirectoryKnown
)
995 return m_bIsSpecialDirectory
;
997 static LPCTSTR specialDirectories
[]
998 = { _T("trunk"), _T("tags"), _T("branches") };
1000 for (int i
= 0; i
< _countof(specialDirectories
); ++i
)
1002 CString name
= GetFileOrDirectoryName();
1003 if (0 == name
.CompareNoCase(specialDirectories
[i
]))
1005 m_bIsSpecialDirectory
= true;
1010 m_bIsSpecialDirectoryKnown
= true;
1012 return m_bIsSpecialDirectory
;
1015 //////////////////////////////////////////////////////////////////////////
1017 CTGitPathList::CTGitPathList()
1022 // A constructor which allows a path list to be easily built which one initial entry in
1023 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
1025 AddPath(firstEntry
);
1027 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
1035 while(pos
>=0 && pos
<out
.size())
1040 g_Git
.StringAppend(&one
,&out
[pos
],CP_ACP
);
1042 path
.m_Action
=path
.ParserAction(out
[pos
]);
1043 one
.Tokenize(_T("\t"),tabstart
);
1046 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1050 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1052 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1054 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1056 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1058 path
.m_Stage
=_ttol(part
);
1060 this->AddPath(path
);
1062 pos
=out
.findNextString(pos
);
1066 int CTGitPathList::FillUnRev(unsigned int action
,CTGitPathList
*list
)
1076 count
=list
->GetCount();
1077 for(int i
=0;i
<count
;i
++)
1083 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1088 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1093 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1095 (*list
)[i
].GetWinPathString());
1100 g_Git
.Run(cmd
, &out
);
1104 while( pos
>=0 && pos
<out
.size())
1107 g_Git
.StringAppend(&one
,&out
[pos
],CP_ACP
);
1110 //SetFromGit will clear all status
1111 path
.SetFromGit(one
);
1112 path
.m_Action
=action
;
1115 pos
=out
.findNextString(pos
);
1121 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1129 while( pos
>=0 && pos
<log
.size())
1131 //one=log.Tokenize(_T("\n"),pos);
1139 if(log
[pos
+1] ==':')
1143 int end
=log
.find(0,pos
);
1146 int file1
=-1,file2
=-1;
1149 actionstart
=log
.find(' ',end
-6);
1156 file1
= log
.find(0,actionstart
);
1162 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1166 file1
= log
.find(0,file1
);
1180 g_Git
.StringAppend(&pathname1
,&log
[file1
],CP_ACP
);
1182 g_Git
.StringAppend(&pathname2
,&log
[file2
],CP_ACP
);
1184 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1188 GitPath
->ParserAction( log
[actionstart
] );
1192 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1193 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1195 m_Action
|=GitPath
->m_Action
;
1200 int ac
=path
.ParserAction(log
[actionstart
] );
1201 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1203 path
.SetFromGit(pathname1
,&pathname2
);
1205 //action must be set after setfromgit. SetFromGit will clear all status.
1222 tabstart
=log
.find('\t',pos
);
1226 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1230 tabstart
=log
.find('\t',pos
);
1235 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1239 if(log
[pos
] == 0) //rename
1242 g_Git
.StringAppend(&file2
,&log
[pos
],CP_ACP
);
1243 int sec
=log
.find(0,pos
);
1247 g_Git
.StringAppend(&file1
,&log
[sec
],CP_ACP
);
1254 g_Git
.StringAppend(&file1
,&log
[pos
],CP_ACP
);
1256 path
.SetFromGit(file1
,&file2
);
1258 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1261 GitPath
->m_StatAdd
=StatAdd
;
1262 GitPath
->m_StatDel
=StatDel
;
1266 //path.SetFromGit(pathname);
1269 path
.m_StatAdd
=_T("0");
1270 path
.m_StatDel
=_T("0");
1271 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1275 path
.m_StatAdd
=StatAdd
;
1276 path
.m_StatDel
=StatDel
;
1277 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1283 pos
=log
.findNextString(pos
);
1288 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1290 m_paths
.push_back(newPath
);
1291 m_commonBaseDirectory
.Reset();
1293 int CTGitPathList::GetCount() const
1295 return (int)m_paths
.size();
1297 void CTGitPathList::Clear()
1300 m_commonBaseDirectory
.Reset();
1303 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1305 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1306 return m_paths
[index
];
1309 bool CTGitPathList::AreAllPathsFiles() const
1311 // Look through the vector for any directories - if we find them, return false
1312 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1316 #if defined(_MFC_VER)
1318 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1324 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1326 // for every selected file/folder
1328 while (file
.ReadString(strLine
))
1330 path
.SetFromUnknown(strLine
);
1335 catch (CFileException
* pE
)
1337 TRACE("CFileException loading target file list\n");
1338 TCHAR error
[10000] = {0};
1339 pE
->GetErrorMessage(error
, 10000);
1340 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1347 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1353 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1354 PathVector::const_iterator it
;
1355 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1357 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1358 file
.Write(line
, line
.GetLength());
1364 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1365 PathVector::const_iterator it
;
1366 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1368 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1373 catch (CFileException
* pE
)
1375 TRACE("CFileException in writing temp file\n");
1383 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1389 temp
= sPathString
.Tokenize(_T("*"),pos
);
1394 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1398 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1401 PathVector::const_iterator it
;
1402 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1404 if (!sRet
.IsEmpty())
1406 sRet
+= it
->GetWinPathString();
1413 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1415 // Check if all the paths are files and in the same directory
1416 PathVector::const_iterator it
;
1417 m_commonBaseDirectory
.Reset();
1418 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1420 if(it
->IsDirectory())
1424 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1425 if(m_commonBaseDirectory
.IsEmpty())
1427 m_commonBaseDirectory
= baseDirectory
;
1429 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1432 m_commonBaseDirectory
.Reset();
1439 CTGitPath
CTGitPathList::GetCommonDirectory() const
1441 if (m_commonBaseDirectory
.IsEmpty())
1443 PathVector::const_iterator it
;
1444 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1446 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1447 if(m_commonBaseDirectory
.IsEmpty())
1449 m_commonBaseDirectory
= baseDirectory
;
1451 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1454 m_commonBaseDirectory
.Reset();
1459 // since we only checked strings, not paths,
1460 // we have to make sure now that we really return a *path* here
1461 PathVector::const_iterator iter
;
1462 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1464 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1466 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1470 return m_commonBaseDirectory
;
1473 CTGitPath
CTGitPathList::GetCommonRoot() const
1475 PathVector::const_iterator it
;
1476 CString sRoot
, sTempRoot
;
1479 if (GetCount() == 1)
1482 int backSlashPos
= 0;
1483 int searchStartPos
= 0;
1489 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1491 if (backSlashPos
== 0)
1493 backSlashPos
= it
->GetWinPathString().Find('\\', searchStartPos
+1);
1494 if ((backSlashPos
< 0)&&(searchStartPos
!= it
->GetWinPathString().GetLength()))
1495 backSlashPos
= it
->GetWinPathString().GetLength();
1496 sTempRoot
= it
->GetWinPathString().Left(backSlashPos
+1);
1498 else if (it
->GetWinPathString().Find('\\', searchStartPos
+1) != backSlashPos
|| it
->GetWinPathString().Left(backSlashPos
+1) != sTempRoot
.Left(backSlashPos
+1))
1500 if (it
->GetWinPathString().Find('\\', searchStartPos
+1) < 0)
1502 if (it
->GetWinPathString().GetLength() != backSlashPos
|| it
->GetWinPathString().Left(backSlashPos
+1) != sTempRoot
.Left(backSlashPos
+1))
1514 if (backSlashPos
< 0)
1520 if (bEqual
== false)
1523 sRoot
= m_paths
[0].GetWinPathString().Left(searchStartPos
+1);
1527 searchStartPos
= backSlashPos
;
1532 return CTGitPath(sRoot
.TrimRight('\\'));
1535 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1537 std::sort(m_paths
.begin(), m_paths
.end());
1539 std::reverse(m_paths
.begin(), m_paths
.end());
1542 void CTGitPathList::DeleteAllFiles(bool bTrash
)
1544 PathVector::const_iterator it
;
1549 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1551 if ((it
->Exists())&&(!it
->IsDirectory()))
1553 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1554 sPaths
+= it
->GetWinPath();
1560 SHFILEOPSTRUCT shop
= {0};
1561 shop
.wFunc
= FO_DELETE
;
1562 shop
.pFrom
= (LPCTSTR
)sPaths
;
1563 shop
.fFlags
= FOF_ALLOWUNDO
|FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
;
1564 SHFileOperation(&shop
);
1568 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1570 if (!it
->IsDirectory())
1572 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1573 ::DeleteFile(it
->GetWinPath());
1580 void CTGitPathList::RemoveDuplicates()
1583 // Remove the duplicates
1584 // (Unique moves them to the end of the vector, then erase chops them off)
1585 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1588 void CTGitPathList::RemoveAdminPaths()
1590 PathVector::iterator it
;
1591 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1593 if (it
->IsAdminDir())
1596 it
= m_paths
.begin();
1603 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1605 PathVector::iterator it
;
1606 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1608 if (it
->IsEquivalentTo(path
))
1616 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1618 PathVector::iterator it
;
1619 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1621 if (it
->GetGitPathString()==path
.GetGitPathString())
1628 void CTGitPathList::RemoveChildren()
1631 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1634 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1636 if (list
.GetCount() != GetCount())
1638 for (int i
=0; i
<list
.GetCount(); ++i
)
1640 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1646 //////////////////////////////////////////////////////////////////////////
1648 apr_array_header_t
* CTGitPathList::MakePathArray (apr_pool_t
*pool
) const
1650 apr_array_header_t
*targets
= apr_array_make (pool
, GetCount(), sizeof(const char *));
1652 for(int nItem
= 0; nItem
< GetCount(); nItem
++)
1654 const char * target
= m_paths
[nItem
].GetGitApiPath(pool
);
1655 (*((const char **) apr_array_push (targets
))) = target
;
1661 //////////////////////////////////////////////////////////////////////////
1665 // Some test cases for these classes
1666 static class CTGitPathTests
1672 pool
= svn_pool_create(NULL
);
1678 RemoveDuplicatesTest();
1679 RemoveChildrenTest();
1680 ContainingDirectoryTest();
1682 SubversionPathTest();
1683 GetCommonRootTest();
1684 #if defined(_MFC_VER)
1685 ValidPathAndUrlTest();
1692 // apr_pool_t * pool;
1693 void GetDirectoryTest()
1695 // Bit tricky, this test, because we need to know something about the file
1696 // layout on the machine which is running the test
1697 TCHAR winDir
[MAX_PATH
+1];
1698 GetWindowsDirectory(winDir
, MAX_PATH
);
1699 CString
sWinDir(winDir
);
1702 // This is a file which we know will always be there
1703 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1704 ATLASSERT(!testPath
.IsDirectory());
1705 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1706 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1708 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1709 // but we know it must be different to the directory itself
1710 testPath
.SetFromUnknown(sWinDir
);
1711 ATLASSERT(testPath
.IsDirectory());
1712 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1713 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1714 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1717 testPath
.SetFromUnknown(_T("C:\\"));
1718 ATLASSERT(testPath
.IsDirectory());
1719 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1720 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1721 // Try a root UNC path
1722 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1723 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1729 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1730 ATLASSERT(!testPath
.IsAdminDir());
1731 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1732 ATLASSERT(!testPath
.IsAdminDir());
1733 testPath
.SetFromUnknown(_T("c:\\.svn"));
1734 ATLASSERT(testPath
.IsAdminDir());
1735 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1736 ATLASSERT(!testPath
.IsAdminDir());
1737 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1738 ATLASSERT(testPath
.IsAdminDir());
1740 CTGitPathList pathList
;
1741 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1742 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1743 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1744 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1745 pathList
.RemoveAdminPaths();
1746 ATLASSERT(pathList
.GetCount()==2);
1748 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1749 pathList
.RemoveAdminPaths();
1750 ATLASSERT(pathList
.GetCount()==1);
1755 CTGitPathList testList
;
1757 testPath
.SetFromUnknown(_T("c:/Z"));
1758 testList
.AddPath(testPath
);
1759 testPath
.SetFromUnknown(_T("c:/B"));
1760 testList
.AddPath(testPath
);
1761 testPath
.SetFromUnknown(_T("c:\\a"));
1762 testList
.AddPath(testPath
);
1763 testPath
.SetFromUnknown(_T("c:/Test"));
1764 testList
.AddPath(testPath
);
1766 testList
.SortByPathname();
1768 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1769 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1770 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1771 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1774 void RawAppendTest()
1776 CTGitPath
testPath(_T("c:/test/"));
1777 testPath
.AppendRawString(_T("/Hello"));
1778 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1780 testPath
.AppendRawString(_T("\\T2"));
1781 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1783 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1784 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1785 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1786 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1789 void PathAppendTest()
1791 CTGitPath
testPath(_T("c:/test/"));
1792 testPath
.AppendPathString(_T("/Hello"));
1793 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1795 testPath
.AppendPathString(_T("T2"));
1796 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1798 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1799 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1800 // You wouldn't want to do this in real life - you'd use append-raw
1801 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1802 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1805 void RemoveDuplicatesTest()
1808 list
.AddPath(CTGitPath(_T("Z")));
1809 list
.AddPath(CTGitPath(_T("A")));
1810 list
.AddPath(CTGitPath(_T("E")));
1811 list
.AddPath(CTGitPath(_T("E")));
1813 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1814 ATLASSERT(list
[2]==list
[3]);
1816 ATLASSERT(list
.GetCount() == 4);
1818 list
.RemoveDuplicates();
1820 ATLASSERT(list
.GetCount() == 3);
1822 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1823 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1824 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1827 void RemoveChildrenTest()
1830 list
.AddPath(CTGitPath(_T("c:\\test")));
1831 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1832 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1833 list
.AddPath(CTGitPath(_T("c:\\parent")));
1834 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1835 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1836 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1838 ATLASSERT(list
.GetCount() == 7);
1840 list
.RemoveChildren();
1842 ATLTRACE("count = %d\n", list
.GetCount());
1843 ATLASSERT(list
.GetCount() == 3);
1845 list
.SortByPathname();
1847 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1848 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1849 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1852 #if defined(_MFC_VER)
1853 void ListLoadingTest()
1855 TCHAR buf
[MAX_PATH
];
1856 GetCurrentDirectory(MAX_PATH
, buf
);
1857 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1858 CTGitPathList testList
;
1859 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1861 ATLASSERT(testList
.GetCount() == 3);
1862 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1863 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1864 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1866 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1868 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1869 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1870 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1874 void ContainingDirectoryTest()
1878 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1880 dir
= testPath
.GetContainingDirectory();
1881 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1882 dir
= dir
.GetContainingDirectory();
1883 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1884 dir
= dir
.GetContainingDirectory();
1885 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1886 dir
= dir
.GetContainingDirectory();
1887 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1888 dir
= dir
.GetContainingDirectory();
1889 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1890 dir
= dir
.GetContainingDirectory();
1891 ATLASSERT(dir
.IsEmpty());
1892 ATLASSERT(dir
.GetWinPathString() == _T(""));
1898 testPath
.SetFromWin(_T("c:\\windows"));
1899 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1900 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1901 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1902 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1903 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1906 void SubversionPathTest()
1909 testPath
.SetFromWin(_T("c:\\"));
1910 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1911 testPath
.SetFromWin(_T("c:\\folder"));
1912 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1913 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1914 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1915 testPath
.SetFromUnknown(_T("http://testing/"));
1916 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1917 testPath
.SetFromGit(NULL
);
1918 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1919 #if defined(_MFC_VER)
1920 testPath
.SetFromUnknown(_T("http://testing again"));
1921 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1922 testPath
.SetFromUnknown(_T("http://testing%20again"));
1923 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1924 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1925 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1929 void GetCommonRootTest()
1931 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1932 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1933 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1936 list
.AddPath(pathA
);
1937 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1938 list
.AddPath(pathB
);
1939 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1940 list
.AddPath(pathC
);
1941 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1944 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");
1945 list
.LoadFromAsteriskSeparatedString(sPathList
);
1946 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1949 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1950 list
.LoadFromAsteriskSeparatedString(sPathList
);
1951 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1954 sPathList
= _T("c:\\windows\\*c:\\windows");
1955 list
.LoadFromAsteriskSeparatedString(sPathList
);
1956 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1959 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1960 list
.LoadFromAsteriskSeparatedString(sPathList
);
1961 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1964 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1965 list
.LoadFromAsteriskSeparatedString(sPathList
);
1966 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1970 void ValidPathAndUrlTest()
1973 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1974 ATLASSERT(testPath
.IsValidOnWindows());
1975 testPath
.SetFromWin(_T("c:\\"));
1976 ATLASSERT(testPath
.IsValidOnWindows());
1977 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1978 ATLASSERT(testPath
.IsValidOnWindows());
1979 testPath
.SetFromWin(_T("c"));
1980 ATLASSERT(testPath
.IsValidOnWindows());
1981 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1982 ATLASSERT(testPath
.IsValidOnWindows());
1983 testPath
.SetFromWin(_T("c:\\folder\\"));
1984 ATLASSERT(testPath
.IsValidOnWindows());
1985 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1986 ATLASSERT(testPath
.IsValidOnWindows());
1987 testPath
.SetFromWin(_T("c:\\.svn"));
1988 ATLASSERT(testPath
.IsValidOnWindows());
1989 testPath
.SetFromWin(_T("c:\\com\\file"));
1990 ATLASSERT(testPath
.IsValidOnWindows());
1991 testPath
.SetFromWin(_T("c:\\test\\conf"));
1992 ATLASSERT(testPath
.IsValidOnWindows());
1993 testPath
.SetFromWin(_T("c:\\LPT"));
1994 ATLASSERT(testPath
.IsValidOnWindows());
1995 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1996 ATLASSERT(testPath
.IsValidOnWindows());
1997 testPath
.SetFromWin(_T("c:\\com1test"));
1998 ATLASSERT(testPath
.IsValidOnWindows());
1999 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
2000 ATLASSERT(testPath
.IsValidOnWindows());
2002 testPath
.SetFromWin(_T("\\\\Share\\filename"));
2003 ATLASSERT(testPath
.IsValidOnWindows());
2004 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
2005 ATLASSERT(testPath
.IsValidOnWindows());
2006 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
2007 ATLASSERT(testPath
.IsValidOnWindows());
2009 // now the negative tests
2010 testPath
.SetFromWin(_T("c:\\test:folder"));
2011 ATLASSERT(!testPath
.IsValidOnWindows());
2012 testPath
.SetFromWin(_T("c:\\file<name"));
2013 ATLASSERT(!testPath
.IsValidOnWindows());
2014 testPath
.SetFromWin(_T("c:\\something*else"));
2015 ATLASSERT(!testPath
.IsValidOnWindows());
2016 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
2017 ATLASSERT(!testPath
.IsValidOnWindows());
2018 testPath
.SetFromWin(_T("c:\\ext.>ension"));
2019 ATLASSERT(!testPath
.IsValidOnWindows());
2020 testPath
.SetFromWin(_T("c:\\com1\\filename"));
2021 ATLASSERT(!testPath
.IsValidOnWindows());
2022 testPath
.SetFromWin(_T("c:\\com1"));
2023 ATLASSERT(!testPath
.IsValidOnWindows());
2024 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
2025 ATLASSERT(!testPath
.IsValidOnWindows());
2027 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
2028 ATLASSERT(!testPath
.IsValidOnWindows());
2029 testPath
.SetFromWin(_T("\\\\Share\\prn"));
2030 ATLASSERT(!testPath
.IsValidOnWindows());
2031 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
2032 ATLASSERT(!testPath
.IsValidOnWindows());
2034 // now come some URL tests
2035 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2036 ATLASSERT(testPath
.IsValidOnWindows());
2037 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2038 ATLASSERT(testPath
.IsValidOnWindows());
2039 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2040 ATLASSERT(testPath
.IsValidOnWindows());
2041 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2042 ATLASSERT(testPath
.IsValidOnWindows());
2043 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2044 ATLASSERT(testPath
.IsValidOnWindows());
2045 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2046 ATLASSERT(testPath
.IsValidOnWindows());
2047 // and some negative URL tests
2048 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2049 ATLASSERT(!testPath
.IsValidOnWindows());
2050 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2051 ATLASSERT(!testPath
.IsValidOnWindows());
2052 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2053 ATLASSERT(!testPath
.IsValidOnWindows());
2054 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2055 ATLASSERT(!testPath
.IsValidOnWindows());
2056 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2057 ATLASSERT(!testPath
.IsValidOnWindows());
2061 } TGitPathTestobject
;
2065 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2068 for(i
=0;i
<this->GetCount();i
++)
2070 if((*this)[i
].GetGitPathString() == path
)
2071 return (CTGitPath
*)&(*this)[i
];
2075 CString
CTGitPath::GetActionName(int action
)
2077 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2078 return _T("Conflict");
2079 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2081 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2082 return _T("Deleted");
2083 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2084 return _T("Merged");
2086 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2087 return _T("Modified");
2088 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2089 return _T("Rename");
2090 if(action
& CTGitPath::LOGACTIONS_COPY
)
2093 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2094 return _T("Forward");
2096 if(action
& CTGitPath::LOGACTIONS_REBASE_EDIT
)
2098 if(action
& CTGitPath::LOGACTIONS_REBASE_SQUASH
)
2099 return _T("Squash");
2100 if(action
& CTGitPath::LOGACTIONS_REBASE_PICK
)
2102 if(action
& CTGitPath::LOGACTIONS_REBASE_SKIP
)
2105 return _T("Unknown");
2107 CString
CTGitPath::GetActionName()
2109 return GetActionName(m_Action
);
2112 int CTGitPathList::GetAction()