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"
28 #include "../TortoiseShell/Globals.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
= 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 (!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 (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 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(!GitAdminDir::HasAdminDir(GetWinPathString(),&topdir
))
846 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
848 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
850 bool CTGitPath::HasGitSVNDir() const
853 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
859 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
861 return !!PathFileExists(dotGitPath
+ _T("svn"));
863 bool CTGitPath::IsBisectActive() const
866 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
872 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
874 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
876 bool CTGitPath::IsMergeActive() const
879 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
885 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
887 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
889 bool CTGitPath::HasRebaseApply() const
892 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
898 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
= 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
= 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
< (int)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
.CombinePath((*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*/)
1151 while (pos
>= 0 && pos
< (int)log
.size())
1160 if(log
[pos
+1] ==':')
1164 int end
=log
.find(0,pos
);
1166 int file1
=-1,file2
=-1;
1169 actionstart
=log
.find(' ',end
-6);
1176 file1
= log
.find(0,actionstart
);
1182 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1185 file1
= log
.find(0,file1
);
1199 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1201 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1203 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1207 GitPath
->ParserAction( log
[actionstart
] );
1211 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1212 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1214 m_Action
|=GitPath
->m_Action
;
1219 int ac
=path
.ParserAction(log
[actionstart
] );
1220 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1222 path
.SetFromGit(pathname1
,&pathname2
);
1224 //action must be set after setfromgit. SetFromGit will clear all status.
1241 tabstart
=log
.find('\t',pos
);
1245 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1249 tabstart
=log
.find('\t',pos
);
1254 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1258 if(log
[pos
] == 0) //rename
1261 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1262 int sec
=log
.find(0,pos
);
1266 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1273 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1275 path
.SetFromGit(file1
,&file2
);
1277 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1280 GitPath
->m_StatAdd
=StatAdd
;
1281 GitPath
->m_StatDel
=StatDel
;
1285 //path.SetFromGit(pathname);
1288 path
.m_StatAdd
=_T("0");
1289 path
.m_StatDel
=_T("0");
1290 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1294 path
.m_StatAdd
=StatAdd
;
1295 path
.m_StatDel
=StatDel
;
1296 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1302 pos
=log
.findNextString(pos
);
1307 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1309 m_paths
.push_back(newPath
);
1310 m_commonBaseDirectory
.Reset();
1312 int CTGitPathList::GetCount() const
1314 return (int)m_paths
.size();
1316 bool CTGitPathList::IsEmpty() const
1318 return m_paths
.empty();
1320 void CTGitPathList::Clear()
1323 m_commonBaseDirectory
.Reset();
1326 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1328 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1329 return m_paths
[index
];
1332 bool CTGitPathList::AreAllPathsFiles() const
1334 // Look through the vector for any directories - if we find them, return false
1335 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1339 #if defined(_MFC_VER)
1341 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1347 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1349 // for every selected file/folder
1351 while (file
.ReadString(strLine
))
1353 path
.SetFromUnknown(strLine
);
1358 catch (CFileException
* pE
)
1360 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException loading target file list\n");
1361 TCHAR error
[10000] = {0};
1362 pE
->GetErrorMessage(error
, 10000);
1363 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1370 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1376 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1377 PathVector::const_iterator it
;
1378 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1380 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1381 file
.Write(line
, line
.GetLength());
1387 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1388 PathVector::const_iterator it
;
1389 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1391 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1396 catch (CFileException
* pE
)
1398 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException in writing temp file\n");
1406 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1412 temp
= sPathString
.Tokenize(_T("*"),pos
);
1417 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1421 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1424 PathVector::const_iterator it
;
1425 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1427 if (!sRet
.IsEmpty())
1429 sRet
+= it
->GetWinPathString();
1436 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1438 // Check if all the paths are files and in the same directory
1439 PathVector::const_iterator it
;
1440 m_commonBaseDirectory
.Reset();
1441 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1443 if(it
->IsDirectory())
1447 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1448 if(m_commonBaseDirectory
.IsEmpty())
1450 m_commonBaseDirectory
= baseDirectory
;
1452 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1455 m_commonBaseDirectory
.Reset();
1462 CTGitPath
CTGitPathList::GetCommonDirectory() const
1464 if (m_commonBaseDirectory
.IsEmpty())
1466 PathVector::const_iterator it
;
1467 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1469 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1470 if(m_commonBaseDirectory
.IsEmpty())
1472 m_commonBaseDirectory
= baseDirectory
;
1474 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1477 m_commonBaseDirectory
.Reset();
1482 // since we only checked strings, not paths,
1483 // we have to make sure now that we really return a *path* here
1484 PathVector::const_iterator iter
;
1485 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1487 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1489 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1493 return m_commonBaseDirectory
;
1496 CTGitPath
CTGitPathList::GetCommonRoot() const
1501 if (GetCount() == 1)
1504 // first entry is common root for itself
1505 // (add trailing '\\' to detect partial matches of the last path element)
1506 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1507 int rootLength
= root
.GetLength();
1509 // determine common path string prefix
1510 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1512 CString path
= it
->GetWinPathString() + _T('\\');
1514 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1515 if (newLength
!= rootLength
)
1517 root
.Delete(newLength
, rootLength
);
1518 rootLength
= newLength
;
1522 // remove the last (partial) path element
1524 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1527 return CTGitPath(root
);
1530 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1532 std::sort(m_paths
.begin(), m_paths
.end());
1534 std::reverse(m_paths
.begin(), m_paths
.end());
1537 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1539 PathVector::const_iterator it
;
1540 SortByPathname(true); // nested ones first
1543 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1545 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1547 if (!it
->IsDirectory())
1548 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1550 sPaths
+= it
->GetWinPath();
1556 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1560 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1562 SHFILEOPSTRUCT shop
= {0};
1563 shop
.wFunc
= FO_DELETE
;
1565 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1567 shop
.fFlags
|= FOF_ALLOWUNDO
;
1568 const bool bRet
= (SHFileOperation(&shop
) == 0);
1572 void CTGitPathList::RemoveDuplicates()
1575 // Remove the duplicates
1576 // (Unique moves them to the end of the vector, then erase chops them off)
1577 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1580 void CTGitPathList::RemoveAdminPaths()
1582 PathVector::iterator it
;
1583 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1585 if (it
->IsAdminDir())
1588 it
= m_paths
.begin();
1595 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1597 PathVector::iterator it
;
1598 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1600 if (it
->IsEquivalentTo(path
))
1608 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1610 PathVector::iterator it
;
1611 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1613 if (it
->GetGitPathString()==path
.GetGitPathString())
1620 void CTGitPathList::RemoveChildren()
1623 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1626 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1628 if (list
.GetCount() != GetCount())
1630 for (int i
=0; i
<list
.GetCount(); ++i
)
1632 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1640 // Some test cases for these classes
1641 static class CTGitPathTests
1651 RemoveDuplicatesTest();
1652 RemoveChildrenTest();
1653 ContainingDirectoryTest();
1655 SubversionPathTest();
1656 GetCommonRootTest();
1657 #if defined(_MFC_VER)
1658 ValidPathAndUrlTest();
1664 void GetDirectoryTest()
1666 // Bit tricky, this test, because we need to know something about the file
1667 // layout on the machine which is running the test
1668 TCHAR winDir
[MAX_PATH
+1] = {0};
1669 GetWindowsDirectory(winDir
, MAX_PATH
);
1670 CString
sWinDir(winDir
);
1673 // This is a file which we know will always be there
1674 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1675 ATLASSERT(!testPath
.IsDirectory());
1676 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1677 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1679 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1680 // but we know it must be different to the directory itself
1681 testPath
.SetFromUnknown(sWinDir
);
1682 ATLASSERT(testPath
.IsDirectory());
1683 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1684 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1685 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1688 testPath
.SetFromUnknown(_T("C:\\"));
1689 ATLASSERT(testPath
.IsDirectory());
1690 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1691 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1692 // Try a root UNC path
1693 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1694 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1700 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1701 ATLASSERT(!testPath
.IsAdminDir());
1702 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1703 ATLASSERT(!testPath
.IsAdminDir());
1704 testPath
.SetFromUnknown(_T("c:\\.svn"));
1705 ATLASSERT(testPath
.IsAdminDir());
1706 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1707 ATLASSERT(!testPath
.IsAdminDir());
1708 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1709 ATLASSERT(testPath
.IsAdminDir());
1711 CTGitPathList pathList
;
1712 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1713 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1714 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1715 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1716 pathList
.RemoveAdminPaths();
1717 ATLASSERT(pathList
.GetCount()==2);
1719 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1720 pathList
.RemoveAdminPaths();
1721 ATLASSERT(pathList
.GetCount()==1);
1726 CTGitPathList testList
;
1728 testPath
.SetFromUnknown(_T("c:/Z"));
1729 testList
.AddPath(testPath
);
1730 testPath
.SetFromUnknown(_T("c:/B"));
1731 testList
.AddPath(testPath
);
1732 testPath
.SetFromUnknown(_T("c:\\a"));
1733 testList
.AddPath(testPath
);
1734 testPath
.SetFromUnknown(_T("c:/Test"));
1735 testList
.AddPath(testPath
);
1737 testList
.SortByPathname();
1739 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1740 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1741 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1742 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1745 void RawAppendTest()
1747 CTGitPath
testPath(_T("c:/test/"));
1748 testPath
.AppendRawString(_T("/Hello"));
1749 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1751 testPath
.AppendRawString(_T("\\T2"));
1752 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1754 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1755 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1756 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1757 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1760 void PathAppendTest()
1762 CTGitPath
testPath(_T("c:/test/"));
1763 testPath
.AppendPathString(_T("/Hello"));
1764 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1766 testPath
.AppendPathString(_T("T2"));
1767 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1769 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1770 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1771 // You wouldn't want to do this in real life - you'd use append-raw
1772 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1773 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1776 void RemoveDuplicatesTest()
1779 list
.AddPath(CTGitPath(_T("Z")));
1780 list
.AddPath(CTGitPath(_T("A")));
1781 list
.AddPath(CTGitPath(_T("E")));
1782 list
.AddPath(CTGitPath(_T("E")));
1784 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1785 ATLASSERT(list
[2]==list
[3]);
1787 ATLASSERT(list
.GetCount() == 4);
1789 list
.RemoveDuplicates();
1791 ATLASSERT(list
.GetCount() == 3);
1793 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1794 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1795 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1798 void RemoveChildrenTest()
1801 list
.AddPath(CTGitPath(_T("c:\\test")));
1802 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1803 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1804 list
.AddPath(CTGitPath(_T("c:\\parent")));
1805 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1806 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1807 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1809 ATLASSERT(list
.GetCount() == 7);
1811 list
.RemoveChildren();
1813 ATLTRACE("count = %d\n", list
.GetCount());
1814 ATLASSERT(list
.GetCount() == 3);
1816 list
.SortByPathname();
1818 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1819 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1820 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1823 #if defined(_MFC_VER)
1824 void ListLoadingTest()
1826 TCHAR buf
[MAX_PATH
] = {0};
1827 GetCurrentDirectory(MAX_PATH
, buf
);
1828 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1829 CTGitPathList testList
;
1830 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1832 ATLASSERT(testList
.GetCount() == 3);
1833 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1834 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1835 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1837 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1839 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1840 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1841 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1845 void ContainingDirectoryTest()
1849 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1851 dir
= testPath
.GetContainingDirectory();
1852 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1853 dir
= dir
.GetContainingDirectory();
1854 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1855 dir
= dir
.GetContainingDirectory();
1856 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1857 dir
= dir
.GetContainingDirectory();
1858 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1859 dir
= dir
.GetContainingDirectory();
1860 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1861 dir
= dir
.GetContainingDirectory();
1862 ATLASSERT(dir
.IsEmpty());
1863 ATLASSERT(dir
.GetWinPathString() == _T(""));
1869 testPath
.SetFromWin(_T("c:\\windows"));
1870 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1871 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1872 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1873 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1874 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1877 void SubversionPathTest()
1880 testPath
.SetFromWin(_T("c:\\"));
1881 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1882 testPath
.SetFromWin(_T("c:\\folder"));
1883 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1884 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1885 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1886 testPath
.SetFromUnknown(_T("http://testing/"));
1887 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1888 testPath
.SetFromGit(NULL
);
1889 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1890 #if defined(_MFC_VER)
1891 testPath
.SetFromUnknown(_T("http://testing again"));
1892 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1893 testPath
.SetFromUnknown(_T("http://testing%20again"));
1894 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1895 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1896 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1900 void GetCommonRootTest()
1902 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1903 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1904 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1907 list
.AddPath(pathA
);
1908 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1909 list
.AddPath(pathB
);
1910 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1911 list
.AddPath(pathC
);
1912 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1915 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");
1916 list
.LoadFromAsteriskSeparatedString(sPathList
);
1917 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1920 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1921 list
.LoadFromAsteriskSeparatedString(sPathList
);
1922 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1925 sPathList
= _T("c:\\windows\\*c:\\windows");
1926 list
.LoadFromAsteriskSeparatedString(sPathList
);
1927 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1930 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1931 list
.LoadFromAsteriskSeparatedString(sPathList
);
1932 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1935 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1936 list
.LoadFromAsteriskSeparatedString(sPathList
);
1937 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1941 void ValidPathAndUrlTest()
1944 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1945 ATLASSERT(testPath
.IsValidOnWindows());
1946 testPath
.SetFromWin(_T("c:\\"));
1947 ATLASSERT(testPath
.IsValidOnWindows());
1948 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1949 ATLASSERT(testPath
.IsValidOnWindows());
1950 testPath
.SetFromWin(_T("c"));
1951 ATLASSERT(testPath
.IsValidOnWindows());
1952 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1953 ATLASSERT(testPath
.IsValidOnWindows());
1954 testPath
.SetFromWin(_T("c:\\folder\\"));
1955 ATLASSERT(testPath
.IsValidOnWindows());
1956 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1957 ATLASSERT(testPath
.IsValidOnWindows());
1958 testPath
.SetFromWin(_T("c:\\.svn"));
1959 ATLASSERT(testPath
.IsValidOnWindows());
1960 testPath
.SetFromWin(_T("c:\\com\\file"));
1961 ATLASSERT(testPath
.IsValidOnWindows());
1962 testPath
.SetFromWin(_T("c:\\test\\conf"));
1963 ATLASSERT(testPath
.IsValidOnWindows());
1964 testPath
.SetFromWin(_T("c:\\LPT"));
1965 ATLASSERT(testPath
.IsValidOnWindows());
1966 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1967 ATLASSERT(testPath
.IsValidOnWindows());
1968 testPath
.SetFromWin(_T("c:\\com1test"));
1969 ATLASSERT(testPath
.IsValidOnWindows());
1970 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1971 ATLASSERT(testPath
.IsValidOnWindows());
1973 testPath
.SetFromWin(_T("\\\\Share\\filename"));
1974 ATLASSERT(testPath
.IsValidOnWindows());
1975 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
1976 ATLASSERT(testPath
.IsValidOnWindows());
1977 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
1978 ATLASSERT(testPath
.IsValidOnWindows());
1980 // now the negative tests
1981 testPath
.SetFromWin(_T("c:\\test:folder"));
1982 ATLASSERT(!testPath
.IsValidOnWindows());
1983 testPath
.SetFromWin(_T("c:\\file<name"));
1984 ATLASSERT(!testPath
.IsValidOnWindows());
1985 testPath
.SetFromWin(_T("c:\\something*else"));
1986 ATLASSERT(!testPath
.IsValidOnWindows());
1987 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
1988 ATLASSERT(!testPath
.IsValidOnWindows());
1989 testPath
.SetFromWin(_T("c:\\ext.>ension"));
1990 ATLASSERT(!testPath
.IsValidOnWindows());
1991 testPath
.SetFromWin(_T("c:\\com1\\filename"));
1992 ATLASSERT(!testPath
.IsValidOnWindows());
1993 testPath
.SetFromWin(_T("c:\\com1"));
1994 ATLASSERT(!testPath
.IsValidOnWindows());
1995 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
1996 ATLASSERT(!testPath
.IsValidOnWindows());
1998 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1999 ATLASSERT(!testPath
.IsValidOnWindows());
2000 testPath
.SetFromWin(_T("\\\\Share\\prn"));
2001 ATLASSERT(!testPath
.IsValidOnWindows());
2002 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
2003 ATLASSERT(!testPath
.IsValidOnWindows());
2005 // now come some URL tests
2006 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2007 ATLASSERT(testPath
.IsValidOnWindows());
2008 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2009 ATLASSERT(testPath
.IsValidOnWindows());
2010 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2011 ATLASSERT(testPath
.IsValidOnWindows());
2012 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2013 ATLASSERT(testPath
.IsValidOnWindows());
2014 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2015 ATLASSERT(testPath
.IsValidOnWindows());
2016 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2017 ATLASSERT(testPath
.IsValidOnWindows());
2018 // and some negative URL tests
2019 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2020 ATLASSERT(!testPath
.IsValidOnWindows());
2021 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2022 ATLASSERT(!testPath
.IsValidOnWindows());
2023 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2024 ATLASSERT(!testPath
.IsValidOnWindows());
2025 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2026 ATLASSERT(!testPath
.IsValidOnWindows());
2027 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2028 ATLASSERT(!testPath
.IsValidOnWindows());
2032 } TGitPathTestobject
;
2036 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2039 for (i
= 0; i
< this->GetCount(); ++i
)
2041 if((*this)[i
].GetGitPathString() == path
)
2042 return (CTGitPath
*)&(*this)[i
];
2046 CString
CTGitPath::GetActionName(int action
)
2048 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2049 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2050 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2051 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2052 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2053 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2054 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2055 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2057 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2058 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2059 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2060 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2061 if(action
& CTGitPath::LOGACTIONS_COPY
)
2062 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2064 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2065 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2067 if (action
& CTGitPath::LOGACTIONS_ASSUMEVALID
)
2068 return MAKEINTRESOURCE(IDS_PATHACTIONS_ASSUMEUNCHANGED
);
2069 if (action
& CTGitPath::LOGACTIONS_SKIPWORKTREE
)
2070 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIPWORKTREE
);
2072 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2074 CString
CTGitPath::GetActionName()
2076 return GetActionName(m_Action
);
2079 int CTGitPathList::GetAction()