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 m_lastWriteTime
= *(__int64
*)&attribs
.ftLastWriteTime
;
381 m_fileSize
= ((INT64
)( (DWORD
)(attribs
.nFileSizeLow
) ) | ( (INT64
)( (DWORD
)(attribs
.nFileSizeHigh
) )<<32 ));
383 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
388 m_bIsDirectory
= false;
391 DWORD err
= GetLastError();
392 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
402 m_bDirectoryKnown
= true;
403 m_bLastWriteTimeKnown
= true;
404 m_bExistsKnown
= true;
407 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
411 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
413 CString str
=GetWinPathString();
414 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
419 void CTGitPath::EnsureBackslashPathSet() const
421 if(m_sBackslashPath
.IsEmpty())
423 SetBackslashPath(m_sFwdslashPath
);
424 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
427 void CTGitPath::EnsureFwdslashPathSet() const
429 if(m_sFwdslashPath
.IsEmpty())
431 SetFwdslashPath(m_sBackslashPath
);
432 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
437 // Reset all the caches
438 void CTGitPath::Reset()
440 m_bDirectoryKnown
= false;
442 m_bLastWriteTimeKnown
= false;
443 m_bHasAdminDirKnown
= false;
444 m_bIsValidOnWindowsKnown
= false;
445 m_bIsAdminDirKnown
= false;
446 m_bExistsKnown
= false;
447 m_bIsSpecialDirectoryKnown
= false;
448 m_bIsSpecialDirectory
= false;
450 m_sBackslashPath
.Empty();
451 m_sFwdslashPath
.Empty();
452 m_sUTF8FwdslashPath
.Empty();
454 this->m_StatAdd
=_T("");
455 this->m_StatDel
=_T("");
457 ATLASSERT(IsEmpty());
460 CTGitPath
CTGitPath::GetDirectory() const
462 if ((IsDirectory())||(!Exists()))
466 return GetContainingDirectory();
469 CTGitPath
CTGitPath::GetContainingDirectory() const
471 EnsureBackslashPathSet();
473 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
474 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
476 // This is a root directory, which needs a trailing slash
478 if(sDirName
== m_sBackslashPath
)
480 // We were clearly provided with a root path to start with - we should return nothing now
484 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
486 // We have an UNC path and we already are the root
490 retVal
.SetFromWin(sDirName
);
494 CString
CTGitPath::GetRootPathString() const
496 EnsureBackslashPathSet();
497 CString workingPath
= m_sBackslashPath
;
498 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
499 ATLVERIFY(::PathStripToRoot(pPath
));
500 workingPath
.ReleaseBuffer();
505 CString
CTGitPath::GetFilename() const
507 //ATLASSERT(!IsDirectory());
508 return GetFileOrDirectoryName();
511 CString
CTGitPath::GetFileOrDirectoryName() const
513 EnsureBackslashPathSet();
514 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
517 CString
CTGitPath::GetUIFileOrDirectoryName() const
520 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
523 CString
CTGitPath::GetFileExtension() const
527 EnsureBackslashPathSet();
528 int dotPos
= m_sBackslashPath
.ReverseFind('.');
529 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
530 if (dotPos
> slashPos
)
531 return m_sBackslashPath
.Mid(dotPos
);
535 CString
CTGitPath::GetBaseFilename() const
538 CString filename
=GetFilename();
539 dot
= filename
.ReverseFind(_T('.'));
541 return filename
.Left(dot
);
546 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
548 int length
= sP1
.GetLength();
549 if(length
!= sP2
.GetLength())
554 // We work from the end of the strings, because path differences
555 // are more likely to occur at the far end of a string
556 LPCTSTR pP1Start
= sP1
;
557 LPCTSTR pP1
= pP1Start
+(length
-1);
558 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
561 if(_totlower(*pP1
--) != _totlower(*pP2
--))
569 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
571 int length
= sP1
.GetLength();
572 if(length
!= sP2
.GetLength())
577 // We work from the end of the strings, because path differences
578 // are more likely to occur at the far end of a string
579 LPCTSTR pP1Start
= sP1
;
580 LPCTSTR pP1
= pP1Start
+(length
-1);
581 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
584 if((*pP1
--) != (*pP2
--))
592 bool CTGitPath::IsEmpty() const
594 // Check the backward slash path first, since the chance that this
595 // one is set is higher. In case of a 'false' return value it's a little
597 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
600 // Test if both paths refer to the same item
601 // Ignores case and slash direction
602 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
604 // Try and find a slash direction which avoids having to convert
606 if(!m_sBackslashPath
.IsEmpty())
608 // *We've* got a \ path - make sure that the RHS also has a \ path
609 rhs
.EnsureBackslashPathSet();
610 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
614 // Assume we've got a fwdslash path and make sure that the RHS has one
615 rhs
.EnsureFwdslashPathSet();
616 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
620 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
622 // Try and find a slash direction which avoids having to convert
624 if(!m_sBackslashPath
.IsEmpty())
626 // *We've* got a \ path - make sure that the RHS also has a \ path
627 rhs
.EnsureBackslashPathSet();
628 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
632 // Assume we've got a fwdslash path and make sure that the RHS has one
633 rhs
.EnsureFwdslashPathSet();
634 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
638 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
640 possibleDescendant
.EnsureBackslashPathSet();
641 EnsureBackslashPathSet();
643 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
644 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
646 return bPathStringsEqual
;
649 return (bPathStringsEqual
&&
650 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
651 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
654 // Get a string representing the file path, optionally with a base
655 // section stripped off the front.
656 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
658 EnsureFwdslashPathSet();
659 if(pOptionalBasePath
!= NULL
)
661 // Find the length of the base-path without having to do an 'ensure' on it
662 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
664 // Now, chop that baseLength of the front of the path
665 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
667 return m_sFwdslashPath
;
670 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
672 left
.EnsureBackslashPathSet();
673 right
.EnsureBackslashPathSet();
674 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
677 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
679 return CTGitPath::Compare(left
, right
) < 0;
682 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
684 return left
.IsEquivalentTo(right
);
687 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
689 if (left
.IsAdminDir() && right
.IsAdminDir())
695 l
= l
.GetContainingDirectory();
696 } while(l
.HasAdminDir());
699 r
= r
.GetContainingDirectory();
700 } while(r
.HasAdminDir());
701 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
703 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
706 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
708 return parent
.IsAncestorOf(child
);
711 void CTGitPath::AppendRawString(const CString
& sAppend
)
713 EnsureFwdslashPathSet();
714 CString strCopy
= m_sFwdslashPath
+= sAppend
;
715 SetFromUnknown(strCopy
);
718 void CTGitPath::AppendPathString(const CString
& sAppend
)
720 EnsureBackslashPathSet();
721 CString
cleanAppend(sAppend
);
722 cleanAppend
.Replace('/', '\\');
723 cleanAppend
.TrimLeft('\\');
724 m_sBackslashPath
.TrimRight('\\');
725 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
729 bool CTGitPath::IsWCRoot() const
731 if (m_bIsWCRootKnown
)
734 m_bIsWCRootKnown
= true;
737 CString topDirectory
;
738 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
743 if (IsEquivalentToWithoutCase(topDirectory
))
751 bool CTGitPath::HasAdminDir() const
753 if (m_bHasAdminDirKnown
)
754 return m_bHasAdminDir
;
756 EnsureBackslashPathSet();
757 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
758 m_bHasAdminDirKnown
= true;
759 return m_bHasAdminDir
;
762 bool CTGitPath::HasSubmodules() const
766 CString path
= m_sProjectRoot
;
767 path
+= _T("\\.gitmodules");
768 if( PathFileExists(path
) )
774 int CTGitPath::GetAdminDirMask() const
778 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
783 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
784 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
788 status
|= ITEMIS_FOLDERINGIT
;
791 status
|= ITEMIS_WCROOT
;
793 CString topProjectDir
;
794 if (g_GitAdminDir
.HasAdminDir(GetWinPathString(), false, &topProjectDir
))
796 if (PathFileExists(topProjectDir
+ _T("\\.gitmodules")))
798 CAutoConfig
config(true);
799 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(topProjectDir
+ _T("\\.gitmodules")), GIT_CONFIG_LEVEL_APP
, FALSE
);
800 CString relativePath
= GetWinPathString().Mid(topProjectDir
.GetLength());
801 relativePath
.Replace(_T("\\"), _T("/"));
802 relativePath
.Trim(_T("/"));
803 CStringA submodulePath
= CUnicodeUtils::GetUTF8(relativePath
);
804 if (git_config_foreach_match(config
, "submodule\\..*\\.path",
805 [](const git_config_entry
*entry
, void *data
) { return entry
->value
== *(CStringA
*)data
? GIT_EUSER
: 0; }, &submodulePath
) == GIT_EUSER
)
806 status
|= ITEMIS_SUBMODULE
;
813 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
815 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
816 status
|= ITEMIS_BISECT
;
818 if (PathFileExists(dotGitPath
+ _T("MERGE_HEAD")))
819 status
|= ITEMIS_MERGEACTIVE
;
821 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
822 status
|= ITEMIS_STASH
;
824 if (PathFileExists(dotGitPath
+ _T("svn")))
825 status
|= ITEMIS_GITSVN
;
827 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
828 status
|= ITEMIS_SUBMODULECONTAINER
;
833 bool CTGitPath::HasStashDir() const
836 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
842 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
844 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
846 bool CTGitPath::HasGitSVNDir() const
849 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
855 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
857 return !!PathFileExists(dotGitPath
+ _T("svn"));
859 bool CTGitPath::IsBisectActive() const
862 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
868 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
870 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
872 bool CTGitPath::IsMergeActive() const
875 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
881 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
883 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
885 bool CTGitPath::HasRebaseApply() const
888 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
894 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
896 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
899 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
901 if (m_bHasAdminDirKnown
)
904 *ProjectTopDir
= m_sProjectRoot
;
905 return m_bHasAdminDir
;
908 EnsureBackslashPathSet();
909 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
910 m_bHasAdminDirKnown
= true;
912 *ProjectTopDir
= m_sProjectRoot
;
913 return m_bHasAdminDir
;
916 bool CTGitPath::IsAdminDir() const
918 if (m_bIsAdminDirKnown
)
919 return m_bIsAdminDir
;
921 EnsureBackslashPathSet();
922 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
923 m_bIsAdminDirKnown
= true;
924 return m_bIsAdminDir
;
927 bool CTGitPath::IsValidOnWindows() const
929 if (m_bIsValidOnWindowsKnown
)
930 return m_bIsValidOnWindows
;
932 m_bIsValidOnWindows
= false;
933 EnsureBackslashPathSet();
934 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
935 std::wstring sPattern
;
936 // the 'file://' URL is just a normal windows path:
937 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
939 sMatch
= sMatch
.Mid(7);
940 sMatch
.TrimLeft(_T("\\"));
941 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
945 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
950 std::tr1::wregex
rx(sPattern
, std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
951 std::tr1::wsmatch match
;
953 std::wstring rmatch
= std::wstring((LPCTSTR
)sMatch
);
954 if (std::tr1::regex_match(rmatch
, match
, rx
))
956 if (std::wstring(match
[0]).compare(sMatch
)==0)
957 m_bIsValidOnWindows
= true;
959 if (m_bIsValidOnWindows
)
961 // now check for illegal filenames
962 std::tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
963 rmatch
= m_sBackslashPath
;
964 if (std::tr1::regex_search(rmatch
, rx2
, std::tr1::regex_constants::match_default
))
965 m_bIsValidOnWindows
= false;
968 catch (std::exception
) {}
970 m_bIsValidOnWindowsKnown
= true;
971 return m_bIsValidOnWindows
;
974 //////////////////////////////////////////////////////////////////////////
976 CTGitPathList::CTGitPathList()
981 // A constructor which allows a path list to be easily built which one initial entry in
982 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
987 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
995 while(pos
>=0 && pos
<out
.size())
1000 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1002 path
.m_Action
=path
.ParserAction(out
[pos
]);
1003 one
.Tokenize(_T("\t"),tabstart
);
1006 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1012 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1016 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1020 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1024 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1028 path
.m_Stage
=_ttol(part
);
1030 this->AddPath(path
);
1032 pos
=out
.findNextString(pos
);
1036 int CTGitPathList::FillUnRev(unsigned int action
, CTGitPathList
*list
, CString
*err
)
1045 count
=list
->GetCount();
1046 for (int i
= 0; i
< count
; ++i
)
1052 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1057 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1062 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1064 (*list
)[i
].GetWinPathString());
1067 BYTE_VECTOR out
, errb
;
1069 if (g_Git
.Run(cmd
, &out
, &errb
))
1072 g_Git
.StringAppend(err
, &errb
[0], CP_UTF8
, (int)errb
.size());
1078 while (pos
>= 0 && pos
< (int)out
.size())
1081 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1084 //SetFromGit will clear all status
1085 path
.SetFromGit(one
);
1086 path
.m_Action
=action
;
1089 pos
=out
.findNextString(pos
);
1095 int CTGitPathList::FillBasedOnIndexFlags(unsigned short flag
, CTGitPathList
* list
/*nullptr*/)
1101 CAutoRepository
repository(g_Git
.GetGitRepository());
1106 if (git_repository_index(index
.GetPointer(), repository
))
1110 if (list
== nullptr)
1113 count
= list
->GetCount();
1114 for (int j
= 0; j
< count
; ++j
)
1116 for (size_t i
= 0, ecount
= git_index_entrycount(index
); i
< ecount
; ++i
)
1118 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
1120 if (!e
|| !((e
->flags
| e
->flags_extended
) & flag
) || !e
->path
)
1124 g_Git
.StringAppend(&one
, (BYTE
*)e
->path
, CP_UTF8
);
1126 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)))
1129 //SetFromGit will clear all status
1130 path
.SetFromGit(one
);
1131 if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_SKIP_WORKTREE
)
1132 path
.m_Action
= CTGitPath::LOGACTIONS_SKIPWORKTREE
;
1133 else if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_VALID
)
1134 path
.m_Action
= CTGitPath::LOGACTIONS_ASSUMEVALID
;
1141 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1149 while (pos
>= 0 && pos
< (int)log
.size())
1151 //one=log.Tokenize(_T("\n"),pos);
1159 if(log
[pos
+1] ==':')
1163 int end
=log
.find(0,pos
);
1165 int file1
=-1,file2
=-1;
1168 actionstart
=log
.find(' ',end
-6);
1175 file1
= log
.find(0,actionstart
);
1181 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1184 file1
= log
.find(0,file1
);
1198 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1200 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1202 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1206 GitPath
->ParserAction( log
[actionstart
] );
1210 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1211 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1213 m_Action
|=GitPath
->m_Action
;
1218 int ac
=path
.ParserAction(log
[actionstart
] );
1219 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1221 path
.SetFromGit(pathname1
,&pathname2
);
1223 //action must be set after setfromgit. SetFromGit will clear all status.
1240 tabstart
=log
.find('\t',pos
);
1244 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1248 tabstart
=log
.find('\t',pos
);
1253 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1257 if(log
[pos
] == 0) //rename
1260 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1261 int sec
=log
.find(0,pos
);
1265 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1272 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1274 path
.SetFromGit(file1
,&file2
);
1276 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1279 GitPath
->m_StatAdd
=StatAdd
;
1280 GitPath
->m_StatDel
=StatDel
;
1284 //path.SetFromGit(pathname);
1287 path
.m_StatAdd
=_T("0");
1288 path
.m_StatDel
=_T("0");
1289 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1293 path
.m_StatAdd
=StatAdd
;
1294 path
.m_StatDel
=StatDel
;
1295 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1301 pos
=log
.findNextString(pos
);
1306 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1308 m_paths
.push_back(newPath
);
1309 m_commonBaseDirectory
.Reset();
1311 int CTGitPathList::GetCount() const
1313 return (int)m_paths
.size();
1315 bool CTGitPathList::IsEmpty() const
1317 return m_paths
.empty();
1319 void CTGitPathList::Clear()
1322 m_commonBaseDirectory
.Reset();
1325 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1327 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1328 return m_paths
[index
];
1331 bool CTGitPathList::AreAllPathsFiles() const
1333 // Look through the vector for any directories - if we find them, return false
1334 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1338 #if defined(_MFC_VER)
1340 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1346 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1348 // for every selected file/folder
1350 while (file
.ReadString(strLine
))
1352 path
.SetFromUnknown(strLine
);
1357 catch (CFileException
* pE
)
1359 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException loading target file list\n");
1360 TCHAR error
[10000] = {0};
1361 pE
->GetErrorMessage(error
, 10000);
1362 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1369 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1375 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1376 PathVector::const_iterator it
;
1377 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1379 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1380 file
.Write(line
, line
.GetLength());
1386 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1387 PathVector::const_iterator it
;
1388 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1390 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1395 catch (CFileException
* pE
)
1397 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException in writing temp file\n");
1405 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1411 temp
= sPathString
.Tokenize(_T("*"),pos
);
1416 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1420 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1423 PathVector::const_iterator it
;
1424 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1426 if (!sRet
.IsEmpty())
1428 sRet
+= it
->GetWinPathString();
1435 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1437 // Check if all the paths are files and in the same directory
1438 PathVector::const_iterator it
;
1439 m_commonBaseDirectory
.Reset();
1440 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1442 if(it
->IsDirectory())
1446 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1447 if(m_commonBaseDirectory
.IsEmpty())
1449 m_commonBaseDirectory
= baseDirectory
;
1451 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1454 m_commonBaseDirectory
.Reset();
1461 CTGitPath
CTGitPathList::GetCommonDirectory() const
1463 if (m_commonBaseDirectory
.IsEmpty())
1465 PathVector::const_iterator it
;
1466 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1468 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1469 if(m_commonBaseDirectory
.IsEmpty())
1471 m_commonBaseDirectory
= baseDirectory
;
1473 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1476 m_commonBaseDirectory
.Reset();
1481 // since we only checked strings, not paths,
1482 // we have to make sure now that we really return a *path* here
1483 PathVector::const_iterator iter
;
1484 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1486 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1488 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1492 return m_commonBaseDirectory
;
1495 CTGitPath
CTGitPathList::GetCommonRoot() const
1500 if (GetCount() == 1)
1503 // first entry is common root for itself
1504 // (add trailing '\\' to detect partial matches of the last path element)
1505 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1506 int rootLength
= root
.GetLength();
1508 // determine common path string prefix
1509 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1511 CString path
= it
->GetWinPathString() + _T('\\');
1513 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1514 if (newLength
!= rootLength
)
1516 root
.Delete(newLength
, rootLength
);
1517 rootLength
= newLength
;
1521 // remove the last (partial) path element
1523 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1526 return CTGitPath(root
);
1529 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1531 std::sort(m_paths
.begin(), m_paths
.end());
1533 std::reverse(m_paths
.begin(), m_paths
.end());
1536 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1538 PathVector::const_iterator it
;
1539 SortByPathname(true); // nested ones first
1542 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1544 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1546 if (!it
->IsDirectory())
1547 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1549 sPaths
+= it
->GetWinPath();
1555 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1559 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1561 SHFILEOPSTRUCT shop
= {0};
1562 shop
.wFunc
= FO_DELETE
;
1564 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1566 shop
.fFlags
|= FOF_ALLOWUNDO
;
1567 const bool bRet
= (SHFileOperation(&shop
) == 0);
1571 void CTGitPathList::RemoveDuplicates()
1574 // Remove the duplicates
1575 // (Unique moves them to the end of the vector, then erase chops them off)
1576 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1579 void CTGitPathList::RemoveAdminPaths()
1581 PathVector::iterator it
;
1582 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1584 if (it
->IsAdminDir())
1587 it
= m_paths
.begin();
1594 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1596 PathVector::iterator it
;
1597 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1599 if (it
->IsEquivalentTo(path
))
1607 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1609 PathVector::iterator it
;
1610 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1612 if (it
->GetGitPathString()==path
.GetGitPathString())
1619 void CTGitPathList::RemoveChildren()
1622 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1625 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1627 if (list
.GetCount() != GetCount())
1629 for (int i
=0; i
<list
.GetCount(); ++i
)
1631 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1639 // Some test cases for these classes
1640 static class CTGitPathTests
1650 RemoveDuplicatesTest();
1651 RemoveChildrenTest();
1652 ContainingDirectoryTest();
1654 SubversionPathTest();
1655 GetCommonRootTest();
1656 #if defined(_MFC_VER)
1657 ValidPathAndUrlTest();
1663 void GetDirectoryTest()
1665 // Bit tricky, this test, because we need to know something about the file
1666 // layout on the machine which is running the test
1667 TCHAR winDir
[MAX_PATH
+1] = {0};
1668 GetWindowsDirectory(winDir
, MAX_PATH
);
1669 CString
sWinDir(winDir
);
1672 // This is a file which we know will always be there
1673 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1674 ATLASSERT(!testPath
.IsDirectory());
1675 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1676 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1678 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1679 // but we know it must be different to the directory itself
1680 testPath
.SetFromUnknown(sWinDir
);
1681 ATLASSERT(testPath
.IsDirectory());
1682 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1683 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1684 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1687 testPath
.SetFromUnknown(_T("C:\\"));
1688 ATLASSERT(testPath
.IsDirectory());
1689 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1690 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1691 // Try a root UNC path
1692 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1693 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1699 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1700 ATLASSERT(!testPath
.IsAdminDir());
1701 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1702 ATLASSERT(!testPath
.IsAdminDir());
1703 testPath
.SetFromUnknown(_T("c:\\.svn"));
1704 ATLASSERT(testPath
.IsAdminDir());
1705 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1706 ATLASSERT(!testPath
.IsAdminDir());
1707 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1708 ATLASSERT(testPath
.IsAdminDir());
1710 CTGitPathList pathList
;
1711 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1712 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1713 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1714 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1715 pathList
.RemoveAdminPaths();
1716 ATLASSERT(pathList
.GetCount()==2);
1718 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1719 pathList
.RemoveAdminPaths();
1720 ATLASSERT(pathList
.GetCount()==1);
1725 CTGitPathList testList
;
1727 testPath
.SetFromUnknown(_T("c:/Z"));
1728 testList
.AddPath(testPath
);
1729 testPath
.SetFromUnknown(_T("c:/B"));
1730 testList
.AddPath(testPath
);
1731 testPath
.SetFromUnknown(_T("c:\\a"));
1732 testList
.AddPath(testPath
);
1733 testPath
.SetFromUnknown(_T("c:/Test"));
1734 testList
.AddPath(testPath
);
1736 testList
.SortByPathname();
1738 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1739 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1740 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1741 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1744 void RawAppendTest()
1746 CTGitPath
testPath(_T("c:/test/"));
1747 testPath
.AppendRawString(_T("/Hello"));
1748 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1750 testPath
.AppendRawString(_T("\\T2"));
1751 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1753 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1754 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1755 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1756 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1759 void PathAppendTest()
1761 CTGitPath
testPath(_T("c:/test/"));
1762 testPath
.AppendPathString(_T("/Hello"));
1763 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1765 testPath
.AppendPathString(_T("T2"));
1766 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1768 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1769 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1770 // You wouldn't want to do this in real life - you'd use append-raw
1771 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1772 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1775 void RemoveDuplicatesTest()
1778 list
.AddPath(CTGitPath(_T("Z")));
1779 list
.AddPath(CTGitPath(_T("A")));
1780 list
.AddPath(CTGitPath(_T("E")));
1781 list
.AddPath(CTGitPath(_T("E")));
1783 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1784 ATLASSERT(list
[2]==list
[3]);
1786 ATLASSERT(list
.GetCount() == 4);
1788 list
.RemoveDuplicates();
1790 ATLASSERT(list
.GetCount() == 3);
1792 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1793 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1794 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1797 void RemoveChildrenTest()
1800 list
.AddPath(CTGitPath(_T("c:\\test")));
1801 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1802 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1803 list
.AddPath(CTGitPath(_T("c:\\parent")));
1804 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1805 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1806 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1808 ATLASSERT(list
.GetCount() == 7);
1810 list
.RemoveChildren();
1812 ATLTRACE("count = %d\n", list
.GetCount());
1813 ATLASSERT(list
.GetCount() == 3);
1815 list
.SortByPathname();
1817 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1818 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1819 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1822 #if defined(_MFC_VER)
1823 void ListLoadingTest()
1825 TCHAR buf
[MAX_PATH
] = {0};
1826 GetCurrentDirectory(MAX_PATH
, buf
);
1827 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1828 CTGitPathList testList
;
1829 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1831 ATLASSERT(testList
.GetCount() == 3);
1832 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1833 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1834 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1836 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1838 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1839 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1840 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1844 void ContainingDirectoryTest()
1848 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1850 dir
= testPath
.GetContainingDirectory();
1851 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1852 dir
= dir
.GetContainingDirectory();
1853 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1854 dir
= dir
.GetContainingDirectory();
1855 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1856 dir
= dir
.GetContainingDirectory();
1857 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1858 dir
= dir
.GetContainingDirectory();
1859 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1860 dir
= dir
.GetContainingDirectory();
1861 ATLASSERT(dir
.IsEmpty());
1862 ATLASSERT(dir
.GetWinPathString() == _T(""));
1868 testPath
.SetFromWin(_T("c:\\windows"));
1869 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1870 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1871 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1872 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1873 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1876 void SubversionPathTest()
1879 testPath
.SetFromWin(_T("c:\\"));
1880 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1881 testPath
.SetFromWin(_T("c:\\folder"));
1882 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1883 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1884 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1885 testPath
.SetFromUnknown(_T("http://testing/"));
1886 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1887 testPath
.SetFromGit(NULL
);
1888 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1889 #if defined(_MFC_VER)
1890 testPath
.SetFromUnknown(_T("http://testing again"));
1891 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1892 testPath
.SetFromUnknown(_T("http://testing%20again"));
1893 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1894 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1895 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1899 void GetCommonRootTest()
1901 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1902 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1903 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1906 list
.AddPath(pathA
);
1907 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1908 list
.AddPath(pathB
);
1909 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1910 list
.AddPath(pathC
);
1911 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1914 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");
1915 list
.LoadFromAsteriskSeparatedString(sPathList
);
1916 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1919 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1920 list
.LoadFromAsteriskSeparatedString(sPathList
);
1921 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1924 sPathList
= _T("c:\\windows\\*c:\\windows");
1925 list
.LoadFromAsteriskSeparatedString(sPathList
);
1926 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1929 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1930 list
.LoadFromAsteriskSeparatedString(sPathList
);
1931 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1934 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1935 list
.LoadFromAsteriskSeparatedString(sPathList
);
1936 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1940 void ValidPathAndUrlTest()
1943 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1944 ATLASSERT(testPath
.IsValidOnWindows());
1945 testPath
.SetFromWin(_T("c:\\"));
1946 ATLASSERT(testPath
.IsValidOnWindows());
1947 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1948 ATLASSERT(testPath
.IsValidOnWindows());
1949 testPath
.SetFromWin(_T("c"));
1950 ATLASSERT(testPath
.IsValidOnWindows());
1951 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1952 ATLASSERT(testPath
.IsValidOnWindows());
1953 testPath
.SetFromWin(_T("c:\\folder\\"));
1954 ATLASSERT(testPath
.IsValidOnWindows());
1955 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1956 ATLASSERT(testPath
.IsValidOnWindows());
1957 testPath
.SetFromWin(_T("c:\\.svn"));
1958 ATLASSERT(testPath
.IsValidOnWindows());
1959 testPath
.SetFromWin(_T("c:\\com\\file"));
1960 ATLASSERT(testPath
.IsValidOnWindows());
1961 testPath
.SetFromWin(_T("c:\\test\\conf"));
1962 ATLASSERT(testPath
.IsValidOnWindows());
1963 testPath
.SetFromWin(_T("c:\\LPT"));
1964 ATLASSERT(testPath
.IsValidOnWindows());
1965 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1966 ATLASSERT(testPath
.IsValidOnWindows());
1967 testPath
.SetFromWin(_T("c:\\com1test"));
1968 ATLASSERT(testPath
.IsValidOnWindows());
1969 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1970 ATLASSERT(testPath
.IsValidOnWindows());
1972 testPath
.SetFromWin(_T("\\\\Share\\filename"));
1973 ATLASSERT(testPath
.IsValidOnWindows());
1974 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
1975 ATLASSERT(testPath
.IsValidOnWindows());
1976 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
1977 ATLASSERT(testPath
.IsValidOnWindows());
1979 // now the negative tests
1980 testPath
.SetFromWin(_T("c:\\test:folder"));
1981 ATLASSERT(!testPath
.IsValidOnWindows());
1982 testPath
.SetFromWin(_T("c:\\file<name"));
1983 ATLASSERT(!testPath
.IsValidOnWindows());
1984 testPath
.SetFromWin(_T("c:\\something*else"));
1985 ATLASSERT(!testPath
.IsValidOnWindows());
1986 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
1987 ATLASSERT(!testPath
.IsValidOnWindows());
1988 testPath
.SetFromWin(_T("c:\\ext.>ension"));
1989 ATLASSERT(!testPath
.IsValidOnWindows());
1990 testPath
.SetFromWin(_T("c:\\com1\\filename"));
1991 ATLASSERT(!testPath
.IsValidOnWindows());
1992 testPath
.SetFromWin(_T("c:\\com1"));
1993 ATLASSERT(!testPath
.IsValidOnWindows());
1994 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
1995 ATLASSERT(!testPath
.IsValidOnWindows());
1997 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1998 ATLASSERT(!testPath
.IsValidOnWindows());
1999 testPath
.SetFromWin(_T("\\\\Share\\prn"));
2000 ATLASSERT(!testPath
.IsValidOnWindows());
2001 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
2002 ATLASSERT(!testPath
.IsValidOnWindows());
2004 // now come some URL tests
2005 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2006 ATLASSERT(testPath
.IsValidOnWindows());
2007 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2008 ATLASSERT(testPath
.IsValidOnWindows());
2009 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2010 ATLASSERT(testPath
.IsValidOnWindows());
2011 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2012 ATLASSERT(testPath
.IsValidOnWindows());
2013 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2014 ATLASSERT(testPath
.IsValidOnWindows());
2015 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2016 ATLASSERT(testPath
.IsValidOnWindows());
2017 // and some negative URL tests
2018 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2019 ATLASSERT(!testPath
.IsValidOnWindows());
2020 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2021 ATLASSERT(!testPath
.IsValidOnWindows());
2022 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2023 ATLASSERT(!testPath
.IsValidOnWindows());
2024 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2025 ATLASSERT(!testPath
.IsValidOnWindows());
2026 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2027 ATLASSERT(!testPath
.IsValidOnWindows());
2031 } TGitPathTestobject
;
2035 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2038 for (i
= 0; i
< this->GetCount(); ++i
)
2040 if((*this)[i
].GetGitPathString() == path
)
2041 return (CTGitPath
*)&(*this)[i
];
2045 CString
CTGitPath::GetActionName(int action
)
2047 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2048 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2049 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2050 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2051 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2052 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2053 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2054 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2056 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2057 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2058 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2059 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2060 if(action
& CTGitPath::LOGACTIONS_COPY
)
2061 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2063 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2064 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2066 if (action
& CTGitPath::LOGACTIONS_ASSUMEVALID
)
2067 return MAKEINTRESOURCE(IDS_PATHACTIONS_ASSUMEUNCHANGED
);
2068 if (action
& CTGitPath::LOGACTIONS_SKIPWORKTREE
)
2069 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIPWORKTREE
);
2071 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2073 CString
CTGitPath::GetActionName()
2075 return GetActionName(m_Action
);
2078 int CTGitPathList::GetAction()