1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2017 - 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 "SmartHandle.h"
30 #include "../Resources/LoglistCommonResource.h"
35 CTGitPath::CTGitPath(void)
36 : m_bDirectoryKnown(false)
37 , 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)
49 , m_bIsWCRootKnown(false)
59 CTGitPath::~CTGitPath(void)
62 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
63 CTGitPath::CTGitPath(const CString
& sUnknownPath
) : CTGitPath()
65 SetFromUnknown(sUnknownPath
);
68 CTGitPath::CTGitPath(const CString
& sUnknownPath
, bool bIsDirectory
) : CTGitPath(sUnknownPath
)
70 m_bDirectoryKnown
= true;
71 m_bIsDirectory
= bIsDirectory
;
74 int CTGitPath::ParserAction(BYTE action
)
76 //action=action.TrimLeft();
77 //TCHAR c=action.GetAt(0);
79 m_Action
|= LOGACTIONS_MODIFIED
;
81 m_Action
|= LOGACTIONS_REPLACED
;
83 m_Action
|= LOGACTIONS_ADDED
;
85 m_Action
|= LOGACTIONS_DELETED
;
87 m_Action
|= LOGACTIONS_UNMERGED
;
89 m_Action
|= LOGACTIONS_DELETED
;
91 m_Action
|= LOGACTIONS_COPY
;
93 m_Action
|= LOGACTIONS_MODIFIED
;
98 int CTGitPath::ParserAction(git_delta_t action
)
100 if (action
== GIT_DELTA_MODIFIED
)
101 m_Action
|= LOGACTIONS_MODIFIED
;
102 if (action
== GIT_DELTA_RENAMED
)
103 m_Action
|= LOGACTIONS_REPLACED
;
104 if (action
== GIT_DELTA_ADDED
)
105 m_Action
|= LOGACTIONS_ADDED
;
106 if (action
== GIT_DELTA_DELETED
)
107 m_Action
|= LOGACTIONS_DELETED
;
108 if (action
== GIT_DELTA_UNMODIFIED
)
109 m_Action
|= LOGACTIONS_UNMERGED
;
110 if (action
== GIT_DELTA_COPIED
)
111 m_Action
|= LOGACTIONS_COPY
;
112 if (action
== GIT_DELTA_TYPECHANGE
)
113 m_Action
|= LOGACTIONS_MODIFIED
;
118 void CTGitPath::SetFromGit(const char* pPath
)
123 int len
= MultiByteToWideChar(CP_UTF8
, 0, pPath
, -1, nullptr, 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
, int* bIsDirectory
)
154 m_sFwdslashPath
= sPath
;
155 SanitizeRootPath(m_sFwdslashPath
, true);
158 m_bDirectoryKnown
= true;
159 m_bIsDirectory
= *bIsDirectory
!= FALSE
;
162 m_sOldFwdslashPath
= *oldpath
;
165 void CTGitPath::SetFromWin(LPCTSTR pPath
)
168 m_sBackslashPath
= pPath
;
169 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
170 SanitizeRootPath(m_sBackslashPath
, false);
171 ATLASSERT(m_sBackslashPath
.Find('/')<0);
173 void CTGitPath::SetFromWin(const CString
& sPath
)
176 m_sBackslashPath
= sPath
;
177 m_sBackslashPath
.Replace(L
"\\\\?\\", L
"");
178 SanitizeRootPath(m_sBackslashPath
, false);
180 void CTGitPath::SetFromWin(LPCTSTR pPath
, bool bIsDirectory
)
183 m_sBackslashPath
= pPath
;
184 m_bIsDirectory
= bIsDirectory
;
185 m_bDirectoryKnown
= true;
186 SanitizeRootPath(m_sBackslashPath
, false);
188 void CTGitPath::SetFromWin(const CString
& sPath
, bool bIsDirectory
)
191 m_sBackslashPath
= sPath
;
192 m_bIsDirectory
= bIsDirectory
;
193 m_bDirectoryKnown
= true;
194 SanitizeRootPath(m_sBackslashPath
, false);
196 void CTGitPath::SetFromUnknown(const CString
& sPath
)
199 // Just set whichever path we think is most likely to be used
200 // GitAdminDir admin;
202 // if(admin.HasAdminDir(sPath,&p))
203 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
205 SetFwdslashPath(sPath
);
208 void CTGitPath::UpdateCase()
210 m_sBackslashPath
= CPathUtils::GetLongPathname(GetWinPathString());
211 CPathUtils::TrimTrailingPathDelimiter(m_sBackslashPath
);
212 SanitizeRootPath(m_sBackslashPath
, false);
213 SetFwdslashPath(m_sBackslashPath
);
216 LPCTSTR
CTGitPath::GetWinPath() const
220 if(m_sBackslashPath
.IsEmpty())
221 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())
229 SetBackslashPath(m_sFwdslashPath
);
230 return m_sBackslashPath
;
233 const CString
& CTGitPath::GetGitPathString() const
235 if(m_sFwdslashPath
.IsEmpty())
236 SetFwdslashPath(m_sBackslashPath
);
237 return m_sFwdslashPath
;
240 const CString
&CTGitPath::GetGitOldPathString() const
242 return m_sOldFwdslashPath
;
245 const CString
& CTGitPath::GetUIPathString() const
247 if (m_sUIPath
.IsEmpty())
248 m_sUIPath
= GetWinPathString();
252 void CTGitPath::SetFwdslashPath(const CString
& sPath
) const
254 CString path
= sPath
;
255 path
.Replace('\\', '/');
257 // We don't leave a trailing /
259 path
.Replace(L
"//?/", L
"");
261 SanitizeRootPath(path
, true);
263 path
.Replace(L
"file:////", L
"file://");
264 m_sFwdslashPath
= path
;
267 void CTGitPath::SetBackslashPath(const CString
& sPath
) const
269 CString path
= sPath
;
270 path
.Replace('/', '\\');
271 path
.TrimRight('\\');
272 SanitizeRootPath(path
, false);
273 m_sBackslashPath
= path
;
276 void CTGitPath::SanitizeRootPath(CString
& sPath
, bool bIsForwardPath
) const
278 // Make sure to add the trailing slash to root paths such as 'C:'
279 if (sPath
.GetLength() == 2 && sPath
[1] == ':')
280 sPath
+= (bIsForwardPath
) ? L
'/' : L
'\\';
283 bool CTGitPath::IsDirectory() const
285 if(!m_bDirectoryKnown
)
287 return m_bIsDirectory
;
290 bool CTGitPath::Exists() const
297 bool CTGitPath::Delete(bool bTrash
, bool bShowErrorUI
) const
299 EnsureBackslashPathSet();
300 ::SetFileAttributes(m_sBackslashPath
, FILE_ATTRIBUTE_NORMAL
);
304 if ((bTrash
)||(IsDirectory()))
306 auto buf
= std::make_unique
<TCHAR
[]>(m_sBackslashPath
.GetLength() + 2);
307 wcscpy_s(buf
.get(), m_sBackslashPath
.GetLength() + 2, m_sBackslashPath
);
308 buf
[m_sBackslashPath
.GetLength()] = L
'\0';
309 buf
[m_sBackslashPath
.GetLength() + 1] = L
'\0';
310 bRet
= CTGitPathList::DeleteViaShell(buf
.get(), bTrash
, bShowErrorUI
);
313 bRet
= !!::DeleteFile(m_sBackslashPath
);
316 m_bExistsKnown
= true;
320 __int64
CTGitPath::GetLastWriteTime() const
322 if(!m_bLastWriteTimeKnown
)
324 return m_lastWriteTime
;
327 __int64
CTGitPath::GetFileSize() const
329 if(!m_bDirectoryKnown
)
334 bool CTGitPath::IsReadOnly() const
336 if(!m_bLastWriteTimeKnown
)
338 return m_bIsReadOnly
;
341 void CTGitPath::UpdateAttributes() const
343 EnsureBackslashPathSet();
344 WIN32_FILE_ATTRIBUTE_DATA attribs
;
345 if (m_sBackslashPath
.IsEmpty())
346 m_sLongBackslashPath
= L
".";
347 else if (m_sBackslashPath
.GetLength() >= 248)
349 if (!PathIsRelative(m_sBackslashPath
))
350 m_sLongBackslashPath
= L
"\\\\?\\" + m_sBackslashPath
;
352 m_sLongBackslashPath
= L
"\\\\?\\" + g_Git
.CombinePath(m_sBackslashPath
);
354 if (GetFileAttributesEx(m_sBackslashPath
.IsEmpty() || m_sBackslashPath
.GetLength() >= 248 ? m_sLongBackslashPath
: m_sBackslashPath
, GetFileExInfoStandard
, &attribs
))
356 m_bIsDirectory
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
357 // don't cast directly to an __int64:
358 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284%28v=vs.85%29.aspx
359 // "Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value
360 // because it can cause alignment faults on 64-bit Windows."
361 m_lastWriteTime
= static_cast<__int64
>(attribs
.ftLastWriteTime
.dwHighDateTime
) << 32 | attribs
.ftLastWriteTime
.dwLowDateTime
;
365 m_fileSize
= ((INT64
)( (DWORD
)(attribs
.nFileSizeLow
) ) | ( (INT64
)( (DWORD
)(attribs
.nFileSizeHigh
) )<<32 ));
366 m_bIsReadOnly
= !!(attribs
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
);
371 m_bIsDirectory
= false;
374 DWORD err
= GetLastError();
375 if ((err
== ERROR_FILE_NOT_FOUND
)||(err
== ERROR_PATH_NOT_FOUND
)||(err
== ERROR_INVALID_NAME
))
383 m_bDirectoryKnown
= true;
384 m_bLastWriteTimeKnown
= true;
385 m_bExistsKnown
= true;
388 CTGitPath
CTGitPath::GetSubPath(const CTGitPath
&root
)
392 if (CStringUtils::StartsWith(GetWinPathString(), root
.GetWinPathString()))
394 CString str
=GetWinPathString();
395 path
.SetFromWin(str
.Right(str
.GetLength()-root
.GetWinPathString().GetLength()-1));
400 void CTGitPath::EnsureBackslashPathSet() const
402 if(m_sBackslashPath
.IsEmpty())
404 SetBackslashPath(m_sFwdslashPath
);
405 ATLASSERT(IsEmpty() || !m_sBackslashPath
.IsEmpty());
408 void CTGitPath::EnsureFwdslashPathSet() const
410 if(m_sFwdslashPath
.IsEmpty())
412 SetFwdslashPath(m_sBackslashPath
);
413 ATLASSERT(IsEmpty() || !m_sFwdslashPath
.IsEmpty());
418 // Reset all the caches
419 void CTGitPath::Reset()
421 m_bDirectoryKnown
= false;
422 m_bLastWriteTimeKnown
= false;
423 m_bHasAdminDirKnown
= false;
424 m_bIsValidOnWindowsKnown
= false;
425 m_bIsAdminDirKnown
= false;
426 m_bExistsKnown
= false;
428 m_sBackslashPath
.Empty();
429 m_sFwdslashPath
.Empty();
431 this->m_StatAdd
.Empty();
432 this->m_StatDel
.Empty();
434 ATLASSERT(IsEmpty());
437 CTGitPath
CTGitPath::GetDirectory() const
439 if ((IsDirectory())||(!Exists()))
441 return GetContainingDirectory();
444 CTGitPath
CTGitPath::GetContainingDirectory() const
446 EnsureBackslashPathSet();
448 CString sDirName
= m_sBackslashPath
.Left(m_sBackslashPath
.ReverseFind('\\'));
449 if(sDirName
.GetLength() == 2 && sDirName
[1] == ':')
451 // This is a root directory, which needs a trailing slash
453 if(sDirName
== m_sBackslashPath
)
455 // We were clearly provided with a root path to start with - we should return nothing now
459 if(sDirName
.GetLength() == 1 && sDirName
[0] == '\\')
461 // We have an UNC path and we already are the root
465 retVal
.SetFromWin(sDirName
);
469 CString
CTGitPath::GetRootPathString() const
471 EnsureBackslashPathSet();
472 CString workingPath
= m_sBackslashPath
;
473 ATLVERIFY(::PathStripToRoot(CStrBuf(workingPath
, MAX_PATH
))); // MAX_PATH ok here.
478 CString
CTGitPath::GetFilename() const
480 //ATLASSERT(!IsDirectory());
481 return GetFileOrDirectoryName();
484 CString
CTGitPath::GetFileOrDirectoryName() const
486 EnsureBackslashPathSet();
487 return m_sBackslashPath
.Mid(m_sBackslashPath
.ReverseFind('\\')+1);
490 CString
CTGitPath::GetUIFileOrDirectoryName() const
493 return m_sUIPath
.Mid(m_sUIPath
.ReverseFind('\\')+1);
496 CString
CTGitPath::GetFileExtension() const
500 EnsureBackslashPathSet();
501 int dotPos
= m_sBackslashPath
.ReverseFind('.');
502 int slashPos
= m_sBackslashPath
.ReverseFind('\\');
503 if (dotPos
> slashPos
)
504 return m_sBackslashPath
.Mid(dotPos
);
508 CString
CTGitPath::GetBaseFilename() const
511 CString filename
=GetFilename();
512 dot
= filename
.ReverseFind(L
'.');
514 filename
.Truncate(dot
);
518 bool CTGitPath::IsEmpty() const
520 // Check the backward slash path first, since the chance that this
521 // one is set is higher. In case of a 'false' return value it's a little
523 return m_sBackslashPath
.IsEmpty() && m_sFwdslashPath
.IsEmpty();
526 // Test if both paths refer to the same item
527 // Ignores case and slash direction
528 bool CTGitPath::IsEquivalentTo(const CTGitPath
& rhs
) const
530 // Try and find a slash direction which avoids having to convert
532 if(!m_sBackslashPath
.IsEmpty())
534 // *We've* got a \ path - make sure that the RHS also has a \ path
535 rhs
.EnsureBackslashPathSet();
536 return CPathUtils::ArePathStringsEqualWithCase(m_sBackslashPath
, rhs
.m_sBackslashPath
);
540 // Assume we've got a fwdslash path and make sure that the RHS has one
541 rhs
.EnsureFwdslashPathSet();
542 return CPathUtils::ArePathStringsEqualWithCase(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
546 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath
& rhs
) const
548 // Try and find a slash direction which avoids having to convert
550 if(!m_sBackslashPath
.IsEmpty())
552 // *We've* got a \ path - make sure that the RHS also has a \ path
553 rhs
.EnsureBackslashPathSet();
554 return CPathUtils::ArePathStringsEqual(m_sBackslashPath
, rhs
.m_sBackslashPath
);
558 // Assume we've got a fwdslash path and make sure that the RHS has one
559 rhs
.EnsureFwdslashPathSet();
560 return CPathUtils::ArePathStringsEqual(m_sFwdslashPath
, rhs
.m_sFwdslashPath
);
564 bool CTGitPath::IsAncestorOf(const CTGitPath
& possibleDescendant
) const
566 possibleDescendant
.EnsureBackslashPathSet();
567 EnsureBackslashPathSet();
569 bool bPathStringsEqual
= CPathUtils::ArePathStringsEqual(m_sBackslashPath
, possibleDescendant
.m_sBackslashPath
.Left(m_sBackslashPath
.GetLength()));
570 if (m_sBackslashPath
.GetLength() >= possibleDescendant
.GetWinPathString().GetLength())
572 return bPathStringsEqual
;
575 return (bPathStringsEqual
&&
576 ((possibleDescendant
.m_sBackslashPath
[m_sBackslashPath
.GetLength()] == '\\')||
577 (m_sBackslashPath
.GetLength()==3 && m_sBackslashPath
[1]==':')));
580 // Get a string representing the file path, optionally with a base
581 // section stripped off the front.
582 CString
CTGitPath::GetDisplayString(const CTGitPath
* pOptionalBasePath
/* = nullptr*/) const
584 EnsureFwdslashPathSet();
585 if (pOptionalBasePath
)
587 // Find the length of the base-path without having to do an 'ensure' on it
588 int baseLength
= max(pOptionalBasePath
->m_sBackslashPath
.GetLength(), pOptionalBasePath
->m_sFwdslashPath
.GetLength());
590 // Now, chop that baseLength of the front of the path
591 return m_sFwdslashPath
.Mid(baseLength
).TrimLeft('/');
593 return m_sFwdslashPath
;
596 int CTGitPath::Compare(const CTGitPath
& left
, const CTGitPath
& right
)
598 left
.EnsureBackslashPathSet();
599 right
.EnsureBackslashPathSet();
600 return CStringUtils::FastCompareNoCase(left
.m_sBackslashPath
, right
.m_sBackslashPath
);
603 bool operator<(const CTGitPath
& left
, const CTGitPath
& right
)
605 return CTGitPath::Compare(left
, right
) < 0;
608 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath
& left
, const CTGitPath
& right
)
610 return left
.IsEquivalentTo(right
);
613 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath
& left
, const CTGitPath
& right
)
615 if (left
.IsAdminDir() && right
.IsAdminDir())
621 l
= l
.GetContainingDirectory();
622 } while(l
.HasAdminDir());
625 r
= r
.GetContainingDirectory();
626 } while(r
.HasAdminDir());
627 return l
.GetContainingDirectory().IsEquivalentTo(r
.GetContainingDirectory());
629 return left
.GetDirectory().IsEquivalentTo(right
.GetDirectory());
632 bool CTGitPath::CheckChild(const CTGitPath
&parent
, const CTGitPath
& child
)
634 return parent
.IsAncestorOf(child
);
637 void CTGitPath::AppendRawString(const CString
& sAppend
)
639 EnsureFwdslashPathSet();
640 CString strCopy
= m_sFwdslashPath
+= sAppend
;
641 SetFromUnknown(strCopy
);
644 void CTGitPath::AppendPathString(const CString
& sAppend
)
646 EnsureBackslashPathSet();
647 CString
cleanAppend(sAppend
);
648 cleanAppend
.Replace(L
'/', L
'\\');
649 cleanAppend
.TrimLeft(L
'\\');
650 m_sBackslashPath
.TrimRight(L
'\\');
651 CString strCopy
= m_sBackslashPath
;
653 strCopy
+= cleanAppend
;
657 bool CTGitPath::IsWCRoot() const
659 if (m_bIsWCRootKnown
)
662 m_bIsWCRootKnown
= true;
665 CString topDirectory
;
666 if (!IsDirectory() || !HasAdminDir(&topDirectory
))
669 if (IsEquivalentToWithoutCase(topDirectory
))
675 bool CTGitPath::HasSubmodules() const
679 CString path
= m_sProjectRoot
;
680 path
+= L
"\\.gitmodules";
681 if( PathFileExists(path
) )
687 int CTGitPath::GetAdminDirMask() const
693 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
694 status
|= ITEMIS_INGIT
|ITEMIS_INVERSIONEDFOLDER
;
698 status
|= ITEMIS_FOLDERINGIT
;
701 status
|= ITEMIS_WCROOT
;
703 CString topProjectDir
;
704 if (GitAdminDir::HasAdminDir(GetWinPathString(), false, &topProjectDir
))
706 if (PathFileExists(topProjectDir
+ L
"\\.gitmodules"))
708 CAutoConfig
config(true);
709 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(topProjectDir
+ L
"\\.gitmodules"), GIT_CONFIG_LEVEL_APP
, FALSE
);
710 CString relativePath
= GetWinPathString().Mid(topProjectDir
.GetLength());
711 relativePath
.Replace(L
'\\', L
'/');
712 relativePath
.Trim(L
'/');
713 CStringA submodulePath
= CUnicodeUtils::GetUTF8(relativePath
);
714 if (git_config_foreach_match(config
, "submodule\\..*\\.path",
715 [](const git_config_entry
*entry
, void *data
) { return entry
->value
== *(CStringA
*)data
? GIT_EUSER
: 0; }, &submodulePath
) == GIT_EUSER
)
716 status
|= ITEMIS_SUBMODULE
;
724 GitAdminDir::GetAdminDirPath(m_sProjectRoot
, dotGitPath
, &isWorktree
);
725 if (HasStashDir(dotGitPath
))
726 status
|= ITEMIS_STASH
;
728 if (PathFileExists(dotGitPath
+ L
"svn"))
729 status
|= ITEMIS_GITSVN
;
734 GitAdminDir::GetWorktreeAdminDirPath(m_sProjectRoot
, dotGitPath
);
737 if (PathFileExists(dotGitPath
+ L
"BISECT_START"))
738 status
|= ITEMIS_BISECT
;
740 if (PathFileExists(dotGitPath
+ L
"MERGE_HEAD"))
741 status
|= ITEMIS_MERGEACTIVE
;
743 if (PathFileExists(m_sProjectRoot
+ L
"\\.gitmodules"))
744 status
|= ITEMIS_SUBMODULECONTAINER
;
749 bool CTGitPath::HasStashDir(const CString
& dotGitPath
) const
751 if (PathFileExists(dotGitPath
+ L
"refs\\stash"))
754 CAutoFile hfile
= CreateFile(dotGitPath
+ L
"packed-refs", GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
, nullptr, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, nullptr);
758 DWORD filesize
= ::GetFileSize(hfile
, nullptr);
759 if (filesize
== 0 || filesize
== INVALID_FILE_SIZE
)
763 auto buff
= std::make_unique
<char[]>(filesize
+ 1);
764 ReadFile(hfile
, buff
.get(), filesize
, &size
, nullptr);
765 buff
.get()[filesize
] = '\0';
767 if (size
!= filesize
)
770 for (DWORD i
= 0; i
< filesize
;)
772 if (buff
[i
] == '#' || buff
[i
] == '^')
774 while (buff
[i
] != '\n')
786 while (buff
[i
] != ' ')
797 if (i
<= filesize
- 10 && (buff
[i
+ 10] == '\n' || buff
[i
+ 10] == '\0') && !strncmp("refs/stash", buff
.get() + i
, 10))
799 while (buff
[i
] != '\n')
806 while (buff
[i
] == '\n')
816 bool CTGitPath::HasStashDir() const
822 GitAdminDir::GetAdminDirPath(m_sProjectRoot
, dotGitPath
);
824 return HasStashDir(dotGitPath
);
827 bool CTGitPath::HasGitSVNDir() const
833 GitAdminDir::GetAdminDirPath(m_sProjectRoot
, dotGitPath
);
835 return !!PathFileExists(dotGitPath
+ L
"svn");
837 bool CTGitPath::IsBisectActive() const
843 GitAdminDir::GetWorktreeAdminDirPath(m_sProjectRoot
, dotGitPath
);
845 return !!PathFileExists(dotGitPath
+ L
"BISECT_START");
847 bool CTGitPath::IsMergeActive() const
853 GitAdminDir::GetWorktreeAdminDirPath(m_sProjectRoot
, dotGitPath
);
855 return !!PathFileExists(dotGitPath
+ L
"MERGE_HEAD");
857 bool CTGitPath::HasRebaseApply() const
863 GitAdminDir::GetWorktreeAdminDirPath(m_sProjectRoot
, dotGitPath
);
865 return !!PathFileExists(dotGitPath
+ L
"rebase-apply");
868 bool CTGitPath::HasAdminDir(CString
*ProjectTopDir
) const
870 if (m_bHasAdminDirKnown
)
873 *ProjectTopDir
= m_sProjectRoot
;
874 return m_bHasAdminDir
;
877 EnsureBackslashPathSet();
878 bool isAdminDir
= false;
879 m_bHasAdminDir
= GitAdminDir::HasAdminDir(m_sBackslashPath
, IsDirectory(), &m_sProjectRoot
);
880 m_bHasAdminDirKnown
= true;
881 if ((m_bHasAdminDir
|| isAdminDir
) && !m_bIsAdminDirKnown
)
883 m_bIsAdminDir
= isAdminDir
;
884 m_bIsAdminDirKnown
= true;
887 *ProjectTopDir
= m_sProjectRoot
;
888 return m_bHasAdminDir
;
891 bool CTGitPath::IsAdminDir() const
893 if (m_bIsAdminDirKnown
)
894 return m_bIsAdminDir
;
896 EnsureBackslashPathSet();
897 m_bIsAdminDir
= GitAdminDir::IsAdminDirPath(m_sBackslashPath
);
898 m_bIsAdminDirKnown
= true;
899 if (m_bIsAdminDir
&& !m_bIsAdminDirKnown
)
901 m_bHasAdminDir
= false;
902 m_bIsAdminDirKnown
= true;
904 return m_bIsAdminDir
;
907 bool CTGitPath::IsValidOnWindows() const
909 if (m_bIsValidOnWindowsKnown
)
910 return m_bIsValidOnWindows
;
912 m_bIsValidOnWindows
= false;
913 EnsureBackslashPathSet();
914 CString sMatch
= m_sBackslashPath
+ L
"\r\n";
915 std::wstring sPattern
;
916 // the 'file://' URL is just a normal windows path:
917 if (CStringUtils::StartsWithI(sMatch
, L
"file:\\\\"))
919 sMatch
= sMatch
.Mid(7);
920 sMatch
.TrimLeft(L
'\\');
921 sPattern
= L
"^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$";
924 sPattern
= L
"^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$";
928 std::tr1::wregex
rx(sPattern
, std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
929 std::tr1::wsmatch match
;
931 std::wstring rmatch
= std::wstring((LPCTSTR
)sMatch
);
932 if (std::tr1::regex_match(rmatch
, match
, rx
))
934 if (std::wstring(match
[0]).compare(sMatch
)==0)
935 m_bIsValidOnWindows
= true;
937 if (m_bIsValidOnWindows
)
939 // now check for illegal filenames
940 std::tr1::wregex
rx2(L
"\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)", std::tr1::regex_constants::icase
| std::tr1::regex_constants::ECMAScript
);
941 rmatch
= m_sBackslashPath
;
942 if (std::tr1::regex_search(rmatch
, rx2
, std::tr1::regex_constants::match_default
))
943 m_bIsValidOnWindows
= false;
946 catch (std::exception
&) {}
948 m_bIsValidOnWindowsKnown
= true;
949 return m_bIsValidOnWindows
;
952 //////////////////////////////////////////////////////////////////////////
954 CTGitPathList::CTGitPathList()
959 // A constructor which allows a path list to be easily built which one initial entry in
960 CTGitPathList::CTGitPathList(const CTGitPath
& firstEntry
)
965 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR
&out
,bool /*staged*/)
973 while (pos
< out
.size())
978 CGit::StringAppend(&one
, &out
[pos
], CP_UTF8
);
980 // m_Action is never used and propably never worked (needs to be set after path.SetFromGit)
981 // also dropped LOGACTIONS_CACHE for 'H'
982 // path.m_Action=path.ParserAction(out[pos]);
983 one
.Tokenize(L
"\t", tabstart
);
988 CString pathstring
= one
.Right(one
.GetLength() - tabstart
);
992 part
= one
.Tokenize(L
" ", tabstart
); //Tag
996 part
= one
.Tokenize(L
" ", tabstart
); //Mode
999 int mode
= wcstol(part
, nullptr, 8);
1000 path
.SetFromGit(pathstring
, (mode
& S_IFDIR
) == S_IFDIR
);
1002 part
= one
.Tokenize(L
" ", tabstart
); //Hash
1006 part
= one
.Tokenize(L
"\t", tabstart
); //Stage
1010 path
.m_Stage
= _wtol(part
);
1012 this->AddPath(path
);
1014 pos
=out
.findNextString(pos
);
1019 int CTGitPathList::FillUnRev(unsigned int action
, const CTGitPathList
* list
, CString
* err
)
1028 count
=list
->GetCount();
1029 for (int i
= 0; i
< count
; ++i
)
1033 if(action
& CTGitPath::LOGACTIONS_IGNORE
)
1038 cmd
= L
"git.exe ls-files --exclude-standard --full-name --others -z";
1044 cmd
.Format(L
"git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\"",
1046 (*list
)[i
].GetWinPath());
1049 BYTE_VECTOR out
, errb
;
1051 if (g_Git
.Run(cmd
, &out
, &errb
))
1054 CGit::StringAppend(err
, errb
.data(), CP_UTF8
, (int)errb
.size());
1060 while (pos
< out
.size())
1063 CGit::StringAppend(&one
, &out
[pos
], CP_UTF8
);
1066 //SetFromGit will clear all status
1067 if (CStringUtils::EndsWith(one
, L
'/'))
1069 one
.Truncate(one
.GetLength() - 1);
1070 path
.SetFromGit(one
, true);
1073 path
.SetFromGit(one
);
1074 path
.m_Action
=action
;
1077 pos
=out
.findNextString(pos
);
1084 int CTGitPathList::FillBasedOnIndexFlags(unsigned short flag
, unsigned short flagextended
, const CTGitPathList
* list
/*nullptr*/)
1089 CAutoRepository
repository(g_Git
.GetGitRepository());
1094 if (git_repository_index(index
.GetPointer(), repository
))
1098 if (list
== nullptr)
1101 count
= list
->GetCount();
1102 for (int j
= 0; j
< count
; ++j
)
1104 for (size_t i
= 0, ecount
= git_index_entrycount(index
); i
< ecount
; ++i
)
1106 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
1108 if (!e
|| !((e
->flags
& flag
) || (e
->flags_extended
& flagextended
)) || !e
->path
)
1111 CString one
= CUnicodeUtils::GetUnicode(e
->path
);
1113 if (!(!list
|| (*list
)[j
].GetWinPathString().IsEmpty() || one
== (*list
)[j
].GetGitPathString() || (PathIsDirectory(g_Git
.CombinePath((*list
)[j
].GetWinPathString())) && CStringUtils::StartsWith(one
, (*list
)[j
].GetGitPathString() + L
'/'))))
1116 //SetFromGit will clear all status
1117 path
.SetFromGit(one
, (e
->mode
& S_IFDIR
) == S_IFDIR
);
1118 if (e
->flags_extended
& GIT_IDXENTRY_SKIP_WORKTREE
)
1119 path
.m_Action
= CTGitPath::LOGACTIONS_SKIPWORKTREE
;
1120 else if (e
->flags
& GIT_IDXENTRY_VALID
)
1121 path
.m_Action
= CTGitPath::LOGACTIONS_ASSUMEVALID
;
1128 int CTGitPathList::ParserFromLog(BYTE_VECTOR
&log
, bool parseDeletes
/*false*/)
1131 std::map
<CString
, size_t> duplicateMap
;
1135 size_t logend
= log
.size();
1136 while (pos
< logend
)
1148 if (pos
+ 1 >= logend
)
1150 if (log
[pos
+ 1] == ':')
1158 size_t end
= log
.find(0, pos
);
1159 size_t actionstart
= BYTE_VECTOR::npos
;
1160 size_t file1
= BYTE_VECTOR::npos
, file2
= BYTE_VECTOR::npos
;
1161 if (end
!= BYTE_VECTOR::npos
&& end
> 7)
1163 modeold
= strtol((const char*)&log
[pos
+ 1], nullptr, 8);
1164 modenew
= strtol((const char*)&log
[pos
+ 7], nullptr, 8);
1165 actionstart
=log
.find(' ',end
-6);
1168 if (actionstart
!= BYTE_VECTOR::npos
&& actionstart
> 0)
1171 if (actionstart
>= logend
)
1174 file1
= log
.find(0,actionstart
);
1175 if (file1
!= BYTE_VECTOR::npos
)
1180 if( log
[actionstart
] == 'C' || log
[actionstart
] == 'R' )
1183 file1
= log
.find(0,file1
);
1184 if (file1
!= BYTE_VECTOR::npos
)
1195 if (file1
!= BYTE_VECTOR::npos
)
1196 CGit::StringAppend(&pathname1
, &log
[file1
], CP_UTF8
);
1197 if (file2
!= BYTE_VECTOR::npos
)
1198 CGit::StringAppend(&pathname2
, &log
[file2
], CP_UTF8
);
1200 if (actionstart
== BYTE_VECTOR::npos
)
1203 auto existing
= duplicateMap
.find(pathname1
);
1204 if (existing
!= duplicateMap
.end())
1206 CTGitPath
& p
= m_paths
[existing
->second
];
1207 p
.ParserAction(log
[actionstart
]);
1209 // reset submodule/folder status if a staged entry is not a folder
1210 if (p
.IsDirectory() && ((modeold
&& !(modeold
& S_IFDIR
)) || (modenew
&& !(modenew
& S_IFDIR
))))
1211 p
.UnsetDirectoryStatus();
1214 p
.m_Action
|= CTGitPath::LOGACTIONS_MERGED
;
1215 m_Action
|= p
.m_Action
;
1219 int ac
=path
.ParserAction(log
[actionstart
] );
1220 ac
|= merged
?CTGitPath::LOGACTIONS_MERGED
:0;
1222 int isSubmodule
= FALSE
;
1223 if (ac
& (CTGitPath::LOGACTIONS_DELETED
| CTGitPath::LOGACTIONS_UNMERGED
))
1224 isSubmodule
= (modeold
& S_IFDIR
) == S_IFDIR
;
1226 isSubmodule
= (modenew
& S_IFDIR
) == S_IFDIR
;
1228 path
.SetFromGit(pathname1
, &pathname2
, &isSubmodule
);
1230 //action must be set after setfromgit. SetFromGit will clear all status.
1234 duplicateMap
.insert(std::pair
<CString
, size_t>(path
.GetGitPathString(), m_paths
.size() - 1));
1244 int isSubmodule
= FALSE
;
1245 size_t tabstart
= log
.find('\t', pos
);
1246 if (tabstart
!= BYTE_VECTOR::npos
)
1248 int modenew
= strtol((const char*)&log
[pos
+ 2], nullptr, 8);
1249 isSubmodule
= (modenew
& S_IFDIR
) == S_IFDIR
;
1251 CGit::StringAppend(&StatAdd
, &log
[pos
], CP_UTF8
);
1255 tabstart
=log
.find('\t',pos
);
1256 if (tabstart
!= BYTE_VECTOR::npos
)
1260 CGit::StringAppend(&StatDel
, &log
[pos
], CP_UTF8
);
1264 if(log
[pos
] == 0) //rename
1267 CGit::StringAppend(&file2
, &log
[pos
], CP_UTF8
);
1268 size_t sec
= log
.find(0, pos
);
1269 if (sec
!= BYTE_VECTOR::npos
)
1272 CGit::StringAppend(&file1
, &log
[sec
], CP_UTF8
);
1278 CGit::StringAppend(&file1
, &log
[pos
], CP_UTF8
);
1280 path
.SetFromGit(file1
, &file2
, &isSubmodule
);
1282 auto existing
= duplicateMap
.find(path
.GetGitPathString());
1283 if (existing
!= duplicateMap
.end())
1285 CTGitPath
& p
= m_paths
[existing
->second
];
1286 p
.m_StatAdd
= StatAdd
;
1287 p
.m_StatDel
= StatDel
;
1291 //path.SetFromGit(pathname);
1294 path
.m_StatAdd
= L
"0";
1295 path
.m_StatDel
= L
"0";
1296 path
.m_Action
|= CTGitPath::LOGACTIONS_DELETED
| CTGitPath::LOGACTIONS_MISSING
;
1300 path
.m_StatAdd
=StatAdd
;
1301 path
.m_StatDel
=StatDel
;
1304 duplicateMap
.insert(std::pair
<CString
, size_t>(path
.GetGitPathString(), m_paths
.size() - 1));
1307 pos
=log
.findNextString(pos
);
1312 void CTGitPathList::AddPath(const CTGitPath
& newPath
)
1314 m_paths
.push_back(newPath
);
1315 m_commonBaseDirectory
.Reset();
1317 int CTGitPathList::GetCount() const
1319 return (int)m_paths
.size();
1321 bool CTGitPathList::IsEmpty() const
1323 return m_paths
.empty();
1325 void CTGitPathList::Clear()
1329 m_commonBaseDirectory
.Reset();
1332 const CTGitPath
& CTGitPathList::operator[](INT_PTR index
) const
1334 ATLASSERT(index
>= 0 && index
< (INT_PTR
)m_paths
.size());
1335 return m_paths
[index
];
1338 bool CTGitPathList::AreAllPathsFiles() const
1340 // Look through the vector for any directories - if we find them, return false
1341 return std::find_if(m_paths
.cbegin(), m_paths
.cend(), std::mem_fn(&CTGitPath::IsDirectory
)) == m_paths
.end();
1344 #if defined(_MFC_VER)
1346 bool CTGitPathList::LoadFromFile(const CTGitPath
& filename
)
1352 CStdioFile
file(filename
.GetWinPath(), CFile::typeBinary
| CFile::modeRead
| CFile::shareDenyWrite
);
1354 // for every selected file/folder
1356 while (file
.ReadString(strLine
))
1358 path
.SetFromUnknown(strLine
);
1363 catch (CFileException
* pE
)
1365 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException loading target file list\n");
1366 TCHAR error
[10000] = {0};
1367 pE
->GetErrorMessage(error
, 10000);
1368 // CMessageBox::Show(nullptr, error, L"TortoiseGit", MB_ICONERROR);
1375 bool CTGitPathList::WriteToFile(const CString
& sFilename
, bool bANSI
/* = false */) const
1381 CStdioFile
file(sFilename
, CFile::typeText
| CFile::modeReadWrite
| CFile::modeCreate
);
1382 for (const auto& path
: m_paths
)
1384 CStringA line
= CStringA(path
.GetGitPathString()) + '\n';
1385 file
.Write(line
, line
.GetLength());
1391 CStdioFile
file(sFilename
, CFile::typeBinary
| CFile::modeReadWrite
| CFile::modeCreate
);
1392 for (const auto& path
: m_paths
)
1393 file
.WriteString(path
.GetGitPathString() + L
'\n');
1397 catch (CFileException
* pE
)
1399 CTraceToOutputDebugString::Instance()(__FUNCTION__
": CFileException in writing temp file\n");
1406 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString
& sPathString
)
1412 temp
= sPathString
.Tokenize(L
"*", pos
);
1415 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp
)));
1419 CString
CTGitPathList::CreateAsteriskSeparatedString() const
1422 for (const auto& path
: m_paths
)
1424 if (!sRet
.IsEmpty())
1426 sRet
+= path
.GetWinPathString();
1433 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1435 // Check if all the paths are files and in the same directory
1436 m_commonBaseDirectory
.Reset();
1437 for (const auto& path
: m_paths
)
1439 if (path
.IsDirectory())
1441 const CTGitPath
& baseDirectory
= path
.GetDirectory();
1442 if(m_commonBaseDirectory
.IsEmpty())
1443 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 for (const auto& path
: m_paths
)
1460 const CTGitPath
& baseDirectory
= path
.GetDirectory();
1461 if(m_commonBaseDirectory
.IsEmpty())
1462 m_commonBaseDirectory
= baseDirectory
;
1463 else if(!m_commonBaseDirectory
.IsEquivalentTo(baseDirectory
))
1466 m_commonBaseDirectory
.Reset();
1471 // since we only checked strings, not paths,
1472 // we have to make sure now that we really return a *path* here
1473 for (const auto& path
: m_paths
)
1475 if (!m_commonBaseDirectory
.IsAncestorOf(path
))
1477 m_commonBaseDirectory
= m_commonBaseDirectory
.GetContainingDirectory();
1481 return m_commonBaseDirectory
;
1484 CTGitPath
CTGitPathList::GetCommonRoot() const
1489 if (GetCount() == 1)
1492 // first entry is common root for itself
1493 // (add trailing '\\' to detect partial matches of the last path element)
1494 CString root
= m_paths
[0].GetWinPathString() + L
'\\';
1495 int rootLength
= root
.GetLength();
1497 // determine common path string prefix
1498 for (auto it
= m_paths
.cbegin() + 1; it
!= m_paths
.cend(); ++it
)
1500 CString path
= it
->GetWinPathString() + L
'\\';
1502 int newLength
= CStringUtils::GetMatchingLength(root
, path
);
1503 if (newLength
!= rootLength
)
1505 root
.Delete(newLength
, rootLength
);
1506 rootLength
= newLength
;
1510 // remove the last (partial) path element
1512 root
.Delete(root
.ReverseFind(L
'\\'), rootLength
);
1515 return CTGitPath(root
);
1518 void CTGitPathList::SortByPathname(bool bReverse
/*= false*/)
1520 std::sort(m_paths
.begin(), m_paths
.end());
1522 std::reverse(m_paths
.begin(), m_paths
.end());
1525 void CTGitPathList::DeleteAllFiles(bool bTrash
, bool bFilesOnly
, bool bShowErrorUI
)
1527 if (m_paths
.empty())
1529 PathVector::const_iterator it
;
1530 SortByPathname(true); // nested ones first
1533 for (it
= m_paths
.cbegin(); it
!= m_paths
.cend(); ++it
)
1535 if ((it
->Exists()) && ((it
->IsDirectory() != bFilesOnly
) || !bFilesOnly
))
1537 if (!it
->IsDirectory())
1538 ::SetFileAttributes(it
->GetWinPath(), FILE_ATTRIBUTE_NORMAL
);
1540 sPaths
+= it
->GetWinPath();
1544 if (sPaths
.IsEmpty())
1548 DeleteViaShell((LPCTSTR
)sPaths
, bTrash
, bShowErrorUI
);
1552 bool CTGitPathList::DeleteViaShell(LPCTSTR path
, bool bTrash
, bool bShowErrorUI
)
1554 SHFILEOPSTRUCT shop
= {0};
1555 shop
.wFunc
= FO_DELETE
;
1557 shop
.fFlags
= FOF_NOCONFIRMATION
|FOF_NO_CONNECTED_ELEMENTS
;
1559 shop
.fFlags
|= FOF_NOERRORUI
| FOF_SILENT
;
1561 shop
.fFlags
|= FOF_ALLOWUNDO
;
1562 const bool bRet
= (SHFileOperation(&shop
) == 0);
1566 void CTGitPathList::RemoveDuplicates()
1569 // Remove the duplicates
1570 // (Unique moves them to the end of the vector, then erase chops them off)
1571 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_paths
.end());
1574 void CTGitPathList::RemoveAdminPaths()
1576 PathVector::iterator it
;
1577 for(it
= m_paths
.begin(); it
!= m_paths
.end(); )
1579 if (it
->IsAdminDir())
1582 it
= m_paths
.begin();
1589 void CTGitPathList::RemovePath(const CTGitPath
& path
)
1591 PathVector::iterator it
;
1592 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1594 if (it
->IsEquivalentTo(path
))
1602 void CTGitPathList::RemoveItem(CTGitPath
& path
)
1604 PathVector::iterator it
;
1605 for(it
= m_paths
.begin(); it
!= m_paths
.end(); ++it
)
1607 if (CPathUtils::ArePathStringsEqualWithCase(it
->GetGitPathString(), path
.GetGitPathString()))
1614 void CTGitPathList::RemoveChildren()
1617 m_paths
.erase(std::unique(m_paths
.begin(), m_paths
.end(), &CTGitPath::CheckChild
), m_paths
.end());
1620 bool CTGitPathList::IsEqual(const CTGitPathList
& list
)
1622 if (list
.GetCount() != GetCount())
1624 for (int i
=0; i
<list
.GetCount(); ++i
)
1626 if (!list
[i
].IsEquivalentTo(m_paths
[i
]))
1632 const CTGitPath
* CTGitPathList::LookForGitPath(const CString
& path
)
1635 for (i
= 0; i
< this->GetCount(); ++i
)
1637 if (CPathUtils::ArePathStringsEqualWithCase((*this)[i
].GetGitPathString(), path
))
1638 return (CTGitPath
*)&(*this)[i
];
1643 CString
CTGitPath::GetActionName(int action
)
1645 if(action
& CTGitPath::LOGACTIONS_UNMERGED
)
1646 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT
);
1647 if(action
& CTGitPath::LOGACTIONS_ADDED
)
1648 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD
);
1649 if (action
& CTGitPath::LOGACTIONS_MISSING
)
1650 return MAKEINTRESOURCE(IDS_PATHACTIONS_MISSING
);
1651 if(action
& CTGitPath::LOGACTIONS_DELETED
)
1652 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE
);
1653 if(action
& CTGitPath::LOGACTIONS_MERGED
)
1654 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED
);
1656 if(action
& CTGitPath::LOGACTIONS_MODIFIED
)
1657 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED
);
1658 if(action
& CTGitPath::LOGACTIONS_REPLACED
)
1659 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME
);
1660 if(action
& CTGitPath::LOGACTIONS_COPY
)
1661 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY
);
1663 if (action
& CTGitPath::LOGACTIONS_ASSUMEVALID
)
1664 return MAKEINTRESOURCE(IDS_PATHACTIONS_ASSUMEUNCHANGED
);
1665 if (action
& CTGitPath::LOGACTIONS_SKIPWORKTREE
)
1666 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIPWORKTREE
);
1668 if (action
& CTGitPath::LOGACTIONS_IGNORE
)
1669 return MAKEINTRESOURCE(IDS_PATHACTIONS_IGNORED
);
1671 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN
);
1674 CString
CTGitPath::GetActionName() const
1676 return GetActionName(m_Action
);
1679 int CTGitPathList::GetAction()
1684 CString
CTGitPath::GetAbbreviatedRename() const
1686 if (GetGitOldPathString().IsEmpty())
1687 return GetFileOrDirectoryName();
1689 // Find common prefix which ends with a slash
1690 int prefix_length
= 0;
1691 for (int i
= 0, maxLength
= min(m_sOldFwdslashPath
.GetLength(), m_sFwdslashPath
.GetLength()); i
< maxLength
; ++i
)
1693 if (m_sOldFwdslashPath
[i
] != m_sFwdslashPath
[i
])
1695 if (m_sOldFwdslashPath
[i
] == L
'/')
1696 prefix_length
= i
+ 1;
1699 LPCTSTR oldName
= (LPCTSTR
)m_sOldFwdslashPath
+ m_sOldFwdslashPath
.GetLength();
1700 LPCTSTR newName
= (LPCTSTR
)m_sFwdslashPath
+ m_sFwdslashPath
.GetLength();
1702 int suffix_length
= 0;
1703 int prefix_adjust_for_slash
= (prefix_length
? 1 : 0);
1704 while ((LPCTSTR
)m_sOldFwdslashPath
+ prefix_length
- prefix_adjust_for_slash
<= oldName
&&
1705 (LPCTSTR
)m_sFwdslashPath
+ prefix_length
- prefix_adjust_for_slash
<= newName
&&
1706 *oldName
== *newName
)
1708 if (*oldName
== L
'/')
1709 suffix_length
= m_sOldFwdslashPath
.GetLength() - (int)(oldName
- (LPCTSTR
)m_sOldFwdslashPath
);
1715 * pfx{old_midlen => new_midlen}sfx
1716 * {pfx-old => pfx-new}sfx
1717 * pfx{sfx-old => sfx-new}
1718 * name-old => name-new
1720 int old_midlen
= m_sOldFwdslashPath
.GetLength() - prefix_length
- suffix_length
;
1721 int new_midlen
= m_sFwdslashPath
.GetLength() - prefix_length
- suffix_length
;
1728 if (prefix_length
+ suffix_length
)
1730 ret
= m_sOldFwdslashPath
.Left(prefix_length
);
1733 ret
+= m_sOldFwdslashPath
.Mid(prefix_length
, old_midlen
);
1735 ret
+= m_sFwdslashPath
.Mid(prefix_length
, new_midlen
);
1736 if (prefix_length
+ suffix_length
)
1739 ret
+= m_sFwdslashPath
.Mid(m_sFwdslashPath
.GetLength() - suffix_length
, suffix_length
);