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"
34 //#include "MessageBox.h"
35 //#include "AppUtils.h"
43 CTGitPath::CTGitPath(void)
44 : m_bDirectoryKnown(false)
45 , m_bIsDirectory(false)
47 , m_bHasAdminDirKnown(false)
48 , m_bHasAdminDir(false)
49 , m_bIsValidOnWindowsKnown(false)
50 , m_bIsValidOnWindows(false)
51 , m_bIsReadOnly(false)
52 , m_bIsAdminDirKnown(false)
53 , m_bIsAdminDir(false)
55 , m_bExistsKnown(false)
56 , m_bLastWriteTimeKnown(0)
59 , m_bIsSpecialDirectoryKnown(false)
60 , m_bIsSpecialDirectory(false)
61 , m_bIsWCRootKnown(false)
71 CTGitPath::~CTGitPath(void)
74 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
75 CTGitPath::CTGitPath(const CString
& sUnknownPath
) :
76 m_bDirectoryKnown(false)
77 , m_bIsDirectory(false)
79 , m_bHasAdminDirKnown(false)
80 , m_bHasAdminDir(false)
81 , m_bIsValidOnWindowsKnown(false)
82 , m_bIsValidOnWindows(false)
83 , m_bIsReadOnly(false)
84 , m_bIsAdminDirKnown(false)
85 , m_bIsAdminDir(false)
87 , m_bExistsKnown(false)
88 , m_bLastWriteTimeKnown(0)
91 , m_bIsSpecialDirectoryKnown(false)
92 , m_bIsSpecialDirectory(false)
93 , m_bIsWCRootKnown(false)
98 SetFromUnknown(sUnknownPath
);
104 int CTGitPath::ParserAction(BYTE action
)
106 //action=action.TrimLeft();
107 //TCHAR c=action.GetAt(0);
109 m_Action
|= LOGACTIONS_MODIFIED
;
111 m_Action
|= LOGACTIONS_REPLACED
;
113 m_Action
|= LOGACTIONS_ADDED
;
115 m_Action
|= LOGACTIONS_DELETED
;
117 m_Action
|= LOGACTIONS_UNMERGED
;
119 m_Action
|= LOGACTIONS_DELETED
;
121 m_Action
|= LOGACTIONS_CACHE
;
123 m_Action
|= LOGACTIONS_COPY
;
125 m_Action
|= LOGACTIONS_MODIFIED
;
129 void CTGitPath::SetFromGit(const char* pPath
)
134 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, NULL
, 0);
137 len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, m_sFwdslashPath
.GetBuffer(len
+1), len
+1);
138 m_sFwdslashPath
.ReleaseBuffer(len
-1);
140 SanitizeRootPath(m_sFwdslashPath
, true);
143 void CTGitPath::SetFromGit(const char* pPath
, bool bIsDirectory
)
146 m_bDirectoryKnown
= true;
147 m_bIsDirectory
= bIsDirectory
;
150 void CTGitPath::SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
)
155 m_sFwdslashPath
= pPath
;
156 SanitizeRootPath(m_sFwdslashPath
, true);
158 m_bDirectoryKnown
= true;
159 m_bIsDirectory
= bIsDirectory
;
162 void CTGitPath::SetFromGit(const CString
& sPath
,CString
*oldpath
)
165 m_sFwdslashPath
= sPath
;
166 SanitizeRootPath(m_sFwdslashPath
, true);
168 m_sOldFwdslashPath
= *oldpath
;
171 void CTGitPath::SetFromWin(LPCTSTR pPath
)
174 m_sBackslashPath
= pPath
;
175 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
176 SanitizeRootPath(m_sBackslashPath
, false);
177 ATLASSERT(m_sBackslashPath
.Find('/')<0);
179 void CTGitPath::SetFromWin(const CString
& sPath
)
182 m_sBackslashPath
= sPath
;
183 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
184 SanitizeRootPath(m_sBackslashPath
, false);
186 void CTGitPath::SetFromWin(LPCTSTR pPath
, bool bIsDirectory
)
189 m_sBackslashPath
= pPath
;
190 m_bIsDirectory
= bIsDirectory
;
191 m_bDirectoryKnown
= true;
192 SanitizeRootPath(m_sBackslashPath
, false);
194 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
197 m_sBackslashPath
= sPath
;
198 m_bIsDirectory
= bIsDirectory
;
199 m_bDirectoryKnown
= true;
200 SanitizeRootPath(m_sBackslashPath
, false);
202 void CTGitPath::SetFromUnknown(const CString
& sPath
)
205 // Just set whichever path we think is most likely to be used
206 // GitAdminDir admin;
208 // if(admin.HasAdminDir(sPath,&p))
209 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
211 SetFwdslashPath(sPath
);
214 LPCTSTR
CTGitPath::GetWinPath() const
220 if(m_sBackslashPath
.IsEmpty())
222 SetBackslashPath(m_sFwdslashPath
);
224 return m_sBackslashPath
;
226 // This is a temporary function, to be used during the migration to
227 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
228 const CString
& CTGitPath::GetWinPathString() const
230 if(m_sBackslashPath
.IsEmpty())
232 SetBackslashPath(m_sFwdslashPath
);
234 return m_sBackslashPath
;
237 const CString
& CTGitPath::GetGitPathString() const
239 if(m_sFwdslashPath
.IsEmpty())
241 SetFwdslashPath(m_sBackslashPath
);
243 return m_sFwdslashPath
;
246 const CString
&CTGitPath::GetGitOldPathString() const
248 return m_sOldFwdslashPath
;
251 const char* CTGitPath::GetGitApiPath(apr_pool_t
*pool
) const
253 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
254 // each call to GetGitApiPath returns a different pointer value.
255 // If you made multiple calls to GetGitApiPath on the same string, only the last
256 // one would give you a valid pointer to an empty string, because each
257 // call would invalidate the previous call's return.
262 if(m_sFwdslashPath
.IsEmpty())
264 SetFwdslashPath(m_sBackslashPath
);
266 if(m_sUTF8FwdslashPath
.IsEmpty())
268 SetUTF8FwdslashPath(m_sFwdslashPath
);
270 if (svn_path_is_url(m_sUTF8FwdslashPath
))
272 m_sUTF8FwdslashPathEscaped
= CPathUtils::PathEscape(m_sUTF8FwdslashPath
);
273 m_sUTF8FwdslashPathEscaped
.Replace("file:////", "file:///\\");
274 m_sUTF8FwdslashPathEscaped
= svn_path_canonicalize(m_sUTF8FwdslashPathEscaped
, pool
);
275 return m_sUTF8FwdslashPathEscaped
;
277 m_sUTF8FwdslashPath
= svn_path_canonicalize(m_sUTF8FwdslashPath
, pool
);
279 return m_sUTF8FwdslashPath
;
283 const CString
& CTGitPath::GetUIPathString() const
285 if (m_sUIPath
.IsEmpty())
287 m_sUIPath
= GetWinPathString();
292 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
294 m_sFwdslashPath
= sPath
;
295 m_sFwdslashPath
.Replace('\\', '/');
297 // We don't leave a trailing /
298 m_sFwdslashPath
.TrimRight('/');
299 m_sFwdslashPath
.Replace(L
"//?/", L
"");
301 SanitizeRootPath(m_sFwdslashPath
, true);
303 m_sFwdslashPath
.Replace(_T("file:////"), _T("file://"));
305 m_sUTF8FwdslashPath
.Empty();
308 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
310 m_sBackslashPath
= sPath
;
311 m_sBackslashPath
.Replace('/', '\\');
312 m_sBackslashPath
.TrimRight('\\');
313 SanitizeRootPath(m_sBackslashPath
, false);
316 void CTGitPath::SetUTF8FwdslashPath(const CString
& sPath
) const
318 m_sUTF8FwdslashPath
= CUnicodeUtils::GetUTF8(sPath
);
321 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
323 // Make sure to add the trailing slash to root paths such as 'C:'
324 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
326 sPath
+= (bIsForwardPath
) ? _T("/") : _T("\\");
330 bool CTGitPath::IsDirectory() const
332 if(!m_bDirectoryKnown
)
336 return m_bIsDirectory
;
339 bool CTGitPath::Exists() const
348 bool CTGitPath::Delete(bool bTrash
) const
350 EnsureBackslashPathSet();
351 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
355 if ((bTrash
)||(IsDirectory()))
357 std::unique_ptr
<TCHAR
[]> buf(new TCHAR
[m_sBackslashPath
.GetLength() + 2]);
358 _tcscpy_s(buf
.get(), m_sBackslashPath
.GetLength() + 2, m_sBackslashPath
);
359 buf
[m_sBackslashPath
.GetLength()] = 0;
360 buf
[m_sBackslashPath
.GetLength()+1] = 0;
361 bRet
= CTGitPathList::DeleteViaShell(buf
.get(), bTrash
);
365 bRet
= !!::DeleteFile(m_sBackslashPath
);
369 m_bExistsKnown
= true;
373 __int64
CTGitPath::GetLastWriteTime() const
375 if(!m_bLastWriteTimeKnown
)
379 return m_lastWriteTime
;
382 __int64
CTGitPath::GetFileSize() const
384 if(!m_bDirectoryKnown
)
391 bool CTGitPath::IsReadOnly() const
393 if(!m_bLastWriteTimeKnown
)
397 return m_bIsReadOnly
;
400 void CTGitPath::UpdateAttributes() const
402 EnsureBackslashPathSet();
403 WIN32_FILE_ATTRIBUTE_DATA attribs
;
404 if (m_sBackslashPath
.GetLength() >= 248)
405 m_sLongBackslashPath
= _T("\\\\?\\") + m_sBackslashPath
;
406 if(GetFileAttributesEx(m_sBackslashPath
.GetLength() >= 248 ? m_sLongBackslashPath
: m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
408 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
409 m_lastWriteTime
= *(__int64
*)&attribs
.ftLastWriteTime
;
416 m_fileSize
= ((INT64
)( (DWORD
)(attribs
.nFileSizeLow
) ) | ( (INT64
)( (DWORD
)(attribs
.nFileSizeHigh
) )<<32 ));
418 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
423 m_bIsDirectory
= false;
426 DWORD err
= GetLastError();
427 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
437 m_bDirectoryKnown
= true;
438 m_bLastWriteTimeKnown
= true;
439 m_bExistsKnown
= true;
442 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
446 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
448 CString str
=GetWinPathString();
449 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
454 void CTGitPath::EnsureBackslashPathSet() const
456 if(m_sBackslashPath
.IsEmpty())
458 SetBackslashPath(m_sFwdslashPath
);
459 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
462 void CTGitPath::EnsureFwdslashPathSet() const
464 if(m_sFwdslashPath
.IsEmpty())
466 SetFwdslashPath(m_sBackslashPath
);
467 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
472 // Reset all the caches
473 void CTGitPath::Reset()
475 m_bDirectoryKnown
= false;
477 m_bLastWriteTimeKnown
= false;
478 m_bHasAdminDirKnown
= false;
479 m_bIsValidOnWindowsKnown
= false;
480 m_bIsAdminDirKnown
= false;
481 m_bExistsKnown
= false;
482 m_bIsSpecialDirectoryKnown
= false;
483 m_bIsSpecialDirectory
= false;
485 m_sBackslashPath
.Empty();
486 m_sFwdslashPath
.Empty();
487 m_sUTF8FwdslashPath
.Empty();
489 this->m_StatAdd
=_T("");
490 this->m_StatDel
=_T("");
492 ATLASSERT(IsEmpty());
495 CTGitPath
CTGitPath::GetDirectory() const
497 if ((IsDirectory())||(!Exists()))
501 return GetContainingDirectory();
504 CTGitPath
CTGitPath::GetContainingDirectory() const
506 EnsureBackslashPathSet();
508 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
509 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
511 // This is a root directory, which needs a trailing slash
513 if(sDirName
== m_sBackslashPath
)
515 // We were clearly provided with a root path to start with - we should return nothing now
519 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
521 // We have an UNC path and we already are the root
525 retVal
.SetFromWin(sDirName
);
529 CString
CTGitPath::GetRootPathString() const
531 EnsureBackslashPathSet();
532 CString workingPath
= m_sBackslashPath
;
533 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
534 ATLVERIFY(::PathStripToRoot(pPath
));
535 workingPath
.ReleaseBuffer();
540 CString
CTGitPath::GetFilename() const
542 //ATLASSERT(!IsDirectory());
543 return GetFileOrDirectoryName();
546 CString
CTGitPath::GetFileOrDirectoryName() const
548 EnsureBackslashPathSet();
549 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
552 CString
CTGitPath::GetUIFileOrDirectoryName() const
555 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
558 CString
CTGitPath::GetFileExtension() const
562 EnsureBackslashPathSet();
563 int dotPos
= m_sBackslashPath
.ReverseFind('.');
564 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
565 if (dotPos
> slashPos
)
566 return m_sBackslashPath
.Mid(dotPos
);
570 CString
CTGitPath::GetBaseFilename() const
573 CString filename
=GetFilename();
574 dot
= filename
.ReverseFind(_T('.'));
576 return filename
.Left(dot
);
581 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
583 int length
= sP1
.GetLength();
584 if(length
!= sP2
.GetLength())
589 // We work from the end of the strings, because path differences
590 // are more likely to occur at the far end of a string
591 LPCTSTR pP1Start
= sP1
;
592 LPCTSTR pP1
= pP1Start
+(length
-1);
593 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
596 if(_totlower(*pP1
--) != _totlower(*pP2
--))
604 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
606 int length
= sP1
.GetLength();
607 if(length
!= sP2
.GetLength())
612 // We work from the end of the strings, because path differences
613 // are more likely to occur at the far end of a string
614 LPCTSTR pP1Start
= sP1
;
615 LPCTSTR pP1
= pP1Start
+(length
-1);
616 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
619 if((*pP1
--) != (*pP2
--))
627 bool CTGitPath::IsEmpty() const
629 // Check the backward slash path first, since the chance that this
630 // one is set is higher. In case of a 'false' return value it's a little
632 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
635 // Test if both paths refer to the same item
636 // Ignores case and slash direction
637 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
639 // Try and find a slash direction which avoids having to convert
641 if(!m_sBackslashPath
.IsEmpty())
643 // *We've* got a \ path - make sure that the RHS also has a \ path
644 rhs
.EnsureBackslashPathSet();
645 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
649 // Assume we've got a fwdslash path and make sure that the RHS has one
650 rhs
.EnsureFwdslashPathSet();
651 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
655 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
657 // Try and find a slash direction which avoids having to convert
659 if(!m_sBackslashPath
.IsEmpty())
661 // *We've* got a \ path - make sure that the RHS also has a \ path
662 rhs
.EnsureBackslashPathSet();
663 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
667 // Assume we've got a fwdslash path and make sure that the RHS has one
668 rhs
.EnsureFwdslashPathSet();
669 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
673 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
675 possibleDescendant
.EnsureBackslashPathSet();
676 EnsureBackslashPathSet();
678 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
679 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
681 return bPathStringsEqual
;
684 return (bPathStringsEqual
&&
685 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
686 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
689 // Get a string representing the file path, optionally with a base
690 // section stripped off the front.
691 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
693 EnsureFwdslashPathSet();
694 if(pOptionalBasePath
!= NULL
)
696 // Find the length of the base-path without having to do an 'ensure' on it
697 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
699 // Now, chop that baseLength of the front of the path
700 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
702 return m_sFwdslashPath
;
705 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
707 left
.EnsureBackslashPathSet();
708 right
.EnsureBackslashPathSet();
709 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
712 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
714 return CTGitPath::Compare(left
, right
) < 0;
717 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
719 return left
.IsEquivalentTo(right
);
722 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
724 if (left
.IsAdminDir() && right
.IsAdminDir())
730 l
= l
.GetContainingDirectory();
731 } while(l
.HasAdminDir());
734 r
= r
.GetContainingDirectory();
735 } while(r
.HasAdminDir());
736 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
738 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
741 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
743 return parent
.IsAncestorOf(child
);
746 void CTGitPath::AppendRawString(const CString
& sAppend
)
748 EnsureFwdslashPathSet();
749 CString strCopy
= m_sFwdslashPath
+= sAppend
;
750 SetFromUnknown(strCopy
);
753 void CTGitPath::AppendPathString(const CString
& sAppend
)
755 EnsureBackslashPathSet();
756 CString
cleanAppend(sAppend
);
757 cleanAppend
.Replace('/', '\\');
758 cleanAppend
.TrimLeft('\\');
759 m_sBackslashPath
.TrimRight('\\');
760 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
764 bool CTGitPath::IsWCRoot() const
766 if (m_bIsWCRootKnown
)
769 m_bIsWCRootKnown
= true;
772 CString topDirectory
;
773 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
778 if (IsEquivalentToWithoutCase(topDirectory
))
786 bool CTGitPath::HasAdminDir() const
788 if (m_bHasAdminDirKnown
)
789 return m_bHasAdminDir
;
791 EnsureBackslashPathSet();
792 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
793 m_bHasAdminDirKnown
= true;
794 return m_bHasAdminDir
;
797 bool CTGitPath::HasSubmodules() const
801 CString path
= m_sProjectRoot
;
802 path
+= _T("\\.gitmodules");
803 if( PathFileExists(path
) )
809 int CTGitPath::GetAdminDirMask() const
813 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
818 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
819 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
823 status
|= ITEMIS_FOLDERINGIT
;
826 status
|= ITEMIS_WCROOT
;
828 CString topProjectDir
;
829 if (g_GitAdminDir
.HasAdminDir(GetWinPathString(), false, &topProjectDir
))
831 if (PathFileExists(topProjectDir
+ _T("\\.gitmodules")))
834 git_config_new(&config
);
835 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(topProjectDir
+ _T("\\.gitmodules")), GIT_CONFIG_LEVEL_APP
, FALSE
);
836 CString relativePath
= GetWinPathString().Mid(topProjectDir
.GetLength());
837 relativePath
.Replace(_T("\\"), _T("/"));
838 relativePath
.Trim(_T("/"));
840 CStringA
key(CUnicodeUtils::GetUTF8(_T("submodule.") + relativePath
+ _T(".path")));
841 if (!git_config_get_string(&out
, config
, key
))
842 status
|= ITEMIS_SUBMODULE
;
843 git_config_free(config
);
850 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
852 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
853 status
|= ITEMIS_BISECT
;
855 if (PathFileExists(dotGitPath
+ _T("MERGE_HEAD")))
856 status
|= ITEMIS_MERGEACTIVE
;
858 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
859 status
|= ITEMIS_STASH
;
861 if (PathFileExists(dotGitPath
+ _T("svn")))
862 status
|= ITEMIS_GITSVN
;
864 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
865 status
|= ITEMIS_SUBMODULECONTAINER
;
870 bool CTGitPath::HasStashDir() const
873 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
879 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
881 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
883 bool CTGitPath::HasGitSVNDir() const
886 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
892 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
894 return !!PathFileExists(dotGitPath
+ _T("svn"));
896 bool CTGitPath::IsBisectActive() const
899 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
905 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
907 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
909 bool CTGitPath::IsMergeActive() const
912 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
918 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
920 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
922 bool CTGitPath::HasRebaseApply() const
925 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
931 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
933 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
936 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
938 if (m_bHasAdminDirKnown
)
941 *ProjectTopDir
= m_sProjectRoot
;
942 return m_bHasAdminDir
;
945 EnsureBackslashPathSet();
946 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
947 m_bHasAdminDirKnown
= true;
949 *ProjectTopDir
= m_sProjectRoot
;
950 return m_bHasAdminDir
;
953 bool CTGitPath::IsAdminDir() const
955 if (m_bIsAdminDirKnown
)
956 return m_bIsAdminDir
;
958 EnsureBackslashPathSet();
959 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
960 m_bIsAdminDirKnown
= true;
961 return m_bIsAdminDir
;
964 bool CTGitPath::IsValidOnWindows() const
966 if (m_bIsValidOnWindowsKnown
)
967 return m_bIsValidOnWindows
;
969 m_bIsValidOnWindows
= false;
970 EnsureBackslashPathSet();
971 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
972 std::wstring sPattern
;
973 // the 'file://' URL is just a normal windows path:
974 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
976 sMatch
= sMatch
.Mid(7);
977 sMatch
.TrimLeft(_T("\\"));
978 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
982 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
987 std::tr1::wregex
rx(sPattern
, std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
988 std::tr1::wsmatch match
;
990 std::wstring rmatch
= std::wstring((LPCTSTR
)sMatch
);
991 if (std::tr1::regex_match(rmatch
, match
, rx
))
993 if (std::wstring(match
[0]).compare(sMatch
)==0)
994 m_bIsValidOnWindows
= true;
996 if (m_bIsValidOnWindows
)
998 // now check for illegal filenames
999 std::tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
1000 rmatch
= m_sBackslashPath
;
1001 if (std::tr1::regex_search(rmatch
, rx2
, std::tr1::regex_constants::match_default
))
1002 m_bIsValidOnWindows
= false;
1005 catch (std::exception
) {}
1007 m_bIsValidOnWindowsKnown
= true;
1008 return m_bIsValidOnWindows
;
1011 //////////////////////////////////////////////////////////////////////////
1013 CTGitPathList::CTGitPathList()
1018 // A constructor which allows a path list to be easily built which one initial entry in
1019 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
1022 AddPath(firstEntry
);
1024 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
1032 while(pos
>=0 && pos
<out
.size())
1037 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1039 path
.m_Action
=path
.ParserAction(out
[pos
]);
1040 one
.Tokenize(_T("\t"),tabstart
);
1043 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1049 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1053 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1057 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1061 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1065 path
.m_Stage
=_ttol(part
);
1067 this->AddPath(path
);
1069 pos
=out
.findNextString(pos
);
1073 int CTGitPathList::FillUnRev(unsigned int action
, CTGitPathList
*list
, CString
*err
)
1082 count
=list
->GetCount();
1083 for (int i
= 0; i
< count
; ++i
)
1089 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1094 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1099 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1101 (*list
)[i
].GetWinPathString());
1104 BYTE_VECTOR out
, errb
;
1106 if (g_Git
.Run(cmd
, &out
, &errb
))
1109 g_Git
.StringAppend(err
, &errb
[0], CP_UTF8
, (int)errb
.size());
1115 while (pos
>= 0 && pos
< (int)out
.size())
1118 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1121 //SetFromGit will clear all status
1122 path
.SetFromGit(one
);
1123 path
.m_Action
=action
;
1126 pos
=out
.findNextString(pos
);
1132 int CTGitPathList::FillBasedOnIndexFlags(unsigned short flag
, CTGitPathList
* list
/*nullptr*/)
1138 CStringA gitdir
= CUnicodeUtils::GetMulti(g_Git
.m_CurrentDir
, CP_UTF8
);
1139 git_repository
*repository
= nullptr;
1140 if (git_repository_open(&repository
, gitdir
.GetBuffer()))
1143 git_index
*index
= nullptr;
1144 if (git_repository_index(&index
, repository
))
1146 git_repository_free(repository
);
1151 if (list
== nullptr)
1154 count
= list
->GetCount();
1155 for (int j
= 0; j
< count
; ++j
)
1157 for (size_t i
= 0, ecount
= git_index_entrycount(index
); i
< ecount
; ++i
)
1159 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
1161 if (!e
|| !((e
->flags
| e
->flags_extended
) & flag
) || !e
->path
)
1165 g_Git
.StringAppend(&one
, (BYTE
*)e
->path
, CP_UTF8
);
1167 if (!(!list
|| (*list
)[j
].GetWinPathString().IsEmpty() || one
== (*list
)[j
].GetWinPathString() || (PathIsDirectory(g_Git
.m_CurrentDir
+ L
"\\" + (*list
)[j
].GetWinPathString()) && one
.Find((*list
)[j
].GetWinPathString() + _T("/")) == 0)))
1170 //SetFromGit will clear all status
1171 path
.SetFromGit(one
);
1172 if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_SKIP_WORKTREE
)
1173 path
.m_Action
= CTGitPath::LOGACTIONS_SKIPWORKTREE
;
1174 else if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_VALID
)
1175 path
.m_Action
= CTGitPath::LOGACTIONS_ASSUMEVALID
;
1179 git_index_free(index
);
1181 git_repository_free(repository
);
1184 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1192 while (pos
>= 0 && pos
< (int)log
.size())
1194 //one=log.Tokenize(_T("\n"),pos);
1202 if(log
[pos
+1] ==':')
1206 int end
=log
.find(0,pos
);
1208 int file1
=-1,file2
=-1;
1211 actionstart
=log
.find(' ',end
-6);
1218 file1
= log
.find(0,actionstart
);
1224 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1227 file1
= log
.find(0,file1
);
1241 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1243 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1245 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1249 GitPath
->ParserAction( log
[actionstart
] );
1253 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1254 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1256 m_Action
|=GitPath
->m_Action
;
1261 int ac
=path
.ParserAction(log
[actionstart
] );
1262 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1264 path
.SetFromGit(pathname1
,&pathname2
);
1266 //action must be set after setfromgit. SetFromGit will clear all status.
1283 tabstart
=log
.find('\t',pos
);
1287 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1291 tabstart
=log
.find('\t',pos
);
1296 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1300 if(log
[pos
] == 0) //rename
1303 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1304 int sec
=log
.find(0,pos
);
1308 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1315 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1317 path
.SetFromGit(file1
,&file2
);
1319 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1322 GitPath
->m_StatAdd
=StatAdd
;
1323 GitPath
->m_StatDel
=StatDel
;
1327 //path.SetFromGit(pathname);
1330 path
.m_StatAdd
=_T("0");
1331 path
.m_StatDel
=_T("0");
1332 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1336 path
.m_StatAdd
=StatAdd
;
1337 path
.m_StatDel
=StatDel
;
1338 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1344 pos
=log
.findNextString(pos
);
1349 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1351 m_paths
.push_back(newPath
);
1352 m_commonBaseDirectory
.Reset();
1354 int CTGitPathList::GetCount() const
1356 return (int)m_paths
.size();
1358 bool CTGitPathList::IsEmpty() const
1360 return m_paths
.empty();
1362 void CTGitPathList::Clear()
1365 m_commonBaseDirectory
.Reset();
1368 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1370 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1371 return m_paths
[index
];
1374 bool CTGitPathList::AreAllPathsFiles() const
1376 // Look through the vector for any directories - if we find them, return false
1377 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1381 #if defined(_MFC_VER)
1383 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1389 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1391 // for every selected file/folder
1393 while (file
.ReadString(strLine
))
1395 path
.SetFromUnknown(strLine
);
1400 catch (CFileException
* pE
)
1402 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException loading target file list\n");
1403 TCHAR error
[10000] = {0};
1404 pE
->GetErrorMessage(error
, 10000);
1405 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1412 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1418 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1419 PathVector::const_iterator it
;
1420 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1422 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1423 file
.Write(line
, line
.GetLength());
1429 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1430 PathVector::const_iterator it
;
1431 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1433 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1438 catch (CFileException
* pE
)
1440 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException in writing temp file\n");
1448 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1454 temp
= sPathString
.Tokenize(_T("*"),pos
);
1459 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1463 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1466 PathVector::const_iterator it
;
1467 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1469 if (!sRet
.IsEmpty())
1471 sRet
+= it
->GetWinPathString();
1478 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1480 // Check if all the paths are files and in the same directory
1481 PathVector::const_iterator it
;
1482 m_commonBaseDirectory
.Reset();
1483 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1485 if(it
->IsDirectory())
1489 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1490 if(m_commonBaseDirectory
.IsEmpty())
1492 m_commonBaseDirectory
= baseDirectory
;
1494 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1497 m_commonBaseDirectory
.Reset();
1504 CTGitPath
CTGitPathList::GetCommonDirectory() const
1506 if (m_commonBaseDirectory
.IsEmpty())
1508 PathVector::const_iterator it
;
1509 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1511 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1512 if(m_commonBaseDirectory
.IsEmpty())
1514 m_commonBaseDirectory
= baseDirectory
;
1516 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1519 m_commonBaseDirectory
.Reset();
1524 // since we only checked strings, not paths,
1525 // we have to make sure now that we really return a *path* here
1526 PathVector::const_iterator iter
;
1527 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1529 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1531 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1535 return m_commonBaseDirectory
;
1538 CTGitPath
CTGitPathList::GetCommonRoot() const
1543 if (GetCount() == 1)
1546 // first entry is common root for itself
1547 // (add trailing '\\' to detect partial matches of the last path element)
1548 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1549 int rootLength
= root
.GetLength();
1551 // determine common path string prefix
1552 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1554 CString path
= it
->GetWinPathString() + _T('\\');
1556 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1557 if (newLength
!= rootLength
)
1559 root
.Delete(newLength
, rootLength
);
1560 rootLength
= newLength
;
1564 // remove the last (partial) path element
1566 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1569 return CTGitPath(root
);
1572 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1574 std::sort(m_paths
.begin(), m_paths
.end());
1576 std::reverse(m_paths
.begin(), m_paths
.end());
1579 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1581 PathVector::const_iterator it
;
1582 SortByPathname(true); // nested ones first
1585 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1587 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1589 if (!it
->IsDirectory())
1590 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1592 sPaths
+= it
->GetWinPath();
1598 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1602 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1604 SHFILEOPSTRUCT shop
= {0};
1605 shop
.wFunc
= FO_DELETE
;
1607 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1609 shop
.fFlags
|= FOF_ALLOWUNDO
;
1610 const bool bRet
= (SHFileOperation(&shop
) == 0);
1614 void CTGitPathList::RemoveDuplicates()
1617 // Remove the duplicates
1618 // (Unique moves them to the end of the vector, then erase chops them off)
1619 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1622 void CTGitPathList::RemoveAdminPaths()
1624 PathVector::iterator it
;
1625 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1627 if (it
->IsAdminDir())
1630 it
= m_paths
.begin();
1637 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1639 PathVector::iterator it
;
1640 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1642 if (it
->IsEquivalentTo(path
))
1650 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1652 PathVector::iterator it
;
1653 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1655 if (it
->GetGitPathString()==path
.GetGitPathString())
1662 void CTGitPathList::RemoveChildren()
1665 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1668 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1670 if (list
.GetCount() != GetCount())
1672 for (int i
=0; i
<list
.GetCount(); ++i
)
1674 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1680 //////////////////////////////////////////////////////////////////////////
1682 apr_array_header_t
* CTGitPathList::MakePathArray (apr_pool_t
*pool
) const
1684 apr_array_header_t
*targets
= apr_array_make (pool
, GetCount(), sizeof(const char *));
1686 for (int nItem
= 0; nItem
< GetCount(); ++nItem
)
1688 const char * target
= m_paths
[nItem
].GetGitApiPath(pool
);
1689 (*((const char **) apr_array_push (targets
))) = target
;
1695 //////////////////////////////////////////////////////////////////////////
1699 // Some test cases for these classes
1700 static class CTGitPathTests
1706 pool
= svn_pool_create(NULL
);
1712 RemoveDuplicatesTest();
1713 RemoveChildrenTest();
1714 ContainingDirectoryTest();
1716 SubversionPathTest();
1717 GetCommonRootTest();
1718 #if defined(_MFC_VER)
1719 ValidPathAndUrlTest();
1726 // apr_pool_t * pool;
1727 void GetDirectoryTest()
1729 // Bit tricky, this test, because we need to know something about the file
1730 // layout on the machine which is running the test
1731 TCHAR winDir
[MAX_PATH
+1] = {0};
1732 GetWindowsDirectory(winDir
, MAX_PATH
);
1733 CString
sWinDir(winDir
);
1736 // This is a file which we know will always be there
1737 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1738 ATLASSERT(!testPath
.IsDirectory());
1739 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1740 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1742 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1743 // but we know it must be different to the directory itself
1744 testPath
.SetFromUnknown(sWinDir
);
1745 ATLASSERT(testPath
.IsDirectory());
1746 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1747 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1748 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1751 testPath
.SetFromUnknown(_T("C:\\"));
1752 ATLASSERT(testPath
.IsDirectory());
1753 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1754 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1755 // Try a root UNC path
1756 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1757 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1763 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1764 ATLASSERT(!testPath
.IsAdminDir());
1765 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1766 ATLASSERT(!testPath
.IsAdminDir());
1767 testPath
.SetFromUnknown(_T("c:\\.svn"));
1768 ATLASSERT(testPath
.IsAdminDir());
1769 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1770 ATLASSERT(!testPath
.IsAdminDir());
1771 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1772 ATLASSERT(testPath
.IsAdminDir());
1774 CTGitPathList pathList
;
1775 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1776 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1777 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1778 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1779 pathList
.RemoveAdminPaths();
1780 ATLASSERT(pathList
.GetCount()==2);
1782 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1783 pathList
.RemoveAdminPaths();
1784 ATLASSERT(pathList
.GetCount()==1);
1789 CTGitPathList testList
;
1791 testPath
.SetFromUnknown(_T("c:/Z"));
1792 testList
.AddPath(testPath
);
1793 testPath
.SetFromUnknown(_T("c:/B"));
1794 testList
.AddPath(testPath
);
1795 testPath
.SetFromUnknown(_T("c:\\a"));
1796 testList
.AddPath(testPath
);
1797 testPath
.SetFromUnknown(_T("c:/Test"));
1798 testList
.AddPath(testPath
);
1800 testList
.SortByPathname();
1802 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1803 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1804 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1805 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1808 void RawAppendTest()
1810 CTGitPath
testPath(_T("c:/test/"));
1811 testPath
.AppendRawString(_T("/Hello"));
1812 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1814 testPath
.AppendRawString(_T("\\T2"));
1815 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1817 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1818 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1819 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1820 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1823 void PathAppendTest()
1825 CTGitPath
testPath(_T("c:/test/"));
1826 testPath
.AppendPathString(_T("/Hello"));
1827 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1829 testPath
.AppendPathString(_T("T2"));
1830 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1832 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1833 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1834 // You wouldn't want to do this in real life - you'd use append-raw
1835 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1836 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1839 void RemoveDuplicatesTest()
1842 list
.AddPath(CTGitPath(_T("Z")));
1843 list
.AddPath(CTGitPath(_T("A")));
1844 list
.AddPath(CTGitPath(_T("E")));
1845 list
.AddPath(CTGitPath(_T("E")));
1847 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1848 ATLASSERT(list
[2]==list
[3]);
1850 ATLASSERT(list
.GetCount() == 4);
1852 list
.RemoveDuplicates();
1854 ATLASSERT(list
.GetCount() == 3);
1856 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1857 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1858 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1861 void RemoveChildrenTest()
1864 list
.AddPath(CTGitPath(_T("c:\\test")));
1865 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1866 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1867 list
.AddPath(CTGitPath(_T("c:\\parent")));
1868 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1869 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1870 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1872 ATLASSERT(list
.GetCount() == 7);
1874 list
.RemoveChildren();
1876 ATLTRACE("count = %d\n", list
.GetCount());
1877 ATLASSERT(list
.GetCount() == 3);
1879 list
.SortByPathname();
1881 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1882 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1883 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1886 #if defined(_MFC_VER)
1887 void ListLoadingTest()
1889 TCHAR buf
[MAX_PATH
] = {0};
1890 GetCurrentDirectory(MAX_PATH
, buf
);
1891 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1892 CTGitPathList testList
;
1893 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1895 ATLASSERT(testList
.GetCount() == 3);
1896 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1897 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1898 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1900 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1902 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1903 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1904 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1908 void ContainingDirectoryTest()
1912 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1914 dir
= testPath
.GetContainingDirectory();
1915 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1916 dir
= dir
.GetContainingDirectory();
1917 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1918 dir
= dir
.GetContainingDirectory();
1919 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1920 dir
= dir
.GetContainingDirectory();
1921 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1922 dir
= dir
.GetContainingDirectory();
1923 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1924 dir
= dir
.GetContainingDirectory();
1925 ATLASSERT(dir
.IsEmpty());
1926 ATLASSERT(dir
.GetWinPathString() == _T(""));
1932 testPath
.SetFromWin(_T("c:\\windows"));
1933 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1934 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1935 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1936 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1937 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1940 void SubversionPathTest()
1943 testPath
.SetFromWin(_T("c:\\"));
1944 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1945 testPath
.SetFromWin(_T("c:\\folder"));
1946 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1947 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1948 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1949 testPath
.SetFromUnknown(_T("http://testing/"));
1950 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1951 testPath
.SetFromGit(NULL
);
1952 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1953 #if defined(_MFC_VER)
1954 testPath
.SetFromUnknown(_T("http://testing again"));
1955 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1956 testPath
.SetFromUnknown(_T("http://testing%20again"));
1957 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1958 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1959 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1963 void GetCommonRootTest()
1965 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1966 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1967 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1970 list
.AddPath(pathA
);
1971 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1972 list
.AddPath(pathB
);
1973 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1974 list
.AddPath(pathC
);
1975 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1978 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");
1979 list
.LoadFromAsteriskSeparatedString(sPathList
);
1980 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1983 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1984 list
.LoadFromAsteriskSeparatedString(sPathList
);
1985 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1988 sPathList
= _T("c:\\windows\\*c:\\windows");
1989 list
.LoadFromAsteriskSeparatedString(sPathList
);
1990 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1993 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1994 list
.LoadFromAsteriskSeparatedString(sPathList
);
1995 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1998 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1999 list
.LoadFromAsteriskSeparatedString(sPathList
);
2000 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
2004 void ValidPathAndUrlTest()
2007 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
2008 ATLASSERT(testPath
.IsValidOnWindows());
2009 testPath
.SetFromWin(_T("c:\\"));
2010 ATLASSERT(testPath
.IsValidOnWindows());
2011 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
2012 ATLASSERT(testPath
.IsValidOnWindows());
2013 testPath
.SetFromWin(_T("c"));
2014 ATLASSERT(testPath
.IsValidOnWindows());
2015 testPath
.SetFromWin(_T("c:\\test folder\\file"));
2016 ATLASSERT(testPath
.IsValidOnWindows());
2017 testPath
.SetFromWin(_T("c:\\folder\\"));
2018 ATLASSERT(testPath
.IsValidOnWindows());
2019 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
2020 ATLASSERT(testPath
.IsValidOnWindows());
2021 testPath
.SetFromWin(_T("c:\\.svn"));
2022 ATLASSERT(testPath
.IsValidOnWindows());
2023 testPath
.SetFromWin(_T("c:\\com\\file"));
2024 ATLASSERT(testPath
.IsValidOnWindows());
2025 testPath
.SetFromWin(_T("c:\\test\\conf"));
2026 ATLASSERT(testPath
.IsValidOnWindows());
2027 testPath
.SetFromWin(_T("c:\\LPT"));
2028 ATLASSERT(testPath
.IsValidOnWindows());
2029 testPath
.SetFromWin(_T("c:\\test\\LPT"));
2030 ATLASSERT(testPath
.IsValidOnWindows());
2031 testPath
.SetFromWin(_T("c:\\com1test"));
2032 ATLASSERT(testPath
.IsValidOnWindows());
2033 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
2034 ATLASSERT(testPath
.IsValidOnWindows());
2036 testPath
.SetFromWin(_T("\\\\Share\\filename"));
2037 ATLASSERT(testPath
.IsValidOnWindows());
2038 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
2039 ATLASSERT(testPath
.IsValidOnWindows());
2040 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
2041 ATLASSERT(testPath
.IsValidOnWindows());
2043 // now the negative tests
2044 testPath
.SetFromWin(_T("c:\\test:folder"));
2045 ATLASSERT(!testPath
.IsValidOnWindows());
2046 testPath
.SetFromWin(_T("c:\\file<name"));
2047 ATLASSERT(!testPath
.IsValidOnWindows());
2048 testPath
.SetFromWin(_T("c:\\something*else"));
2049 ATLASSERT(!testPath
.IsValidOnWindows());
2050 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
2051 ATLASSERT(!testPath
.IsValidOnWindows());
2052 testPath
.SetFromWin(_T("c:\\ext.>ension"));
2053 ATLASSERT(!testPath
.IsValidOnWindows());
2054 testPath
.SetFromWin(_T("c:\\com1\\filename"));
2055 ATLASSERT(!testPath
.IsValidOnWindows());
2056 testPath
.SetFromWin(_T("c:\\com1"));
2057 ATLASSERT(!testPath
.IsValidOnWindows());
2058 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
2059 ATLASSERT(!testPath
.IsValidOnWindows());
2061 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
2062 ATLASSERT(!testPath
.IsValidOnWindows());
2063 testPath
.SetFromWin(_T("\\\\Share\\prn"));
2064 ATLASSERT(!testPath
.IsValidOnWindows());
2065 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
2066 ATLASSERT(!testPath
.IsValidOnWindows());
2068 // now come some URL tests
2069 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2070 ATLASSERT(testPath
.IsValidOnWindows());
2071 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2072 ATLASSERT(testPath
.IsValidOnWindows());
2073 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2074 ATLASSERT(testPath
.IsValidOnWindows());
2075 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2076 ATLASSERT(testPath
.IsValidOnWindows());
2077 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2078 ATLASSERT(testPath
.IsValidOnWindows());
2079 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2080 ATLASSERT(testPath
.IsValidOnWindows());
2081 // and some negative URL tests
2082 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2083 ATLASSERT(!testPath
.IsValidOnWindows());
2084 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2085 ATLASSERT(!testPath
.IsValidOnWindows());
2086 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2087 ATLASSERT(!testPath
.IsValidOnWindows());
2088 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2089 ATLASSERT(!testPath
.IsValidOnWindows());
2090 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2091 ATLASSERT(!testPath
.IsValidOnWindows());
2095 } TGitPathTestobject
;
2099 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2102 for (i
= 0; i
< this->GetCount(); ++i
)
2104 if((*this)[i
].GetGitPathString() == path
)
2105 return (CTGitPath
*)&(*this)[i
];
2109 CString
CTGitPath::GetActionName(int action
)
2111 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2112 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2113 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2114 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2115 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2116 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2117 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2118 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2120 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2121 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2122 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2123 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2124 if(action
& CTGitPath::LOGACTIONS_COPY
)
2125 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2127 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2128 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2130 if (action
& CTGitPath::LOGACTIONS_ASSUMEVALID
)
2131 return MAKEINTRESOURCE(IDS_PATHACTIONS_ASSUMEUNCHANGED
);
2132 if (action
& CTGitPath::LOGACTIONS_SKIPWORKTREE
)
2133 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIPWORKTREE
);
2135 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2137 CString
CTGitPath::GetActionName()
2139 return GetActionName(m_Action
);
2142 int CTGitPathList::GetAction()