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"
29 #include "../Resources/LoglistCommonResource.h"
32 //#include "MessageBox.h"
33 //#include "AppUtils.h"
42 CTGitPath::CTGitPath(void)
43 : m_bDirectoryKnown(false)
44 , m_bIsDirectory(false)
47 , m_bHasAdminDirKnown(false)
48 , m_bHasAdminDir(false)
49 , m_bIsValidOnWindowsKnown(false)
50 , m_bIsValidOnWindows(false)
51 , m_bIsReadOnly(false)
52 , m_bIsAdminDirKnown(false)
53 , m_bIsAdminDir(false)
55 , m_bExistsKnown(false)
56 , m_bLastWriteTimeKnown(0)
59 , m_bIsSpecialDirectoryKnown(false)
60 , m_bIsSpecialDirectory(false)
61 , m_bIsWCRootKnown(false)
68 CTGitPath::~CTGitPath(void)
71 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
72 CTGitPath::CTGitPath(const CString
& sUnknownPath
) :
73 m_bDirectoryKnown(false)
74 , m_bIsDirectory(false)
77 , m_bHasAdminDirKnown(false)
78 , m_bHasAdminDir(false)
79 , m_bIsValidOnWindowsKnown(false)
80 , m_bIsValidOnWindows(false)
81 , m_bIsReadOnly(false)
82 , m_bIsAdminDirKnown(false)
83 , m_bIsAdminDir(false)
85 , m_bExistsKnown(false)
86 , m_bLastWriteTimeKnown(0)
89 , m_bIsSpecialDirectoryKnown(false)
90 , m_bIsSpecialDirectory(false)
91 , m_bIsWCRootKnown(false)
94 SetFromUnknown(sUnknownPath
);
100 int CTGitPath::ParserAction(BYTE action
)
102 //action=action.TrimLeft();
103 //TCHAR c=action.GetAt(0);
105 m_Action
|= LOGACTIONS_MODIFIED
;
107 m_Action
|= LOGACTIONS_REPLACED
;
109 m_Action
|= LOGACTIONS_ADDED
;
111 m_Action
|= LOGACTIONS_DELETED
;
113 m_Action
|= LOGACTIONS_UNMERGED
;
115 m_Action
|= LOGACTIONS_DELETED
;
117 m_Action
|= LOGACTIONS_CACHE
;
119 m_Action
|= LOGACTIONS_COPY
;
123 void CTGitPath::SetFromGit(const char* pPath
)
128 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, NULL
, 0);
131 len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, m_sFwdslashPath
.GetBuffer(len
+1), len
+1);
132 m_sFwdslashPath
.ReleaseBuffer(len
-1);
134 SanitizeRootPath(m_sFwdslashPath
, true);
137 void CTGitPath::SetFromGit(const char* pPath
, bool bIsDirectory
)
140 m_bDirectoryKnown
= true;
141 m_bIsDirectory
= bIsDirectory
;
144 void CTGitPath::SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
)
149 m_sFwdslashPath
= pPath
;
150 SanitizeRootPath(m_sFwdslashPath
, true);
152 m_bDirectoryKnown
= true;
153 m_bIsDirectory
= bIsDirectory
;
156 void CTGitPath::SetFromGit(const CString
& sPath
,CString
*oldpath
)
159 m_sFwdslashPath
= sPath
;
160 SanitizeRootPath(m_sFwdslashPath
, true);
162 m_sOldFwdslashPath
= *oldpath
;
165 void CTGitPath::SetFromWin(LPCTSTR pPath
)
168 m_sBackslashPath
= pPath
;
169 SanitizeRootPath(m_sBackslashPath
, false);
170 ATLASSERT(m_sBackslashPath
.Find('/')<0);
172 void CTGitPath::SetFromWin(const CString
& sPath
)
175 m_sBackslashPath
= sPath
;
176 SanitizeRootPath(m_sBackslashPath
, false);
178 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
181 m_sBackslashPath
= sPath
;
182 m_bIsDirectory
= bIsDirectory
;
183 m_bDirectoryKnown
= true;
184 SanitizeRootPath(m_sBackslashPath
, false);
186 void CTGitPath::SetFromUnknown(const CString
& sPath
)
189 // Just set whichever path we think is most likely to be used
190 // GitAdminDir admin;
192 // if(admin.HasAdminDir(sPath,&p))
193 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
195 SetFwdslashPath(sPath
);
198 LPCTSTR
CTGitPath::GetWinPath() const
204 if(m_sBackslashPath
.IsEmpty())
206 SetBackslashPath(m_sFwdslashPath
);
208 return m_sBackslashPath
;
210 // This is a temporary function, to be used during the migration to
211 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
212 const CString
& CTGitPath::GetWinPathString() const
214 if(m_sBackslashPath
.IsEmpty())
216 SetBackslashPath(m_sFwdslashPath
);
218 return m_sBackslashPath
;
221 const CString
& CTGitPath::GetGitPathString() const
223 if(m_sFwdslashPath
.IsEmpty())
225 SetFwdslashPath(m_sBackslashPath
);
227 return m_sFwdslashPath
;
230 const CString
&CTGitPath::GetGitOldPathString() const
232 return m_sOldFwdslashPath
;
235 const char* CTGitPath::GetGitApiPath(apr_pool_t
*pool
) const
237 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
238 // each call to GetGitApiPath returns a different pointer value.
239 // If you made multiple calls to GetGitApiPath on the same string, only the last
240 // one would give you a valid pointer to an empty string, because each
241 // call would invalidate the previous call's return.
246 if(m_sFwdslashPath
.IsEmpty())
248 SetFwdslashPath(m_sBackslashPath
);
250 if(m_sUTF8FwdslashPath
.IsEmpty())
252 SetUTF8FwdslashPath(m_sFwdslashPath
);
254 if (svn_path_is_url(m_sUTF8FwdslashPath
))
256 m_sUTF8FwdslashPathEscaped
= CPathUtils::PathEscape(m_sUTF8FwdslashPath
);
257 m_sUTF8FwdslashPathEscaped
.Replace("file:////", "file:///\\");
258 m_sUTF8FwdslashPathEscaped
= svn_path_canonicalize(m_sUTF8FwdslashPathEscaped
, pool
);
259 return m_sUTF8FwdslashPathEscaped
;
261 m_sUTF8FwdslashPath
= svn_path_canonicalize(m_sUTF8FwdslashPath
, pool
);
263 return m_sUTF8FwdslashPath
;
267 const CString
& CTGitPath::GetUIPathString() const
269 if (m_sUIPath
.IsEmpty())
271 #if defined(_MFC_VER)
272 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
275 m_sUIPath
= CPathUtils::PathUnescape(GetGitPathString());
276 m_sUIPath
.Replace(_T("file:////"), _T("file:///\\"));
282 m_sUIPath
= GetWinPathString();
288 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
290 m_sFwdslashPath
= sPath
;
291 m_sFwdslashPath
.Replace('\\', '/');
293 // We don't leave a trailing /
294 m_sFwdslashPath
.TrimRight('/');
296 SanitizeRootPath(m_sFwdslashPath
, true);
298 m_sFwdslashPath
.Replace(_T("file:////"), _T("file:///\\"));
300 m_sUTF8FwdslashPath
.Empty();
303 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
305 m_sBackslashPath
= sPath
;
306 m_sBackslashPath
.Replace('/', '\\');
307 m_sBackslashPath
.TrimRight('\\');
308 SanitizeRootPath(m_sBackslashPath
, false);
311 void CTGitPath::SetUTF8FwdslashPath(const CString
& sPath
) const
313 m_sUTF8FwdslashPath
= CUnicodeUtils::GetUTF8(sPath
);
316 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
318 // Make sure to add the trailing slash to root paths such as 'C:'
319 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
321 sPath
+= (bIsForwardPath
) ? _T("/") : _T("\\");
325 bool CTGitPath::IsUrl() const
330 EnsureFwdslashPathSet();
331 if(m_sUTF8FwdslashPath
.IsEmpty())
333 SetUTF8FwdslashPath(m_sFwdslashPath
);
335 m_bIsURL
= !!svn_path_is_url(m_sUTF8FwdslashPath
);
343 bool CTGitPath::IsDirectory() const
345 if(!m_bDirectoryKnown
)
349 return m_bIsDirectory
;
352 bool CTGitPath::Exists() const
361 bool CTGitPath::Delete(bool bTrash
) const
363 EnsureBackslashPathSet();
364 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
368 if ((bTrash
)||(IsDirectory()))
370 TCHAR
* buf
= new TCHAR
[m_sBackslashPath
.GetLength()+2];
371 _tcscpy_s(buf
, m_sBackslashPath
.GetLength()+2, m_sBackslashPath
);
372 buf
[m_sBackslashPath
.GetLength()] = 0;
373 buf
[m_sBackslashPath
.GetLength()+1] = 0;
374 SHFILEOPSTRUCT shop
= {0};
375 shop
.wFunc
= FO_DELETE
;
377 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
;
379 shop
.fFlags
|= FOF_ALLOWUNDO
;
380 bRet
= (SHFileOperation(&shop
) == 0);
385 bRet
= !!::DeleteFile(m_sBackslashPath
);
389 m_bExistsKnown
= true;
393 __int64
CTGitPath::GetLastWriteTime() const
395 if(!m_bLastWriteTimeKnown
)
399 return m_lastWriteTime
;
402 bool CTGitPath::IsReadOnly() const
404 if(!m_bLastWriteTimeKnown
)
408 return m_bIsReadOnly
;
411 void CTGitPath::UpdateAttributes() const
413 EnsureBackslashPathSet();
414 WIN32_FILE_ATTRIBUTE_DATA attribs
;
415 if(GetFileAttributesEx(m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
417 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
418 m_lastWriteTime
= *(__int64
*)&attribs
.ftLastWriteTime
;
419 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
424 DWORD err
= GetLastError();
425 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
427 m_bIsDirectory
= false;
433 m_bIsDirectory
= false;
439 m_bDirectoryKnown
= true;
440 m_bLastWriteTimeKnown
= true;
441 m_bExistsKnown
= true;
444 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
448 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
450 CString str
=GetWinPathString();
451 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
456 void CTGitPath::EnsureBackslashPathSet() const
458 if(m_sBackslashPath
.IsEmpty())
460 SetBackslashPath(m_sFwdslashPath
);
461 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
464 void CTGitPath::EnsureFwdslashPathSet() const
466 if(m_sFwdslashPath
.IsEmpty())
468 SetFwdslashPath(m_sBackslashPath
);
469 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
474 // Reset all the caches
475 void CTGitPath::Reset()
477 m_bDirectoryKnown
= false;
479 m_bLastWriteTimeKnown
= false;
480 m_bHasAdminDirKnown
= false;
481 m_bIsValidOnWindowsKnown
= false;
482 m_bIsAdminDirKnown
= false;
483 m_bExistsKnown
= false;
484 m_bIsSpecialDirectoryKnown
= false;
485 m_bIsSpecialDirectory
= false;
487 m_sBackslashPath
.Empty();
488 m_sFwdslashPath
.Empty();
489 m_sUTF8FwdslashPath
.Empty();
491 this->m_StatAdd
=_T("");
492 this->m_StatDel
=_T("");
494 ATLASSERT(IsEmpty());
497 CTGitPath
CTGitPath::GetDirectory() const
499 if ((IsDirectory())||(!Exists()))
503 return GetContainingDirectory();
506 CTGitPath
CTGitPath::GetContainingDirectory() const
508 EnsureBackslashPathSet();
510 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
511 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
513 // This is a root directory, which needs a trailing slash
515 if(sDirName
== m_sBackslashPath
)
517 // We were clearly provided with a root path to start with - we should return nothing now
521 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
523 // We have an UNC path and we already are the root
527 retVal
.SetFromWin(sDirName
);
531 CString
CTGitPath::GetRootPathString() const
533 EnsureBackslashPathSet();
534 CString workingPath
= m_sBackslashPath
;
535 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
536 ATLVERIFY(::PathStripToRoot(pPath
));
537 workingPath
.ReleaseBuffer();
542 CString
CTGitPath::GetFilename() const
544 //ATLASSERT(!IsDirectory());
545 return GetFileOrDirectoryName();
548 CString
CTGitPath::GetFileOrDirectoryName() const
550 EnsureBackslashPathSet();
551 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
554 CString
CTGitPath::GetUIFileOrDirectoryName() const
557 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
560 CString
CTGitPath::GetFileExtension() const
564 EnsureBackslashPathSet();
565 int dotPos
= m_sBackslashPath
.ReverseFind('.');
566 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
567 if (dotPos
> slashPos
)
568 return m_sBackslashPath
.Mid(dotPos
);
572 CString
CTGitPath::GetBaseFilename() const
575 CString filename
=GetFilename();
576 dot
= filename
.ReverseFind(_T('.'));
578 return filename
.Left(dot
);
583 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
585 int length
= sP1
.GetLength();
586 if(length
!= sP2
.GetLength())
591 // We work from the end of the strings, because path differences
592 // are more likely to occur at the far end of a string
593 LPCTSTR pP1Start
= sP1
;
594 LPCTSTR pP1
= pP1Start
+(length
-1);
595 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
598 if(_totlower(*pP1
--) != _totlower(*pP2
--))
606 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
608 int length
= sP1
.GetLength();
609 if(length
!= sP2
.GetLength())
614 // We work from the end of the strings, because path differences
615 // are more likely to occur at the far end of a string
616 LPCTSTR pP1Start
= sP1
;
617 LPCTSTR pP1
= pP1Start
+(length
-1);
618 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
621 if((*pP1
--) != (*pP2
--))
629 bool CTGitPath::IsEmpty() const
631 // Check the backward slash path first, since the chance that this
632 // one is set is higher. In case of a 'false' return value it's a little
634 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
637 // Test if both paths refer to the same item
638 // Ignores case and slash direction
639 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
641 // Try and find a slash direction which avoids having to convert
643 if(!m_sBackslashPath
.IsEmpty())
645 // *We've* got a \ path - make sure that the RHS also has a \ path
646 rhs
.EnsureBackslashPathSet();
647 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
651 // Assume we've got a fwdslash path and make sure that the RHS has one
652 rhs
.EnsureFwdslashPathSet();
653 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
657 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
659 // Try and find a slash direction which avoids having to convert
661 if(!m_sBackslashPath
.IsEmpty())
663 // *We've* got a \ path - make sure that the RHS also has a \ path
664 rhs
.EnsureBackslashPathSet();
665 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
669 // Assume we've got a fwdslash path and make sure that the RHS has one
670 rhs
.EnsureFwdslashPathSet();
671 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
675 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
677 possibleDescendant
.EnsureBackslashPathSet();
678 EnsureBackslashPathSet();
680 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
681 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
683 return bPathStringsEqual
;
686 return (bPathStringsEqual
&&
687 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
688 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
691 // Get a string representing the file path, optionally with a base
692 // section stripped off the front.
693 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
695 EnsureFwdslashPathSet();
696 if(pOptionalBasePath
!= NULL
)
698 // Find the length of the base-path without having to do an 'ensure' on it
699 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
701 // Now, chop that baseLength of the front of the path
702 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
704 return m_sFwdslashPath
;
707 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
709 left
.EnsureBackslashPathSet();
710 right
.EnsureBackslashPathSet();
711 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
714 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
716 return CTGitPath::Compare(left
, right
) < 0;
719 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
721 return left
.IsEquivalentTo(right
);
724 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
726 if (left
.IsAdminDir() && right
.IsAdminDir())
732 l
= l
.GetContainingDirectory();
733 } while(l
.HasAdminDir());
736 r
= r
.GetContainingDirectory();
737 } while(r
.HasAdminDir());
738 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
740 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
743 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
745 return parent
.IsAncestorOf(child
);
748 void CTGitPath::AppendRawString(const CString
& sAppend
)
750 EnsureFwdslashPathSet();
751 CString strCopy
= m_sFwdslashPath
+= sAppend
;
752 SetFromUnknown(strCopy
);
755 void CTGitPath::AppendPathString(const CString
& sAppend
)
757 EnsureBackslashPathSet();
758 CString
cleanAppend(sAppend
);
759 cleanAppend
.Replace('/', '\\');
760 cleanAppend
.TrimLeft('\\');
761 m_sBackslashPath
.TrimRight('\\');
762 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
766 bool CTGitPath::IsWCRoot() const
768 if (m_bIsWCRootKnown
)
771 m_bIsWCRootKnown
= true;
774 CString topDirectory
;
775 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
780 if (IsEquivalentToWithoutCase(topDirectory
))
788 bool CTGitPath::HasAdminDir() const
790 if (m_bHasAdminDirKnown
)
791 return m_bHasAdminDir
;
793 EnsureBackslashPathSet();
794 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
795 m_bHasAdminDirKnown
= true;
796 return m_bHasAdminDir
;
799 bool CTGitPath::HasSubmodules() const
803 CString path
= m_sProjectRoot
;
804 path
+= _T("\\.gitmodules");
805 if( PathFileExists(path
) )
811 int CTGitPath::GetAdminDirMask() const
815 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
820 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
821 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
825 status
|= ITEMIS_FOLDERINGIT
;
827 status
|= ITEMIS_WCROOT
;
831 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
833 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
834 status
|= ITEMIS_BISECT
;
836 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
837 status
|= ITEMIS_STASH
;
839 if (PathFileExists(dotGitPath
+ _T("svn")))
840 status
|= ITEMIS_GITSVN
;
842 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
843 status
|= ITEMIS_SUBMODULECONTAINER
;
848 bool CTGitPath::HasStashDir() const
851 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
857 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
859 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
861 bool CTGitPath::HasGitSVNDir() const
864 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
870 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
872 return !!PathFileExists(dotGitPath
+ _T("svn"));
874 bool CTGitPath::IsBisectActive() const
877 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
883 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
885 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
887 bool CTGitPath::IsMergeActive() const
890 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
896 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
898 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
900 bool CTGitPath::HasRebaseApply() const
903 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
909 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
911 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
914 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
916 if (m_bHasAdminDirKnown
)
919 *ProjectTopDir
= m_sProjectRoot
;
920 return m_bHasAdminDir
;
923 EnsureBackslashPathSet();
924 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
925 m_bHasAdminDirKnown
= true;
927 *ProjectTopDir
= m_sProjectRoot
;
928 return m_bHasAdminDir
;
931 bool CTGitPath::IsAdminDir() const
933 if (m_bIsAdminDirKnown
)
934 return m_bIsAdminDir
;
936 EnsureBackslashPathSet();
937 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
938 m_bIsAdminDirKnown
= true;
939 return m_bIsAdminDir
;
942 bool CTGitPath::IsValidOnWindows() const
944 if (m_bIsValidOnWindowsKnown
)
945 return m_bIsValidOnWindows
;
947 m_bIsValidOnWindows
= false;
948 EnsureBackslashPathSet();
949 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
951 // the 'file://' URL is just a normal windows path:
952 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
954 sMatch
= sMatch
.Mid(7);
955 sMatch
.TrimLeft(_T("\\"));
956 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
960 sPattern
= _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
964 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
969 tr1::wregex
rx(sPattern
, tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
972 wstring rmatch
= wstring((LPCTSTR
)sMatch
);
973 if (tr1::regex_match(rmatch
, match
, rx
))
975 if (wstring(match
[0]).compare(sMatch
)==0)
976 m_bIsValidOnWindows
= true;
978 if (m_bIsValidOnWindows
)
980 // now check for illegal filenames
981 tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
982 rmatch
= m_sBackslashPath
;
983 if (tr1::regex_search(rmatch
, rx2
, tr1::regex_constants::match_default
))
984 m_bIsValidOnWindows
= false;
989 m_bIsValidOnWindowsKnown
= true;
990 return m_bIsValidOnWindows
;
993 bool CTGitPath::IsSpecialDirectory() const
995 if (m_bIsSpecialDirectoryKnown
)
996 return m_bIsSpecialDirectory
;
998 static LPCTSTR specialDirectories
[]
999 = { _T("trunk"), _T("tags"), _T("branches") };
1001 for (int i
= 0; i
< _countof(specialDirectories
); ++i
)
1003 CString name
= GetFileOrDirectoryName();
1004 if (0 == name
.CompareNoCase(specialDirectories
[i
]))
1006 m_bIsSpecialDirectory
= true;
1011 m_bIsSpecialDirectoryKnown
= true;
1013 return m_bIsSpecialDirectory
;
1016 //////////////////////////////////////////////////////////////////////////
1018 CTGitPathList::CTGitPathList()
1023 // A constructor which allows a path list to be easily built which one initial entry in
1024 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
1026 AddPath(firstEntry
);
1028 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
1036 while(pos
>=0 && pos
<out
.size())
1041 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1043 path
.m_Action
=path
.ParserAction(out
[pos
]);
1044 one
.Tokenize(_T("\t"),tabstart
);
1047 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1051 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1053 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1055 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1057 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1059 path
.m_Stage
=_ttol(part
);
1061 this->AddPath(path
);
1063 pos
=out
.findNextString(pos
);
1067 int CTGitPathList::FillUnRev(unsigned int action
,CTGitPathList
*list
)
1077 count
=list
->GetCount();
1078 for(int i
=0;i
<count
;i
++)
1084 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1089 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1094 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1096 (*list
)[i
].GetWinPathString());
1101 g_Git
.Run(cmd
, &out
);
1105 while( pos
>=0 && pos
<out
.size())
1108 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1111 //SetFromGit will clear all status
1112 path
.SetFromGit(one
);
1113 path
.m_Action
=action
;
1116 pos
=out
.findNextString(pos
);
1122 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1130 while( pos
>=0 && pos
<log
.size())
1132 //one=log.Tokenize(_T("\n"),pos);
1140 if(log
[pos
+1] ==':')
1144 int end
=log
.find(0,pos
);
1147 int file1
=-1,file2
=-1;
1150 actionstart
=log
.find(' ',end
-6);
1157 file1
= log
.find(0,actionstart
);
1163 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1167 file1
= log
.find(0,file1
);
1181 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1183 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1185 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1189 GitPath
->ParserAction( log
[actionstart
] );
1193 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1194 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1196 m_Action
|=GitPath
->m_Action
;
1201 int ac
=path
.ParserAction(log
[actionstart
] );
1202 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1204 path
.SetFromGit(pathname1
,&pathname2
);
1206 //action must be set after setfromgit. SetFromGit will clear all status.
1223 tabstart
=log
.find('\t',pos
);
1227 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1231 tabstart
=log
.find('\t',pos
);
1236 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1240 if(log
[pos
] == 0) //rename
1243 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1244 int sec
=log
.find(0,pos
);
1248 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1255 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1257 path
.SetFromGit(file1
,&file2
);
1259 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1262 GitPath
->m_StatAdd
=StatAdd
;
1263 GitPath
->m_StatDel
=StatDel
;
1267 //path.SetFromGit(pathname);
1270 path
.m_StatAdd
=_T("0");
1271 path
.m_StatDel
=_T("0");
1272 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1276 path
.m_StatAdd
=StatAdd
;
1277 path
.m_StatDel
=StatDel
;
1278 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1284 pos
=log
.findNextString(pos
);
1289 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1291 m_paths
.push_back(newPath
);
1292 m_commonBaseDirectory
.Reset();
1294 int CTGitPathList::GetCount() const
1296 return (int)m_paths
.size();
1298 void CTGitPathList::Clear()
1301 m_commonBaseDirectory
.Reset();
1304 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1306 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1307 return m_paths
[index
];
1310 bool CTGitPathList::AreAllPathsFiles() const
1312 // Look through the vector for any directories - if we find them, return false
1313 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1317 #if defined(_MFC_VER)
1319 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1325 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1327 // for every selected file/folder
1329 while (file
.ReadString(strLine
))
1331 path
.SetFromUnknown(strLine
);
1336 catch (CFileException
* pE
)
1338 TRACE("CFileException loading target file list\n");
1339 TCHAR error
[10000] = {0};
1340 pE
->GetErrorMessage(error
, 10000);
1341 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1348 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1354 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1355 PathVector::const_iterator it
;
1356 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1358 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1359 file
.Write(line
, line
.GetLength());
1365 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1366 PathVector::const_iterator it
;
1367 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1369 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1374 catch (CFileException
* pE
)
1376 TRACE("CFileException in writing temp file\n");
1384 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1390 temp
= sPathString
.Tokenize(_T("*"),pos
);
1395 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1399 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1402 PathVector::const_iterator it
;
1403 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1405 if (!sRet
.IsEmpty())
1407 sRet
+= it
->GetWinPathString();
1414 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1416 // Check if all the paths are files and in the same directory
1417 PathVector::const_iterator it
;
1418 m_commonBaseDirectory
.Reset();
1419 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1421 if(it
->IsDirectory())
1425 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1426 if(m_commonBaseDirectory
.IsEmpty())
1428 m_commonBaseDirectory
= baseDirectory
;
1430 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1433 m_commonBaseDirectory
.Reset();
1440 CTGitPath
CTGitPathList::GetCommonDirectory() const
1442 if (m_commonBaseDirectory
.IsEmpty())
1444 PathVector::const_iterator it
;
1445 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1447 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1448 if(m_commonBaseDirectory
.IsEmpty())
1450 m_commonBaseDirectory
= baseDirectory
;
1452 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1455 m_commonBaseDirectory
.Reset();
1460 // since we only checked strings, not paths,
1461 // we have to make sure now that we really return a *path* here
1462 PathVector::const_iterator iter
;
1463 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1465 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1467 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1471 return m_commonBaseDirectory
;
1474 CTGitPath
CTGitPathList::GetCommonRoot() const
1476 PathVector::const_iterator it
;
1477 CString sRoot
, sTempRoot
;
1480 if (GetCount() == 1)
1483 int backSlashPos
= 0;
1484 int searchStartPos
= 0;
1490 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1492 if (backSlashPos
== 0)
1494 backSlashPos
= it
->GetWinPathString().Find('\\', searchStartPos
+1);
1495 if ((backSlashPos
< 0)&&(searchStartPos
!= it
->GetWinPathString().GetLength()))
1496 backSlashPos
= it
->GetWinPathString().GetLength();
1497 sTempRoot
= it
->GetWinPathString().Left(backSlashPos
+1);
1499 else if (it
->GetWinPathString().Find('\\', searchStartPos
+1) != backSlashPos
|| it
->GetWinPathString().Left(backSlashPos
+1) != sTempRoot
.Left(backSlashPos
+1))
1501 if (it
->GetWinPathString().Find('\\', searchStartPos
+1) < 0)
1503 if (it
->GetWinPathString().GetLength() != backSlashPos
|| it
->GetWinPathString().Left(backSlashPos
+1) != sTempRoot
.Left(backSlashPos
+1))
1515 if (backSlashPos
< 0)
1521 if (bEqual
== false)
1524 sRoot
= m_paths
[0].GetWinPathString().Left(searchStartPos
+1);
1528 searchStartPos
= backSlashPos
;
1533 return CTGitPath(sRoot
.TrimRight('\\'));
1536 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1538 std::sort(m_paths
.begin(), m_paths
.end());
1540 std::reverse(m_paths
.begin(), m_paths
.end());
1543 void CTGitPathList::DeleteAllFiles(bool bTrash
)
1545 PathVector::const_iterator it
;
1548 SortByPathname(true); // nested ones first
1550 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1552 if ((it
->Exists())&&(!it
->IsDirectory()))
1554 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1555 sPaths
+= it
->GetWinPath();
1561 SHFILEOPSTRUCT shop
= {0};
1562 shop
.wFunc
= FO_DELETE
;
1563 shop
.pFrom
= (LPCTSTR
)sPaths
;
1564 shop
.fFlags
= FOF_ALLOWUNDO
|FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
;
1565 SHFileOperation(&shop
);
1569 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1571 if (!it
->IsDirectory())
1573 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1574 ::DeleteFile(it
->GetWinPath());
1581 void CTGitPathList::RemoveDuplicates()
1584 // Remove the duplicates
1585 // (Unique moves them to the end of the vector, then erase chops them off)
1586 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1589 void CTGitPathList::RemoveAdminPaths()
1591 PathVector::iterator it
;
1592 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1594 if (it
->IsAdminDir())
1597 it
= m_paths
.begin();
1604 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1606 PathVector::iterator it
;
1607 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1609 if (it
->IsEquivalentTo(path
))
1617 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1619 PathVector::iterator it
;
1620 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1622 if (it
->GetGitPathString()==path
.GetGitPathString())
1629 void CTGitPathList::RemoveChildren()
1632 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1635 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1637 if (list
.GetCount() != GetCount())
1639 for (int i
=0; i
<list
.GetCount(); ++i
)
1641 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1647 //////////////////////////////////////////////////////////////////////////
1649 apr_array_header_t
* CTGitPathList::MakePathArray (apr_pool_t
*pool
) const
1651 apr_array_header_t
*targets
= apr_array_make (pool
, GetCount(), sizeof(const char *));
1653 for(int nItem
= 0; nItem
< GetCount(); nItem
++)
1655 const char * target
= m_paths
[nItem
].GetGitApiPath(pool
);
1656 (*((const char **) apr_array_push (targets
))) = target
;
1662 //////////////////////////////////////////////////////////////////////////
1666 // Some test cases for these classes
1667 static class CTGitPathTests
1673 pool
= svn_pool_create(NULL
);
1679 RemoveDuplicatesTest();
1680 RemoveChildrenTest();
1681 ContainingDirectoryTest();
1683 SubversionPathTest();
1684 GetCommonRootTest();
1685 #if defined(_MFC_VER)
1686 ValidPathAndUrlTest();
1693 // apr_pool_t * pool;
1694 void GetDirectoryTest()
1696 // Bit tricky, this test, because we need to know something about the file
1697 // layout on the machine which is running the test
1698 TCHAR winDir
[MAX_PATH
+1];
1699 GetWindowsDirectory(winDir
, MAX_PATH
);
1700 CString
sWinDir(winDir
);
1703 // This is a file which we know will always be there
1704 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1705 ATLASSERT(!testPath
.IsDirectory());
1706 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1707 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1709 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1710 // but we know it must be different to the directory itself
1711 testPath
.SetFromUnknown(sWinDir
);
1712 ATLASSERT(testPath
.IsDirectory());
1713 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1714 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1715 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1718 testPath
.SetFromUnknown(_T("C:\\"));
1719 ATLASSERT(testPath
.IsDirectory());
1720 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1721 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1722 // Try a root UNC path
1723 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1724 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1730 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1731 ATLASSERT(!testPath
.IsAdminDir());
1732 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1733 ATLASSERT(!testPath
.IsAdminDir());
1734 testPath
.SetFromUnknown(_T("c:\\.svn"));
1735 ATLASSERT(testPath
.IsAdminDir());
1736 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1737 ATLASSERT(!testPath
.IsAdminDir());
1738 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1739 ATLASSERT(testPath
.IsAdminDir());
1741 CTGitPathList pathList
;
1742 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1743 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1744 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1745 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1746 pathList
.RemoveAdminPaths();
1747 ATLASSERT(pathList
.GetCount()==2);
1749 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1750 pathList
.RemoveAdminPaths();
1751 ATLASSERT(pathList
.GetCount()==1);
1756 CTGitPathList testList
;
1758 testPath
.SetFromUnknown(_T("c:/Z"));
1759 testList
.AddPath(testPath
);
1760 testPath
.SetFromUnknown(_T("c:/B"));
1761 testList
.AddPath(testPath
);
1762 testPath
.SetFromUnknown(_T("c:\\a"));
1763 testList
.AddPath(testPath
);
1764 testPath
.SetFromUnknown(_T("c:/Test"));
1765 testList
.AddPath(testPath
);
1767 testList
.SortByPathname();
1769 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1770 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1771 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1772 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1775 void RawAppendTest()
1777 CTGitPath
testPath(_T("c:/test/"));
1778 testPath
.AppendRawString(_T("/Hello"));
1779 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1781 testPath
.AppendRawString(_T("\\T2"));
1782 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1784 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1785 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1786 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1787 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1790 void PathAppendTest()
1792 CTGitPath
testPath(_T("c:/test/"));
1793 testPath
.AppendPathString(_T("/Hello"));
1794 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1796 testPath
.AppendPathString(_T("T2"));
1797 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1799 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1800 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1801 // You wouldn't want to do this in real life - you'd use append-raw
1802 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1803 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1806 void RemoveDuplicatesTest()
1809 list
.AddPath(CTGitPath(_T("Z")));
1810 list
.AddPath(CTGitPath(_T("A")));
1811 list
.AddPath(CTGitPath(_T("E")));
1812 list
.AddPath(CTGitPath(_T("E")));
1814 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1815 ATLASSERT(list
[2]==list
[3]);
1817 ATLASSERT(list
.GetCount() == 4);
1819 list
.RemoveDuplicates();
1821 ATLASSERT(list
.GetCount() == 3);
1823 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1824 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1825 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1828 void RemoveChildrenTest()
1831 list
.AddPath(CTGitPath(_T("c:\\test")));
1832 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1833 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1834 list
.AddPath(CTGitPath(_T("c:\\parent")));
1835 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1836 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1837 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1839 ATLASSERT(list
.GetCount() == 7);
1841 list
.RemoveChildren();
1843 ATLTRACE("count = %d\n", list
.GetCount());
1844 ATLASSERT(list
.GetCount() == 3);
1846 list
.SortByPathname();
1848 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1849 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1850 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1853 #if defined(_MFC_VER)
1854 void ListLoadingTest()
1856 TCHAR buf
[MAX_PATH
];
1857 GetCurrentDirectory(MAX_PATH
, buf
);
1858 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1859 CTGitPathList testList
;
1860 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1862 ATLASSERT(testList
.GetCount() == 3);
1863 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1864 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1865 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1867 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1869 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1870 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1871 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1875 void ContainingDirectoryTest()
1879 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1881 dir
= testPath
.GetContainingDirectory();
1882 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1883 dir
= dir
.GetContainingDirectory();
1884 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1885 dir
= dir
.GetContainingDirectory();
1886 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1887 dir
= dir
.GetContainingDirectory();
1888 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1889 dir
= dir
.GetContainingDirectory();
1890 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1891 dir
= dir
.GetContainingDirectory();
1892 ATLASSERT(dir
.IsEmpty());
1893 ATLASSERT(dir
.GetWinPathString() == _T(""));
1899 testPath
.SetFromWin(_T("c:\\windows"));
1900 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1901 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1902 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1903 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1904 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1907 void SubversionPathTest()
1910 testPath
.SetFromWin(_T("c:\\"));
1911 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1912 testPath
.SetFromWin(_T("c:\\folder"));
1913 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1914 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1915 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1916 testPath
.SetFromUnknown(_T("http://testing/"));
1917 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1918 testPath
.SetFromGit(NULL
);
1919 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1920 #if defined(_MFC_VER)
1921 testPath
.SetFromUnknown(_T("http://testing again"));
1922 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1923 testPath
.SetFromUnknown(_T("http://testing%20again"));
1924 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1925 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1926 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1930 void GetCommonRootTest()
1932 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1933 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1934 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1937 list
.AddPath(pathA
);
1938 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1939 list
.AddPath(pathB
);
1940 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1941 list
.AddPath(pathC
);
1942 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1945 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");
1946 list
.LoadFromAsteriskSeparatedString(sPathList
);
1947 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1950 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1951 list
.LoadFromAsteriskSeparatedString(sPathList
);
1952 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1955 sPathList
= _T("c:\\windows\\*c:\\windows");
1956 list
.LoadFromAsteriskSeparatedString(sPathList
);
1957 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1960 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1961 list
.LoadFromAsteriskSeparatedString(sPathList
);
1962 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1965 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1966 list
.LoadFromAsteriskSeparatedString(sPathList
);
1967 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1971 void ValidPathAndUrlTest()
1974 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1975 ATLASSERT(testPath
.IsValidOnWindows());
1976 testPath
.SetFromWin(_T("c:\\"));
1977 ATLASSERT(testPath
.IsValidOnWindows());
1978 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1979 ATLASSERT(testPath
.IsValidOnWindows());
1980 testPath
.SetFromWin(_T("c"));
1981 ATLASSERT(testPath
.IsValidOnWindows());
1982 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1983 ATLASSERT(testPath
.IsValidOnWindows());
1984 testPath
.SetFromWin(_T("c:\\folder\\"));
1985 ATLASSERT(testPath
.IsValidOnWindows());
1986 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1987 ATLASSERT(testPath
.IsValidOnWindows());
1988 testPath
.SetFromWin(_T("c:\\.svn"));
1989 ATLASSERT(testPath
.IsValidOnWindows());
1990 testPath
.SetFromWin(_T("c:\\com\\file"));
1991 ATLASSERT(testPath
.IsValidOnWindows());
1992 testPath
.SetFromWin(_T("c:\\test\\conf"));
1993 ATLASSERT(testPath
.IsValidOnWindows());
1994 testPath
.SetFromWin(_T("c:\\LPT"));
1995 ATLASSERT(testPath
.IsValidOnWindows());
1996 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1997 ATLASSERT(testPath
.IsValidOnWindows());
1998 testPath
.SetFromWin(_T("c:\\com1test"));
1999 ATLASSERT(testPath
.IsValidOnWindows());
2000 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
2001 ATLASSERT(testPath
.IsValidOnWindows());
2003 testPath
.SetFromWin(_T("\\\\Share\\filename"));
2004 ATLASSERT(testPath
.IsValidOnWindows());
2005 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
2006 ATLASSERT(testPath
.IsValidOnWindows());
2007 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
2008 ATLASSERT(testPath
.IsValidOnWindows());
2010 // now the negative tests
2011 testPath
.SetFromWin(_T("c:\\test:folder"));
2012 ATLASSERT(!testPath
.IsValidOnWindows());
2013 testPath
.SetFromWin(_T("c:\\file<name"));
2014 ATLASSERT(!testPath
.IsValidOnWindows());
2015 testPath
.SetFromWin(_T("c:\\something*else"));
2016 ATLASSERT(!testPath
.IsValidOnWindows());
2017 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
2018 ATLASSERT(!testPath
.IsValidOnWindows());
2019 testPath
.SetFromWin(_T("c:\\ext.>ension"));
2020 ATLASSERT(!testPath
.IsValidOnWindows());
2021 testPath
.SetFromWin(_T("c:\\com1\\filename"));
2022 ATLASSERT(!testPath
.IsValidOnWindows());
2023 testPath
.SetFromWin(_T("c:\\com1"));
2024 ATLASSERT(!testPath
.IsValidOnWindows());
2025 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
2026 ATLASSERT(!testPath
.IsValidOnWindows());
2028 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
2029 ATLASSERT(!testPath
.IsValidOnWindows());
2030 testPath
.SetFromWin(_T("\\\\Share\\prn"));
2031 ATLASSERT(!testPath
.IsValidOnWindows());
2032 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
2033 ATLASSERT(!testPath
.IsValidOnWindows());
2035 // now come some URL tests
2036 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2037 ATLASSERT(testPath
.IsValidOnWindows());
2038 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2039 ATLASSERT(testPath
.IsValidOnWindows());
2040 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2041 ATLASSERT(testPath
.IsValidOnWindows());
2042 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2043 ATLASSERT(testPath
.IsValidOnWindows());
2044 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2045 ATLASSERT(testPath
.IsValidOnWindows());
2046 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2047 ATLASSERT(testPath
.IsValidOnWindows());
2048 // and some negative URL tests
2049 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2050 ATLASSERT(!testPath
.IsValidOnWindows());
2051 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2052 ATLASSERT(!testPath
.IsValidOnWindows());
2053 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2054 ATLASSERT(!testPath
.IsValidOnWindows());
2055 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2056 ATLASSERT(!testPath
.IsValidOnWindows());
2057 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2058 ATLASSERT(!testPath
.IsValidOnWindows());
2062 } TGitPathTestobject
;
2066 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2069 for(i
=0;i
<this->GetCount();i
++)
2071 if((*this)[i
].GetGitPathString() == path
)
2072 return (CTGitPath
*)&(*this)[i
];
2076 CString
CTGitPath::GetActionName(int action
)
2078 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2079 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2080 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2081 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2082 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2083 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2084 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2085 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2087 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2088 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2089 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2090 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2091 if(action
& CTGitPath::LOGACTIONS_COPY
)
2092 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2094 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2095 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2097 if(action
& CTGitPath::LOGACTIONS_REBASE_EDIT
)
2098 return MAKEINTRESOURCE(IDS_PATHACTIONS_EDIT
);
2099 if(action
& CTGitPath::LOGACTIONS_REBASE_SQUASH
)
2100 return MAKEINTRESOURCE(IDS_PATHACTIONS_SQUASH
);
2101 if(action
& CTGitPath::LOGACTIONS_REBASE_PICK
)
2102 return MAKEINTRESOURCE(IDS_PATHACTIONS_PICK
);
2103 if(action
& CTGitPath::LOGACTIONS_REBASE_SKIP
)
2104 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIP
);
2106 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2108 CString
CTGitPath::GetActionName()
2110 return GetActionName(m_Action
);
2113 int CTGitPathList::GetAction()