1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "UnicodeUtils.h"
23 #include "GitAdminDir.h"
24 #include "PathUtils.h"
27 #include "../TortoiseShell/Globals.h"
28 #include "StringUtils.h"
29 #include "../Resources/LoglistCommonResource.h"
34 CTGitPath::CTGitPath(void)
35 : m_bDirectoryKnown(false)
36 , m_bIsDirectory(false)
38 , m_bHasAdminDirKnown(false)
39 , m_bHasAdminDir(false)
40 , m_bIsValidOnWindowsKnown(false)
41 , m_bIsValidOnWindows(false)
42 , m_bIsReadOnly(false)
43 , m_bIsAdminDirKnown(false)
44 , m_bIsAdminDir(false)
46 , m_bExistsKnown(false)
47 , m_bLastWriteTimeKnown(0)
50 , m_bIsSpecialDirectoryKnown(false)
51 , m_bIsSpecialDirectory(false)
52 , m_bIsWCRootKnown(false)
62 CTGitPath::~CTGitPath(void)
65 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
66 CTGitPath::CTGitPath(const CString
& sUnknownPath
) :
67 m_bDirectoryKnown(false)
68 , m_bIsDirectory(false)
70 , m_bHasAdminDirKnown(false)
71 , m_bHasAdminDir(false)
72 , m_bIsValidOnWindowsKnown(false)
73 , m_bIsValidOnWindows(false)
74 , m_bIsReadOnly(false)
75 , m_bIsAdminDirKnown(false)
76 , m_bIsAdminDir(false)
78 , m_bExistsKnown(false)
79 , m_bLastWriteTimeKnown(0)
82 , m_bIsSpecialDirectoryKnown(false)
83 , m_bIsSpecialDirectory(false)
84 , m_bIsWCRootKnown(false)
89 SetFromUnknown(sUnknownPath
);
95 int CTGitPath::ParserAction(BYTE action
)
97 //action=action.TrimLeft();
98 //TCHAR c=action.GetAt(0);
100 m_Action
|= LOGACTIONS_MODIFIED
;
102 m_Action
|= LOGACTIONS_REPLACED
;
104 m_Action
|= LOGACTIONS_ADDED
;
106 m_Action
|= LOGACTIONS_DELETED
;
108 m_Action
|= LOGACTIONS_UNMERGED
;
110 m_Action
|= LOGACTIONS_DELETED
;
112 m_Action
|= LOGACTIONS_COPY
;
114 m_Action
|= LOGACTIONS_MODIFIED
;
118 void CTGitPath::SetFromGit(const char* pPath
)
123 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, NULL
, 0);
126 len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, m_sFwdslashPath
.GetBuffer(len
+1), len
+1);
127 m_sFwdslashPath
.ReleaseBuffer(len
-1);
129 SanitizeRootPath(m_sFwdslashPath
, true);
132 void CTGitPath::SetFromGit(const char* pPath
, bool bIsDirectory
)
135 m_bDirectoryKnown
= true;
136 m_bIsDirectory
= bIsDirectory
;
139 void CTGitPath::SetFromGit(const TCHAR
* pPath
, bool bIsDirectory
)
144 m_sFwdslashPath
= pPath
;
145 SanitizeRootPath(m_sFwdslashPath
, true);
147 m_bDirectoryKnown
= true;
148 m_bIsDirectory
= bIsDirectory
;
151 void CTGitPath::SetFromGit(const CString
& sPath
,CString
*oldpath
)
154 m_sFwdslashPath
= sPath
;
155 SanitizeRootPath(m_sFwdslashPath
, true);
157 m_sOldFwdslashPath
= *oldpath
;
160 void CTGitPath::SetFromWin(LPCTSTR pPath
)
163 m_sBackslashPath
= pPath
;
164 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
165 SanitizeRootPath(m_sBackslashPath
, false);
166 ATLASSERT(m_sBackslashPath
.Find('/')<0);
168 void CTGitPath::SetFromWin(const CString
& sPath
)
171 m_sBackslashPath
= sPath
;
172 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
173 SanitizeRootPath(m_sBackslashPath
, false);
175 void CTGitPath::SetFromWin(LPCTSTR pPath
, bool bIsDirectory
)
178 m_sBackslashPath
= pPath
;
179 m_bIsDirectory
= bIsDirectory
;
180 m_bDirectoryKnown
= true;
181 SanitizeRootPath(m_sBackslashPath
, false);
183 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
186 m_sBackslashPath
= sPath
;
187 m_bIsDirectory
= bIsDirectory
;
188 m_bDirectoryKnown
= true;
189 SanitizeRootPath(m_sBackslashPath
, false);
191 void CTGitPath::SetFromUnknown(const CString
& sPath
)
194 // Just set whichever path we think is most likely to be used
195 // GitAdminDir admin;
197 // if(admin.HasAdminDir(sPath,&p))
198 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
200 SetFwdslashPath(sPath
);
203 LPCTSTR
CTGitPath::GetWinPath() const
209 if(m_sBackslashPath
.IsEmpty())
211 SetBackslashPath(m_sFwdslashPath
);
213 return m_sBackslashPath
;
215 // This is a temporary function, to be used during the migration to
216 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
217 const CString
& CTGitPath::GetWinPathString() const
219 if(m_sBackslashPath
.IsEmpty())
221 SetBackslashPath(m_sFwdslashPath
);
223 return m_sBackslashPath
;
226 const CString
& CTGitPath::GetGitPathString() const
228 if(m_sFwdslashPath
.IsEmpty())
230 SetFwdslashPath(m_sBackslashPath
);
232 return m_sFwdslashPath
;
235 const CString
&CTGitPath::GetGitOldPathString() const
237 return m_sOldFwdslashPath
;
240 const CString
& CTGitPath::GetUIPathString() const
242 if (m_sUIPath
.IsEmpty())
244 m_sUIPath
= GetWinPathString();
249 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
251 CString path
= sPath
;
252 path
.Replace('\\', '/');
254 // We don't leave a trailing /
256 path
.Replace(L
"//?/", L
"");
258 SanitizeRootPath(path
, true);
260 path
.Replace(_T("file:////"), _T("file://"));
261 m_sFwdslashPath
= path
;
263 m_sUTF8FwdslashPath
.Empty();
266 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
268 CString path
= sPath
;
269 path
.Replace('/', '\\');
270 path
.TrimRight('\\');
271 SanitizeRootPath(path
, false);
272 m_sBackslashPath
= path
;
275 void CTGitPath::SetUTF8FwdslashPath(const CString
& sPath
) const
277 m_sUTF8FwdslashPath
= CUnicodeUtils::GetUTF8(sPath
);
280 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
282 // Make sure to add the trailing slash to root paths such as 'C:'
283 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
285 sPath
+= (bIsForwardPath
) ? _T("/") : _T("\\");
289 bool CTGitPath::IsDirectory() const
291 if(!m_bDirectoryKnown
)
295 return m_bIsDirectory
;
298 bool CTGitPath::Exists() const
307 bool CTGitPath::Delete(bool bTrash
) const
309 EnsureBackslashPathSet();
310 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
314 if ((bTrash
)||(IsDirectory()))
316 std::unique_ptr
<TCHAR
[]> buf(new TCHAR
[m_sBackslashPath
.GetLength() + 2]);
317 _tcscpy_s(buf
.get(), m_sBackslashPath
.GetLength() + 2, m_sBackslashPath
);
318 buf
[m_sBackslashPath
.GetLength()] = 0;
319 buf
[m_sBackslashPath
.GetLength()+1] = 0;
320 bRet
= CTGitPathList::DeleteViaShell(buf
.get(), bTrash
);
324 bRet
= !!::DeleteFile(m_sBackslashPath
);
328 m_bExistsKnown
= true;
332 __int64
CTGitPath::GetLastWriteTime() const
334 if(!m_bLastWriteTimeKnown
)
338 return m_lastWriteTime
;
341 __int64
CTGitPath::GetFileSize() const
343 if(!m_bDirectoryKnown
)
350 bool CTGitPath::IsReadOnly() const
352 if(!m_bLastWriteTimeKnown
)
356 return m_bIsReadOnly
;
359 void CTGitPath::UpdateAttributes() const
361 EnsureBackslashPathSet();
362 WIN32_FILE_ATTRIBUTE_DATA attribs
;
363 if (m_sBackslashPath
.GetLength() >= 248)
364 m_sLongBackslashPath
= _T("\\\\?\\") + m_sBackslashPath
;
365 if(GetFileAttributesEx(m_sBackslashPath
.GetLength() >= 248 ? m_sLongBackslashPath
: m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
367 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
368 // don't cast directly to an __int64:
369 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284%28v=vs.85%29.aspx
370 // "Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value
371 // because it can cause alignment faults on 64-bit Windows."
372 m_lastWriteTime
= static_cast<__int64
>(attribs
.ftLastWriteTime
.dwHighDateTime
) << 32 | attribs
.ftLastWriteTime
.dwLowDateTime
;
379 m_fileSize
= ((INT64
)( (DWORD
)(attribs
.nFileSizeLow
) ) | ( (INT64
)( (DWORD
)(attribs
.nFileSizeHigh
) )<<32 ));
381 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
386 m_bIsDirectory
= false;
389 DWORD err
= GetLastError();
390 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
400 m_bDirectoryKnown
= true;
401 m_bLastWriteTimeKnown
= true;
402 m_bExistsKnown
= true;
405 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
409 if(GetWinPathString().Left(root
.GetWinPathString().GetLength()) == root
.GetWinPathString())
411 CString str
=GetWinPathString();
412 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
417 void CTGitPath::EnsureBackslashPathSet() const
419 if(m_sBackslashPath
.IsEmpty())
421 SetBackslashPath(m_sFwdslashPath
);
422 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
425 void CTGitPath::EnsureFwdslashPathSet() const
427 if(m_sFwdslashPath
.IsEmpty())
429 SetFwdslashPath(m_sBackslashPath
);
430 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
435 // Reset all the caches
436 void CTGitPath::Reset()
438 m_bDirectoryKnown
= false;
440 m_bLastWriteTimeKnown
= false;
441 m_bHasAdminDirKnown
= false;
442 m_bIsValidOnWindowsKnown
= false;
443 m_bIsAdminDirKnown
= false;
444 m_bExistsKnown
= false;
445 m_bIsSpecialDirectoryKnown
= false;
446 m_bIsSpecialDirectory
= false;
448 m_sBackslashPath
.Empty();
449 m_sFwdslashPath
.Empty();
450 m_sUTF8FwdslashPath
.Empty();
452 this->m_StatAdd
=_T("");
453 this->m_StatDel
=_T("");
455 ATLASSERT(IsEmpty());
458 CTGitPath
CTGitPath::GetDirectory() const
460 if ((IsDirectory())||(!Exists()))
464 return GetContainingDirectory();
467 CTGitPath
CTGitPath::GetContainingDirectory() const
469 EnsureBackslashPathSet();
471 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
472 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
474 // This is a root directory, which needs a trailing slash
476 if(sDirName
== m_sBackslashPath
)
478 // We were clearly provided with a root path to start with - we should return nothing now
482 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
484 // We have an UNC path and we already are the root
488 retVal
.SetFromWin(sDirName
);
492 CString
CTGitPath::GetRootPathString() const
494 EnsureBackslashPathSet();
495 CString workingPath
= m_sBackslashPath
;
496 LPTSTR pPath
= workingPath
.GetBuffer(MAX_PATH
); // MAX_PATH ok here.
497 ATLVERIFY(::PathStripToRoot(pPath
));
498 workingPath
.ReleaseBuffer();
503 CString
CTGitPath::GetFilename() const
505 //ATLASSERT(!IsDirectory());
506 return GetFileOrDirectoryName();
509 CString
CTGitPath::GetFileOrDirectoryName() const
511 EnsureBackslashPathSet();
512 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
515 CString
CTGitPath::GetUIFileOrDirectoryName() const
518 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
521 CString
CTGitPath::GetFileExtension() const
525 EnsureBackslashPathSet();
526 int dotPos
= m_sBackslashPath
.ReverseFind('.');
527 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
528 if (dotPos
> slashPos
)
529 return m_sBackslashPath
.Mid(dotPos
);
533 CString
CTGitPath::GetBaseFilename() const
536 CString filename
=GetFilename();
537 dot
= filename
.ReverseFind(_T('.'));
539 return filename
.Left(dot
);
544 bool CTGitPath::ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
)
546 int length
= sP1
.GetLength();
547 if(length
!= sP2
.GetLength())
552 // We work from the end of the strings, because path differences
553 // are more likely to occur at the far end of a string
554 LPCTSTR pP1Start
= sP1
;
555 LPCTSTR pP1
= pP1Start
+(length
-1);
556 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
559 if(_totlower(*pP1
--) != _totlower(*pP2
--))
567 bool CTGitPath::ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
)
569 int length
= sP1
.GetLength();
570 if(length
!= sP2
.GetLength())
575 // We work from the end of the strings, because path differences
576 // are more likely to occur at the far end of a string
577 LPCTSTR pP1Start
= sP1
;
578 LPCTSTR pP1
= pP1Start
+(length
-1);
579 LPCTSTR pP2
= ((LPCTSTR
)sP2
)+(length
-1);
582 if((*pP1
--) != (*pP2
--))
590 bool CTGitPath::IsEmpty() const
592 // Check the backward slash path first, since the chance that this
593 // one is set is higher. In case of a 'false' return value it's a little
595 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
598 // Test if both paths refer to the same item
599 // Ignores case and slash direction
600 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
602 // Try and find a slash direction which avoids having to convert
604 if(!m_sBackslashPath
.IsEmpty())
606 // *We've* got a \ path - make sure that the RHS also has a \ path
607 rhs
.EnsureBackslashPathSet();
608 return ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
612 // Assume we've got a fwdslash path and make sure that the RHS has one
613 rhs
.EnsureFwdslashPathSet();
614 return ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
618 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
620 // Try and find a slash direction which avoids having to convert
622 if(!m_sBackslashPath
.IsEmpty())
624 // *We've* got a \ path - make sure that the RHS also has a \ path
625 rhs
.EnsureBackslashPathSet();
626 return ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
630 // Assume we've got a fwdslash path and make sure that the RHS has one
631 rhs
.EnsureFwdslashPathSet();
632 return ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
636 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
638 possibleDescendant
.EnsureBackslashPathSet();
639 EnsureBackslashPathSet();
641 bool bPathStringsEqual
= ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
642 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
644 return bPathStringsEqual
;
647 return (bPathStringsEqual
&&
648 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
649 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
652 // Get a string representing the file path, optionally with a base
653 // section stripped off the front.
654 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = NULL*/) const
656 EnsureFwdslashPathSet();
657 if(pOptionalBasePath
!= NULL
)
659 // Find the length of the base-path without having to do an 'ensure' on it
660 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
662 // Now, chop that baseLength of the front of the path
663 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
665 return m_sFwdslashPath
;
668 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
670 left
.EnsureBackslashPathSet();
671 right
.EnsureBackslashPathSet();
672 return left
.m_sBackslashPath
.CompareNoCase(right
.m_sBackslashPath
);
675 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
677 return CTGitPath::Compare(left
, right
) < 0;
680 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
682 return left
.IsEquivalentTo(right
);
685 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
687 if (left
.IsAdminDir() && right
.IsAdminDir())
693 l
= l
.GetContainingDirectory();
694 } while(l
.HasAdminDir());
697 r
= r
.GetContainingDirectory();
698 } while(r
.HasAdminDir());
699 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
701 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
704 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
706 return parent
.IsAncestorOf(child
);
709 void CTGitPath::AppendRawString(const CString
& sAppend
)
711 EnsureFwdslashPathSet();
712 CString strCopy
= m_sFwdslashPath
+= sAppend
;
713 SetFromUnknown(strCopy
);
716 void CTGitPath::AppendPathString(const CString
& sAppend
)
718 EnsureBackslashPathSet();
719 CString
cleanAppend(sAppend
);
720 cleanAppend
.Replace('/', '\\');
721 cleanAppend
.TrimLeft('\\');
722 m_sBackslashPath
.TrimRight('\\');
723 CString strCopy
= m_sBackslashPath
+ _T("\\") + cleanAppend
;
727 bool CTGitPath::IsWCRoot() const
729 if (m_bIsWCRootKnown
)
732 m_bIsWCRootKnown
= true;
735 CString topDirectory
;
736 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
741 if (IsEquivalentToWithoutCase(topDirectory
))
749 bool CTGitPath::HasAdminDir() const
751 if (m_bHasAdminDirKnown
)
752 return m_bHasAdminDir
;
754 EnsureBackslashPathSet();
755 m_bHasAdminDir
= GitAdminDir::HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
756 m_bHasAdminDirKnown
= true;
757 return m_bHasAdminDir
;
760 bool CTGitPath::HasSubmodules() const
764 CString path
= m_sProjectRoot
;
765 path
+= _T("\\.gitmodules");
766 if( PathFileExists(path
) )
772 int CTGitPath::GetAdminDirMask() const
776 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
781 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
782 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
786 status
|= ITEMIS_FOLDERINGIT
;
789 status
|= ITEMIS_WCROOT
;
791 CString topProjectDir
;
792 if (GitAdminDir::HasAdminDir(GetWinPathString(), false, &topProjectDir
))
794 if (PathFileExists(topProjectDir
+ _T("\\.gitmodules")))
796 CAutoConfig
config(true);
797 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(topProjectDir
+ _T("\\.gitmodules")), GIT_CONFIG_LEVEL_APP
, FALSE
);
798 CString relativePath
= GetWinPathString().Mid(topProjectDir
.GetLength());
799 relativePath
.Replace(_T("\\"), _T("/"));
800 relativePath
.Trim(_T("/"));
801 CStringA submodulePath
= CUnicodeUtils::GetUTF8(relativePath
);
802 if (git_config_foreach_match(config
, "submodule\\..*\\.path",
803 [](const git_config_entry
*entry
, void *data
) { return entry
->value
== *(CStringA
*)data
? GIT_EUSER
: 0; }, &submodulePath
) == GIT_EUSER
)
804 status
|= ITEMIS_SUBMODULE
;
811 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
813 if (PathFileExists(dotGitPath
+ _T("BISECT_START")))
814 status
|= ITEMIS_BISECT
;
816 if (PathFileExists(dotGitPath
+ _T("MERGE_HEAD")))
817 status
|= ITEMIS_MERGEACTIVE
;
819 if (PathFileExists(dotGitPath
+ _T("refs\\stash")))
820 status
|= ITEMIS_STASH
;
822 if (PathFileExists(dotGitPath
+ _T("svn")))
823 status
|= ITEMIS_GITSVN
;
825 if (PathFileExists(topdir
+ _T("\\.gitmodules")))
826 status
|= ITEMIS_SUBMODULECONTAINER
;
831 bool CTGitPath::HasStashDir() const
834 if(!GitAdminDir::HasAdminDir(GetWinPathString(),&topdir
))
840 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
842 return !!PathFileExists(dotGitPath
+ _T("\\refs\\stash"));
844 bool CTGitPath::HasGitSVNDir() const
847 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
853 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
855 return !!PathFileExists(dotGitPath
+ _T("svn"));
857 bool CTGitPath::IsBisectActive() const
860 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
866 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
868 return !!PathFileExists(dotGitPath
+ _T("BISECT_START"));
870 bool CTGitPath::IsMergeActive() const
873 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
879 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
881 return !!PathFileExists(dotGitPath
+ _T("MERGE_HEAD"));
883 bool CTGitPath::HasRebaseApply() const
886 if (!GitAdminDir::HasAdminDir(GetWinPathString(), &topdir
))
892 GitAdminDir::GetAdminDirPath(topdir
, dotGitPath
);
894 return !!PathFileExists(dotGitPath
+ _T("rebase-apply"));
897 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
899 if (m_bHasAdminDirKnown
)
902 *ProjectTopDir
= m_sProjectRoot
;
903 return m_bHasAdminDir
;
906 EnsureBackslashPathSet();
907 m_bHasAdminDir
= GitAdminDir::HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
908 m_bHasAdminDirKnown
= true;
910 *ProjectTopDir
= m_sProjectRoot
;
911 return m_bHasAdminDir
;
914 bool CTGitPath::IsAdminDir() const
916 if (m_bIsAdminDirKnown
)
917 return m_bIsAdminDir
;
919 EnsureBackslashPathSet();
920 m_bIsAdminDir
= GitAdminDir::IsAdminDirPath(m_sBackslashPath
);
921 m_bIsAdminDirKnown
= true;
922 return m_bIsAdminDir
;
925 bool CTGitPath::IsValidOnWindows() const
927 if (m_bIsValidOnWindowsKnown
)
928 return m_bIsValidOnWindows
;
930 m_bIsValidOnWindows
= false;
931 EnsureBackslashPathSet();
932 CString sMatch
= m_sBackslashPath
+ _T("\r\n");
933 std::wstring sPattern
;
934 // the 'file://' URL is just a normal windows path:
935 if (sMatch
.Left(7).CompareNoCase(_T("file:\\\\"))==0)
937 sMatch
= sMatch
.Mid(7);
938 sMatch
.TrimLeft(_T("\\"));
939 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
943 sPattern
= _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
948 std::tr1::wregex
rx(sPattern
, std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
949 std::tr1::wsmatch match
;
951 std::wstring rmatch
= std::wstring((LPCTSTR
)sMatch
);
952 if (std::tr1::regex_match(rmatch
, match
, rx
))
954 if (std::wstring(match
[0]).compare(sMatch
)==0)
955 m_bIsValidOnWindows
= true;
957 if (m_bIsValidOnWindows
)
959 // now check for illegal filenames
960 std::tr1::wregex
rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
961 rmatch
= m_sBackslashPath
;
962 if (std::tr1::regex_search(rmatch
, rx2
, std::tr1::regex_constants::match_default
))
963 m_bIsValidOnWindows
= false;
966 catch (std::exception
) {}
968 m_bIsValidOnWindowsKnown
= true;
969 return m_bIsValidOnWindows
;
972 //////////////////////////////////////////////////////////////////////////
974 CTGitPathList::CTGitPathList()
979 // A constructor which allows a path list to be easily built which one initial entry in
980 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
985 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
993 while (pos
>= 0 && pos
< (int)out
.size())
998 CGit::StringAppend(&one
, &out
[pos
], CP_UTF8
);
1000 // m_Action is never used and propably never worked (needs to be set after path.SetFromGit)
1001 // also dropped LOGACTIONS_CACHE for 'H'
1002 // path.m_Action=path.ParserAction(out[pos]);
1003 one
.Tokenize(_T("\t"),tabstart
);
1006 path
.SetFromGit(one
.Right(one
.GetLength()-tabstart
));
1012 part
=one
.Tokenize(_T(" "),tabstart
); //Tag
1016 part
=one
.Tokenize(_T(" "),tabstart
); //Mode
1020 part
=one
.Tokenize(_T(" "),tabstart
); //Hash
1024 part
=one
.Tokenize(_T("\t"),tabstart
); //Stage
1028 path
.m_Stage
=_ttol(part
);
1030 this->AddPath(path
);
1032 pos
=out
.findNextString(pos
);
1036 int CTGitPathList::FillUnRev(unsigned int action
, CTGitPathList
*list
, CString
*err
)
1045 count
=list
->GetCount();
1046 for (int i
= 0; i
< count
; ++i
)
1052 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1057 cmd
=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1062 { cmd
.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1064 (*list
)[i
].GetWinPathString());
1067 BYTE_VECTOR out
, errb
;
1069 if (g_Git
.Run(cmd
, &out
, &errb
))
1072 CGit::StringAppend(err
, &errb
[0], CP_UTF8
, (int)errb
.size());
1078 while (pos
>= 0 && pos
< (int)out
.size())
1081 CGit::StringAppend(&one
, &out
[pos
], CP_UTF8
);
1084 //SetFromGit will clear all status
1085 path
.SetFromGit(one
);
1086 path
.m_Action
=action
;
1089 pos
=out
.findNextString(pos
);
1095 int CTGitPathList::FillBasedOnIndexFlags(unsigned short flag
, CTGitPathList
* list
/*nullptr*/)
1100 CAutoRepository
repository(g_Git
.GetGitRepository());
1105 if (git_repository_index(index
.GetPointer(), repository
))
1109 if (list
== nullptr)
1112 count
= list
->GetCount();
1113 for (int j
= 0; j
< count
; ++j
)
1115 for (size_t i
= 0, ecount
= git_index_entrycount(index
); i
< ecount
; ++i
)
1117 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
1119 if (!e
|| !((e
->flags
| e
->flags_extended
) & flag
) || !e
->path
)
1122 CString one
= CUnicodeUtils::GetUnicode(e
->path
);
1124 if (!(!list
|| (*list
)[j
].GetWinPathString().IsEmpty() || one
== (*list
)[j
].GetGitPathString() || (PathIsDirectory(g_Git
.CombinePath((*list
)[j
].GetWinPathString())) && one
.Find((*list
)[j
].GetGitPathString() + _T("/")) == 0)))
1127 //SetFromGit will clear all status
1128 path
.SetFromGit(one
);
1129 if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_SKIP_WORKTREE
)
1130 path
.m_Action
= CTGitPath::LOGACTIONS_SKIPWORKTREE
;
1131 else if ((e
->flags
| e
->flags_extended
) & GIT_IDXENTRY_VALID
)
1132 path
.m_Action
= CTGitPath::LOGACTIONS_ASSUMEVALID
;
1139 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1145 while (pos
>= 0 && pos
< (int)log
.size())
1154 if(log
[pos
+1] ==':')
1158 int end
=log
.find(0,pos
);
1160 int file1
=-1,file2
=-1;
1163 actionstart
=log
.find(' ',end
-6);
1170 file1
= log
.find(0,actionstart
);
1176 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1179 file1
= log
.find(0,file1
);
1193 CGit::StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1195 CGit::StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1197 CTGitPath
*GitPath
=LookForGitPath(pathname1
);
1201 GitPath
->ParserAction( log
[actionstart
] );
1205 GitPath
->m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1207 m_Action
|=GitPath
->m_Action
;
1212 int ac
=path
.ParserAction(log
[actionstart
] );
1213 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1215 path
.SetFromGit(pathname1
,&pathname2
);
1217 //action must be set after setfromgit. SetFromGit will clear all status.
1234 tabstart
=log
.find('\t',pos
);
1238 CGit::StringAppend(&StatAdd
, &log
[pos
], CP_UTF8
);
1242 tabstart
=log
.find('\t',pos
);
1247 CGit::StringAppend(&StatDel
, &log
[pos
], CP_UTF8
);
1251 if(log
[pos
] == 0) //rename
1254 CGit::StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1255 int sec
=log
.find(0,pos
);
1259 CGit::StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1266 CGit::StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1268 path
.SetFromGit(file1
,&file2
);
1270 CTGitPath
*GitPath
=LookForGitPath(path
.GetGitPathString());
1273 GitPath
->m_StatAdd
=StatAdd
;
1274 GitPath
->m_StatDel
=StatDel
;
1278 //path.SetFromGit(pathname);
1281 path
.m_StatAdd
=_T("0");
1282 path
.m_StatDel
=_T("0");
1283 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
;
1287 path
.m_StatAdd
=StatAdd
;
1288 path
.m_StatDel
=StatDel
;
1294 pos
=log
.findNextString(pos
);
1299 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1301 m_paths
.push_back(newPath
);
1302 m_commonBaseDirectory
.Reset();
1304 int CTGitPathList::GetCount() const
1306 return (int)m_paths
.size();
1308 bool CTGitPathList::IsEmpty() const
1310 return m_paths
.empty();
1312 void CTGitPathList::Clear()
1315 m_commonBaseDirectory
.Reset();
1318 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1320 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1321 return m_paths
[index
];
1324 bool CTGitPathList::AreAllPathsFiles() const
1326 // Look through the vector for any directories - if we find them, return false
1327 return std::find_if(m_paths
.begin(), m_paths
.end(), std::mem_fun_ref(&CTGitPath::IsDirectory
)) == m_paths
.end();
1331 #if defined(_MFC_VER)
1333 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1339 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1341 // for every selected file/folder
1343 while (file
.ReadString(strLine
))
1345 path
.SetFromUnknown(strLine
);
1350 catch (CFileException
* pE
)
1352 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException loading target file list\n");
1353 TCHAR error
[10000] = {0};
1354 pE
->GetErrorMessage(error
, 10000);
1355 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1362 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1368 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1369 PathVector::const_iterator it
;
1370 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1372 CStringA line
= CStringA(it
->GetGitPathString()) + '\n';
1373 file
.Write(line
, line
.GetLength());
1379 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1380 PathVector::const_iterator it
;
1381 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1383 file
.WriteString(it
->GetGitPathString()+_T("\n"));
1388 catch (CFileException
* pE
)
1390 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException in writing temp file\n");
1398 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1404 temp
= sPathString
.Tokenize(_T("*"),pos
);
1409 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1413 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1416 PathVector::const_iterator it
;
1417 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1419 if (!sRet
.IsEmpty())
1421 sRet
+= it
->GetWinPathString();
1428 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1430 // Check if all the paths are files and in the same directory
1431 PathVector::const_iterator it
;
1432 m_commonBaseDirectory
.Reset();
1433 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1435 if(it
->IsDirectory())
1439 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1440 if(m_commonBaseDirectory
.IsEmpty())
1442 m_commonBaseDirectory
= baseDirectory
;
1444 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1447 m_commonBaseDirectory
.Reset();
1454 CTGitPath
CTGitPathList::GetCommonDirectory() const
1456 if (m_commonBaseDirectory
.IsEmpty())
1458 PathVector::const_iterator it
;
1459 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1461 const CTGitPath
& baseDirectory
= it
->GetDirectory();
1462 if(m_commonBaseDirectory
.IsEmpty())
1464 m_commonBaseDirectory
= baseDirectory
;
1466 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1469 m_commonBaseDirectory
.Reset();
1474 // since we only checked strings, not paths,
1475 // we have to make sure now that we really return a *path* here
1476 PathVector::const_iterator iter
;
1477 for(iter
= m_paths
.begin(); iter
!= m_paths
.end(); ++iter
)
1479 if (!m_commonBaseDirectory
.IsAncestorOf(*iter
))
1481 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1485 return m_commonBaseDirectory
;
1488 CTGitPath
CTGitPathList::GetCommonRoot() const
1493 if (GetCount() == 1)
1496 // first entry is common root for itself
1497 // (add trailing '\\' to detect partial matches of the last path element)
1498 CString root
= m_paths
[0].GetWinPathString() + _T('\\');
1499 int rootLength
= root
.GetLength();
1501 // determine common path string prefix
1502 for (PathVector::const_iterator it
= m_paths
.begin() + 1; it
!= m_paths
.end(); ++it
)
1504 CString path
= it
->GetWinPathString() + _T('\\');
1506 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1507 if (newLength
!= rootLength
)
1509 root
.Delete(newLength
, rootLength
);
1510 rootLength
= newLength
;
1514 // remove the last (partial) path element
1516 root
.Delete(root
.ReverseFind(_T('\\')), rootLength
);
1519 return CTGitPath(root
);
1522 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1524 std::sort(m_paths
.begin(), m_paths
.end());
1526 std::reverse(m_paths
.begin(), m_paths
.end());
1529 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
)
1531 PathVector::const_iterator it
;
1532 SortByPathname(true); // nested ones first
1535 for (it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1537 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1539 if (!it
->IsDirectory())
1540 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1542 sPaths
+= it
->GetWinPath();
1548 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
);
1552 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
)
1554 SHFILEOPSTRUCT shop
= {0};
1555 shop
.wFunc
= FO_DELETE
;
1557 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NOERRORUI
|FOF_SILENT
|FOF_NO_CONNECTED_ELEMENTS
;
1559 shop
.fFlags
|= FOF_ALLOWUNDO
;
1560 const bool bRet
= (SHFileOperation(&shop
) == 0);
1564 void CTGitPathList::RemoveDuplicates()
1567 // Remove the duplicates
1568 // (Unique moves them to the end of the vector, then erase chops them off)
1569 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1572 void CTGitPathList::RemoveAdminPaths()
1574 PathVector::iterator it
;
1575 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1577 if (it
->IsAdminDir())
1580 it
= m_paths
.begin();
1587 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1589 PathVector::iterator it
;
1590 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1592 if (it
->IsEquivalentTo(path
))
1600 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1602 PathVector::iterator it
;
1603 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1605 if (it
->GetGitPathString()==path
.GetGitPathString())
1612 void CTGitPathList::RemoveChildren()
1615 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1618 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1620 if (list
.GetCount() != GetCount())
1622 for (int i
=0; i
<list
.GetCount(); ++i
)
1624 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1630 CTGitPath
* CTGitPathList::LookForGitPath(CString path
)
1633 for (i
= 0; i
< this->GetCount(); ++i
)
1635 if((*this)[i
].GetGitPathString() == path
)
1636 return (CTGitPath
*)&(*this)[i
];
1640 CString
CTGitPath::GetActionName(int action
)
1642 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
1643 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
1644 if(action
& CTGitPath::LOGACTIONS_ADDED
)
1645 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
1646 if(action
& CTGitPath::LOGACTIONS_DELETED
)
1647 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
1648 if(action
& CTGitPath::LOGACTIONS_MERGED
)
1649 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
1651 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
1652 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
1653 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
1654 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
1655 if(action
& CTGitPath::LOGACTIONS_COPY
)
1656 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
1658 if (action
& CTGitPath::LOGACTIONS_ASSUMEVALID
)
1659 return MAKEINTRESOURCE(IDS_PATHACTIONS_ASSUMEUNCHANGED
);
1660 if (action
& CTGitPath::LOGACTIONS_SKIPWORKTREE
)
1661 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIPWORKTREE
);
1663 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
1665 CString
CTGitPath::GetActionName()
1667 return GetActionName(m_Action
);
1670 int CTGitPathList::GetAction()