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 "StringUtils.h"
30 #include "../Resources/LoglistCommonResource.h"
33 //#include "MessageBox.h"
34 //#include "AppUtils.h"
43 CTGitPath::CTGitPath(void)
44 : m_bDirectoryKnown(false)
45 , m_bIsDirectory(false)
48 , m_bHasAdminDirKnown(false)
49 , m_bHasAdminDir(false)
50 , m_bIsValidOnWindowsKnown(false)
51 , m_bIsValidOnWindows(false)
52 , m_bIsReadOnly(false)
53 , m_bIsAdminDirKnown(false)
54 , m_bIsAdminDir(false)
56 , m_bExistsKnown(false)
57 , m_bLastWriteTimeKnown(0)
60 , m_bIsSpecialDirectoryKnown(false)
61 , m_bIsSpecialDirectory(false)
62 , m_bIsWCRootKnown(false)
69 CTGitPath::~CTGitPath(void)
72 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
73 CTGitPath::CTGitPath(const CString
& sUnknownPath
) :
74 m_bDirectoryKnown(false)
75 , m_bIsDirectory(false)
78 , m_bHasAdminDirKnown(false)
79 , m_bHasAdminDir(false)
80 , m_bIsValidOnWindowsKnown(false)
81 , m_bIsValidOnWindows(false)
82 , m_bIsReadOnly(false)
83 , m_bIsAdminDirKnown(false)
84 , m_bIsAdminDir(false)
86 , m_bExistsKnown(false)
87 , m_bLastWriteTimeKnown(0)
90 , m_bIsSpecialDirectoryKnown(false)
91 , m_bIsSpecialDirectory(false)
92 , m_bIsWCRootKnown(false)
95 SetFromUnknown(sUnknownPath
);
101 int CTGitPath::ParserAction(BYTE action
)
103 //action=action.TrimLeft();
104 //TCHAR c=action.GetAt(0);
106 m_Action
|= LOGACTIONS_MODIFIED
;
108 m_Action
|= LOGACTIONS_REPLACED
;
110 m_Action
|= LOGACTIONS_ADDED
;
112 m_Action
|= LOGACTIONS_DELETED
;
114 m_Action
|= LOGACTIONS_UNMERGED
;
116 m_Action
|= LOGACTIONS_DELETED
;
118 m_Action
|= LOGACTIONS_CACHE
;
120 m_Action
|= LOGACTIONS_COPY
;
124 void CTGitPath::SetFromGit(const char* pPath
)
129 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, NULL
, 0);
132 len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, m_sFwdslashPath
.GetBuffer(len
+1), len
+1);
133 m_sFwdslashPath
.ReleaseBuffer(len
-1);
135 SanitizeRootPath(m_sFwdslashPath
, true);
138 void CTGitPath::SetFromGit(const char* pPath
, bool bIsDirectory
)
141 m_bDirectoryKnown
= true;
142 m_bIsDirectory
= bIsDirectory
;
145 void CTGitPath::SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
)
150 m_sFwdslashPath
= pPath
;
151 SanitizeRootPath(m_sFwdslashPath
, true);
153 m_bDirectoryKnown
= true;
154 m_bIsDirectory
= bIsDirectory
;
157 void CTGitPath::SetFromGit(const CString
& sPath
,CString
*oldpath
)
160 m_sFwdslashPath
= sPath
;
161 SanitizeRootPath(m_sFwdslashPath
, true);
163 m_sOldFwdslashPath
= *oldpath
;
166 void CTGitPath::SetFromWin(LPCTSTR pPath
)
169 m_sBackslashPath
= pPath
;
170 SanitizeRootPath(m_sBackslashPath
, false);
171 ATLASSERT(m_sBackslashPath
.Find('/')<0);
173 void CTGitPath::SetFromWin(const CString
& sPath
)
176 m_sBackslashPath
= sPath
;
177 SanitizeRootPath(m_sBackslashPath
, false);
179 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
182 m_sBackslashPath
= sPath
;
183 m_bIsDirectory
= bIsDirectory
;
184 m_bDirectoryKnown
= true;
185 SanitizeRootPath(m_sBackslashPath
, false);
187 void CTGitPath::SetFromUnknown(const CString
& sPath
)
190 // Just set whichever path we think is most likely to be used
191 // GitAdminDir admin;
193 // if(admin.HasAdminDir(sPath,&p))
194 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
196 SetFwdslashPath(sPath
);
199 LPCTSTR
CTGitPath::GetWinPath() const
205 if(m_sBackslashPath
.IsEmpty())
207 SetBackslashPath(m_sFwdslashPath
);
209 return m_sBackslashPath
;
211 // This is a temporary function, to be used during the migration to
212 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
213 const CString
& CTGitPath::GetWinPathString() const
215 if(m_sBackslashPath
.IsEmpty())
217 SetBackslashPath(m_sFwdslashPath
);
219 return m_sBackslashPath
;
222 const CString
& CTGitPath::GetGitPathString() const
224 if(m_sFwdslashPath
.IsEmpty())
226 SetFwdslashPath(m_sBackslashPath
);
228 return m_sFwdslashPath
;
231 const CString
&CTGitPath::GetGitOldPathString() const
233 return m_sOldFwdslashPath
;
236 const char* CTGitPath::GetGitApiPath(apr_pool_t
*pool
) const
238 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
239 // each call to GetGitApiPath returns a different pointer value.
240 // If you made multiple calls to GetGitApiPath on the same string, only the last
241 // one would give you a valid pointer to an empty string, because each
242 // call would invalidate the previous call's return.
247 if(m_sFwdslashPath
.IsEmpty())
249 SetFwdslashPath(m_sBackslashPath
);
251 if(m_sUTF8FwdslashPath
.IsEmpty())
253 SetUTF8FwdslashPath(m_sFwdslashPath
);
255 if (svn_path_is_url(m_sUTF8FwdslashPath
))
257 m_sUTF8FwdslashPathEscaped
= CPathUtils::PathEscape(m_sUTF8FwdslashPath
);
258 m_sUTF8FwdslashPathEscaped
.Replace("file:////", "file:///\\");
259 m_sUTF8FwdslashPathEscaped
= svn_path_canonicalize(m_sUTF8FwdslashPathEscaped
, pool
);
260 return m_sUTF8FwdslashPathEscaped
;
262 m_sUTF8FwdslashPath
= svn_path_canonicalize(m_sUTF8FwdslashPath
, pool
);
264 return m_sUTF8FwdslashPath
;
268 const CString
& CTGitPath::GetUIPathString() const
270 if (m_sUIPath
.IsEmpty())
272 #if defined(_MFC_VER)
273 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
276 m_sUIPath
= CPathUtils::PathUnescape(GetGitPathString());
277 m_sUIPath
.Replace(_T("file:////"), _T("file:///\\"));
283 m_sUIPath
= GetWinPathString();
289 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
291 m_sFwdslashPath
= sPath
;
292 m_sFwdslashPath
.Replace('\\', '/');
294 // We don't leave a trailing /
295 m_sFwdslashPath
.TrimRight('/');
297 SanitizeRootPath(m_sFwdslashPath
, true);
299 m_sFwdslashPath
.Replace(_T("file:////"), _T("file:///\\"));
301 m_sUTF8FwdslashPath
.Empty();
304 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
306 m_sBackslashPath
= sPath
;
307 m_sBackslashPath
.Replace('/', '\\');
308 m_sBackslashPath
.TrimRight('\\');
309 SanitizeRootPath(m_sBackslashPath
, false);
312 void CTGitPath::SetUTF8FwdslashPath(const CString
& sPath
) const
314 m_sUTF8FwdslashPath
= CUnicodeUtils::GetUTF8(sPath
);
317 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
319 // Make sure to add the trailing slash to root paths such as 'C:'
320 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
322 sPath
+= (bIsForwardPath
) ? _T("/") : _T("\\");
326 bool CTGitPath::IsUrl() const
331 EnsureFwdslashPathSet();
332 if(m_sUTF8FwdslashPath
.IsEmpty())
334 SetUTF8FwdslashPath(m_sFwdslashPath
);
336 m_bIsURL
= !!svn_path_is_url(m_sUTF8FwdslashPath
);
344 bool CTGitPath::IsDirectory() const
346 if(!m_bDirectoryKnown
)
350 return m_bIsDirectory
;
353 bool CTGitPath::Exists() const
362 bool CTGitPath::Delete(bool bTrash
) const
364 EnsureBackslashPathSet();
365 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
369 if ((bTrash
)||(IsDirectory()))
371 TCHAR
* buf
= new TCHAR
[m_sBackslashPath
.GetLength()+2];
372 _tcscpy_s(buf
, m_sBackslashPath
.GetLength()+2, m_sBackslashPath
);
373 buf
[m_sBackslashPath
.GetLength()] = 0;
374 buf
[m_sBackslashPath
.GetLength()+1] = 0;
375 bRet
= CTGitPathList::DeleteViaShell(buf
, bTrash
);
380 bRet
= !!::DeleteFile(m_sBackslashPath
);
384 m_bExistsKnown
= true;
388 __int64
CTGitPath::GetLastWriteTime() const
390 if(!m_bLastWriteTimeKnown
)
394 return m_lastWriteTime
;
397 bool CTGitPath::IsReadOnly() const
399 if(!m_bLastWriteTimeKnown
)
403 return m_bIsReadOnly
;
406 void CTGitPath::UpdateAttributes() const
408 EnsureBackslashPathSet();
409 WIN32_FILE_ATTRIBUTE_DATA attribs
;
410 if(GetFileAttributesEx(m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
412 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
413 m_lastWriteTime
= *(__int64
*)&attribs
.ftLastWriteTime
;
414 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
419 DWORD err
= GetLastError();
420 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
422 m_bIsDirectory
= false;
428 m_bIsDirectory
= false;
434 m_bDirectoryKnown
= true;
435 m_bLastWriteTimeKnown
= true;
436 m_bExistsKnown
= true;
439 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
443 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
445 CString str
=GetWinPathString();
446 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
451 void CTGitPath::EnsureBackslashPathSet() const
453 if(m_sBackslashPath
.IsEmpty())
455 SetBackslashPath(m_sFwdslashPath
);
456 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
459 void CTGitPath::EnsureFwdslashPathSet() const
461 if(m_sFwdslashPath
.IsEmpty())
463 SetFwdslashPath(m_sBackslashPath
);
464 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
469 // Reset all the caches
470 void CTGitPath::Reset()
472 m_bDirectoryKnown
= false;
474 m_bLastWriteTimeKnown
= false;
475 m_bHasAdminDirKnown
= false;
476 m_bIsValidOnWindowsKnown
= false;
477 m_bIsAdminDirKnown
= false;
478 m_bExistsKnown
= false;
479 m_bIsSpecialDirectoryKnown
= false;
480 m_bIsSpecialDirectory
= false;
482 m_sBackslashPath
.Empty();
483 m_sFwdslashPath
.Empty();
484 m_sUTF8FwdslashPath
.Empty();
486 this->m_StatAdd
=_T("");
487 this->m_StatDel
=_T("");
489 ATLASSERT(IsEmpty());
492 CTGitPath
CTGitPath::GetDirectory() const
494 if ((IsDirectory())||(!Exists()))
498 return GetContainingDirectory();
501 CTGitPath
CTGitPath::GetContainingDirectory() const
503 EnsureBackslashPathSet();
505 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
506 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
508 // This is a root directory, which needs a trailing slash
510 if(sDirName
== m_sBackslashPath
)
512 // We were clearly provided with a root path to start with - we should return nothing now
516 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
518 // We have an UNC path and we already are the root
522 retVal
.SetFromWin(sDirName
);
526 CString
CTGitPath::GetRootPathString() const
528 EnsureBackslashPathSet();
529 CString workingPath
= m_sBackslashPath
;
530 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
531 ATLVERIFY(::PathStripToRoot(pPath
));
532 workingPath
.ReleaseBuffer();
537 CString
CTGitPath::GetFilename() const
539 //ATLASSERT(!IsDirectory());
540 return GetFileOrDirectoryName();
543 CString
CTGitPath::GetFileOrDirectoryName() const
545 EnsureBackslashPathSet();
546 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
549 CString
CTGitPath::GetUIFileOrDirectoryName() const
552 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
555 CString
CTGitPath::GetFileExtension() const
559 EnsureBackslashPathSet();
560 int dotPos
= m_sBackslashPath
.ReverseFind('.');
561 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
562 if (dotPos
> slashPos
)
563 return m_sBackslashPath
.Mid(dotPos
);
567 CString
CTGitPath::GetBaseFilename() const
570 CString filename
=GetFilename();
571 dot
= filename
.ReverseFind(_T('.'));
573 return filename
.Left(dot
);
578 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
580 int length
= sP1
.GetLength();
581 if(length
!= sP2
.GetLength())
586 // We work from the end of the strings, because path differences
587 // are more likely to occur at the far end of a string
588 LPCTSTR pP1Start
= sP1
;
589 LPCTSTR pP1
= pP1Start
+(length
-1);
590 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
593 if(_totlower(*pP1
--) != _totlower(*pP2
--))
601 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
603 int length
= sP1
.GetLength();
604 if(length
!= sP2
.GetLength())
609 // We work from the end of the strings, because path differences
610 // are more likely to occur at the far end of a string
611 LPCTSTR pP1Start
= sP1
;
612 LPCTSTR pP1
= pP1Start
+(length
-1);
613 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
616 if((*pP1
--) != (*pP2
--))
624 bool CTGitPath::IsEmpty() const
626 // Check the backward slash path first, since the chance that this
627 // one is set is higher. In case of a 'false' return value it's a little
629 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
632 // Test if both paths refer to the same item
633 // Ignores case and slash direction
634 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
636 // Try and find a slash direction which avoids having to convert
638 if(!m_sBackslashPath
.IsEmpty())
640 // *We've* got a \ path - make sure that the RHS also has a \ path
641 rhs
.EnsureBackslashPathSet();
642 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
646 // Assume we've got a fwdslash path and make sure that the RHS has one
647 rhs
.EnsureFwdslashPathSet();
648 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
652 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
654 // Try and find a slash direction which avoids having to convert
656 if(!m_sBackslashPath
.IsEmpty())
658 // *We've* got a \ path - make sure that the RHS also has a \ path
659 rhs
.EnsureBackslashPathSet();
660 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
664 // Assume we've got a fwdslash path and make sure that the RHS has one
665 rhs
.EnsureFwdslashPathSet();
666 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
670 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
672 possibleDescendant
.EnsureBackslashPathSet();
673 EnsureBackslashPathSet();
675 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
676 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
678 return bPathStringsEqual
;
681 return (bPathStringsEqual
&&
682 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
683 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
686 // Get a string representing the file path, optionally with a base
687 // section stripped off the front.
688 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
690 EnsureFwdslashPathSet();
691 if(pOptionalBasePath
!= NULL
)
693 // Find the length of the base-path without having to do an 'ensure' on it
694 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
696 // Now, chop that baseLength of the front of the path
697 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
699 return m_sFwdslashPath
;
702 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
704 left
.EnsureBackslashPathSet();
705 right
.EnsureBackslashPathSet();
706 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
709 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
711 return CTGitPath::Compare(left
, right
) < 0;
714 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
716 return left
.IsEquivalentTo(right
);
719 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
721 if (left
.IsAdminDir() && right
.IsAdminDir())
727 l
= l
.GetContainingDirectory();
728 } while(l
.HasAdminDir());
731 r
= r
.GetContainingDirectory();
732 } while(r
.HasAdminDir());
733 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
735 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
738 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
740 return parent
.IsAncestorOf(child
);
743 void CTGitPath::AppendRawString(const CString
& sAppend
)
745 EnsureFwdslashPathSet();
746 CString strCopy
= m_sFwdslashPath
+= sAppend
;
747 SetFromUnknown(strCopy
);
750 void CTGitPath::AppendPathString(const CString
& sAppend
)
752 EnsureBackslashPathSet();
753 CString
cleanAppend(sAppend
);
754 cleanAppend
.Replace('/', '\\');
755 cleanAppend
.TrimLeft('\\');
756 m_sBackslashPath
.TrimRight('\\');
757 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
761 bool CTGitPath::IsWCRoot() const
763 if (m_bIsWCRootKnown
)
766 m_bIsWCRootKnown
= true;
769 CString topDirectory
;
770 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
775 if (IsEquivalentToWithoutCase(topDirectory
))
783 bool CTGitPath::HasAdminDir() const
785 if (m_bHasAdminDirKnown
)
786 return m_bHasAdminDir
;
788 EnsureBackslashPathSet();
789 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
790 m_bHasAdminDirKnown
= true;
791 return m_bHasAdminDir
;
794 bool CTGitPath::HasSubmodules() const
798 CString path
= m_sProjectRoot
;
799 path
+= _T("\\.gitmodules");
800 if( PathFileExists(path
) )
806 int CTGitPath::GetAdminDirMask() const
810 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
815 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
816 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
820 status
|= ITEMIS_FOLDERINGIT
;
822 status
|= ITEMIS_WCROOT
;
826 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
828 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
829 status
|= ITEMIS_BISECT
;
831 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
832 status
|= ITEMIS_STASH
;
834 if (PathFileExists(dotGitPath
+ _T("svn")))
835 status
|= ITEMIS_GITSVN
;
837 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
838 status
|= ITEMIS_SUBMODULECONTAINER
;
843 bool CTGitPath::HasStashDir() const
846 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
852 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
854 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
856 bool CTGitPath::HasGitSVNDir() const
859 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
865 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
867 return !!PathFileExists(dotGitPath
+ _T("svn"));
869 bool CTGitPath::IsBisectActive() const
872 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
878 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
880 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
882 bool CTGitPath::IsMergeActive() const
885 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
891 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
893 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
895 bool CTGitPath::HasRebaseApply() const
898 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
904 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
906 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
909 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
911 if (m_bHasAdminDirKnown
)
914 *ProjectTopDir
= m_sProjectRoot
;
915 return m_bHasAdminDir
;
918 EnsureBackslashPathSet();
919 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
920 m_bHasAdminDirKnown
= true;
922 *ProjectTopDir
= m_sProjectRoot
;
923 return m_bHasAdminDir
;
926 bool CTGitPath::IsAdminDir() const
928 if (m_bIsAdminDirKnown
)
929 return m_bIsAdminDir
;
931 EnsureBackslashPathSet();
932 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
933 m_bIsAdminDirKnown
= true;
934 return m_bIsAdminDir
;
937 bool CTGitPath::IsValidOnWindows() const
939 if (m_bIsValidOnWindowsKnown
)
940 return m_bIsValidOnWindows
;
942 m_bIsValidOnWindows
= false;
943 EnsureBackslashPathSet();
944 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
946 // the 'file://' URL is just a normal windows path:
947 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
949 sMatch
= sMatch
.Mid(7);
950 sMatch
.TrimLeft(_T("\\"));
951 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
955 sPattern
= _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
959 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
964 tr1::wregex
rx(sPattern
, tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
967 wstring rmatch
= wstring((LPCTSTR
)sMatch
);
968 if (tr1::regex_match(rmatch
, match
, rx
))
970 if (wstring(match
[0]).compare(sMatch
)==0)
971 m_bIsValidOnWindows
= true;
973 if (m_bIsValidOnWindows
)
975 // now check for illegal filenames
976 tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
977 rmatch
= m_sBackslashPath
;
978 if (tr1::regex_search(rmatch
, rx2
, tr1::regex_constants::match_default
))
979 m_bIsValidOnWindows
= false;
984 m_bIsValidOnWindowsKnown
= true;
985 return m_bIsValidOnWindows
;
988 bool CTGitPath::IsSpecialDirectory() const
990 if (m_bIsSpecialDirectoryKnown
)
991 return m_bIsSpecialDirectory
;
993 static LPCTSTR specialDirectories
[]
994 = { _T("trunk"), _T("tags"), _T("branches") };
996 for (int i
= 0; i
< _countof(specialDirectories
); ++i
)
998 CString name
= GetFileOrDirectoryName();
999 if (0 == name
.CompareNoCase(specialDirectories
[i
]))
1001 m_bIsSpecialDirectory
= true;
1006 m_bIsSpecialDirectoryKnown
= true;
1008 return m_bIsSpecialDirectory
;
1011 //////////////////////////////////////////////////////////////////////////
1013 CTGitPathList::CTGitPathList()
1018 // A constructor which allows a path list to be easily built which one initial entry in
1019 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
1021 AddPath(firstEntry
);
1023 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
1031 while(pos
>=0 && pos
<out
.size())
1036 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1038 path
.m_Action
=path
.ParserAction(out
[pos
]);
1039 one
.Tokenize(_T("\t"),tabstart
);
1042 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1046 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1048 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1050 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1052 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1054 path
.m_Stage
=_ttol(part
);
1056 this->AddPath(path
);
1058 pos
=out
.findNextString(pos
);
1062 int CTGitPathList::FillUnRev(unsigned int action
,CTGitPathList
*list
)
1072 count
=list
->GetCount();
1073 for(int i
=0;i
<count
;i
++)
1079 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1084 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1089 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1091 (*list
)[i
].GetWinPathString());
1096 g_Git
.Run(cmd
, &out
);
1100 while( pos
>=0 && pos
<out
.size())
1103 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1106 //SetFromGit will clear all status
1107 path
.SetFromGit(one
);
1108 path
.m_Action
=action
;
1111 pos
=out
.findNextString(pos
);
1117 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1125 while( pos
>=0 && pos
<log
.size())
1127 //one=log.Tokenize(_T("\n"),pos);
1135 if(log
[pos
+1] ==':')
1139 int end
=log
.find(0,pos
);
1142 int file1
=-1,file2
=-1;
1145 actionstart
=log
.find(' ',end
-6);
1152 file1
= log
.find(0,actionstart
);
1158 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1162 file1
= log
.find(0,file1
);
1176 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1178 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1180 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1184 GitPath
->ParserAction( log
[actionstart
] );
1188 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1189 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1191 m_Action
|=GitPath
->m_Action
;
1196 int ac
=path
.ParserAction(log
[actionstart
] );
1197 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1199 path
.SetFromGit(pathname1
,&pathname2
);
1201 //action must be set after setfromgit. SetFromGit will clear all status.
1218 tabstart
=log
.find('\t',pos
);
1222 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1226 tabstart
=log
.find('\t',pos
);
1231 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1235 if(log
[pos
] == 0) //rename
1238 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1239 int sec
=log
.find(0,pos
);
1243 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1250 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1252 path
.SetFromGit(file1
,&file2
);
1254 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1257 GitPath
->m_StatAdd
=StatAdd
;
1258 GitPath
->m_StatDel
=StatDel
;
1262 //path.SetFromGit(pathname);
1265 path
.m_StatAdd
=_T("0");
1266 path
.m_StatDel
=_T("0");
1267 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1271 path
.m_StatAdd
=StatAdd
;
1272 path
.m_StatDel
=StatDel
;
1273 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1279 pos
=log
.findNextString(pos
);
1284 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1286 m_paths
.push_back(newPath
);
1287 m_commonBaseDirectory
.Reset();
1289 int CTGitPathList::GetCount() const
1291 return (int)m_paths
.size();
1293 void CTGitPathList::Clear()
1296 m_commonBaseDirectory
.Reset();
1299 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1301 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1302 return m_paths
[index
];
1305 bool CTGitPathList::AreAllPathsFiles() const
1307 // Look through the vector for any directories - if we find them, return false
1308 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1312 #if defined(_MFC_VER)
1314 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1320 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1322 // for every selected file/folder
1324 while (file
.ReadString(strLine
))
1326 path
.SetFromUnknown(strLine
);
1331 catch (CFileException
* pE
)
1333 TRACE("CFileException loading target file list\n");
1334 TCHAR error
[10000] = {0};
1335 pE
->GetErrorMessage(error
, 10000);
1336 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1343 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1349 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1350 PathVector::const_iterator it
;
1351 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1353 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1354 file
.Write(line
, line
.GetLength());
1360 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1361 PathVector::const_iterator it
;
1362 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1364 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1369 catch (CFileException
* pE
)
1371 TRACE("CFileException in writing temp file\n");
1379 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1385 temp
= sPathString
.Tokenize(_T("*"),pos
);
1390 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1394 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1397 PathVector::const_iterator it
;
1398 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1400 if (!sRet
.IsEmpty())
1402 sRet
+= it
->GetWinPathString();
1409 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1411 // Check if all the paths are files and in the same directory
1412 PathVector::const_iterator it
;
1413 m_commonBaseDirectory
.Reset();
1414 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1416 if(it
->IsDirectory())
1420 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1421 if(m_commonBaseDirectory
.IsEmpty())
1423 m_commonBaseDirectory
= baseDirectory
;
1425 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1428 m_commonBaseDirectory
.Reset();
1435 CTGitPath
CTGitPathList::GetCommonDirectory() const
1437 if (m_commonBaseDirectory
.IsEmpty())
1439 PathVector::const_iterator it
;
1440 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1442 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1443 if(m_commonBaseDirectory
.IsEmpty())
1445 m_commonBaseDirectory
= baseDirectory
;
1447 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1450 m_commonBaseDirectory
.Reset();
1455 // since we only checked strings, not paths,
1456 // we have to make sure now that we really return a *path* here
1457 PathVector::const_iterator iter
;
1458 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1460 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1462 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1466 return m_commonBaseDirectory
;
1469 CTGitPath
CTGitPathList::GetCommonRoot() const
1471 if (GetCount() == 0)
1474 if (GetCount() == 1)
1477 // first entry is common root for itself
1478 // (add trailing '\\' to detect partial matches of the last path element)
1479 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1480 int rootLength
= root
.GetLength();
1482 // determine common path string prefix
1483 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1485 CString path
= it
->GetWinPathString() + _T('\\');
1487 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1488 if (newLength
!= rootLength
)
1490 root
.Delete(newLength
, rootLength
);
1491 rootLength
= newLength
;
1495 // remove the last (partial) path element
1497 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1500 return CTGitPath(root
);
1503 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1505 std::sort(m_paths
.begin(), m_paths
.end());
1507 std::reverse(m_paths
.begin(), m_paths
.end());
1510 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1512 PathVector::const_iterator it
;
1513 SortByPathname(true); // nested ones first
1516 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1518 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1520 if (!it
->IsDirectory())
1521 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1523 sPaths
+= it
->GetWinPath();
1529 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1533 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1535 SHFILEOPSTRUCT shop
= {0};
1536 shop
.wFunc
= FO_DELETE
;
1538 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1540 shop
.fFlags
|= FOF_ALLOWUNDO
;
1541 const bool bRet
= (SHFileOperation(&shop
) == 0);
1545 void CTGitPathList::RemoveDuplicates()
1548 // Remove the duplicates
1549 // (Unique moves them to the end of the vector, then erase chops them off)
1550 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1553 void CTGitPathList::RemoveAdminPaths()
1555 PathVector::iterator it
;
1556 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1558 if (it
->IsAdminDir())
1561 it
= m_paths
.begin();
1568 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1570 PathVector::iterator it
;
1571 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1573 if (it
->IsEquivalentTo(path
))
1581 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1583 PathVector::iterator it
;
1584 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1586 if (it
->GetGitPathString()==path
.GetGitPathString())
1593 void CTGitPathList::RemoveChildren()
1596 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1599 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1601 if (list
.GetCount() != GetCount())
1603 for (int i
=0; i
<list
.GetCount(); ++i
)
1605 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1611 //////////////////////////////////////////////////////////////////////////
1613 apr_array_header_t
* CTGitPathList::MakePathArray (apr_pool_t
*pool
) const
1615 apr_array_header_t
*targets
= apr_array_make (pool
, GetCount(), sizeof(const char *));
1617 for(int nItem
= 0; nItem
< GetCount(); nItem
++)
1619 const char * target
= m_paths
[nItem
].GetGitApiPath(pool
);
1620 (*((const char **) apr_array_push (targets
))) = target
;
1626 //////////////////////////////////////////////////////////////////////////
1630 // Some test cases for these classes
1631 static class CTGitPathTests
1637 pool
= svn_pool_create(NULL
);
1643 RemoveDuplicatesTest();
1644 RemoveChildrenTest();
1645 ContainingDirectoryTest();
1647 SubversionPathTest();
1648 GetCommonRootTest();
1649 #if defined(_MFC_VER)
1650 ValidPathAndUrlTest();
1657 // apr_pool_t * pool;
1658 void GetDirectoryTest()
1660 // Bit tricky, this test, because we need to know something about the file
1661 // layout on the machine which is running the test
1662 TCHAR winDir
[MAX_PATH
+1];
1663 GetWindowsDirectory(winDir
, MAX_PATH
);
1664 CString
sWinDir(winDir
);
1667 // This is a file which we know will always be there
1668 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1669 ATLASSERT(!testPath
.IsDirectory());
1670 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1671 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1673 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1674 // but we know it must be different to the directory itself
1675 testPath
.SetFromUnknown(sWinDir
);
1676 ATLASSERT(testPath
.IsDirectory());
1677 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1678 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1679 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1682 testPath
.SetFromUnknown(_T("C:\\"));
1683 ATLASSERT(testPath
.IsDirectory());
1684 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1685 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1686 // Try a root UNC path
1687 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1688 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1694 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1695 ATLASSERT(!testPath
.IsAdminDir());
1696 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1697 ATLASSERT(!testPath
.IsAdminDir());
1698 testPath
.SetFromUnknown(_T("c:\\.svn"));
1699 ATLASSERT(testPath
.IsAdminDir());
1700 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1701 ATLASSERT(!testPath
.IsAdminDir());
1702 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1703 ATLASSERT(testPath
.IsAdminDir());
1705 CTGitPathList pathList
;
1706 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1707 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1708 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1709 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1710 pathList
.RemoveAdminPaths();
1711 ATLASSERT(pathList
.GetCount()==2);
1713 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1714 pathList
.RemoveAdminPaths();
1715 ATLASSERT(pathList
.GetCount()==1);
1720 CTGitPathList testList
;
1722 testPath
.SetFromUnknown(_T("c:/Z"));
1723 testList
.AddPath(testPath
);
1724 testPath
.SetFromUnknown(_T("c:/B"));
1725 testList
.AddPath(testPath
);
1726 testPath
.SetFromUnknown(_T("c:\\a"));
1727 testList
.AddPath(testPath
);
1728 testPath
.SetFromUnknown(_T("c:/Test"));
1729 testList
.AddPath(testPath
);
1731 testList
.SortByPathname();
1733 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1734 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1735 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1736 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1739 void RawAppendTest()
1741 CTGitPath
testPath(_T("c:/test/"));
1742 testPath
.AppendRawString(_T("/Hello"));
1743 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1745 testPath
.AppendRawString(_T("\\T2"));
1746 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1748 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1749 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1750 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1751 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1754 void PathAppendTest()
1756 CTGitPath
testPath(_T("c:/test/"));
1757 testPath
.AppendPathString(_T("/Hello"));
1758 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1760 testPath
.AppendPathString(_T("T2"));
1761 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1763 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1764 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1765 // You wouldn't want to do this in real life - you'd use append-raw
1766 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1767 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1770 void RemoveDuplicatesTest()
1773 list
.AddPath(CTGitPath(_T("Z")));
1774 list
.AddPath(CTGitPath(_T("A")));
1775 list
.AddPath(CTGitPath(_T("E")));
1776 list
.AddPath(CTGitPath(_T("E")));
1778 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1779 ATLASSERT(list
[2]==list
[3]);
1781 ATLASSERT(list
.GetCount() == 4);
1783 list
.RemoveDuplicates();
1785 ATLASSERT(list
.GetCount() == 3);
1787 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1788 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1789 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1792 void RemoveChildrenTest()
1795 list
.AddPath(CTGitPath(_T("c:\\test")));
1796 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1797 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1798 list
.AddPath(CTGitPath(_T("c:\\parent")));
1799 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1800 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1801 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1803 ATLASSERT(list
.GetCount() == 7);
1805 list
.RemoveChildren();
1807 ATLTRACE("count = %d\n", list
.GetCount());
1808 ATLASSERT(list
.GetCount() == 3);
1810 list
.SortByPathname();
1812 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1813 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1814 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1817 #if defined(_MFC_VER)
1818 void ListLoadingTest()
1820 TCHAR buf
[MAX_PATH
];
1821 GetCurrentDirectory(MAX_PATH
, buf
);
1822 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1823 CTGitPathList testList
;
1824 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1826 ATLASSERT(testList
.GetCount() == 3);
1827 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1828 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1829 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1831 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1833 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1834 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1835 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1839 void ContainingDirectoryTest()
1843 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1845 dir
= testPath
.GetContainingDirectory();
1846 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1847 dir
= dir
.GetContainingDirectory();
1848 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1849 dir
= dir
.GetContainingDirectory();
1850 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1851 dir
= dir
.GetContainingDirectory();
1852 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1853 dir
= dir
.GetContainingDirectory();
1854 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1855 dir
= dir
.GetContainingDirectory();
1856 ATLASSERT(dir
.IsEmpty());
1857 ATLASSERT(dir
.GetWinPathString() == _T(""));
1863 testPath
.SetFromWin(_T("c:\\windows"));
1864 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1865 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1866 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1867 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1868 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1871 void SubversionPathTest()
1874 testPath
.SetFromWin(_T("c:\\"));
1875 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1876 testPath
.SetFromWin(_T("c:\\folder"));
1877 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1878 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1879 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1880 testPath
.SetFromUnknown(_T("http://testing/"));
1881 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1882 testPath
.SetFromGit(NULL
);
1883 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1884 #if defined(_MFC_VER)
1885 testPath
.SetFromUnknown(_T("http://testing again"));
1886 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1887 testPath
.SetFromUnknown(_T("http://testing%20again"));
1888 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1889 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1890 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1894 void GetCommonRootTest()
1896 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1897 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1898 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1901 list
.AddPath(pathA
);
1902 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1903 list
.AddPath(pathB
);
1904 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1905 list
.AddPath(pathC
);
1906 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1909 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");
1910 list
.LoadFromAsteriskSeparatedString(sPathList
);
1911 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1914 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1915 list
.LoadFromAsteriskSeparatedString(sPathList
);
1916 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1919 sPathList
= _T("c:\\windows\\*c:\\windows");
1920 list
.LoadFromAsteriskSeparatedString(sPathList
);
1921 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1924 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1925 list
.LoadFromAsteriskSeparatedString(sPathList
);
1926 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1929 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1930 list
.LoadFromAsteriskSeparatedString(sPathList
);
1931 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1935 void ValidPathAndUrlTest()
1938 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1939 ATLASSERT(testPath
.IsValidOnWindows());
1940 testPath
.SetFromWin(_T("c:\\"));
1941 ATLASSERT(testPath
.IsValidOnWindows());
1942 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1943 ATLASSERT(testPath
.IsValidOnWindows());
1944 testPath
.SetFromWin(_T("c"));
1945 ATLASSERT(testPath
.IsValidOnWindows());
1946 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1947 ATLASSERT(testPath
.IsValidOnWindows());
1948 testPath
.SetFromWin(_T("c:\\folder\\"));
1949 ATLASSERT(testPath
.IsValidOnWindows());
1950 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1951 ATLASSERT(testPath
.IsValidOnWindows());
1952 testPath
.SetFromWin(_T("c:\\.svn"));
1953 ATLASSERT(testPath
.IsValidOnWindows());
1954 testPath
.SetFromWin(_T("c:\\com\\file"));
1955 ATLASSERT(testPath
.IsValidOnWindows());
1956 testPath
.SetFromWin(_T("c:\\test\\conf"));
1957 ATLASSERT(testPath
.IsValidOnWindows());
1958 testPath
.SetFromWin(_T("c:\\LPT"));
1959 ATLASSERT(testPath
.IsValidOnWindows());
1960 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1961 ATLASSERT(testPath
.IsValidOnWindows());
1962 testPath
.SetFromWin(_T("c:\\com1test"));
1963 ATLASSERT(testPath
.IsValidOnWindows());
1964 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1965 ATLASSERT(testPath
.IsValidOnWindows());
1967 testPath
.SetFromWin(_T("\\\\Share\\filename"));
1968 ATLASSERT(testPath
.IsValidOnWindows());
1969 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
1970 ATLASSERT(testPath
.IsValidOnWindows());
1971 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
1972 ATLASSERT(testPath
.IsValidOnWindows());
1974 // now the negative tests
1975 testPath
.SetFromWin(_T("c:\\test:folder"));
1976 ATLASSERT(!testPath
.IsValidOnWindows());
1977 testPath
.SetFromWin(_T("c:\\file<name"));
1978 ATLASSERT(!testPath
.IsValidOnWindows());
1979 testPath
.SetFromWin(_T("c:\\something*else"));
1980 ATLASSERT(!testPath
.IsValidOnWindows());
1981 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
1982 ATLASSERT(!testPath
.IsValidOnWindows());
1983 testPath
.SetFromWin(_T("c:\\ext.>ension"));
1984 ATLASSERT(!testPath
.IsValidOnWindows());
1985 testPath
.SetFromWin(_T("c:\\com1\\filename"));
1986 ATLASSERT(!testPath
.IsValidOnWindows());
1987 testPath
.SetFromWin(_T("c:\\com1"));
1988 ATLASSERT(!testPath
.IsValidOnWindows());
1989 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
1990 ATLASSERT(!testPath
.IsValidOnWindows());
1992 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1993 ATLASSERT(!testPath
.IsValidOnWindows());
1994 testPath
.SetFromWin(_T("\\\\Share\\prn"));
1995 ATLASSERT(!testPath
.IsValidOnWindows());
1996 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
1997 ATLASSERT(!testPath
.IsValidOnWindows());
1999 // now come some URL tests
2000 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2001 ATLASSERT(testPath
.IsValidOnWindows());
2002 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2003 ATLASSERT(testPath
.IsValidOnWindows());
2004 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2005 ATLASSERT(testPath
.IsValidOnWindows());
2006 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2007 ATLASSERT(testPath
.IsValidOnWindows());
2008 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2009 ATLASSERT(testPath
.IsValidOnWindows());
2010 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2011 ATLASSERT(testPath
.IsValidOnWindows());
2012 // and some negative URL tests
2013 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2014 ATLASSERT(!testPath
.IsValidOnWindows());
2015 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2016 ATLASSERT(!testPath
.IsValidOnWindows());
2017 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2018 ATLASSERT(!testPath
.IsValidOnWindows());
2019 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2020 ATLASSERT(!testPath
.IsValidOnWindows());
2021 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2022 ATLASSERT(!testPath
.IsValidOnWindows());
2026 } TGitPathTestobject
;
2030 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2033 for(i
=0;i
<this->GetCount();i
++)
2035 if((*this)[i
].GetGitPathString() == path
)
2036 return (CTGitPath
*)&(*this)[i
];
2040 CString
CTGitPath::GetActionName(int action
)
2042 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2043 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2044 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2045 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2046 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2047 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2048 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2049 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2051 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2052 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2053 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2054 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2055 if(action
& CTGitPath::LOGACTIONS_COPY
)
2056 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2058 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2059 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2061 if(action
& CTGitPath::LOGACTIONS_REBASE_EDIT
)
2062 return MAKEINTRESOURCE(IDS_PATHACTIONS_EDIT
);
2063 if(action
& CTGitPath::LOGACTIONS_REBASE_SQUASH
)
2064 return MAKEINTRESOURCE(IDS_PATHACTIONS_SQUASH
);
2065 if(action
& CTGitPath::LOGACTIONS_REBASE_PICK
)
2066 return MAKEINTRESOURCE(IDS_PATHACTIONS_PICK
);
2067 if(action
& CTGitPath::LOGACTIONS_REBASE_SKIP
)
2068 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIP
);
2070 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2072 CString
CTGitPath::GetActionName()
2074 return GetActionName(m_Action
);
2077 int CTGitPathList::GetAction()