1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
4 // Copyright (C) 2010-2014 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"
38 CTGitPath::CTGitPath(void)
39 : m_bDirectoryKnown(false)
40 , m_bIsDirectory(false)
42 , m_bHasAdminDirKnown(false)
43 , m_bHasAdminDir(false)
44 , m_bIsValidOnWindowsKnown(false)
45 , m_bIsValidOnWindows(false)
46 , m_bIsReadOnly(false)
47 , m_bIsAdminDirKnown(false)
48 , m_bIsAdminDir(false)
50 , m_bExistsKnown(false)
51 , m_bLastWriteTimeKnown(0)
54 , m_bIsSpecialDirectoryKnown(false)
55 , m_bIsSpecialDirectory(false)
56 , m_bIsWCRootKnown(false)
66 CTGitPath::~CTGitPath(void)
69 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
70 CTGitPath::CTGitPath(const CString
& sUnknownPath
) :
71 m_bDirectoryKnown(false)
72 , m_bIsDirectory(false)
74 , m_bHasAdminDirKnown(false)
75 , m_bHasAdminDir(false)
76 , m_bIsValidOnWindowsKnown(false)
77 , m_bIsValidOnWindows(false)
78 , m_bIsReadOnly(false)
79 , m_bIsAdminDirKnown(false)
80 , m_bIsAdminDir(false)
82 , m_bExistsKnown(false)
83 , m_bLastWriteTimeKnown(0)
86 , m_bIsSpecialDirectoryKnown(false)
87 , m_bIsSpecialDirectory(false)
88 , m_bIsWCRootKnown(false)
93 SetFromUnknown(sUnknownPath
);
99 int CTGitPath::ParserAction(BYTE action
)
101 //action=action.TrimLeft();
102 //TCHAR c=action.GetAt(0);
104 m_Action
|= LOGACTIONS_MODIFIED
;
106 m_Action
|= LOGACTIONS_REPLACED
;
108 m_Action
|= LOGACTIONS_ADDED
;
110 m_Action
|= LOGACTIONS_DELETED
;
112 m_Action
|= LOGACTIONS_UNMERGED
;
114 m_Action
|= LOGACTIONS_DELETED
;
116 m_Action
|= LOGACTIONS_CACHE
;
118 m_Action
|= LOGACTIONS_COPY
;
120 m_Action
|= LOGACTIONS_MODIFIED
;
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 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
171 SanitizeRootPath(m_sBackslashPath
, false);
172 ATLASSERT(m_sBackslashPath
.Find('/')<0);
174 void CTGitPath::SetFromWin(const CString
& sPath
)
177 m_sBackslashPath
= sPath
;
178 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
179 SanitizeRootPath(m_sBackslashPath
, false);
181 void CTGitPath::SetFromWin(LPCTSTR pPath
, bool bIsDirectory
)
184 m_sBackslashPath
= pPath
;
185 m_bIsDirectory
= bIsDirectory
;
186 m_bDirectoryKnown
= true;
187 SanitizeRootPath(m_sBackslashPath
, false);
189 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
192 m_sBackslashPath
= sPath
;
193 m_bIsDirectory
= bIsDirectory
;
194 m_bDirectoryKnown
= true;
195 SanitizeRootPath(m_sBackslashPath
, false);
197 void CTGitPath::SetFromUnknown(const CString
& sPath
)
200 // Just set whichever path we think is most likely to be used
201 // GitAdminDir admin;
203 // if(admin.HasAdminDir(sPath,&p))
204 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
206 SetFwdslashPath(sPath
);
209 LPCTSTR
CTGitPath::GetWinPath() const
215 if(m_sBackslashPath
.IsEmpty())
217 SetBackslashPath(m_sFwdslashPath
);
219 return m_sBackslashPath
;
221 // This is a temporary function, to be used during the migration to
222 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
223 const CString
& CTGitPath::GetWinPathString() const
225 if(m_sBackslashPath
.IsEmpty())
227 SetBackslashPath(m_sFwdslashPath
);
229 return m_sBackslashPath
;
232 const CString
& CTGitPath::GetGitPathString() const
234 if(m_sFwdslashPath
.IsEmpty())
236 SetFwdslashPath(m_sBackslashPath
);
238 return m_sFwdslashPath
;
241 const CString
&CTGitPath::GetGitOldPathString() const
243 return m_sOldFwdslashPath
;
246 const CString
& CTGitPath::GetUIPathString() const
248 if (m_sUIPath
.IsEmpty())
250 m_sUIPath
= GetWinPathString();
255 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
257 CString path
= sPath
;
258 path
.Replace('\\', '/');
260 // We don't leave a trailing /
262 path
.Replace(L
"//?/", L
"");
264 SanitizeRootPath(path
, true);
266 path
.Replace(_T("file:////"), _T("file://"));
267 m_sFwdslashPath
= path
;
269 m_sUTF8FwdslashPath
.Empty();
272 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
274 CString path
= sPath
;
275 path
.Replace('/', '\\');
276 path
.TrimRight('\\');
277 SanitizeRootPath(path
, false);
278 m_sBackslashPath
= path
;
281 void CTGitPath::SetUTF8FwdslashPath(const CString
& sPath
) const
283 m_sUTF8FwdslashPath
= CUnicodeUtils::GetUTF8(sPath
);
286 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
288 // Make sure to add the trailing slash to root paths such as 'C:'
289 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
291 sPath
+= (bIsForwardPath
) ? _T("/") : _T("\\");
295 bool CTGitPath::IsDirectory() const
297 if(!m_bDirectoryKnown
)
301 return m_bIsDirectory
;
304 bool CTGitPath::Exists() const
313 bool CTGitPath::Delete(bool bTrash
) const
315 EnsureBackslashPathSet();
316 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
320 if ((bTrash
)||(IsDirectory()))
322 std::unique_ptr
<TCHAR
[]> buf(new TCHAR
[m_sBackslashPath
.GetLength() + 2]);
323 _tcscpy_s(buf
.get(), m_sBackslashPath
.GetLength() + 2, m_sBackslashPath
);
324 buf
[m_sBackslashPath
.GetLength()] = 0;
325 buf
[m_sBackslashPath
.GetLength()+1] = 0;
326 bRet
= CTGitPathList::DeleteViaShell(buf
.get(), bTrash
);
330 bRet
= !!::DeleteFile(m_sBackslashPath
);
334 m_bExistsKnown
= true;
338 __int64
CTGitPath::GetLastWriteTime() const
340 if(!m_bLastWriteTimeKnown
)
344 return m_lastWriteTime
;
347 __int64
CTGitPath::GetFileSize() const
349 if(!m_bDirectoryKnown
)
356 bool CTGitPath::IsReadOnly() const
358 if(!m_bLastWriteTimeKnown
)
362 return m_bIsReadOnly
;
365 void CTGitPath::UpdateAttributes() const
367 EnsureBackslashPathSet();
368 WIN32_FILE_ATTRIBUTE_DATA attribs
;
369 if (m_sBackslashPath
.GetLength() >= 248)
370 m_sLongBackslashPath
= _T("\\\\?\\") + m_sBackslashPath
;
371 if(GetFileAttributesEx(m_sBackslashPath
.GetLength() >= 248 ? m_sLongBackslashPath
: m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
373 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
374 // don't cast directly to an __int64:
375 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284%28v=vs.85%29.aspx
376 // "Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value
377 // because it can cause alignment faults on 64-bit Windows."
378 m_lastWriteTime
= static_cast<__int64
>(attribs
.ftLastWriteTime
.dwHighDateTime
) << 32 | attribs
.ftLastWriteTime
.dwLowDateTime
;
385 m_fileSize
= ((INT64
)( (DWORD
)(attribs
.nFileSizeLow
) ) | ( (INT64
)( (DWORD
)(attribs
.nFileSizeHigh
) )<<32 ));
387 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
392 m_bIsDirectory
= false;
395 DWORD err
= GetLastError();
396 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
406 m_bDirectoryKnown
= true;
407 m_bLastWriteTimeKnown
= true;
408 m_bExistsKnown
= true;
411 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
415 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
417 CString str
=GetWinPathString();
418 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
423 void CTGitPath::EnsureBackslashPathSet() const
425 if(m_sBackslashPath
.IsEmpty())
427 SetBackslashPath(m_sFwdslashPath
);
428 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
431 void CTGitPath::EnsureFwdslashPathSet() const
433 if(m_sFwdslashPath
.IsEmpty())
435 SetFwdslashPath(m_sBackslashPath
);
436 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
441 // Reset all the caches
442 void CTGitPath::Reset()
444 m_bDirectoryKnown
= false;
446 m_bLastWriteTimeKnown
= false;
447 m_bHasAdminDirKnown
= false;
448 m_bIsValidOnWindowsKnown
= false;
449 m_bIsAdminDirKnown
= false;
450 m_bExistsKnown
= false;
451 m_bIsSpecialDirectoryKnown
= false;
452 m_bIsSpecialDirectory
= false;
454 m_sBackslashPath
.Empty();
455 m_sFwdslashPath
.Empty();
456 m_sUTF8FwdslashPath
.Empty();
458 this->m_StatAdd
=_T("");
459 this->m_StatDel
=_T("");
461 ATLASSERT(IsEmpty());
464 CTGitPath
CTGitPath::GetDirectory() const
466 if ((IsDirectory())||(!Exists()))
470 return GetContainingDirectory();
473 CTGitPath
CTGitPath::GetContainingDirectory() const
475 EnsureBackslashPathSet();
477 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
478 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
480 // This is a root directory, which needs a trailing slash
482 if(sDirName
== m_sBackslashPath
)
484 // We were clearly provided with a root path to start with - we should return nothing now
488 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
490 // We have an UNC path and we already are the root
494 retVal
.SetFromWin(sDirName
);
498 CString
CTGitPath::GetRootPathString() const
500 EnsureBackslashPathSet();
501 CString workingPath
= m_sBackslashPath
;
502 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
503 ATLVERIFY(::PathStripToRoot(pPath
));
504 workingPath
.ReleaseBuffer();
509 CString
CTGitPath::GetFilename() const
511 //ATLASSERT(!IsDirectory());
512 return GetFileOrDirectoryName();
515 CString
CTGitPath::GetFileOrDirectoryName() const
517 EnsureBackslashPathSet();
518 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
521 CString
CTGitPath::GetUIFileOrDirectoryName() const
524 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
527 CString
CTGitPath::GetFileExtension() const
531 EnsureBackslashPathSet();
532 int dotPos
= m_sBackslashPath
.ReverseFind('.');
533 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
534 if (dotPos
> slashPos
)
535 return m_sBackslashPath
.Mid(dotPos
);
539 CString
CTGitPath::GetBaseFilename() const
542 CString filename
=GetFilename();
543 dot
= filename
.ReverseFind(_T('.'));
545 return filename
.Left(dot
);
550 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
552 int length
= sP1
.GetLength();
553 if(length
!= sP2
.GetLength())
558 // We work from the end of the strings, because path differences
559 // are more likely to occur at the far end of a string
560 LPCTSTR pP1Start
= sP1
;
561 LPCTSTR pP1
= pP1Start
+(length
-1);
562 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
565 if(_totlower(*pP1
--) != _totlower(*pP2
--))
573 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
575 int length
= sP1
.GetLength();
576 if(length
!= sP2
.GetLength())
581 // We work from the end of the strings, because path differences
582 // are more likely to occur at the far end of a string
583 LPCTSTR pP1Start
= sP1
;
584 LPCTSTR pP1
= pP1Start
+(length
-1);
585 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
588 if((*pP1
--) != (*pP2
--))
596 bool CTGitPath::IsEmpty() const
598 // Check the backward slash path first, since the chance that this
599 // one is set is higher. In case of a 'false' return value it's a little
601 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
604 // Test if both paths refer to the same item
605 // Ignores case and slash direction
606 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
608 // Try and find a slash direction which avoids having to convert
610 if(!m_sBackslashPath
.IsEmpty())
612 // *We've* got a \ path - make sure that the RHS also has a \ path
613 rhs
.EnsureBackslashPathSet();
614 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
618 // Assume we've got a fwdslash path and make sure that the RHS has one
619 rhs
.EnsureFwdslashPathSet();
620 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
624 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
626 // Try and find a slash direction which avoids having to convert
628 if(!m_sBackslashPath
.IsEmpty())
630 // *We've* got a \ path - make sure that the RHS also has a \ path
631 rhs
.EnsureBackslashPathSet();
632 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
636 // Assume we've got a fwdslash path and make sure that the RHS has one
637 rhs
.EnsureFwdslashPathSet();
638 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
642 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
644 possibleDescendant
.EnsureBackslashPathSet();
645 EnsureBackslashPathSet();
647 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
648 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
650 return bPathStringsEqual
;
653 return (bPathStringsEqual
&&
654 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
655 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
658 // Get a string representing the file path, optionally with a base
659 // section stripped off the front.
660 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
662 EnsureFwdslashPathSet();
663 if(pOptionalBasePath
!= NULL
)
665 // Find the length of the base-path without having to do an 'ensure' on it
666 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
668 // Now, chop that baseLength of the front of the path
669 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
671 return m_sFwdslashPath
;
674 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
676 left
.EnsureBackslashPathSet();
677 right
.EnsureBackslashPathSet();
678 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
681 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
683 return CTGitPath::Compare(left
, right
) < 0;
686 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
688 return left
.IsEquivalentTo(right
);
691 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
693 if (left
.IsAdminDir() && right
.IsAdminDir())
699 l
= l
.GetContainingDirectory();
700 } while(l
.HasAdminDir());
703 r
= r
.GetContainingDirectory();
704 } while(r
.HasAdminDir());
705 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
707 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
710 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
712 return parent
.IsAncestorOf(child
);
715 void CTGitPath::AppendRawString(const CString
& sAppend
)
717 EnsureFwdslashPathSet();
718 CString strCopy
= m_sFwdslashPath
+= sAppend
;
719 SetFromUnknown(strCopy
);
722 void CTGitPath::AppendPathString(const CString
& sAppend
)
724 EnsureBackslashPathSet();
725 CString
cleanAppend(sAppend
);
726 cleanAppend
.Replace('/', '\\');
727 cleanAppend
.TrimLeft('\\');
728 m_sBackslashPath
.TrimRight('\\');
729 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
733 bool CTGitPath::IsWCRoot() const
735 if (m_bIsWCRootKnown
)
738 m_bIsWCRootKnown
= true;
741 CString topDirectory
;
742 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
747 if (IsEquivalentToWithoutCase(topDirectory
))
755 bool CTGitPath::HasAdminDir() const
757 if (m_bHasAdminDirKnown
)
758 return m_bHasAdminDir
;
760 EnsureBackslashPathSet();
761 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
762 m_bHasAdminDirKnown
= true;
763 return m_bHasAdminDir
;
766 bool CTGitPath::HasSubmodules() const
770 CString path
= m_sProjectRoot
;
771 path
+= _T("\\.gitmodules");
772 if( PathFileExists(path
) )
778 int CTGitPath::GetAdminDirMask() const
782 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
787 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
788 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
792 status
|= ITEMIS_FOLDERINGIT
;
795 status
|= ITEMIS_WCROOT
;
797 CString topProjectDir
;
798 if (g_GitAdminDir
.HasAdminDir(GetWinPathString(), false, &topProjectDir
))
800 if (PathFileExists(topProjectDir
+ _T("\\.gitmodules")))
802 CAutoConfig
config(true);
803 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(topProjectDir
+ _T("\\.gitmodules")), GIT_CONFIG_LEVEL_APP
, FALSE
);
804 CString relativePath
= GetWinPathString().Mid(topProjectDir
.GetLength());
805 relativePath
.Replace(_T("\\"), _T("/"));
806 relativePath
.Trim(_T("/"));
807 CStringA submodulePath
= CUnicodeUtils::GetUTF8(relativePath
);
808 if (git_config_foreach_match(config
, "submodule\\..*\\.path",
809 [](const git_config_entry
*entry
, void *data
) { return entry
->value
== *(CStringA
*)data
? GIT_EUSER
: 0; }, &submodulePath
) == GIT_EUSER
)
810 status
|= ITEMIS_SUBMODULE
;
817 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
819 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
820 status
|= ITEMIS_BISECT
;
822 if (PathFileExists(dotGitPath
+ _T("MERGE_HEAD")))
823 status
|= ITEMIS_MERGEACTIVE
;
825 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
826 status
|= ITEMIS_STASH
;
828 if (PathFileExists(dotGitPath
+ _T("svn")))
829 status
|= ITEMIS_GITSVN
;
831 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
832 status
|= ITEMIS_SUBMODULECONTAINER
;
837 bool CTGitPath::HasStashDir() const
840 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
846 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
848 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
850 bool CTGitPath::HasGitSVNDir() const
853 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
859 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
861 return !!PathFileExists(dotGitPath
+ _T("svn"));
863 bool CTGitPath::IsBisectActive() const
866 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
872 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
874 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
876 bool CTGitPath::IsMergeActive() const
879 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
885 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
887 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
889 bool CTGitPath::HasRebaseApply() const
892 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
898 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
900 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
903 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
905 if (m_bHasAdminDirKnown
)
908 *ProjectTopDir
= m_sProjectRoot
;
909 return m_bHasAdminDir
;
912 EnsureBackslashPathSet();
913 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
914 m_bHasAdminDirKnown
= true;
916 *ProjectTopDir
= m_sProjectRoot
;
917 return m_bHasAdminDir
;
920 bool CTGitPath::IsAdminDir() const
922 if (m_bIsAdminDirKnown
)
923 return m_bIsAdminDir
;
925 EnsureBackslashPathSet();
926 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
927 m_bIsAdminDirKnown
= true;
928 return m_bIsAdminDir
;
931 bool CTGitPath::IsValidOnWindows() const
933 if (m_bIsValidOnWindowsKnown
)
934 return m_bIsValidOnWindows
;
936 m_bIsValidOnWindows
= false;
937 EnsureBackslashPathSet();
938 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
939 std::wstring sPattern
;
940 // the 'file://' URL is just a normal windows path:
941 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
943 sMatch
= sMatch
.Mid(7);
944 sMatch
.TrimLeft(_T("\\"));
945 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
949 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
954 std::tr1::wregex
rx(sPattern
, std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
955 std::tr1::wsmatch match
;
957 std::wstring rmatch
= std::wstring((LPCTSTR
)sMatch
);
958 if (std::tr1::regex_match(rmatch
, match
, rx
))
960 if (std::wstring(match
[0]).compare(sMatch
)==0)
961 m_bIsValidOnWindows
= true;
963 if (m_bIsValidOnWindows
)
965 // now check for illegal filenames
966 std::tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
967 rmatch
= m_sBackslashPath
;
968 if (std::tr1::regex_search(rmatch
, rx2
, std::tr1::regex_constants::match_default
))
969 m_bIsValidOnWindows
= false;
972 catch (std::exception
) {}
974 m_bIsValidOnWindowsKnown
= true;
975 return m_bIsValidOnWindows
;
978 //////////////////////////////////////////////////////////////////////////
980 CTGitPathList::CTGitPathList()
985 // A constructor which allows a path list to be easily built which one initial entry in
986 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
991 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
999 while(pos
>=0 && pos
<out
.size())
1004 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1006 path
.m_Action
=path
.ParserAction(out
[pos
]);
1007 one
.Tokenize(_T("\t"),tabstart
);
1010 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1016 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1020 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1024 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1028 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1032 path
.m_Stage
=_ttol(part
);
1034 this->AddPath(path
);
1036 pos
=out
.findNextString(pos
);
1040 int CTGitPathList::FillUnRev(unsigned int action
, CTGitPathList
*list
, CString
*err
)
1049 count
=list
->GetCount();
1050 for (int i
= 0; i
< count
; ++i
)
1056 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1061 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1066 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1068 (*list
)[i
].GetWinPathString());
1071 BYTE_VECTOR out
, errb
;
1073 if (g_Git
.Run(cmd
, &out
, &errb
))
1076 g_Git
.StringAppend(err
, &errb
[0], CP_UTF8
, (int)errb
.size());
1082 while (pos
>= 0 && pos
< (int)out
.size())
1085 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1088 //SetFromGit will clear all status
1089 path
.SetFromGit(one
);
1090 path
.m_Action
=action
;
1093 pos
=out
.findNextString(pos
);
1099 int CTGitPathList::FillBasedOnIndexFlags(unsigned short flag
, CTGitPathList
* list
/*nullptr*/)
1105 CAutoRepository
repository(g_Git
.GetGitRepository());
1110 if (git_repository_index(index
.GetPointer(), repository
))
1114 if (list
== nullptr)
1117 count
= list
->GetCount();
1118 for (int j
= 0; j
< count
; ++j
)
1120 for (size_t i
= 0, ecount
= git_index_entrycount(index
); i
< ecount
; ++i
)
1122 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
1124 if (!e
|| !((e
->flags
| e
->flags_extended
) & flag
) || !e
->path
)
1128 g_Git
.StringAppend(&one
, (BYTE
*)e
->path
, CP_UTF8
);
1130 if (!(!list
|| (*list
)[j
].GetWinPathString().IsEmpty() || one
== (*list
)[j
].GetGitPathString() || (PathIsDirectory(g_Git
.m_CurrentDir
+ L
"\\" + (*list
)[j
].GetWinPathString()) && one
.Find((*list
)[j
].GetGitPathString() + _T("/")) == 0)))
1133 //SetFromGit will clear all status
1134 path
.SetFromGit(one
);
1135 if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_SKIP_WORKTREE
)
1136 path
.m_Action
= CTGitPath::LOGACTIONS_SKIPWORKTREE
;
1137 else if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_VALID
)
1138 path
.m_Action
= CTGitPath::LOGACTIONS_ASSUMEVALID
;
1145 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1153 while (pos
>= 0 && pos
< (int)log
.size())
1155 //one=log.Tokenize(_T("\n"),pos);
1163 if(log
[pos
+1] ==':')
1167 int end
=log
.find(0,pos
);
1169 int file1
=-1,file2
=-1;
1172 actionstart
=log
.find(' ',end
-6);
1179 file1
= log
.find(0,actionstart
);
1185 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1188 file1
= log
.find(0,file1
);
1202 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1204 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1206 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1210 GitPath
->ParserAction( log
[actionstart
] );
1214 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1215 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1217 m_Action
|=GitPath
->m_Action
;
1222 int ac
=path
.ParserAction(log
[actionstart
] );
1223 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1225 path
.SetFromGit(pathname1
,&pathname2
);
1227 //action must be set after setfromgit. SetFromGit will clear all status.
1244 tabstart
=log
.find('\t',pos
);
1248 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1252 tabstart
=log
.find('\t',pos
);
1257 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1261 if(log
[pos
] == 0) //rename
1264 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1265 int sec
=log
.find(0,pos
);
1269 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1276 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1278 path
.SetFromGit(file1
,&file2
);
1280 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1283 GitPath
->m_StatAdd
=StatAdd
;
1284 GitPath
->m_StatDel
=StatDel
;
1288 //path.SetFromGit(pathname);
1291 path
.m_StatAdd
=_T("0");
1292 path
.m_StatDel
=_T("0");
1293 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1297 path
.m_StatAdd
=StatAdd
;
1298 path
.m_StatDel
=StatDel
;
1299 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1305 pos
=log
.findNextString(pos
);
1310 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1312 m_paths
.push_back(newPath
);
1313 m_commonBaseDirectory
.Reset();
1315 int CTGitPathList::GetCount() const
1317 return (int)m_paths
.size();
1319 bool CTGitPathList::IsEmpty() const
1321 return m_paths
.empty();
1323 void CTGitPathList::Clear()
1326 m_commonBaseDirectory
.Reset();
1329 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1331 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1332 return m_paths
[index
];
1335 bool CTGitPathList::AreAllPathsFiles() const
1337 // Look through the vector for any directories - if we find them, return false
1338 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1342 #if defined(_MFC_VER)
1344 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1350 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1352 // for every selected file/folder
1354 while (file
.ReadString(strLine
))
1356 path
.SetFromUnknown(strLine
);
1361 catch (CFileException
* pE
)
1363 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException loading target file list\n");
1364 TCHAR error
[10000] = {0};
1365 pE
->GetErrorMessage(error
, 10000);
1366 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1373 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1379 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1380 PathVector::const_iterator it
;
1381 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1383 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1384 file
.Write(line
, line
.GetLength());
1390 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1391 PathVector::const_iterator it
;
1392 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1394 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1399 catch (CFileException
* pE
)
1401 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException in writing temp file\n");
1409 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1415 temp
= sPathString
.Tokenize(_T("*"),pos
);
1420 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1424 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1427 PathVector::const_iterator it
;
1428 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1430 if (!sRet
.IsEmpty())
1432 sRet
+= it
->GetWinPathString();
1439 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1441 // Check if all the paths are files and in the same directory
1442 PathVector::const_iterator it
;
1443 m_commonBaseDirectory
.Reset();
1444 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1446 if(it
->IsDirectory())
1450 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1451 if(m_commonBaseDirectory
.IsEmpty())
1453 m_commonBaseDirectory
= baseDirectory
;
1455 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1458 m_commonBaseDirectory
.Reset();
1465 CTGitPath
CTGitPathList::GetCommonDirectory() const
1467 if (m_commonBaseDirectory
.IsEmpty())
1469 PathVector::const_iterator it
;
1470 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1472 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1473 if(m_commonBaseDirectory
.IsEmpty())
1475 m_commonBaseDirectory
= baseDirectory
;
1477 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1480 m_commonBaseDirectory
.Reset();
1485 // since we only checked strings, not paths,
1486 // we have to make sure now that we really return a *path* here
1487 PathVector::const_iterator iter
;
1488 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1490 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1492 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1496 return m_commonBaseDirectory
;
1499 CTGitPath
CTGitPathList::GetCommonRoot() const
1504 if (GetCount() == 1)
1507 // first entry is common root for itself
1508 // (add trailing '\\' to detect partial matches of the last path element)
1509 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1510 int rootLength
= root
.GetLength();
1512 // determine common path string prefix
1513 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1515 CString path
= it
->GetWinPathString() + _T('\\');
1517 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1518 if (newLength
!= rootLength
)
1520 root
.Delete(newLength
, rootLength
);
1521 rootLength
= newLength
;
1525 // remove the last (partial) path element
1527 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1530 return CTGitPath(root
);
1533 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1535 std::sort(m_paths
.begin(), m_paths
.end());
1537 std::reverse(m_paths
.begin(), m_paths
.end());
1540 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1542 PathVector::const_iterator it
;
1543 SortByPathname(true); // nested ones first
1546 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1548 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1550 if (!it
->IsDirectory())
1551 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1553 sPaths
+= it
->GetWinPath();
1559 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1563 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1565 SHFILEOPSTRUCT shop
= {0};
1566 shop
.wFunc
= FO_DELETE
;
1568 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1570 shop
.fFlags
|= FOF_ALLOWUNDO
;
1571 const bool bRet
= (SHFileOperation(&shop
) == 0);
1575 void CTGitPathList::RemoveDuplicates()
1578 // Remove the duplicates
1579 // (Unique moves them to the end of the vector, then erase chops them off)
1580 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1583 void CTGitPathList::RemoveAdminPaths()
1585 PathVector::iterator it
;
1586 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1588 if (it
->IsAdminDir())
1591 it
= m_paths
.begin();
1598 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1600 PathVector::iterator it
;
1601 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1603 if (it
->IsEquivalentTo(path
))
1611 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1613 PathVector::iterator it
;
1614 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1616 if (it
->GetGitPathString()==path
.GetGitPathString())
1623 void CTGitPathList::RemoveChildren()
1626 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1629 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1631 if (list
.GetCount() != GetCount())
1633 for (int i
=0; i
<list
.GetCount(); ++i
)
1635 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1643 // Some test cases for these classes
1644 static class CTGitPathTests
1654 RemoveDuplicatesTest();
1655 RemoveChildrenTest();
1656 ContainingDirectoryTest();
1658 SubversionPathTest();
1659 GetCommonRootTest();
1660 #if defined(_MFC_VER)
1661 ValidPathAndUrlTest();
1667 void GetDirectoryTest()
1669 // Bit tricky, this test, because we need to know something about the file
1670 // layout on the machine which is running the test
1671 TCHAR winDir
[MAX_PATH
+1] = {0};
1672 GetWindowsDirectory(winDir
, MAX_PATH
);
1673 CString
sWinDir(winDir
);
1676 // This is a file which we know will always be there
1677 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1678 ATLASSERT(!testPath
.IsDirectory());
1679 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1680 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1682 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1683 // but we know it must be different to the directory itself
1684 testPath
.SetFromUnknown(sWinDir
);
1685 ATLASSERT(testPath
.IsDirectory());
1686 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1687 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1688 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1691 testPath
.SetFromUnknown(_T("C:\\"));
1692 ATLASSERT(testPath
.IsDirectory());
1693 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1694 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1695 // Try a root UNC path
1696 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1697 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1703 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1704 ATLASSERT(!testPath
.IsAdminDir());
1705 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1706 ATLASSERT(!testPath
.IsAdminDir());
1707 testPath
.SetFromUnknown(_T("c:\\.svn"));
1708 ATLASSERT(testPath
.IsAdminDir());
1709 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1710 ATLASSERT(!testPath
.IsAdminDir());
1711 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1712 ATLASSERT(testPath
.IsAdminDir());
1714 CTGitPathList pathList
;
1715 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1716 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1717 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1718 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1719 pathList
.RemoveAdminPaths();
1720 ATLASSERT(pathList
.GetCount()==2);
1722 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1723 pathList
.RemoveAdminPaths();
1724 ATLASSERT(pathList
.GetCount()==1);
1729 CTGitPathList testList
;
1731 testPath
.SetFromUnknown(_T("c:/Z"));
1732 testList
.AddPath(testPath
);
1733 testPath
.SetFromUnknown(_T("c:/B"));
1734 testList
.AddPath(testPath
);
1735 testPath
.SetFromUnknown(_T("c:\\a"));
1736 testList
.AddPath(testPath
);
1737 testPath
.SetFromUnknown(_T("c:/Test"));
1738 testList
.AddPath(testPath
);
1740 testList
.SortByPathname();
1742 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1743 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1744 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1745 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1748 void RawAppendTest()
1750 CTGitPath
testPath(_T("c:/test/"));
1751 testPath
.AppendRawString(_T("/Hello"));
1752 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1754 testPath
.AppendRawString(_T("\\T2"));
1755 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1757 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1758 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1759 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1760 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1763 void PathAppendTest()
1765 CTGitPath
testPath(_T("c:/test/"));
1766 testPath
.AppendPathString(_T("/Hello"));
1767 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1769 testPath
.AppendPathString(_T("T2"));
1770 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1772 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1773 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1774 // You wouldn't want to do this in real life - you'd use append-raw
1775 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1776 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1779 void RemoveDuplicatesTest()
1782 list
.AddPath(CTGitPath(_T("Z")));
1783 list
.AddPath(CTGitPath(_T("A")));
1784 list
.AddPath(CTGitPath(_T("E")));
1785 list
.AddPath(CTGitPath(_T("E")));
1787 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1788 ATLASSERT(list
[2]==list
[3]);
1790 ATLASSERT(list
.GetCount() == 4);
1792 list
.RemoveDuplicates();
1794 ATLASSERT(list
.GetCount() == 3);
1796 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1797 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1798 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1801 void RemoveChildrenTest()
1804 list
.AddPath(CTGitPath(_T("c:\\test")));
1805 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1806 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1807 list
.AddPath(CTGitPath(_T("c:\\parent")));
1808 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1809 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1810 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1812 ATLASSERT(list
.GetCount() == 7);
1814 list
.RemoveChildren();
1816 ATLTRACE("count = %d\n", list
.GetCount());
1817 ATLASSERT(list
.GetCount() == 3);
1819 list
.SortByPathname();
1821 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1822 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1823 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1826 #if defined(_MFC_VER)
1827 void ListLoadingTest()
1829 TCHAR buf
[MAX_PATH
] = {0};
1830 GetCurrentDirectory(MAX_PATH
, buf
);
1831 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1832 CTGitPathList testList
;
1833 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1835 ATLASSERT(testList
.GetCount() == 3);
1836 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1837 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1838 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1840 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1842 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1843 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1844 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1848 void ContainingDirectoryTest()
1852 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1854 dir
= testPath
.GetContainingDirectory();
1855 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1856 dir
= dir
.GetContainingDirectory();
1857 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1858 dir
= dir
.GetContainingDirectory();
1859 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1860 dir
= dir
.GetContainingDirectory();
1861 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1862 dir
= dir
.GetContainingDirectory();
1863 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1864 dir
= dir
.GetContainingDirectory();
1865 ATLASSERT(dir
.IsEmpty());
1866 ATLASSERT(dir
.GetWinPathString() == _T(""));
1872 testPath
.SetFromWin(_T("c:\\windows"));
1873 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1874 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1875 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1876 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1877 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1880 void SubversionPathTest()
1883 testPath
.SetFromWin(_T("c:\\"));
1884 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1885 testPath
.SetFromWin(_T("c:\\folder"));
1886 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1887 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1888 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1889 testPath
.SetFromUnknown(_T("http://testing/"));
1890 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1891 testPath
.SetFromGit(NULL
);
1892 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1893 #if defined(_MFC_VER)
1894 testPath
.SetFromUnknown(_T("http://testing again"));
1895 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1896 testPath
.SetFromUnknown(_T("http://testing%20again"));
1897 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1898 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1899 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1903 void GetCommonRootTest()
1905 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1906 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1907 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1910 list
.AddPath(pathA
);
1911 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1912 list
.AddPath(pathB
);
1913 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1914 list
.AddPath(pathC
);
1915 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1918 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");
1919 list
.LoadFromAsteriskSeparatedString(sPathList
);
1920 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1923 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1924 list
.LoadFromAsteriskSeparatedString(sPathList
);
1925 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1928 sPathList
= _T("c:\\windows\\*c:\\windows");
1929 list
.LoadFromAsteriskSeparatedString(sPathList
);
1930 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1933 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1934 list
.LoadFromAsteriskSeparatedString(sPathList
);
1935 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1938 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1939 list
.LoadFromAsteriskSeparatedString(sPathList
);
1940 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1944 void ValidPathAndUrlTest()
1947 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1948 ATLASSERT(testPath
.IsValidOnWindows());
1949 testPath
.SetFromWin(_T("c:\\"));
1950 ATLASSERT(testPath
.IsValidOnWindows());
1951 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1952 ATLASSERT(testPath
.IsValidOnWindows());
1953 testPath
.SetFromWin(_T("c"));
1954 ATLASSERT(testPath
.IsValidOnWindows());
1955 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1956 ATLASSERT(testPath
.IsValidOnWindows());
1957 testPath
.SetFromWin(_T("c:\\folder\\"));
1958 ATLASSERT(testPath
.IsValidOnWindows());
1959 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1960 ATLASSERT(testPath
.IsValidOnWindows());
1961 testPath
.SetFromWin(_T("c:\\.svn"));
1962 ATLASSERT(testPath
.IsValidOnWindows());
1963 testPath
.SetFromWin(_T("c:\\com\\file"));
1964 ATLASSERT(testPath
.IsValidOnWindows());
1965 testPath
.SetFromWin(_T("c:\\test\\conf"));
1966 ATLASSERT(testPath
.IsValidOnWindows());
1967 testPath
.SetFromWin(_T("c:\\LPT"));
1968 ATLASSERT(testPath
.IsValidOnWindows());
1969 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1970 ATLASSERT(testPath
.IsValidOnWindows());
1971 testPath
.SetFromWin(_T("c:\\com1test"));
1972 ATLASSERT(testPath
.IsValidOnWindows());
1973 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1974 ATLASSERT(testPath
.IsValidOnWindows());
1976 testPath
.SetFromWin(_T("\\\\Share\\filename"));
1977 ATLASSERT(testPath
.IsValidOnWindows());
1978 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
1979 ATLASSERT(testPath
.IsValidOnWindows());
1980 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
1981 ATLASSERT(testPath
.IsValidOnWindows());
1983 // now the negative tests
1984 testPath
.SetFromWin(_T("c:\\test:folder"));
1985 ATLASSERT(!testPath
.IsValidOnWindows());
1986 testPath
.SetFromWin(_T("c:\\file<name"));
1987 ATLASSERT(!testPath
.IsValidOnWindows());
1988 testPath
.SetFromWin(_T("c:\\something*else"));
1989 ATLASSERT(!testPath
.IsValidOnWindows());
1990 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
1991 ATLASSERT(!testPath
.IsValidOnWindows());
1992 testPath
.SetFromWin(_T("c:\\ext.>ension"));
1993 ATLASSERT(!testPath
.IsValidOnWindows());
1994 testPath
.SetFromWin(_T("c:\\com1\\filename"));
1995 ATLASSERT(!testPath
.IsValidOnWindows());
1996 testPath
.SetFromWin(_T("c:\\com1"));
1997 ATLASSERT(!testPath
.IsValidOnWindows());
1998 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
1999 ATLASSERT(!testPath
.IsValidOnWindows());
2001 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
2002 ATLASSERT(!testPath
.IsValidOnWindows());
2003 testPath
.SetFromWin(_T("\\\\Share\\prn"));
2004 ATLASSERT(!testPath
.IsValidOnWindows());
2005 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
2006 ATLASSERT(!testPath
.IsValidOnWindows());
2008 // now come some URL tests
2009 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2010 ATLASSERT(testPath
.IsValidOnWindows());
2011 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2012 ATLASSERT(testPath
.IsValidOnWindows());
2013 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2014 ATLASSERT(testPath
.IsValidOnWindows());
2015 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2016 ATLASSERT(testPath
.IsValidOnWindows());
2017 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2018 ATLASSERT(testPath
.IsValidOnWindows());
2019 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2020 ATLASSERT(testPath
.IsValidOnWindows());
2021 // and some negative URL tests
2022 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2023 ATLASSERT(!testPath
.IsValidOnWindows());
2024 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2025 ATLASSERT(!testPath
.IsValidOnWindows());
2026 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2027 ATLASSERT(!testPath
.IsValidOnWindows());
2028 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2029 ATLASSERT(!testPath
.IsValidOnWindows());
2030 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2031 ATLASSERT(!testPath
.IsValidOnWindows());
2035 } TGitPathTestobject
;
2039 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2042 for (i
= 0; i
< this->GetCount(); ++i
)
2044 if((*this)[i
].GetGitPathString() == path
)
2045 return (CTGitPath
*)&(*this)[i
];
2049 CString
CTGitPath::GetActionName(int action
)
2051 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2052 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2053 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2054 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2055 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2056 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2057 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2058 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2060 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2061 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2062 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2063 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2064 if(action
& CTGitPath::LOGACTIONS_COPY
)
2065 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2067 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2068 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2070 if (action
& CTGitPath::LOGACTIONS_ASSUMEVALID
)
2071 return MAKEINTRESOURCE(IDS_PATHACTIONS_ASSUMEUNCHANGED
);
2072 if (action
& CTGitPath::LOGACTIONS_SKIPWORKTREE
)
2073 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIPWORKTREE
);
2075 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2077 CString
CTGitPath::GetActionName()
2079 return GetActionName(m_Action
);
2082 int CTGitPathList::GetAction()