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
;
127 void CTGitPath::SetFromGit(const char* pPath
)
132 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, NULL
, 0);
135 len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, m_sFwdslashPath
.GetBuffer(len
+1), len
+1);
136 m_sFwdslashPath
.ReleaseBuffer(len
-1);
138 SanitizeRootPath(m_sFwdslashPath
, true);
141 void CTGitPath::SetFromGit(const char* pPath
, bool bIsDirectory
)
144 m_bDirectoryKnown
= true;
145 m_bIsDirectory
= bIsDirectory
;
148 void CTGitPath::SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
)
153 m_sFwdslashPath
= pPath
;
154 SanitizeRootPath(m_sFwdslashPath
, true);
156 m_bDirectoryKnown
= true;
157 m_bIsDirectory
= bIsDirectory
;
160 void CTGitPath::SetFromGit(const CString
& sPath
,CString
*oldpath
)
163 m_sFwdslashPath
= sPath
;
164 SanitizeRootPath(m_sFwdslashPath
, true);
166 m_sOldFwdslashPath
= *oldpath
;
169 void CTGitPath::SetFromWin(LPCTSTR pPath
)
172 m_sBackslashPath
= pPath
;
173 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
174 SanitizeRootPath(m_sBackslashPath
, false);
175 ATLASSERT(m_sBackslashPath
.Find('/')<0);
177 void CTGitPath::SetFromWin(const CString
& sPath
)
180 m_sBackslashPath
= sPath
;
181 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
182 SanitizeRootPath(m_sBackslashPath
, false);
184 void CTGitPath::SetFromWin(LPCTSTR pPath
, bool bIsDirectory
)
187 m_sBackslashPath
= pPath
;
188 m_bIsDirectory
= bIsDirectory
;
189 m_bDirectoryKnown
= true;
190 SanitizeRootPath(m_sBackslashPath
, false);
192 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
195 m_sBackslashPath
= sPath
;
196 m_bIsDirectory
= bIsDirectory
;
197 m_bDirectoryKnown
= true;
198 SanitizeRootPath(m_sBackslashPath
, false);
200 void CTGitPath::SetFromUnknown(const CString
& sPath
)
203 // Just set whichever path we think is most likely to be used
204 // GitAdminDir admin;
206 // if(admin.HasAdminDir(sPath,&p))
207 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
209 SetFwdslashPath(sPath
);
212 LPCTSTR
CTGitPath::GetWinPath() const
218 if(m_sBackslashPath
.IsEmpty())
220 SetBackslashPath(m_sFwdslashPath
);
222 return m_sBackslashPath
;
224 // This is a temporary function, to be used during the migration to
225 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
226 const CString
& CTGitPath::GetWinPathString() const
228 if(m_sBackslashPath
.IsEmpty())
230 SetBackslashPath(m_sFwdslashPath
);
232 return m_sBackslashPath
;
235 const CString
& CTGitPath::GetGitPathString() const
237 if(m_sFwdslashPath
.IsEmpty())
239 SetFwdslashPath(m_sBackslashPath
);
241 return m_sFwdslashPath
;
244 const CString
&CTGitPath::GetGitOldPathString() const
246 return m_sOldFwdslashPath
;
249 const char* CTGitPath::GetGitApiPath(apr_pool_t
*pool
) const
251 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
252 // each call to GetGitApiPath returns a different pointer value.
253 // If you made multiple calls to GetGitApiPath on the same string, only the last
254 // one would give you a valid pointer to an empty string, because each
255 // call would invalidate the previous call's return.
260 if(m_sFwdslashPath
.IsEmpty())
262 SetFwdslashPath(m_sBackslashPath
);
264 if(m_sUTF8FwdslashPath
.IsEmpty())
266 SetUTF8FwdslashPath(m_sFwdslashPath
);
268 if (svn_path_is_url(m_sUTF8FwdslashPath
))
270 m_sUTF8FwdslashPathEscaped
= CPathUtils::PathEscape(m_sUTF8FwdslashPath
);
271 m_sUTF8FwdslashPathEscaped
.Replace("file:////", "file:///\\");
272 m_sUTF8FwdslashPathEscaped
= svn_path_canonicalize(m_sUTF8FwdslashPathEscaped
, pool
);
273 return m_sUTF8FwdslashPathEscaped
;
275 m_sUTF8FwdslashPath
= svn_path_canonicalize(m_sUTF8FwdslashPath
, pool
);
277 return m_sUTF8FwdslashPath
;
281 const CString
& CTGitPath::GetUIPathString() const
283 if (m_sUIPath
.IsEmpty())
285 m_sUIPath
= GetWinPathString();
290 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
292 m_sFwdslashPath
= sPath
;
293 m_sFwdslashPath
.Replace('\\', '/');
295 // We don't leave a trailing /
296 m_sFwdslashPath
.TrimRight('/');
297 m_sFwdslashPath
.Replace(L
"//?/", L
"");
299 SanitizeRootPath(m_sFwdslashPath
, true);
301 m_sFwdslashPath
.Replace(_T("file:////"), _T("file://"));
303 m_sUTF8FwdslashPath
.Empty();
306 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
308 m_sBackslashPath
= sPath
;
309 m_sBackslashPath
.Replace('/', '\\');
310 m_sBackslashPath
.TrimRight('\\');
311 SanitizeRootPath(m_sBackslashPath
, false);
314 void CTGitPath::SetUTF8FwdslashPath(const CString
& sPath
) const
316 m_sUTF8FwdslashPath
= CUnicodeUtils::GetUTF8(sPath
);
319 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
321 // Make sure to add the trailing slash to root paths such as 'C:'
322 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
324 sPath
+= (bIsForwardPath
) ? _T("/") : _T("\\");
328 bool CTGitPath::IsDirectory() const
330 if(!m_bDirectoryKnown
)
334 return m_bIsDirectory
;
337 bool CTGitPath::Exists() const
346 bool CTGitPath::Delete(bool bTrash
) const
348 EnsureBackslashPathSet();
349 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
353 if ((bTrash
)||(IsDirectory()))
355 std::unique_ptr
<TCHAR
[]> buf(new TCHAR
[m_sBackslashPath
.GetLength() + 2]);
356 _tcscpy_s(buf
.get(), m_sBackslashPath
.GetLength() + 2, m_sBackslashPath
);
357 buf
[m_sBackslashPath
.GetLength()] = 0;
358 buf
[m_sBackslashPath
.GetLength()+1] = 0;
359 bRet
= CTGitPathList::DeleteViaShell(buf
.get(), bTrash
);
363 bRet
= !!::DeleteFile(m_sBackslashPath
);
367 m_bExistsKnown
= true;
371 __int64
CTGitPath::GetLastWriteTime() const
373 if(!m_bLastWriteTimeKnown
)
377 return m_lastWriteTime
;
380 __int64
CTGitPath::GetFileSize() const
382 if(!m_bDirectoryKnown
)
389 bool CTGitPath::IsReadOnly() const
391 if(!m_bLastWriteTimeKnown
)
395 return m_bIsReadOnly
;
398 void CTGitPath::UpdateAttributes() const
400 EnsureBackslashPathSet();
401 WIN32_FILE_ATTRIBUTE_DATA attribs
;
402 if (m_sBackslashPath
.GetLength() >= 248)
403 m_sLongBackslashPath
= _T("\\\\?\\") + m_sBackslashPath
;
404 if(GetFileAttributesEx(m_sBackslashPath
.GetLength() >= 248 ? m_sLongBackslashPath
: m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
406 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
407 m_lastWriteTime
= *(__int64
*)&attribs
.ftLastWriteTime
;
414 m_fileSize
= ((INT64
)( (DWORD
)(attribs
.nFileSizeLow
) ) | ( (INT64
)( (DWORD
)(attribs
.nFileSizeHigh
) )<<32 ));
416 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
421 m_bIsDirectory
= false;
424 DWORD err
= GetLastError();
425 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
435 m_bDirectoryKnown
= true;
436 m_bLastWriteTimeKnown
= true;
437 m_bExistsKnown
= true;
440 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
444 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
446 CString str
=GetWinPathString();
447 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
452 void CTGitPath::EnsureBackslashPathSet() const
454 if(m_sBackslashPath
.IsEmpty())
456 SetBackslashPath(m_sFwdslashPath
);
457 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
460 void CTGitPath::EnsureFwdslashPathSet() const
462 if(m_sFwdslashPath
.IsEmpty())
464 SetFwdslashPath(m_sBackslashPath
);
465 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
470 // Reset all the caches
471 void CTGitPath::Reset()
473 m_bDirectoryKnown
= false;
475 m_bLastWriteTimeKnown
= false;
476 m_bHasAdminDirKnown
= false;
477 m_bIsValidOnWindowsKnown
= false;
478 m_bIsAdminDirKnown
= false;
479 m_bExistsKnown
= false;
480 m_bIsSpecialDirectoryKnown
= false;
481 m_bIsSpecialDirectory
= false;
483 m_sBackslashPath
.Empty();
484 m_sFwdslashPath
.Empty();
485 m_sUTF8FwdslashPath
.Empty();
487 this->m_StatAdd
=_T("");
488 this->m_StatDel
=_T("");
490 ATLASSERT(IsEmpty());
493 CTGitPath
CTGitPath::GetDirectory() const
495 if ((IsDirectory())||(!Exists()))
499 return GetContainingDirectory();
502 CTGitPath
CTGitPath::GetContainingDirectory() const
504 EnsureBackslashPathSet();
506 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
507 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
509 // This is a root directory, which needs a trailing slash
511 if(sDirName
== m_sBackslashPath
)
513 // We were clearly provided with a root path to start with - we should return nothing now
517 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
519 // We have an UNC path and we already are the root
523 retVal
.SetFromWin(sDirName
);
527 CString
CTGitPath::GetRootPathString() const
529 EnsureBackslashPathSet();
530 CString workingPath
= m_sBackslashPath
;
531 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
532 ATLVERIFY(::PathStripToRoot(pPath
));
533 workingPath
.ReleaseBuffer();
538 CString
CTGitPath::GetFilename() const
540 //ATLASSERT(!IsDirectory());
541 return GetFileOrDirectoryName();
544 CString
CTGitPath::GetFileOrDirectoryName() const
546 EnsureBackslashPathSet();
547 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
550 CString
CTGitPath::GetUIFileOrDirectoryName() const
553 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
556 CString
CTGitPath::GetFileExtension() const
560 EnsureBackslashPathSet();
561 int dotPos
= m_sBackslashPath
.ReverseFind('.');
562 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
563 if (dotPos
> slashPos
)
564 return m_sBackslashPath
.Mid(dotPos
);
568 CString
CTGitPath::GetBaseFilename() const
571 CString filename
=GetFilename();
572 dot
= filename
.ReverseFind(_T('.'));
574 return filename
.Left(dot
);
579 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
581 int length
= sP1
.GetLength();
582 if(length
!= sP2
.GetLength())
587 // We work from the end of the strings, because path differences
588 // are more likely to occur at the far end of a string
589 LPCTSTR pP1Start
= sP1
;
590 LPCTSTR pP1
= pP1Start
+(length
-1);
591 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
594 if(_totlower(*pP1
--) != _totlower(*pP2
--))
602 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
604 int length
= sP1
.GetLength();
605 if(length
!= sP2
.GetLength())
610 // We work from the end of the strings, because path differences
611 // are more likely to occur at the far end of a string
612 LPCTSTR pP1Start
= sP1
;
613 LPCTSTR pP1
= pP1Start
+(length
-1);
614 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
617 if((*pP1
--) != (*pP2
--))
625 bool CTGitPath::IsEmpty() const
627 // Check the backward slash path first, since the chance that this
628 // one is set is higher. In case of a 'false' return value it's a little
630 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
633 // Test if both paths refer to the same item
634 // Ignores case and slash direction
635 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
637 // Try and find a slash direction which avoids having to convert
639 if(!m_sBackslashPath
.IsEmpty())
641 // *We've* got a \ path - make sure that the RHS also has a \ path
642 rhs
.EnsureBackslashPathSet();
643 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
647 // Assume we've got a fwdslash path and make sure that the RHS has one
648 rhs
.EnsureFwdslashPathSet();
649 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
653 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
655 // Try and find a slash direction which avoids having to convert
657 if(!m_sBackslashPath
.IsEmpty())
659 // *We've* got a \ path - make sure that the RHS also has a \ path
660 rhs
.EnsureBackslashPathSet();
661 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
665 // Assume we've got a fwdslash path and make sure that the RHS has one
666 rhs
.EnsureFwdslashPathSet();
667 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
671 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
673 possibleDescendant
.EnsureBackslashPathSet();
674 EnsureBackslashPathSet();
676 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
677 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
679 return bPathStringsEqual
;
682 return (bPathStringsEqual
&&
683 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
684 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
687 // Get a string representing the file path, optionally with a base
688 // section stripped off the front.
689 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
691 EnsureFwdslashPathSet();
692 if(pOptionalBasePath
!= NULL
)
694 // Find the length of the base-path without having to do an 'ensure' on it
695 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
697 // Now, chop that baseLength of the front of the path
698 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
700 return m_sFwdslashPath
;
703 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
705 left
.EnsureBackslashPathSet();
706 right
.EnsureBackslashPathSet();
707 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
710 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
712 return CTGitPath::Compare(left
, right
) < 0;
715 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
717 return left
.IsEquivalentTo(right
);
720 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
722 if (left
.IsAdminDir() && right
.IsAdminDir())
728 l
= l
.GetContainingDirectory();
729 } while(l
.HasAdminDir());
732 r
= r
.GetContainingDirectory();
733 } while(r
.HasAdminDir());
734 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
736 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
739 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
741 return parent
.IsAncestorOf(child
);
744 void CTGitPath::AppendRawString(const CString
& sAppend
)
746 EnsureFwdslashPathSet();
747 CString strCopy
= m_sFwdslashPath
+= sAppend
;
748 SetFromUnknown(strCopy
);
751 void CTGitPath::AppendPathString(const CString
& sAppend
)
753 EnsureBackslashPathSet();
754 CString
cleanAppend(sAppend
);
755 cleanAppend
.Replace('/', '\\');
756 cleanAppend
.TrimLeft('\\');
757 m_sBackslashPath
.TrimRight('\\');
758 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
762 bool CTGitPath::IsWCRoot() const
764 if (m_bIsWCRootKnown
)
767 m_bIsWCRootKnown
= true;
770 CString topDirectory
;
771 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
776 if (IsEquivalentToWithoutCase(topDirectory
))
784 bool CTGitPath::HasAdminDir() const
786 if (m_bHasAdminDirKnown
)
787 return m_bHasAdminDir
;
789 EnsureBackslashPathSet();
790 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
791 m_bHasAdminDirKnown
= true;
792 return m_bHasAdminDir
;
795 bool CTGitPath::HasSubmodules() const
799 CString path
= m_sProjectRoot
;
800 path
+= _T("\\.gitmodules");
801 if( PathFileExists(path
) )
807 int CTGitPath::GetAdminDirMask() const
811 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
816 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
817 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
821 status
|= ITEMIS_FOLDERINGIT
;
824 status
|= ITEMIS_WCROOT
;
826 CString topProjectDir
;
827 if (g_GitAdminDir
.HasAdminDir(GetWinPathString(), false, &topProjectDir
))
829 if (PathFileExists(topProjectDir
+ _T("\\.gitmodules")))
832 git_config_new(&config
);
833 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(topProjectDir
+ _T("\\.gitmodules")), GIT_CONFIG_LEVEL_APP
, FALSE
);
834 CString relativePath
= GetWinPathString().Mid(topProjectDir
.GetLength());
835 relativePath
.Replace(_T("\\"), _T("/"));
836 relativePath
.Trim(_T("/"));
838 CStringA
key(CUnicodeUtils::GetUTF8(_T("submodule.") + relativePath
+ _T(".path")));
839 if (!git_config_get_string(&out
, config
, key
))
840 status
|= ITEMIS_SUBMODULE
;
841 git_config_free(config
);
848 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
850 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
851 status
|= ITEMIS_BISECT
;
853 if (PathFileExists(dotGitPath
+ _T("MERGE_HEAD")))
854 status
|= ITEMIS_MERGEACTIVE
;
856 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
857 status
|= ITEMIS_STASH
;
859 if (PathFileExists(dotGitPath
+ _T("svn")))
860 status
|= ITEMIS_GITSVN
;
862 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
863 status
|= ITEMIS_SUBMODULECONTAINER
;
868 bool CTGitPath::HasStashDir() const
871 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
877 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
879 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
881 bool CTGitPath::HasGitSVNDir() const
884 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
890 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
892 return !!PathFileExists(dotGitPath
+ _T("svn"));
894 bool CTGitPath::IsBisectActive() const
897 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
903 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
905 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
907 bool CTGitPath::IsMergeActive() const
910 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
916 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
918 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
920 bool CTGitPath::HasRebaseApply() const
923 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
929 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
931 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
934 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
936 if (m_bHasAdminDirKnown
)
939 *ProjectTopDir
= m_sProjectRoot
;
940 return m_bHasAdminDir
;
943 EnsureBackslashPathSet();
944 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
945 m_bHasAdminDirKnown
= true;
947 *ProjectTopDir
= m_sProjectRoot
;
948 return m_bHasAdminDir
;
951 bool CTGitPath::IsAdminDir() const
953 if (m_bIsAdminDirKnown
)
954 return m_bIsAdminDir
;
956 EnsureBackslashPathSet();
957 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
958 m_bIsAdminDirKnown
= true;
959 return m_bIsAdminDir
;
962 bool CTGitPath::IsValidOnWindows() const
964 if (m_bIsValidOnWindowsKnown
)
965 return m_bIsValidOnWindows
;
967 m_bIsValidOnWindows
= false;
968 EnsureBackslashPathSet();
969 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
970 std::wstring sPattern
;
971 // the 'file://' URL is just a normal windows path:
972 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
974 sMatch
= sMatch
.Mid(7);
975 sMatch
.TrimLeft(_T("\\"));
976 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
980 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
985 std::tr1::wregex
rx(sPattern
, std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
986 std::tr1::wsmatch match
;
988 std::wstring rmatch
= std::wstring((LPCTSTR
)sMatch
);
989 if (std::tr1::regex_match(rmatch
, match
, rx
))
991 if (std::wstring(match
[0]).compare(sMatch
)==0)
992 m_bIsValidOnWindows
= true;
994 if (m_bIsValidOnWindows
)
996 // now check for illegal filenames
997 std::tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
998 rmatch
= m_sBackslashPath
;
999 if (std::tr1::regex_search(rmatch
, rx2
, std::tr1::regex_constants::match_default
))
1000 m_bIsValidOnWindows
= false;
1003 catch (std::exception
) {}
1005 m_bIsValidOnWindowsKnown
= true;
1006 return m_bIsValidOnWindows
;
1009 //////////////////////////////////////////////////////////////////////////
1011 CTGitPathList::CTGitPathList()
1016 // A constructor which allows a path list to be easily built which one initial entry in
1017 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
1020 AddPath(firstEntry
);
1022 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
1030 while(pos
>=0 && pos
<out
.size())
1035 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1037 path
.m_Action
=path
.ParserAction(out
[pos
]);
1038 one
.Tokenize(_T("\t"),tabstart
);
1041 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1047 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1051 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1055 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1059 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1063 path
.m_Stage
=_ttol(part
);
1065 this->AddPath(path
);
1067 pos
=out
.findNextString(pos
);
1071 int CTGitPathList::FillUnRev(unsigned int action
,CTGitPathList
*list
)
1080 count
=list
->GetCount();
1081 for (int i
= 0; i
< count
; ++i
)
1087 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1092 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1097 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1099 (*list
)[i
].GetWinPathString());
1104 g_Git
.Run(cmd
, &out
);
1108 while (pos
>= 0 && pos
< (int)out
.size())
1111 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1114 //SetFromGit will clear all status
1115 path
.SetFromGit(one
);
1116 path
.m_Action
=action
;
1119 pos
=out
.findNextString(pos
);
1125 int CTGitPathList::FillBasedOnIndexFlags(unsigned short flag
, CTGitPathList
* list
/*nullptr*/)
1131 CStringA gitdir
= CUnicodeUtils::GetMulti(g_Git
.m_CurrentDir
, CP_UTF8
);
1132 git_repository
*repository
= nullptr;
1133 if (git_repository_open(&repository
, gitdir
.GetBuffer()))
1136 git_index
*index
= nullptr;
1137 if (git_repository_index(&index
, repository
))
1139 git_repository_free(repository
);
1144 if (list
== nullptr)
1147 count
= list
->GetCount();
1148 for (int j
= 0; j
< count
; ++j
)
1150 for (size_t i
= 0, ecount
= git_index_entrycount(index
); i
< ecount
; ++i
)
1152 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
1154 if (!e
|| !((e
->flags
| e
->flags_extended
) & flag
) || !e
->path
)
1158 g_Git
.StringAppend(&one
, (BYTE
*)e
->path
, CP_UTF8
);
1160 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)))
1163 //SetFromGit will clear all status
1164 path
.SetFromGit(one
);
1165 if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_SKIP_WORKTREE
)
1166 path
.m_Action
= CTGitPath::LOGACTIONS_SKIPWORKTREE
;
1167 else if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_VALID
)
1168 path
.m_Action
= CTGitPath::LOGACTIONS_ASSUMEVALID
;
1172 git_index_free(index
);
1174 git_repository_free(repository
);
1177 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1185 while (pos
>= 0 && pos
< (int)log
.size())
1187 //one=log.Tokenize(_T("\n"),pos);
1195 if(log
[pos
+1] ==':')
1199 int end
=log
.find(0,pos
);
1201 int file1
=-1,file2
=-1;
1204 actionstart
=log
.find(' ',end
-6);
1211 file1
= log
.find(0,actionstart
);
1217 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1220 file1
= log
.find(0,file1
);
1234 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1236 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1238 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1242 GitPath
->ParserAction( log
[actionstart
] );
1246 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1247 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1249 m_Action
|=GitPath
->m_Action
;
1254 int ac
=path
.ParserAction(log
[actionstart
] );
1255 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1257 path
.SetFromGit(pathname1
,&pathname2
);
1259 //action must be set after setfromgit. SetFromGit will clear all status.
1276 tabstart
=log
.find('\t',pos
);
1280 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1284 tabstart
=log
.find('\t',pos
);
1289 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1293 if(log
[pos
] == 0) //rename
1296 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1297 int sec
=log
.find(0,pos
);
1301 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1308 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1310 path
.SetFromGit(file1
,&file2
);
1312 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1315 GitPath
->m_StatAdd
=StatAdd
;
1316 GitPath
->m_StatDel
=StatDel
;
1320 //path.SetFromGit(pathname);
1323 path
.m_StatAdd
=_T("0");
1324 path
.m_StatDel
=_T("0");
1325 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1329 path
.m_StatAdd
=StatAdd
;
1330 path
.m_StatDel
=StatDel
;
1331 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1337 pos
=log
.findNextString(pos
);
1342 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1344 m_paths
.push_back(newPath
);
1345 m_commonBaseDirectory
.Reset();
1347 int CTGitPathList::GetCount() const
1349 return (int)m_paths
.size();
1351 bool CTGitPathList::IsEmpty() const
1353 return m_paths
.empty();
1355 void CTGitPathList::Clear()
1358 m_commonBaseDirectory
.Reset();
1361 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1363 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1364 return m_paths
[index
];
1367 bool CTGitPathList::AreAllPathsFiles() const
1369 // Look through the vector for any directories - if we find them, return false
1370 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1374 #if defined(_MFC_VER)
1376 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1382 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1384 // for every selected file/folder
1386 while (file
.ReadString(strLine
))
1388 path
.SetFromUnknown(strLine
);
1393 catch (CFileException
* pE
)
1395 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException loading target file list\n");
1396 TCHAR error
[10000] = {0};
1397 pE
->GetErrorMessage(error
, 10000);
1398 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1405 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1411 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1412 PathVector::const_iterator it
;
1413 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1415 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1416 file
.Write(line
, line
.GetLength());
1422 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1423 PathVector::const_iterator it
;
1424 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1426 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1431 catch (CFileException
* pE
)
1433 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException in writing temp file\n");
1441 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1447 temp
= sPathString
.Tokenize(_T("*"),pos
);
1452 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1456 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1459 PathVector::const_iterator it
;
1460 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1462 if (!sRet
.IsEmpty())
1464 sRet
+= it
->GetWinPathString();
1471 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1473 // Check if all the paths are files and in the same directory
1474 PathVector::const_iterator it
;
1475 m_commonBaseDirectory
.Reset();
1476 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1478 if(it
->IsDirectory())
1482 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1483 if(m_commonBaseDirectory
.IsEmpty())
1485 m_commonBaseDirectory
= baseDirectory
;
1487 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1490 m_commonBaseDirectory
.Reset();
1497 CTGitPath
CTGitPathList::GetCommonDirectory() const
1499 if (m_commonBaseDirectory
.IsEmpty())
1501 PathVector::const_iterator it
;
1502 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1504 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1505 if(m_commonBaseDirectory
.IsEmpty())
1507 m_commonBaseDirectory
= baseDirectory
;
1509 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1512 m_commonBaseDirectory
.Reset();
1517 // since we only checked strings, not paths,
1518 // we have to make sure now that we really return a *path* here
1519 PathVector::const_iterator iter
;
1520 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1522 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1524 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1528 return m_commonBaseDirectory
;
1531 CTGitPath
CTGitPathList::GetCommonRoot() const
1536 if (GetCount() == 1)
1539 // first entry is common root for itself
1540 // (add trailing '\\' to detect partial matches of the last path element)
1541 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1542 int rootLength
= root
.GetLength();
1544 // determine common path string prefix
1545 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1547 CString path
= it
->GetWinPathString() + _T('\\');
1549 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1550 if (newLength
!= rootLength
)
1552 root
.Delete(newLength
, rootLength
);
1553 rootLength
= newLength
;
1557 // remove the last (partial) path element
1559 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1562 return CTGitPath(root
);
1565 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1567 std::sort(m_paths
.begin(), m_paths
.end());
1569 std::reverse(m_paths
.begin(), m_paths
.end());
1572 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1574 PathVector::const_iterator it
;
1575 SortByPathname(true); // nested ones first
1578 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1580 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1582 if (!it
->IsDirectory())
1583 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1585 sPaths
+= it
->GetWinPath();
1591 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1595 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1597 SHFILEOPSTRUCT shop
= {0};
1598 shop
.wFunc
= FO_DELETE
;
1600 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1602 shop
.fFlags
|= FOF_ALLOWUNDO
;
1603 const bool bRet
= (SHFileOperation(&shop
) == 0);
1607 void CTGitPathList::RemoveDuplicates()
1610 // Remove the duplicates
1611 // (Unique moves them to the end of the vector, then erase chops them off)
1612 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1615 void CTGitPathList::RemoveAdminPaths()
1617 PathVector::iterator it
;
1618 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1620 if (it
->IsAdminDir())
1623 it
= m_paths
.begin();
1630 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1632 PathVector::iterator it
;
1633 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1635 if (it
->IsEquivalentTo(path
))
1643 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1645 PathVector::iterator it
;
1646 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1648 if (it
->GetGitPathString()==path
.GetGitPathString())
1655 void CTGitPathList::RemoveChildren()
1658 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1661 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1663 if (list
.GetCount() != GetCount())
1665 for (int i
=0; i
<list
.GetCount(); ++i
)
1667 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1673 //////////////////////////////////////////////////////////////////////////
1675 apr_array_header_t
* CTGitPathList::MakePathArray (apr_pool_t
*pool
) const
1677 apr_array_header_t
*targets
= apr_array_make (pool
, GetCount(), sizeof(const char *));
1679 for (int nItem
= 0; nItem
< GetCount(); ++nItem
)
1681 const char * target
= m_paths
[nItem
].GetGitApiPath(pool
);
1682 (*((const char **) apr_array_push (targets
))) = target
;
1688 //////////////////////////////////////////////////////////////////////////
1692 // Some test cases for these classes
1693 static class CTGitPathTests
1699 pool
= svn_pool_create(NULL
);
1705 RemoveDuplicatesTest();
1706 RemoveChildrenTest();
1707 ContainingDirectoryTest();
1709 SubversionPathTest();
1710 GetCommonRootTest();
1711 #if defined(_MFC_VER)
1712 ValidPathAndUrlTest();
1719 // apr_pool_t * pool;
1720 void GetDirectoryTest()
1722 // Bit tricky, this test, because we need to know something about the file
1723 // layout on the machine which is running the test
1724 TCHAR winDir
[MAX_PATH
+1] = {0};
1725 GetWindowsDirectory(winDir
, MAX_PATH
);
1726 CString
sWinDir(winDir
);
1729 // This is a file which we know will always be there
1730 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1731 ATLASSERT(!testPath
.IsDirectory());
1732 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1733 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1735 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1736 // but we know it must be different to the directory itself
1737 testPath
.SetFromUnknown(sWinDir
);
1738 ATLASSERT(testPath
.IsDirectory());
1739 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1740 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1741 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1744 testPath
.SetFromUnknown(_T("C:\\"));
1745 ATLASSERT(testPath
.IsDirectory());
1746 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1747 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1748 // Try a root UNC path
1749 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1750 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1756 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1757 ATLASSERT(!testPath
.IsAdminDir());
1758 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1759 ATLASSERT(!testPath
.IsAdminDir());
1760 testPath
.SetFromUnknown(_T("c:\\.svn"));
1761 ATLASSERT(testPath
.IsAdminDir());
1762 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1763 ATLASSERT(!testPath
.IsAdminDir());
1764 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1765 ATLASSERT(testPath
.IsAdminDir());
1767 CTGitPathList pathList
;
1768 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1769 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1770 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1771 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1772 pathList
.RemoveAdminPaths();
1773 ATLASSERT(pathList
.GetCount()==2);
1775 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1776 pathList
.RemoveAdminPaths();
1777 ATLASSERT(pathList
.GetCount()==1);
1782 CTGitPathList testList
;
1784 testPath
.SetFromUnknown(_T("c:/Z"));
1785 testList
.AddPath(testPath
);
1786 testPath
.SetFromUnknown(_T("c:/B"));
1787 testList
.AddPath(testPath
);
1788 testPath
.SetFromUnknown(_T("c:\\a"));
1789 testList
.AddPath(testPath
);
1790 testPath
.SetFromUnknown(_T("c:/Test"));
1791 testList
.AddPath(testPath
);
1793 testList
.SortByPathname();
1795 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1796 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1797 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1798 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1801 void RawAppendTest()
1803 CTGitPath
testPath(_T("c:/test/"));
1804 testPath
.AppendRawString(_T("/Hello"));
1805 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1807 testPath
.AppendRawString(_T("\\T2"));
1808 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1810 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1811 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1812 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1813 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1816 void PathAppendTest()
1818 CTGitPath
testPath(_T("c:/test/"));
1819 testPath
.AppendPathString(_T("/Hello"));
1820 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1822 testPath
.AppendPathString(_T("T2"));
1823 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1825 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1826 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1827 // You wouldn't want to do this in real life - you'd use append-raw
1828 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1829 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1832 void RemoveDuplicatesTest()
1835 list
.AddPath(CTGitPath(_T("Z")));
1836 list
.AddPath(CTGitPath(_T("A")));
1837 list
.AddPath(CTGitPath(_T("E")));
1838 list
.AddPath(CTGitPath(_T("E")));
1840 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1841 ATLASSERT(list
[2]==list
[3]);
1843 ATLASSERT(list
.GetCount() == 4);
1845 list
.RemoveDuplicates();
1847 ATLASSERT(list
.GetCount() == 3);
1849 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1850 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1851 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1854 void RemoveChildrenTest()
1857 list
.AddPath(CTGitPath(_T("c:\\test")));
1858 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1859 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1860 list
.AddPath(CTGitPath(_T("c:\\parent")));
1861 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1862 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1863 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1865 ATLASSERT(list
.GetCount() == 7);
1867 list
.RemoveChildren();
1869 ATLTRACE("count = %d\n", list
.GetCount());
1870 ATLASSERT(list
.GetCount() == 3);
1872 list
.SortByPathname();
1874 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1875 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1876 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1879 #if defined(_MFC_VER)
1880 void ListLoadingTest()
1882 TCHAR buf
[MAX_PATH
] = {0};
1883 GetCurrentDirectory(MAX_PATH
, buf
);
1884 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1885 CTGitPathList testList
;
1886 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1888 ATLASSERT(testList
.GetCount() == 3);
1889 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1890 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1891 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1893 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1895 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1896 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1897 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1901 void ContainingDirectoryTest()
1905 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1907 dir
= testPath
.GetContainingDirectory();
1908 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1909 dir
= dir
.GetContainingDirectory();
1910 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1911 dir
= dir
.GetContainingDirectory();
1912 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1913 dir
= dir
.GetContainingDirectory();
1914 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1915 dir
= dir
.GetContainingDirectory();
1916 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1917 dir
= dir
.GetContainingDirectory();
1918 ATLASSERT(dir
.IsEmpty());
1919 ATLASSERT(dir
.GetWinPathString() == _T(""));
1925 testPath
.SetFromWin(_T("c:\\windows"));
1926 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1927 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1928 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1929 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1930 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1933 void SubversionPathTest()
1936 testPath
.SetFromWin(_T("c:\\"));
1937 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1938 testPath
.SetFromWin(_T("c:\\folder"));
1939 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1940 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1941 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1942 testPath
.SetFromUnknown(_T("http://testing/"));
1943 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1944 testPath
.SetFromGit(NULL
);
1945 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1946 #if defined(_MFC_VER)
1947 testPath
.SetFromUnknown(_T("http://testing again"));
1948 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1949 testPath
.SetFromUnknown(_T("http://testing%20again"));
1950 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1951 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1952 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1956 void GetCommonRootTest()
1958 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1959 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1960 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1963 list
.AddPath(pathA
);
1964 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1965 list
.AddPath(pathB
);
1966 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1967 list
.AddPath(pathC
);
1968 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1971 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");
1972 list
.LoadFromAsteriskSeparatedString(sPathList
);
1973 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1976 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1977 list
.LoadFromAsteriskSeparatedString(sPathList
);
1978 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1981 sPathList
= _T("c:\\windows\\*c:\\windows");
1982 list
.LoadFromAsteriskSeparatedString(sPathList
);
1983 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1986 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1987 list
.LoadFromAsteriskSeparatedString(sPathList
);
1988 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1991 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1992 list
.LoadFromAsteriskSeparatedString(sPathList
);
1993 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1997 void ValidPathAndUrlTest()
2000 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
2001 ATLASSERT(testPath
.IsValidOnWindows());
2002 testPath
.SetFromWin(_T("c:\\"));
2003 ATLASSERT(testPath
.IsValidOnWindows());
2004 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
2005 ATLASSERT(testPath
.IsValidOnWindows());
2006 testPath
.SetFromWin(_T("c"));
2007 ATLASSERT(testPath
.IsValidOnWindows());
2008 testPath
.SetFromWin(_T("c:\\test folder\\file"));
2009 ATLASSERT(testPath
.IsValidOnWindows());
2010 testPath
.SetFromWin(_T("c:\\folder\\"));
2011 ATLASSERT(testPath
.IsValidOnWindows());
2012 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
2013 ATLASSERT(testPath
.IsValidOnWindows());
2014 testPath
.SetFromWin(_T("c:\\.svn"));
2015 ATLASSERT(testPath
.IsValidOnWindows());
2016 testPath
.SetFromWin(_T("c:\\com\\file"));
2017 ATLASSERT(testPath
.IsValidOnWindows());
2018 testPath
.SetFromWin(_T("c:\\test\\conf"));
2019 ATLASSERT(testPath
.IsValidOnWindows());
2020 testPath
.SetFromWin(_T("c:\\LPT"));
2021 ATLASSERT(testPath
.IsValidOnWindows());
2022 testPath
.SetFromWin(_T("c:\\test\\LPT"));
2023 ATLASSERT(testPath
.IsValidOnWindows());
2024 testPath
.SetFromWin(_T("c:\\com1test"));
2025 ATLASSERT(testPath
.IsValidOnWindows());
2026 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
2027 ATLASSERT(testPath
.IsValidOnWindows());
2029 testPath
.SetFromWin(_T("\\\\Share\\filename"));
2030 ATLASSERT(testPath
.IsValidOnWindows());
2031 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
2032 ATLASSERT(testPath
.IsValidOnWindows());
2033 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
2034 ATLASSERT(testPath
.IsValidOnWindows());
2036 // now the negative tests
2037 testPath
.SetFromWin(_T("c:\\test:folder"));
2038 ATLASSERT(!testPath
.IsValidOnWindows());
2039 testPath
.SetFromWin(_T("c:\\file<name"));
2040 ATLASSERT(!testPath
.IsValidOnWindows());
2041 testPath
.SetFromWin(_T("c:\\something*else"));
2042 ATLASSERT(!testPath
.IsValidOnWindows());
2043 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
2044 ATLASSERT(!testPath
.IsValidOnWindows());
2045 testPath
.SetFromWin(_T("c:\\ext.>ension"));
2046 ATLASSERT(!testPath
.IsValidOnWindows());
2047 testPath
.SetFromWin(_T("c:\\com1\\filename"));
2048 ATLASSERT(!testPath
.IsValidOnWindows());
2049 testPath
.SetFromWin(_T("c:\\com1"));
2050 ATLASSERT(!testPath
.IsValidOnWindows());
2051 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
2052 ATLASSERT(!testPath
.IsValidOnWindows());
2054 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
2055 ATLASSERT(!testPath
.IsValidOnWindows());
2056 testPath
.SetFromWin(_T("\\\\Share\\prn"));
2057 ATLASSERT(!testPath
.IsValidOnWindows());
2058 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
2059 ATLASSERT(!testPath
.IsValidOnWindows());
2061 // now come some URL tests
2062 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
2063 ATLASSERT(testPath
.IsValidOnWindows());
2064 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
2065 ATLASSERT(testPath
.IsValidOnWindows());
2066 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
2067 ATLASSERT(testPath
.IsValidOnWindows());
2068 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
2069 ATLASSERT(testPath
.IsValidOnWindows());
2070 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
2071 ATLASSERT(testPath
.IsValidOnWindows());
2072 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2073 ATLASSERT(testPath
.IsValidOnWindows());
2074 // and some negative URL tests
2075 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2076 ATLASSERT(!testPath
.IsValidOnWindows());
2077 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2078 ATLASSERT(!testPath
.IsValidOnWindows());
2079 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2080 ATLASSERT(!testPath
.IsValidOnWindows());
2081 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2082 ATLASSERT(!testPath
.IsValidOnWindows());
2083 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2084 ATLASSERT(!testPath
.IsValidOnWindows());
2088 } TGitPathTestobject
;
2092 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2095 for (i
= 0; i
< this->GetCount(); ++i
)
2097 if((*this)[i
].GetGitPathString() == path
)
2098 return (CTGitPath
*)&(*this)[i
];
2102 CString
CTGitPath::GetActionName(int action
)
2104 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2105 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2106 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2107 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2108 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2109 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2110 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2111 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2113 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2114 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2115 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2116 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2117 if(action
& CTGitPath::LOGACTIONS_COPY
)
2118 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2120 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2121 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2123 if (action
& CTGitPath::LOGACTIONS_ASSUMEVALID
)
2124 return MAKEINTRESOURCE(IDS_PATHACTIONS_ASSUMEUNCHANGED
);
2125 if (action
& CTGitPath::LOGACTIONS_SKIPWORKTREE
)
2126 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIPWORKTREE
);
2128 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2130 CString
CTGitPath::GetActionName()
2132 return GetActionName(m_Action
);
2135 int CTGitPathList::GetAction()