1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - TortoiseGit
4 // Copyright (C) 2010-2013 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"
44 CTGitPath::CTGitPath(void)
45 : m_bDirectoryKnown(false)
46 , m_bIsDirectory(false)
48 , m_bHasAdminDirKnown(false)
49 , m_bHasAdminDir(false)
50 , m_bIsValidOnWindowsKnown(false)
51 , m_bIsValidOnWindows(false)
52 , m_bIsReadOnly(false)
53 , m_bIsAdminDirKnown(false)
54 , m_bIsAdminDir(false)
56 , m_bExistsKnown(false)
57 , m_bLastWriteTimeKnown(0)
60 , m_bIsSpecialDirectoryKnown(false)
61 , m_bIsSpecialDirectory(false)
62 , m_bIsWCRootKnown(false)
72 CTGitPath::~CTGitPath(void)
75 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
76 CTGitPath::CTGitPath(const CString
& sUnknownPath
) :
77 m_bDirectoryKnown(false)
78 , m_bIsDirectory(false)
80 , m_bHasAdminDirKnown(false)
81 , m_bHasAdminDir(false)
82 , m_bIsValidOnWindowsKnown(false)
83 , m_bIsValidOnWindows(false)
84 , m_bIsReadOnly(false)
85 , m_bIsAdminDirKnown(false)
86 , m_bIsAdminDir(false)
88 , m_bExistsKnown(false)
89 , m_bLastWriteTimeKnown(0)
92 , m_bIsSpecialDirectoryKnown(false)
93 , m_bIsSpecialDirectory(false)
94 , 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 if (g_GitAdminDir
.HasAdminDir(GetWinPathString(), false))
827 status
|= ITEMIS_SUBMODULE
;
832 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
834 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
835 status
|= ITEMIS_BISECT
;
837 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
838 status
|= ITEMIS_STASH
;
840 if (PathFileExists(dotGitPath
+ _T("svn")))
841 status
|= ITEMIS_GITSVN
;
843 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
844 status
|= ITEMIS_SUBMODULECONTAINER
;
849 bool CTGitPath::HasStashDir() const
852 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
858 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
860 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
862 bool CTGitPath::HasGitSVNDir() const
865 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
871 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
873 return !!PathFileExists(dotGitPath
+ _T("svn"));
875 bool CTGitPath::IsBisectActive() const
878 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
884 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
886 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
888 bool CTGitPath::IsMergeActive() const
891 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
897 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
899 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
901 bool CTGitPath::HasRebaseApply() const
904 if(!g_GitAdminDir
.HasAdminDir(GetWinPathString(),&topdir
))
910 g_GitAdminDir
.GetAdminDirPath(topdir
, dotGitPath
);
912 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
915 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
917 if (m_bHasAdminDirKnown
)
920 *ProjectTopDir
= m_sProjectRoot
;
921 return m_bHasAdminDir
;
924 EnsureBackslashPathSet();
925 m_bHasAdminDir
= g_GitAdminDir
.HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
926 m_bHasAdminDirKnown
= true;
928 *ProjectTopDir
= m_sProjectRoot
;
929 return m_bHasAdminDir
;
932 bool CTGitPath::IsAdminDir() const
934 if (m_bIsAdminDirKnown
)
935 return m_bIsAdminDir
;
937 EnsureBackslashPathSet();
938 m_bIsAdminDir
= g_GitAdminDir
.IsAdminDirPath(m_sBackslashPath
);
939 m_bIsAdminDirKnown
= true;
940 return m_bIsAdminDir
;
943 bool CTGitPath::IsValidOnWindows() const
945 if (m_bIsValidOnWindowsKnown
)
946 return m_bIsValidOnWindows
;
948 m_bIsValidOnWindows
= false;
949 EnsureBackslashPathSet();
950 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
952 // the 'file://' URL is just a normal windows path:
953 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
955 sMatch
= sMatch
.Mid(7);
956 sMatch
.TrimLeft(_T("\\"));
957 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
961 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
966 tr1::wregex
rx(sPattern
, tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
969 wstring rmatch
= wstring((LPCTSTR
)sMatch
);
970 if (tr1::regex_match(rmatch
, match
, rx
))
972 if (wstring(match
[0]).compare(sMatch
)==0)
973 m_bIsValidOnWindows
= true;
975 if (m_bIsValidOnWindows
)
977 // now check for illegal filenames
978 tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase
| tr1::regex_constants::ECMAScript
);
979 rmatch
= m_sBackslashPath
;
980 if (tr1::regex_search(rmatch
, rx2
, tr1::regex_constants::match_default
))
981 m_bIsValidOnWindows
= false;
986 m_bIsValidOnWindowsKnown
= true;
987 return m_bIsValidOnWindows
;
990 //////////////////////////////////////////////////////////////////////////
992 CTGitPathList::CTGitPathList()
997 // A constructor which allows a path list to be easily built which one initial entry in
998 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
1001 AddPath(firstEntry
);
1003 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
1011 while(pos
>=0 && pos
<out
.size())
1016 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1018 path
.m_Action
=path
.ParserAction(out
[pos
]);
1019 one
.Tokenize(_T("\t"),tabstart
);
1022 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1028 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1032 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1036 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1040 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1044 path
.m_Stage
=_ttol(part
);
1046 this->AddPath(path
);
1048 pos
=out
.findNextString(pos
);
1052 int CTGitPathList::FillUnRev(unsigned int action
,CTGitPathList
*list
)
1062 count
=list
->GetCount();
1063 for (int i
= 0; i
< count
; ++i
)
1069 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1074 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1079 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1081 (*list
)[i
].GetWinPathString());
1086 g_Git
.Run(cmd
, &out
);
1090 while (pos
>= 0 && pos
< (int)out
.size())
1093 g_Git
.StringAppend(&one
, &out
[pos
], CP_UTF8
);
1096 //SetFromGit will clear all status
1097 path
.SetFromGit(one
);
1098 path
.m_Action
=action
;
1101 pos
=out
.findNextString(pos
);
1107 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1115 while (pos
>= 0 && pos
< (int)log
.size())
1117 //one=log.Tokenize(_T("\n"),pos);
1125 if(log
[pos
+1] ==':')
1129 int end
=log
.find(0,pos
);
1132 int file1
=-1,file2
=-1;
1135 actionstart
=log
.find(' ',end
-6);
1142 file1
= log
.find(0,actionstart
);
1148 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1152 file1
= log
.find(0,file1
);
1166 g_Git
.StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1168 g_Git
.StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1170 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1174 GitPath
->ParserAction( log
[actionstart
] );
1178 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1179 GitPath
->m_Action
&= ~CTGitPath::LOGACTIONS_FORWORD
;
1181 m_Action
|=GitPath
->m_Action
;
1186 int ac
=path
.ParserAction(log
[actionstart
] );
1187 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1189 path
.SetFromGit(pathname1
,&pathname2
);
1191 //action must be set after setfromgit. SetFromGit will clear all status.
1208 tabstart
=log
.find('\t',pos
);
1212 g_Git
.StringAppend(&StatAdd
,&log
[pos
],CP_UTF8
);
1216 tabstart
=log
.find('\t',pos
);
1221 g_Git
.StringAppend(&StatDel
,&log
[pos
],CP_UTF8
);
1225 if(log
[pos
] == 0) //rename
1228 g_Git
.StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1229 int sec
=log
.find(0,pos
);
1233 g_Git
.StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1240 g_Git
.StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1242 path
.SetFromGit(file1
,&file2
);
1244 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1247 GitPath
->m_StatAdd
=StatAdd
;
1248 GitPath
->m_StatDel
=StatDel
;
1252 //path.SetFromGit(pathname);
1255 path
.m_StatAdd
=_T("0");
1256 path
.m_StatDel
=_T("0");
1257 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1261 path
.m_StatAdd
=StatAdd
;
1262 path
.m_StatDel
=StatDel
;
1263 path
.m_Action
|= CTGitPath::LOGACTIONS_FORWORD
;
1269 pos
=log
.findNextString(pos
);
1274 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1276 m_paths
.push_back(newPath
);
1277 m_commonBaseDirectory
.Reset();
1279 int CTGitPathList::GetCount() const
1281 return (int)m_paths
.size();
1283 void CTGitPathList::Clear()
1286 m_commonBaseDirectory
.Reset();
1289 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1291 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1292 return m_paths
[index
];
1295 bool CTGitPathList::AreAllPathsFiles() const
1297 // Look through the vector for any directories - if we find them, return false
1298 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1302 #if defined(_MFC_VER)
1304 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1310 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1312 // for every selected file/folder
1314 while (file
.ReadString(strLine
))
1316 path
.SetFromUnknown(strLine
);
1321 catch (CFileException
* pE
)
1323 TRACE("CFileException loading target file list\n");
1324 TCHAR error
[10000] = {0};
1325 pE
->GetErrorMessage(error
, 10000);
1326 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1333 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1339 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1340 PathVector::const_iterator it
;
1341 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1343 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1344 file
.Write(line
, line
.GetLength());
1350 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1351 PathVector::const_iterator it
;
1352 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1354 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1359 catch (CFileException
* pE
)
1361 TRACE("CFileException in writing temp file\n");
1369 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1375 temp
= sPathString
.Tokenize(_T("*"),pos
);
1380 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1384 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1387 PathVector::const_iterator it
;
1388 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1390 if (!sRet
.IsEmpty())
1392 sRet
+= it
->GetWinPathString();
1399 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1401 // Check if all the paths are files and in the same directory
1402 PathVector::const_iterator it
;
1403 m_commonBaseDirectory
.Reset();
1404 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1406 if(it
->IsDirectory())
1410 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1411 if(m_commonBaseDirectory
.IsEmpty())
1413 m_commonBaseDirectory
= baseDirectory
;
1415 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1418 m_commonBaseDirectory
.Reset();
1425 CTGitPath
CTGitPathList::GetCommonDirectory() const
1427 if (m_commonBaseDirectory
.IsEmpty())
1429 PathVector::const_iterator it
;
1430 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1432 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1433 if(m_commonBaseDirectory
.IsEmpty())
1435 m_commonBaseDirectory
= baseDirectory
;
1437 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1440 m_commonBaseDirectory
.Reset();
1445 // since we only checked strings, not paths,
1446 // we have to make sure now that we really return a *path* here
1447 PathVector::const_iterator iter
;
1448 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1450 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1452 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1456 return m_commonBaseDirectory
;
1459 CTGitPath
CTGitPathList::GetCommonRoot() const
1461 if (GetCount() == 0)
1464 if (GetCount() == 1)
1467 // first entry is common root for itself
1468 // (add trailing '\\' to detect partial matches of the last path element)
1469 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1470 int rootLength
= root
.GetLength();
1472 // determine common path string prefix
1473 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1475 CString path
= it
->GetWinPathString() + _T('\\');
1477 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1478 if (newLength
!= rootLength
)
1480 root
.Delete(newLength
, rootLength
);
1481 rootLength
= newLength
;
1485 // remove the last (partial) path element
1487 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1490 return CTGitPath(root
);
1493 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1495 std::sort(m_paths
.begin(), m_paths
.end());
1497 std::reverse(m_paths
.begin(), m_paths
.end());
1500 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1502 PathVector::const_iterator it
;
1503 SortByPathname(true); // nested ones first
1506 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1508 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1510 if (!it
->IsDirectory())
1511 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1513 sPaths
+= it
->GetWinPath();
1519 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1523 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1525 SHFILEOPSTRUCT shop
= {0};
1526 shop
.wFunc
= FO_DELETE
;
1528 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1530 shop
.fFlags
|= FOF_ALLOWUNDO
;
1531 const bool bRet
= (SHFileOperation(&shop
) == 0);
1535 void CTGitPathList::RemoveDuplicates()
1538 // Remove the duplicates
1539 // (Unique moves them to the end of the vector, then erase chops them off)
1540 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1543 void CTGitPathList::RemoveAdminPaths()
1545 PathVector::iterator it
;
1546 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1548 if (it
->IsAdminDir())
1551 it
= m_paths
.begin();
1558 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1560 PathVector::iterator it
;
1561 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1563 if (it
->IsEquivalentTo(path
))
1571 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1573 PathVector::iterator it
;
1574 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1576 if (it
->GetGitPathString()==path
.GetGitPathString())
1583 void CTGitPathList::RemoveChildren()
1586 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1589 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1591 if (list
.GetCount() != GetCount())
1593 for (int i
=0; i
<list
.GetCount(); ++i
)
1595 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1601 //////////////////////////////////////////////////////////////////////////
1603 apr_array_header_t
* CTGitPathList::MakePathArray (apr_pool_t
*pool
) const
1605 apr_array_header_t
*targets
= apr_array_make (pool
, GetCount(), sizeof(const char *));
1607 for (int nItem
= 0; nItem
< GetCount(); ++nItem
)
1609 const char * target
= m_paths
[nItem
].GetGitApiPath(pool
);
1610 (*((const char **) apr_array_push (targets
))) = target
;
1616 //////////////////////////////////////////////////////////////////////////
1620 // Some test cases for these classes
1621 static class CTGitPathTests
1627 pool
= svn_pool_create(NULL
);
1633 RemoveDuplicatesTest();
1634 RemoveChildrenTest();
1635 ContainingDirectoryTest();
1637 SubversionPathTest();
1638 GetCommonRootTest();
1639 #if defined(_MFC_VER)
1640 ValidPathAndUrlTest();
1647 // apr_pool_t * pool;
1648 void GetDirectoryTest()
1650 // Bit tricky, this test, because we need to know something about the file
1651 // layout on the machine which is running the test
1652 TCHAR winDir
[MAX_PATH
+1];
1653 GetWindowsDirectory(winDir
, MAX_PATH
);
1654 CString
sWinDir(winDir
);
1657 // This is a file which we know will always be there
1658 testPath
.SetFromUnknown(sWinDir
+ _T("\\win.ini"));
1659 ATLASSERT(!testPath
.IsDirectory());
1660 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1661 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() == sWinDir
);
1663 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1664 // but we know it must be different to the directory itself
1665 testPath
.SetFromUnknown(sWinDir
);
1666 ATLASSERT(testPath
.IsDirectory());
1667 ATLASSERT(testPath
.GetDirectory().GetWinPathString() == sWinDir
);
1668 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString() != sWinDir
);
1669 ATLASSERT(testPath
.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir
.GetLength());
1672 testPath
.SetFromUnknown(_T("C:\\"));
1673 ATLASSERT(testPath
.IsDirectory());
1674 ATLASSERT(testPath
.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1675 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1676 // Try a root UNC path
1677 testPath
.SetFromUnknown(_T("\\MYSTATION"));
1678 ATLASSERT(testPath
.GetContainingDirectory().IsEmpty());
1684 testPath
.SetFromUnknown(_T("c:\\.svndir"));
1685 ATLASSERT(!testPath
.IsAdminDir());
1686 testPath
.SetFromUnknown(_T("c:\\test.svn"));
1687 ATLASSERT(!testPath
.IsAdminDir());
1688 testPath
.SetFromUnknown(_T("c:\\.svn"));
1689 ATLASSERT(testPath
.IsAdminDir());
1690 testPath
.SetFromUnknown(_T("c:\\.svndir\\test"));
1691 ATLASSERT(!testPath
.IsAdminDir());
1692 testPath
.SetFromUnknown(_T("c:\\.svn\\test"));
1693 ATLASSERT(testPath
.IsAdminDir());
1695 CTGitPathList pathList
;
1696 pathList
.AddPath(CTGitPath(_T("c:\\.svndir")));
1697 pathList
.AddPath(CTGitPath(_T("c:\\.svn")));
1698 pathList
.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1699 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1700 pathList
.RemoveAdminPaths();
1701 ATLASSERT(pathList
.GetCount()==2);
1703 pathList
.AddPath(CTGitPath(_T("c:\\test")));
1704 pathList
.RemoveAdminPaths();
1705 ATLASSERT(pathList
.GetCount()==1);
1710 CTGitPathList testList
;
1712 testPath
.SetFromUnknown(_T("c:/Z"));
1713 testList
.AddPath(testPath
);
1714 testPath
.SetFromUnknown(_T("c:/B"));
1715 testList
.AddPath(testPath
);
1716 testPath
.SetFromUnknown(_T("c:\\a"));
1717 testList
.AddPath(testPath
);
1718 testPath
.SetFromUnknown(_T("c:/Test"));
1719 testList
.AddPath(testPath
);
1721 testList
.SortByPathname();
1723 ATLASSERT(testList
[0].GetWinPathString() == _T("c:\\a"));
1724 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\B"));
1725 ATLASSERT(testList
[2].GetWinPathString() == _T("c:\\Test"));
1726 ATLASSERT(testList
[3].GetWinPathString() == _T("c:\\Z"));
1729 void RawAppendTest()
1731 CTGitPath
testPath(_T("c:/test/"));
1732 testPath
.AppendRawString(_T("/Hello"));
1733 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1735 testPath
.AppendRawString(_T("\\T2"));
1736 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1738 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1739 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1740 testBasePath
.AppendRawString(testFilePath
.GetFileExtension());
1741 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1744 void PathAppendTest()
1746 CTGitPath
testPath(_T("c:/test/"));
1747 testPath
.AppendPathString(_T("/Hello"));
1748 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello"));
1750 testPath
.AppendPathString(_T("T2"));
1751 ATLASSERT(testPath
.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1753 CTGitPath
testFilePath(_T("C:\\windows\\win.ini"));
1754 CTGitPath
testBasePath(_T("c:/temp/myfile.txt"));
1755 // You wouldn't want to do this in real life - you'd use append-raw
1756 testBasePath
.AppendPathString(testFilePath
.GetFileExtension());
1757 ATLASSERT(testBasePath
.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1760 void RemoveDuplicatesTest()
1763 list
.AddPath(CTGitPath(_T("Z")));
1764 list
.AddPath(CTGitPath(_T("A")));
1765 list
.AddPath(CTGitPath(_T("E")));
1766 list
.AddPath(CTGitPath(_T("E")));
1768 ATLASSERT(list
[2].IsEquivalentTo(list
[3]));
1769 ATLASSERT(list
[2]==list
[3]);
1771 ATLASSERT(list
.GetCount() == 4);
1773 list
.RemoveDuplicates();
1775 ATLASSERT(list
.GetCount() == 3);
1777 ATLASSERT(list
[0].GetWinPathString() == _T("A"));
1778 ATLASSERT(list
[1].GetWinPathString().Compare(_T("E")) == 0);
1779 ATLASSERT(list
[2].GetWinPathString() == _T("Z"));
1782 void RemoveChildrenTest()
1785 list
.AddPath(CTGitPath(_T("c:\\test")));
1786 list
.AddPath(CTGitPath(_T("c:\\test\\file")));
1787 list
.AddPath(CTGitPath(_T("c:\\testfile")));
1788 list
.AddPath(CTGitPath(_T("c:\\parent")));
1789 list
.AddPath(CTGitPath(_T("c:\\parent\\child")));
1790 list
.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1791 list
.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1793 ATLASSERT(list
.GetCount() == 7);
1795 list
.RemoveChildren();
1797 ATLTRACE("count = %d\n", list
.GetCount());
1798 ATLASSERT(list
.GetCount() == 3);
1800 list
.SortByPathname();
1802 ATLASSERT(list
[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1803 ATLASSERT(list
[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1804 ATLASSERT(list
[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1807 #if defined(_MFC_VER)
1808 void ListLoadingTest()
1810 TCHAR buf
[MAX_PATH
];
1811 GetCurrentDirectory(MAX_PATH
, buf
);
1812 CString
sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1813 CTGitPathList testList
;
1814 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1816 ATLASSERT(testList
.GetCount() == 3);
1817 ATLASSERT(testList
[0].GetWinPathString() == CString(buf
) + _T("\\Path1"));
1818 ATLASSERT(testList
[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1819 ATLASSERT(testList
[2].GetWinPathString() == _T("\\funnypath"));
1821 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T(""));
1823 sPathList
= _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1824 testList
.LoadFromAsteriskSeparatedString(sPathList
);
1825 ATLASSERT(testList
.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1829 void ContainingDirectoryTest()
1833 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1835 dir
= testPath
.GetContainingDirectory();
1836 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1837 dir
= dir
.GetContainingDirectory();
1838 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b\\c"));
1839 dir
= dir
.GetContainingDirectory();
1840 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a\\b"));
1841 dir
= dir
.GetContainingDirectory();
1842 ATLASSERT(dir
.GetWinPathString() == _T("c:\\a"));
1843 dir
= dir
.GetContainingDirectory();
1844 ATLASSERT(dir
.GetWinPathString() == _T("c:\\"));
1845 dir
= dir
.GetContainingDirectory();
1846 ATLASSERT(dir
.IsEmpty());
1847 ATLASSERT(dir
.GetWinPathString() == _T(""));
1853 testPath
.SetFromWin(_T("c:\\windows"));
1854 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1855 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1856 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1857 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1858 ATLASSERT(testPath
.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1861 void SubversionPathTest()
1864 testPath
.SetFromWin(_T("c:\\"));
1865 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:") == 0);
1866 testPath
.SetFromWin(_T("c:\\folder"));
1867 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/folder") == 0);
1868 testPath
.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1869 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "c:/a/b/c/d/e") == 0);
1870 testPath
.SetFromUnknown(_T("http://testing/"));
1871 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing") == 0);
1872 testPath
.SetFromGit(NULL
);
1873 ATLASSERT(strlen(testPath
.GetGitApiPath(pool
))==0);
1874 #if defined(_MFC_VER)
1875 testPath
.SetFromUnknown(_T("http://testing again"));
1876 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1877 testPath
.SetFromUnknown(_T("http://testing%20again"));
1878 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20again") == 0);
1879 testPath
.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1880 ATLASSERT(strcmp(testPath
.GetGitApiPath(pool
), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1884 void GetCommonRootTest()
1886 CTGitPath
pathA (_T("C:\\Development\\LogDlg.cpp"));
1887 CTGitPath
pathB (_T("C:\\Development\\LogDlg.h"));
1888 CTGitPath
pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1891 list
.AddPath(pathA
);
1892 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1893 list
.AddPath(pathB
);
1894 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1895 list
.AddPath(pathC
);
1896 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1899 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");
1900 list
.LoadFromAsteriskSeparatedString(sPathList
);
1901 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1904 sPathList
= _T("c:\\windows\\explorer.exe*c:\\windows");
1905 list
.LoadFromAsteriskSeparatedString(sPathList
);
1906 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1909 sPathList
= _T("c:\\windows\\*c:\\windows");
1910 list
.LoadFromAsteriskSeparatedString(sPathList
);
1911 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1914 sPathList
= _T("c:\\windows\\system32*c:\\windows\\system");
1915 list
.LoadFromAsteriskSeparatedString(sPathList
);
1916 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1919 sPathList
= _T("c:\\windowsdummy*c:\\windows");
1920 list
.LoadFromAsteriskSeparatedString(sPathList
);
1921 ATLASSERT(list
.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1925 void ValidPathAndUrlTest()
1928 testPath
.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1929 ATLASSERT(testPath
.IsValidOnWindows());
1930 testPath
.SetFromWin(_T("c:\\"));
1931 ATLASSERT(testPath
.IsValidOnWindows());
1932 testPath
.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1933 ATLASSERT(testPath
.IsValidOnWindows());
1934 testPath
.SetFromWin(_T("c"));
1935 ATLASSERT(testPath
.IsValidOnWindows());
1936 testPath
.SetFromWin(_T("c:\\test folder\\file"));
1937 ATLASSERT(testPath
.IsValidOnWindows());
1938 testPath
.SetFromWin(_T("c:\\folder\\"));
1939 ATLASSERT(testPath
.IsValidOnWindows());
1940 testPath
.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1941 ATLASSERT(testPath
.IsValidOnWindows());
1942 testPath
.SetFromWin(_T("c:\\.svn"));
1943 ATLASSERT(testPath
.IsValidOnWindows());
1944 testPath
.SetFromWin(_T("c:\\com\\file"));
1945 ATLASSERT(testPath
.IsValidOnWindows());
1946 testPath
.SetFromWin(_T("c:\\test\\conf"));
1947 ATLASSERT(testPath
.IsValidOnWindows());
1948 testPath
.SetFromWin(_T("c:\\LPT"));
1949 ATLASSERT(testPath
.IsValidOnWindows());
1950 testPath
.SetFromWin(_T("c:\\test\\LPT"));
1951 ATLASSERT(testPath
.IsValidOnWindows());
1952 testPath
.SetFromWin(_T("c:\\com1test"));
1953 ATLASSERT(testPath
.IsValidOnWindows());
1954 testPath
.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1955 ATLASSERT(testPath
.IsValidOnWindows());
1957 testPath
.SetFromWin(_T("\\\\Share\\filename"));
1958 ATLASSERT(testPath
.IsValidOnWindows());
1959 testPath
.SetFromWin(_T("\\\\Share\\filename.extension"));
1960 ATLASSERT(testPath
.IsValidOnWindows());
1961 testPath
.SetFromWin(_T("\\\\Share\\.svn"));
1962 ATLASSERT(testPath
.IsValidOnWindows());
1964 // now the negative tests
1965 testPath
.SetFromWin(_T("c:\\test:folder"));
1966 ATLASSERT(!testPath
.IsValidOnWindows());
1967 testPath
.SetFromWin(_T("c:\\file<name"));
1968 ATLASSERT(!testPath
.IsValidOnWindows());
1969 testPath
.SetFromWin(_T("c:\\something*else"));
1970 ATLASSERT(!testPath
.IsValidOnWindows());
1971 testPath
.SetFromWin(_T("c:\\folder\\file?nofile"));
1972 ATLASSERT(!testPath
.IsValidOnWindows());
1973 testPath
.SetFromWin(_T("c:\\ext.>ension"));
1974 ATLASSERT(!testPath
.IsValidOnWindows());
1975 testPath
.SetFromWin(_T("c:\\com1\\filename"));
1976 ATLASSERT(!testPath
.IsValidOnWindows());
1977 testPath
.SetFromWin(_T("c:\\com1"));
1978 ATLASSERT(!testPath
.IsValidOnWindows());
1979 testPath
.SetFromWin(_T("c:\\com1\\AuX"));
1980 ATLASSERT(!testPath
.IsValidOnWindows());
1982 testPath
.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1983 ATLASSERT(!testPath
.IsValidOnWindows());
1984 testPath
.SetFromWin(_T("\\\\Share\\prn"));
1985 ATLASSERT(!testPath
.IsValidOnWindows());
1986 testPath
.SetFromWin(_T("\\\\Share\\NUL"));
1987 ATLASSERT(!testPath
.IsValidOnWindows());
1989 // now come some URL tests
1990 testPath
.SetFromGit(_T("http://myserver.com/repos/trunk"));
1991 ATLASSERT(testPath
.IsValidOnWindows());
1992 testPath
.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1993 ATLASSERT(testPath
.IsValidOnWindows());
1994 testPath
.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1995 ATLASSERT(testPath
.IsValidOnWindows());
1996 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1997 ATLASSERT(testPath
.IsValidOnWindows());
1998 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk"));
1999 ATLASSERT(testPath
.IsValidOnWindows());
2000 testPath
.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
2001 ATLASSERT(testPath
.IsValidOnWindows());
2002 // and some negative URL tests
2003 testPath
.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
2004 ATLASSERT(!testPath
.IsValidOnWindows());
2005 testPath
.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
2006 ATLASSERT(!testPath
.IsValidOnWindows());
2007 testPath
.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
2008 ATLASSERT(!testPath
.IsValidOnWindows());
2009 testPath
.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2010 ATLASSERT(!testPath
.IsValidOnWindows());
2011 testPath
.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2012 ATLASSERT(!testPath
.IsValidOnWindows());
2016 } TGitPathTestobject
;
2020 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
2023 for (i
= 0; i
< this->GetCount(); ++i
)
2025 if((*this)[i
].GetGitPathString() == path
)
2026 return (CTGitPath
*)&(*this)[i
];
2030 CString
CTGitPath::GetActionName(int action
)
2032 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
2033 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
2034 if(action
& CTGitPath::LOGACTIONS_ADDED
)
2035 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
2036 if(action
& CTGitPath::LOGACTIONS_DELETED
)
2037 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
2038 if(action
& CTGitPath::LOGACTIONS_MERGED
)
2039 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
2041 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
2042 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
2043 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
2044 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
2045 if(action
& CTGitPath::LOGACTIONS_COPY
)
2046 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
2048 if(action
& CTGitPath::LOGACTIONS_FORWORD
)
2049 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD
);
2051 if(action
& CTGitPath::LOGACTIONS_REBASE_EDIT
)
2052 return MAKEINTRESOURCE(IDS_PATHACTIONS_EDIT
);
2053 if(action
& CTGitPath::LOGACTIONS_REBASE_SQUASH
)
2054 return MAKEINTRESOURCE(IDS_PATHACTIONS_SQUASH
);
2055 if(action
& CTGitPath::LOGACTIONS_REBASE_PICK
)
2056 return MAKEINTRESOURCE(IDS_PATHACTIONS_PICK
);
2057 if(action
& CTGitPath::LOGACTIONS_REBASE_SKIP
)
2058 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIP
);
2060 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
2062 CString
CTGitPath::GetActionName()
2064 return GetActionName(m_Action
);
2067 int CTGitPathList::GetAction()