Added Italian language pack
[TortoiseGit.git] / src / Git / TGitPath.cpp
blobc1847676b8dd9c72a13c7a600bc5a2cbc8ba290d
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.
21 #include "stdafx.h"
22 #include "TGitPath.h"
23 #include "UnicodeUtils.h"
24 #include "GitAdminDir.h"
25 #include "PathUtils.h"
26 #include <regex>
27 #include "git.h"
28 #include "Globals.h"
29 #include "StringUtils.h"
30 #include "../Resources/LoglistCommonResource.h"
31 #include <memory>
33 #if defined(_MFC_VER)
34 //#include "MessageBox.h"
35 //#include "AppUtils.h"
36 #endif
38 #ifndef ASSERT
39 #define ASSERT()
40 #endif
41 using namespace std;
42 extern CGit g_Git;
44 CTGitPath::CTGitPath(void)
45 : m_bDirectoryKnown(false)
46 , m_bIsDirectory(false)
47 , m_bURLKnown(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)
55 , m_bExists(false)
56 , m_bExistsKnown(false)
57 , m_bLastWriteTimeKnown(0)
58 , m_lastWriteTime(0)
59 , m_customData(NULL)
60 , m_bIsSpecialDirectoryKnown(false)
61 , m_bIsSpecialDirectory(false)
62 , m_bIsWCRootKnown(false)
63 , m_bIsWCRoot(false)
64 , m_fileSize(0)
66 m_Action=0;
67 m_ParentNo=0;
68 m_Stage = 0;
69 m_Checked = 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)
79 , m_bURLKnown(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)
87 , m_bExists(false)
88 , m_bExistsKnown(false)
89 , m_bLastWriteTimeKnown(0)
90 , m_lastWriteTime(0)
91 , m_customData(NULL)
92 , m_bIsSpecialDirectoryKnown(false)
93 , m_bIsSpecialDirectory(false)
94 , m_bIsWCRootKnown(false)
95 , m_bIsWCRoot(false)
96 , m_fileSize(0)
98 SetFromUnknown(sUnknownPath);
99 m_Action=0;
100 m_Stage=0;
101 m_ParentNo=0;
104 int CTGitPath::ParserAction(BYTE action)
106 //action=action.TrimLeft();
107 //TCHAR c=action.GetAt(0);
108 if(action == 'M')
109 m_Action|= LOGACTIONS_MODIFIED;
110 if(action == 'R')
111 m_Action|= LOGACTIONS_REPLACED;
112 if(action == 'A')
113 m_Action|= LOGACTIONS_ADDED;
114 if(action == 'D')
115 m_Action|= LOGACTIONS_DELETED;
116 if(action == 'U')
117 m_Action|= LOGACTIONS_UNMERGED;
118 if(action == 'K')
119 m_Action|= LOGACTIONS_DELETED;
120 if(action == 'H')
121 m_Action|= LOGACTIONS_CACHE;
122 if(action == 'C' )
123 m_Action|= LOGACTIONS_COPY;
125 return m_Action;
127 void CTGitPath::SetFromGit(const char* pPath)
129 Reset();
130 if (pPath == NULL)
131 return;
132 int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);
133 if (len)
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)
143 SetFromGit(pPath);
144 m_bDirectoryKnown = true;
145 m_bIsDirectory = bIsDirectory;
148 void CTGitPath::SetFromGit(const TCHAR* pPath, bool bIsDirectory)
150 Reset();
151 if (pPath)
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)
162 Reset();
163 m_sFwdslashPath = sPath;
164 SanitizeRootPath(m_sFwdslashPath, true);
165 if(oldpath)
166 m_sOldFwdslashPath = *oldpath;
169 void CTGitPath::SetFromWin(LPCTSTR pPath)
171 Reset();
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)
179 Reset();
180 m_sBackslashPath = sPath;
181 m_sBackslashPath.Replace(L"\\\\?\\", L"");
182 SanitizeRootPath(m_sBackslashPath, false);
184 void CTGitPath::SetFromWin(LPCTSTR pPath, bool bIsDirectory)
186 Reset();
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)
194 Reset();
195 m_sBackslashPath = sPath;
196 m_bIsDirectory = bIsDirectory;
197 m_bDirectoryKnown = true;
198 SanitizeRootPath(m_sBackslashPath, false);
200 void CTGitPath::SetFromUnknown(const CString& sPath)
202 Reset();
203 // Just set whichever path we think is most likely to be used
204 // GitAdminDir admin;
205 // CString p;
206 // if(admin.HasAdminDir(sPath,&p))
207 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
208 // else
209 SetFwdslashPath(sPath);
212 LPCTSTR CTGitPath::GetWinPath() const
214 if(IsEmpty())
216 return _T("");
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;
248 #if 0
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.
256 if(IsEmpty())
258 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;
279 #endif
281 const CString& CTGitPath::GetUIPathString() const
283 if (m_sUIPath.IsEmpty())
285 m_sUIPath = GetWinPathString();
287 return m_sUIPath;
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)
332 UpdateAttributes();
334 return m_bIsDirectory;
337 bool CTGitPath::Exists() const
339 if (!m_bExistsKnown)
341 UpdateAttributes();
343 return m_bExists;
346 bool CTGitPath::Delete(bool bTrash) const
348 EnsureBackslashPathSet();
349 ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);
350 bool bRet = false;
351 if (Exists())
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);
361 else
363 bRet = !!::DeleteFile(m_sBackslashPath);
366 m_bExists = false;
367 m_bExistsKnown = true;
368 return bRet;
371 __int64 CTGitPath::GetLastWriteTime() const
373 if(!m_bLastWriteTimeKnown)
375 UpdateAttributes();
377 return m_lastWriteTime;
380 __int64 CTGitPath::GetFileSize() const
382 if(!m_bDirectoryKnown)
384 UpdateAttributes();
386 return m_fileSize;
389 bool CTGitPath::IsReadOnly() const
391 if(!m_bLastWriteTimeKnown)
393 UpdateAttributes();
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;
408 if (m_bIsDirectory)
410 m_fileSize = 0;
412 else
414 m_fileSize = ((INT64)( (DWORD)(attribs.nFileSizeLow) ) | ( (INT64)( (DWORD)(attribs.nFileSizeHigh) )<<32 ));
416 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
417 m_bExists = true;
419 else
421 m_bIsDirectory = false;
422 m_lastWriteTime = 0;
423 m_fileSize = 0;
424 DWORD err = GetLastError();
425 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))
427 m_bExists = false;
429 else
431 m_bExists = true;
432 return;
435 m_bDirectoryKnown = true;
436 m_bLastWriteTimeKnown = true;
437 m_bExistsKnown = true;
440 CTGitPath CTGitPath::GetSubPath(const CTGitPath &root)
442 CTGitPath path;
444 if(GetWinPathString().Left(root.GetWinPathString().GetLength()) == root.GetWinPathString())
446 CString str=GetWinPathString();
447 path.SetFromWin(str.Right(str.GetLength()-root.GetWinPathString().GetLength()-1));
449 return path;
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;
474 m_bURLKnown = 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();
486 this->m_Action=0;
487 this->m_StatAdd=_T("");
488 this->m_StatDel=_T("");
489 m_ParentNo=0;
490 ATLASSERT(IsEmpty());
493 CTGitPath CTGitPath::GetDirectory() const
495 if ((IsDirectory())||(!Exists()))
497 return *this;
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
510 sDirName += '\\';
511 if(sDirName == m_sBackslashPath)
513 // We were clearly provided with a root path to start with - we should return nothing now
514 sDirName.Empty();
517 if(sDirName.GetLength() == 1 && sDirName[0] == '\\')
519 // We have an UNC path and we already are the root
520 sDirName.Empty();
522 CTGitPath retVal;
523 retVal.SetFromWin(sDirName);
524 return retVal;
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();
534 return workingPath;
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
552 GetUIPathString();
553 return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);
556 CString CTGitPath::GetFileExtension() const
558 if(!IsDirectory())
560 EnsureBackslashPathSet();
561 int dotPos = m_sBackslashPath.ReverseFind('.');
562 int slashPos = m_sBackslashPath.ReverseFind('\\');
563 if (dotPos > slashPos)
564 return m_sBackslashPath.Mid(dotPos);
566 return CString();
568 CString CTGitPath::GetBaseFilename() const
570 int dot;
571 CString filename=GetFilename();
572 dot = filename.ReverseFind(_T('.'));
573 if(dot>0)
574 return filename.Left(dot);
575 else
576 return filename;
579 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)
581 int length = sP1.GetLength();
582 if(length != sP2.GetLength())
584 // Different lengths
585 return false;
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);
592 while(length-- > 0)
594 if(_totlower(*pP1--) != _totlower(*pP2--))
596 return false;
599 return true;
602 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)
604 int length = sP1.GetLength();
605 if(length != sP2.GetLength())
607 // Different lengths
608 return false;
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);
615 while(length-- > 0)
617 if((*pP1--) != (*pP2--))
619 return false;
622 return true;
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
629 // bit faster.
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
638 // both filenames
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);
645 else
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
656 // both filenames
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);
663 else
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())
724 CTGitPath l = left;
725 CTGitPath r = right;
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;
759 SetFromWin(strCopy);
762 bool CTGitPath::IsWCRoot() const
764 if (m_bIsWCRootKnown)
765 return m_bIsWCRoot;
767 m_bIsWCRootKnown = true;
768 m_bIsWCRoot = false;
770 CString topDirectory;
771 if (!IsDirectory() || !HasAdminDir(&topDirectory))
773 return m_bIsWCRoot;
776 if (IsEquivalentToWithoutCase(topDirectory))
778 m_bIsWCRoot = true;
781 return m_bIsWCRoot;
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
797 if (HasAdminDir())
799 CString path = m_sProjectRoot;
800 path += _T("\\.gitmodules");
801 if( PathFileExists(path) )
802 return true;
804 return false;
807 int CTGitPath::GetAdminDirMask() const
809 int status = 0;
810 CString topdir;
811 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
813 return status;
816 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
817 status |= ITEMIS_INGIT|ITEMIS_INVERSIONEDFOLDER;
819 if (IsDirectory())
821 status |= ITEMIS_FOLDERINGIT;
822 if (IsWCRoot())
824 status |= ITEMIS_WCROOT;
826 if (g_GitAdminDir.HasAdminDir(GetWinPathString(), false))
827 status |= ITEMIS_SUBMODULE;
831 CString dotGitPath;
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;
846 return status;
849 bool CTGitPath::HasStashDir() const
851 CString topdir;
852 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
854 return false;
857 CString dotGitPath;
858 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
860 return !!PathFileExists(dotGitPath + _T("\\refs\\stash"));
862 bool CTGitPath::HasGitSVNDir() const
864 CString topdir;
865 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
867 return false;
870 CString dotGitPath;
871 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
873 return !!PathFileExists(dotGitPath + _T("svn"));
875 bool CTGitPath::IsBisectActive() const
877 CString topdir;
878 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
880 return false;
883 CString dotGitPath;
884 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
886 return !!PathFileExists(dotGitPath + _T("BISECT_START"));
888 bool CTGitPath::IsMergeActive() const
890 CString topdir;
891 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
893 return false;
896 CString dotGitPath;
897 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
899 return !!PathFileExists(dotGitPath + _T("MERGE_HEAD"));
901 bool CTGitPath::HasRebaseApply() const
903 CString topdir;
904 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
906 return false;
909 CString dotGitPath;
910 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
912 return !!PathFileExists(dotGitPath + _T("rebase-apply"));
915 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const
917 if (m_bHasAdminDirKnown)
919 if (ProjectTopDir)
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;
927 if (ProjectTopDir)
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");
951 wstring sPattern;
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]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
959 else
961 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
966 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
967 tr1::wsmatch match;
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;
984 catch (exception) {}
986 m_bIsValidOnWindowsKnown = true;
987 return m_bIsValidOnWindows;
990 //////////////////////////////////////////////////////////////////////////
992 CTGitPathList::CTGitPathList()
994 m_Action = 0;
997 // A constructor which allows a path list to be easily built which one initial entry in
998 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)
1000 m_Action = 0;
1001 AddPath(firstEntry);
1003 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool /*staged*/)
1005 unsigned int pos=0;
1006 CString one;
1007 CTGitPath path;
1008 CString part;
1009 this->Clear();
1011 while(pos>=0 && pos<out.size())
1013 one.Empty();
1014 path.Reset();
1016 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1017 int tabstart=0;
1018 path.m_Action=path.ParserAction(out[pos]);
1019 one.Tokenize(_T("\t"),tabstart);
1021 if(tabstart>=0)
1022 path.SetFromGit(one.Right(one.GetLength()-tabstart));
1024 tabstart=0;
1026 part=one.Tokenize(_T(" "),tabstart); //Tag
1028 part=one.Tokenize(_T(" "),tabstart); //Mode
1030 part=one.Tokenize(_T(" "),tabstart); //Hash
1032 part=one.Tokenize(_T("\t"),tabstart); //Stage
1034 path.m_Stage=_ttol(part);
1036 this->AddPath(path);
1038 pos=out.findNextString(pos);
1040 return pos;
1042 int CTGitPathList::FillUnRev(unsigned int action,CTGitPathList *list)
1044 int pos=0;
1045 this->Clear();
1046 CTGitPath path;
1048 int count;
1049 if(list==NULL)
1050 count=1;
1051 else
1052 count=list->GetCount();
1053 for (int i = 0; i < count; ++i)
1055 CString cmd;
1056 pos=0;
1058 CString ignored;
1059 if(action & CTGitPath::LOGACTIONS_IGNORE)
1060 ignored= _T(" -i");
1062 if(list==NULL)
1064 cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1065 cmd+=ignored;
1068 else
1069 { cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1070 ignored,
1071 (*list)[i].GetWinPathString());
1074 BYTE_VECTOR out;
1075 out.clear();
1076 g_Git.Run(cmd, &out);
1078 pos=0;
1079 CString one;
1080 while (pos >= 0 && pos < (int)out.size())
1082 one.Empty();
1083 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1084 if(!one.IsEmpty())
1086 //SetFromGit will clear all status
1087 path.SetFromGit(one);
1088 path.m_Action=action;
1089 AddPath(path);
1091 pos=out.findNextString(pos);
1095 return 0;
1097 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log, bool parseDeletes /*false*/)
1099 this->Clear();
1100 int pos=0;
1101 //BYTE *p=&log[0];
1102 //CString one;
1103 CTGitPath path;
1104 m_Action=0;
1105 while (pos >= 0 && pos < (int)log.size())
1107 //one=log.Tokenize(_T("\n"),pos);
1108 path.Reset();
1109 if(log[pos]=='\n')
1110 ++pos;
1112 if(log[pos]==':')
1114 bool merged=false;
1115 if(log[pos+1] ==':')
1117 merged=true;
1119 int end=log.find(0,pos);
1120 int actionstart=-1;
1121 int numfile=1;
1122 int file1=-1,file2=-1;
1123 if( end>0 )
1125 actionstart=log.find(' ',end-6);
1126 pos=actionstart;
1128 if( actionstart>0 )
1130 ++actionstart;
1132 file1 = log.find(0,actionstart);
1133 if( file1>=0 )
1135 ++file1;
1136 pos=file1;
1138 if( log[actionstart] == 'C' || log[actionstart] == 'R' )
1140 file2=file1;
1141 numfile=2;
1142 file1 = log.find(0,file1);
1143 if(file1>=0 )
1145 ++file1;
1146 pos=file1;
1152 CString pathname1;
1153 CString pathname2;
1155 if( file1>=0 )
1156 g_Git.StringAppend(&pathname1, &log[file1], CP_UTF8);
1157 if( file2>=0 )
1158 g_Git.StringAppend(&pathname2, &log[file2], CP_UTF8);
1160 CTGitPath *GitPath=LookForGitPath(pathname1);
1162 if(GitPath)
1164 GitPath->ParserAction( log[actionstart] );
1166 if(merged)
1168 GitPath->m_Action |= CTGitPath::LOGACTIONS_MERGED;
1169 GitPath->m_Action &= ~CTGitPath::LOGACTIONS_FORWORD;
1171 m_Action |=GitPath->m_Action;
1174 else
1176 int ac=path.ParserAction(log[actionstart] );
1177 ac |= merged?CTGitPath::LOGACTIONS_MERGED:0;
1179 path.SetFromGit(pathname1,&pathname2);
1180 path.m_Action=ac;
1181 //action must be set after setfromgit. SetFromGit will clear all status.
1182 this->m_Action|=ac;
1184 AddPath(path);
1189 else
1191 int tabstart=0;
1192 path.Reset();
1193 CString StatAdd;
1194 CString StatDel;
1195 CString file1;
1196 CString file2;
1198 tabstart=log.find('\t',pos);
1199 if(tabstart >=0)
1201 log[tabstart]=0;
1202 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);
1203 pos=tabstart+1;
1206 tabstart=log.find('\t',pos);
1207 if(tabstart >=0)
1209 log[tabstart]=0;
1211 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);
1212 pos=tabstart+1;
1215 if(log[pos] == 0) //rename
1217 ++pos;
1218 g_Git.StringAppend(&file2, &log[pos], CP_UTF8);
1219 int sec=log.find(0,pos);
1220 if(sec>=0)
1222 ++sec;
1223 g_Git.StringAppend(&file1, &log[sec], CP_UTF8);
1225 pos=sec;
1228 else
1230 g_Git.StringAppend(&file1, &log[pos], CP_UTF8);
1232 path.SetFromGit(file1,&file2);
1234 CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());
1235 if(GitPath)
1237 GitPath->m_StatAdd=StatAdd;
1238 GitPath->m_StatDel=StatDel;
1240 else
1242 //path.SetFromGit(pathname);
1243 if (parseDeletes)
1245 path.m_StatAdd=_T("0");
1246 path.m_StatDel=_T("0");
1247 path.m_Action |= CTGitPath::LOGACTIONS_DELETED;
1249 else
1251 path.m_StatAdd=StatAdd;
1252 path.m_StatDel=StatDel;
1253 path.m_Action |= CTGitPath::LOGACTIONS_FORWORD;
1255 AddPath(path);
1259 pos=log.findNextString(pos);
1261 return pos;
1264 void CTGitPathList::AddPath(const CTGitPath& newPath)
1266 m_paths.push_back(newPath);
1267 m_commonBaseDirectory.Reset();
1269 int CTGitPathList::GetCount() const
1271 return (int)m_paths.size();
1273 void CTGitPathList::Clear()
1275 m_paths.clear();
1276 m_commonBaseDirectory.Reset();
1279 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const
1281 ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());
1282 return m_paths[index];
1285 bool CTGitPathList::AreAllPathsFiles() const
1287 // Look through the vector for any directories - if we find them, return false
1288 return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();
1292 #if defined(_MFC_VER)
1294 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)
1296 Clear();
1299 CString strLine;
1300 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
1302 // for every selected file/folder
1303 CTGitPath path;
1304 while (file.ReadString(strLine))
1306 path.SetFromUnknown(strLine);
1307 AddPath(path);
1309 file.Close();
1311 catch (CFileException* pE)
1313 TRACE("CFileException loading target file list\n");
1314 TCHAR error[10000] = {0};
1315 pE->GetErrorMessage(error, 10000);
1316 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1317 pE->Delete();
1318 return false;
1320 return true;
1323 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const
1327 if (bANSI)
1329 CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
1330 PathVector::const_iterator it;
1331 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1333 CStringA line = CStringA(it->GetGitPathString()) + '\n';
1334 file.Write(line, line.GetLength());
1336 file.Close();
1338 else
1340 CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);
1341 PathVector::const_iterator it;
1342 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1344 file.WriteString(it->GetGitPathString()+_T("\n"));
1346 file.Close();
1349 catch (CFileException* pE)
1351 TRACE("CFileException in writing temp file\n");
1352 pE->Delete();
1353 return false;
1355 return true;
1359 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)
1361 int pos = 0;
1362 CString temp;
1363 for(;;)
1365 temp = sPathString.Tokenize(_T("*"),pos);
1366 if(temp.IsEmpty())
1368 break;
1370 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));
1374 CString CTGitPathList::CreateAsteriskSeparatedString() const
1376 CString sRet;
1377 PathVector::const_iterator it;
1378 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1380 if (!sRet.IsEmpty())
1381 sRet += _T("*");
1382 sRet += it->GetWinPathString();
1384 return sRet;
1386 #endif // _MFC_VER
1388 bool
1389 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1391 // Check if all the paths are files and in the same directory
1392 PathVector::const_iterator it;
1393 m_commonBaseDirectory.Reset();
1394 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1396 if(it->IsDirectory())
1398 return false;
1400 const CTGitPath& baseDirectory = it->GetDirectory();
1401 if(m_commonBaseDirectory.IsEmpty())
1403 m_commonBaseDirectory = baseDirectory;
1405 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1407 // Different path
1408 m_commonBaseDirectory.Reset();
1409 return false;
1412 return true;
1415 CTGitPath CTGitPathList::GetCommonDirectory() const
1417 if (m_commonBaseDirectory.IsEmpty())
1419 PathVector::const_iterator it;
1420 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1422 const CTGitPath& baseDirectory = it->GetDirectory();
1423 if(m_commonBaseDirectory.IsEmpty())
1425 m_commonBaseDirectory = baseDirectory;
1427 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1429 // Different path
1430 m_commonBaseDirectory.Reset();
1431 break;
1435 // since we only checked strings, not paths,
1436 // we have to make sure now that we really return a *path* here
1437 PathVector::const_iterator iter;
1438 for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)
1440 if (!m_commonBaseDirectory.IsAncestorOf(*iter))
1442 m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();
1443 break;
1446 return m_commonBaseDirectory;
1449 CTGitPath CTGitPathList::GetCommonRoot() const
1451 if (GetCount() == 0)
1452 return CTGitPath();
1454 if (GetCount() == 1)
1455 return m_paths[0];
1457 // first entry is common root for itself
1458 // (add trailing '\\' to detect partial matches of the last path element)
1459 CString root = m_paths[0].GetWinPathString() + _T('\\');
1460 int rootLength = root.GetLength();
1462 // determine common path string prefix
1463 for (PathVector::const_iterator it = m_paths.begin() + 1; it != m_paths.end(); ++it)
1465 CString path = it->GetWinPathString() + _T('\\');
1467 int newLength = CStringUtils::GetMatchingLength(root, path);
1468 if (newLength != rootLength)
1470 root.Delete(newLength, rootLength);
1471 rootLength = newLength;
1475 // remove the last (partial) path element
1476 if (rootLength > 0)
1477 root.Delete(root.ReverseFind(_T('\\')), rootLength);
1479 // done
1480 return CTGitPath(root);
1483 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)
1485 std::sort(m_paths.begin(), m_paths.end());
1486 if (bReverse)
1487 std::reverse(m_paths.begin(), m_paths.end());
1490 void CTGitPathList::DeleteAllFiles(bool bTrash, bool bFilesOnly)
1492 PathVector::const_iterator it;
1493 SortByPathname(true); // nested ones first
1495 CString sPaths;
1496 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1498 if ((it->Exists()) && ((it->IsDirectory() != bFilesOnly) || !bFilesOnly))
1500 if (!it->IsDirectory())
1501 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1503 sPaths += it->GetWinPath();
1504 sPaths += '\0';
1507 sPaths += '\0';
1508 sPaths += '\0';
1509 DeleteViaShell((LPCTSTR)sPaths, bTrash);
1510 Clear();
1513 bool CTGitPathList::DeleteViaShell(LPCTSTR path, bool bTrash)
1515 SHFILEOPSTRUCT shop = {0};
1516 shop.wFunc = FO_DELETE;
1517 shop.pFrom = path;
1518 shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT|FOF_NO_CONNECTED_ELEMENTS;
1519 if (bTrash)
1520 shop.fFlags |= FOF_ALLOWUNDO;
1521 const bool bRet = (SHFileOperation(&shop) == 0);
1522 return bRet;
1525 void CTGitPathList::RemoveDuplicates()
1527 SortByPathname();
1528 // Remove the duplicates
1529 // (Unique moves them to the end of the vector, then erase chops them off)
1530 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());
1533 void CTGitPathList::RemoveAdminPaths()
1535 PathVector::iterator it;
1536 for(it = m_paths.begin(); it != m_paths.end(); )
1538 if (it->IsAdminDir())
1540 m_paths.erase(it);
1541 it = m_paths.begin();
1543 else
1544 ++it;
1548 void CTGitPathList::RemovePath(const CTGitPath& path)
1550 PathVector::iterator it;
1551 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1553 if (it->IsEquivalentTo(path))
1555 m_paths.erase(it);
1556 return;
1561 void CTGitPathList::RemoveItem(CTGitPath & path)
1563 PathVector::iterator it;
1564 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1566 if (it->GetGitPathString()==path.GetGitPathString())
1568 m_paths.erase(it);
1569 return;
1573 void CTGitPathList::RemoveChildren()
1575 SortByPathname();
1576 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());
1579 bool CTGitPathList::IsEqual(const CTGitPathList& list)
1581 if (list.GetCount() != GetCount())
1582 return false;
1583 for (int i=0; i<list.GetCount(); ++i)
1585 if (!list[i].IsEquivalentTo(m_paths[i]))
1586 return false;
1588 return true;
1591 //////////////////////////////////////////////////////////////////////////
1592 #if 0
1593 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const
1595 apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));
1597 for (int nItem = 0; nItem < GetCount(); ++nItem)
1599 const char * target = m_paths[nItem].GetGitApiPath(pool);
1600 (*((const char **) apr_array_push (targets))) = target;
1603 return targets;
1605 #endif
1606 //////////////////////////////////////////////////////////////////////////
1608 #if 0
1609 #if defined(_DEBUG)
1610 // Some test cases for these classes
1611 static class CTGitPathTests
1613 public:
1614 CTGitPathTests()
1616 apr_initialize();
1617 pool = svn_pool_create(NULL);
1618 GetDirectoryTest();
1619 AdminDirTest();
1620 SortTest();
1621 RawAppendTest();
1622 PathAppendTest();
1623 RemoveDuplicatesTest();
1624 RemoveChildrenTest();
1625 ContainingDirectoryTest();
1626 AncestorTest();
1627 SubversionPathTest();
1628 GetCommonRootTest();
1629 #if defined(_MFC_VER)
1630 ValidPathAndUrlTest();
1631 ListLoadingTest();
1632 #endif
1633 apr_terminate();
1636 private:
1637 // apr_pool_t * pool;
1638 void GetDirectoryTest()
1640 // Bit tricky, this test, because we need to know something about the file
1641 // layout on the machine which is running the test
1642 TCHAR winDir[MAX_PATH+1];
1643 GetWindowsDirectory(winDir, MAX_PATH);
1644 CString sWinDir(winDir);
1646 CTGitPath testPath;
1647 // This is a file which we know will always be there
1648 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));
1649 ATLASSERT(!testPath.IsDirectory());
1650 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1651 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);
1653 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1654 // but we know it must be different to the directory itself
1655 testPath.SetFromUnknown(sWinDir);
1656 ATLASSERT(testPath.IsDirectory());
1657 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1658 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);
1659 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());
1661 // Try a root path
1662 testPath.SetFromUnknown(_T("C:\\"));
1663 ATLASSERT(testPath.IsDirectory());
1664 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1665 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1666 // Try a root UNC path
1667 testPath.SetFromUnknown(_T("\\MYSTATION"));
1668 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1671 void AdminDirTest()
1673 CTGitPath testPath;
1674 testPath.SetFromUnknown(_T("c:\\.svndir"));
1675 ATLASSERT(!testPath.IsAdminDir());
1676 testPath.SetFromUnknown(_T("c:\\test.svn"));
1677 ATLASSERT(!testPath.IsAdminDir());
1678 testPath.SetFromUnknown(_T("c:\\.svn"));
1679 ATLASSERT(testPath.IsAdminDir());
1680 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));
1681 ATLASSERT(!testPath.IsAdminDir());
1682 testPath.SetFromUnknown(_T("c:\\.svn\\test"));
1683 ATLASSERT(testPath.IsAdminDir());
1685 CTGitPathList pathList;
1686 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));
1687 pathList.AddPath(CTGitPath(_T("c:\\.svn")));
1688 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1689 pathList.AddPath(CTGitPath(_T("c:\\test")));
1690 pathList.RemoveAdminPaths();
1691 ATLASSERT(pathList.GetCount()==2);
1692 pathList.Clear();
1693 pathList.AddPath(CTGitPath(_T("c:\\test")));
1694 pathList.RemoveAdminPaths();
1695 ATLASSERT(pathList.GetCount()==1);
1698 void SortTest()
1700 CTGitPathList testList;
1701 CTGitPath testPath;
1702 testPath.SetFromUnknown(_T("c:/Z"));
1703 testList.AddPath(testPath);
1704 testPath.SetFromUnknown(_T("c:/B"));
1705 testList.AddPath(testPath);
1706 testPath.SetFromUnknown(_T("c:\\a"));
1707 testList.AddPath(testPath);
1708 testPath.SetFromUnknown(_T("c:/Test"));
1709 testList.AddPath(testPath);
1711 testList.SortByPathname();
1713 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));
1714 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));
1715 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));
1716 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));
1719 void RawAppendTest()
1721 CTGitPath testPath(_T("c:/test/"));
1722 testPath.AppendRawString(_T("/Hello"));
1723 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1725 testPath.AppendRawString(_T("\\T2"));
1726 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1728 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1729 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1730 testBasePath.AppendRawString(testFilePath.GetFileExtension());
1731 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1734 void PathAppendTest()
1736 CTGitPath testPath(_T("c:/test/"));
1737 testPath.AppendPathString(_T("/Hello"));
1738 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1740 testPath.AppendPathString(_T("T2"));
1741 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1743 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1744 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1745 // You wouldn't want to do this in real life - you'd use append-raw
1746 testBasePath.AppendPathString(testFilePath.GetFileExtension());
1747 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1750 void RemoveDuplicatesTest()
1752 CTGitPathList list;
1753 list.AddPath(CTGitPath(_T("Z")));
1754 list.AddPath(CTGitPath(_T("A")));
1755 list.AddPath(CTGitPath(_T("E")));
1756 list.AddPath(CTGitPath(_T("E")));
1758 ATLASSERT(list[2].IsEquivalentTo(list[3]));
1759 ATLASSERT(list[2]==list[3]);
1761 ATLASSERT(list.GetCount() == 4);
1763 list.RemoveDuplicates();
1765 ATLASSERT(list.GetCount() == 3);
1767 ATLASSERT(list[0].GetWinPathString() == _T("A"));
1768 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);
1769 ATLASSERT(list[2].GetWinPathString() == _T("Z"));
1772 void RemoveChildrenTest()
1774 CTGitPathList list;
1775 list.AddPath(CTGitPath(_T("c:\\test")));
1776 list.AddPath(CTGitPath(_T("c:\\test\\file")));
1777 list.AddPath(CTGitPath(_T("c:\\testfile")));
1778 list.AddPath(CTGitPath(_T("c:\\parent")));
1779 list.AddPath(CTGitPath(_T("c:\\parent\\child")));
1780 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1781 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1783 ATLASSERT(list.GetCount() == 7);
1785 list.RemoveChildren();
1787 ATLTRACE("count = %d\n", list.GetCount());
1788 ATLASSERT(list.GetCount() == 3);
1790 list.SortByPathname();
1792 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1793 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1794 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1797 #if defined(_MFC_VER)
1798 void ListLoadingTest()
1800 TCHAR buf[MAX_PATH];
1801 GetCurrentDirectory(MAX_PATH, buf);
1802 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1803 CTGitPathList testList;
1804 testList.LoadFromAsteriskSeparatedString(sPathList);
1806 ATLASSERT(testList.GetCount() == 3);
1807 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));
1808 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1809 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));
1811 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));
1812 testList.Clear();
1813 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1814 testList.LoadFromAsteriskSeparatedString(sPathList);
1815 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1817 #endif
1819 void ContainingDirectoryTest()
1822 CTGitPath testPath;
1823 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1824 CTGitPath dir;
1825 dir = testPath.GetContainingDirectory();
1826 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1827 dir = dir.GetContainingDirectory();
1828 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));
1829 dir = dir.GetContainingDirectory();
1830 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));
1831 dir = dir.GetContainingDirectory();
1832 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));
1833 dir = dir.GetContainingDirectory();
1834 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));
1835 dir = dir.GetContainingDirectory();
1836 ATLASSERT(dir.IsEmpty());
1837 ATLASSERT(dir.GetWinPathString() == _T(""));
1840 void AncestorTest()
1842 CTGitPath testPath;
1843 testPath.SetFromWin(_T("c:\\windows"));
1844 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1845 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1846 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1847 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1848 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1851 void SubversionPathTest()
1853 CTGitPath testPath;
1854 testPath.SetFromWin(_T("c:\\"));
1855 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);
1856 testPath.SetFromWin(_T("c:\\folder"));
1857 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);
1858 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1859 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);
1860 testPath.SetFromUnknown(_T("http://testing/"));
1861 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);
1862 testPath.SetFromGit(NULL);
1863 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);
1864 #if defined(_MFC_VER)
1865 testPath.SetFromUnknown(_T("http://testing again"));
1866 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1867 testPath.SetFromUnknown(_T("http://testing%20again"));
1868 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1869 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1870 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1871 #endif
1874 void GetCommonRootTest()
1876 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));
1877 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));
1878 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1880 CTGitPathList list;
1881 list.AddPath(pathA);
1882 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1883 list.AddPath(pathB);
1884 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1885 list.AddPath(pathC);
1886 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1887 #ifdef _MFC_VER
1888 list.Clear();
1889 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");
1890 list.LoadFromAsteriskSeparatedString(sPathList);
1891 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1893 list.Clear();
1894 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");
1895 list.LoadFromAsteriskSeparatedString(sPathList);
1896 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1898 list.Clear();
1899 sPathList = _T("c:\\windows\\*c:\\windows");
1900 list.LoadFromAsteriskSeparatedString(sPathList);
1901 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1903 list.Clear();
1904 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");
1905 list.LoadFromAsteriskSeparatedString(sPathList);
1906 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1908 list.Clear();
1909 sPathList = _T("c:\\windowsdummy*c:\\windows");
1910 list.LoadFromAsteriskSeparatedString(sPathList);
1911 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1912 #endif
1915 void ValidPathAndUrlTest()
1917 CTGitPath testPath;
1918 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1919 ATLASSERT(testPath.IsValidOnWindows());
1920 testPath.SetFromWin(_T("c:\\"));
1921 ATLASSERT(testPath.IsValidOnWindows());
1922 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1923 ATLASSERT(testPath.IsValidOnWindows());
1924 testPath.SetFromWin(_T("c"));
1925 ATLASSERT(testPath.IsValidOnWindows());
1926 testPath.SetFromWin(_T("c:\\test folder\\file"));
1927 ATLASSERT(testPath.IsValidOnWindows());
1928 testPath.SetFromWin(_T("c:\\folder\\"));
1929 ATLASSERT(testPath.IsValidOnWindows());
1930 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1931 ATLASSERT(testPath.IsValidOnWindows());
1932 testPath.SetFromWin(_T("c:\\.svn"));
1933 ATLASSERT(testPath.IsValidOnWindows());
1934 testPath.SetFromWin(_T("c:\\com\\file"));
1935 ATLASSERT(testPath.IsValidOnWindows());
1936 testPath.SetFromWin(_T("c:\\test\\conf"));
1937 ATLASSERT(testPath.IsValidOnWindows());
1938 testPath.SetFromWin(_T("c:\\LPT"));
1939 ATLASSERT(testPath.IsValidOnWindows());
1940 testPath.SetFromWin(_T("c:\\test\\LPT"));
1941 ATLASSERT(testPath.IsValidOnWindows());
1942 testPath.SetFromWin(_T("c:\\com1test"));
1943 ATLASSERT(testPath.IsValidOnWindows());
1944 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1945 ATLASSERT(testPath.IsValidOnWindows());
1947 testPath.SetFromWin(_T("\\\\Share\\filename"));
1948 ATLASSERT(testPath.IsValidOnWindows());
1949 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));
1950 ATLASSERT(testPath.IsValidOnWindows());
1951 testPath.SetFromWin(_T("\\\\Share\\.svn"));
1952 ATLASSERT(testPath.IsValidOnWindows());
1954 // now the negative tests
1955 testPath.SetFromWin(_T("c:\\test:folder"));
1956 ATLASSERT(!testPath.IsValidOnWindows());
1957 testPath.SetFromWin(_T("c:\\file<name"));
1958 ATLASSERT(!testPath.IsValidOnWindows());
1959 testPath.SetFromWin(_T("c:\\something*else"));
1960 ATLASSERT(!testPath.IsValidOnWindows());
1961 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));
1962 ATLASSERT(!testPath.IsValidOnWindows());
1963 testPath.SetFromWin(_T("c:\\ext.>ension"));
1964 ATLASSERT(!testPath.IsValidOnWindows());
1965 testPath.SetFromWin(_T("c:\\com1\\filename"));
1966 ATLASSERT(!testPath.IsValidOnWindows());
1967 testPath.SetFromWin(_T("c:\\com1"));
1968 ATLASSERT(!testPath.IsValidOnWindows());
1969 testPath.SetFromWin(_T("c:\\com1\\AuX"));
1970 ATLASSERT(!testPath.IsValidOnWindows());
1972 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1973 ATLASSERT(!testPath.IsValidOnWindows());
1974 testPath.SetFromWin(_T("\\\\Share\\prn"));
1975 ATLASSERT(!testPath.IsValidOnWindows());
1976 testPath.SetFromWin(_T("\\\\Share\\NUL"));
1977 ATLASSERT(!testPath.IsValidOnWindows());
1979 // now come some URL tests
1980 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));
1981 ATLASSERT(testPath.IsValidOnWindows());
1982 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1983 ATLASSERT(testPath.IsValidOnWindows());
1984 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1985 ATLASSERT(testPath.IsValidOnWindows());
1986 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1987 ATLASSERT(testPath.IsValidOnWindows());
1988 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));
1989 ATLASSERT(testPath.IsValidOnWindows());
1990 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1991 ATLASSERT(testPath.IsValidOnWindows());
1992 // and some negative URL tests
1993 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1994 ATLASSERT(!testPath.IsValidOnWindows());
1995 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1996 ATLASSERT(!testPath.IsValidOnWindows());
1997 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1998 ATLASSERT(!testPath.IsValidOnWindows());
1999 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
2000 ATLASSERT(!testPath.IsValidOnWindows());
2001 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
2002 ATLASSERT(!testPath.IsValidOnWindows());
2006 } TGitPathTestobject;
2007 #endif
2008 #endif
2010 CTGitPath * CTGitPathList::LookForGitPath(CString path)
2012 int i=0;
2013 for (i = 0; i < this->GetCount(); ++i)
2015 if((*this)[i].GetGitPathString() == path )
2016 return (CTGitPath*)&(*this)[i];
2018 return NULL;
2020 CString CTGitPath::GetActionName(int action)
2022 if(action & CTGitPath::LOGACTIONS_UNMERGED)
2023 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT);
2024 if(action & CTGitPath::LOGACTIONS_ADDED)
2025 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD);
2026 if(action & CTGitPath::LOGACTIONS_DELETED)
2027 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE);
2028 if(action & CTGitPath::LOGACTIONS_MERGED )
2029 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED);
2031 if(action & CTGitPath::LOGACTIONS_MODIFIED)
2032 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED);
2033 if(action & CTGitPath::LOGACTIONS_REPLACED)
2034 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME);
2035 if(action & CTGitPath::LOGACTIONS_COPY)
2036 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY);
2038 if(action & CTGitPath::LOGACTIONS_FORWORD )
2039 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD);
2041 if(action & CTGitPath::LOGACTIONS_REBASE_EDIT)
2042 return MAKEINTRESOURCE(IDS_PATHACTIONS_EDIT);
2043 if(action & CTGitPath::LOGACTIONS_REBASE_SQUASH)
2044 return MAKEINTRESOURCE(IDS_PATHACTIONS_SQUASH);
2045 if(action & CTGitPath::LOGACTIONS_REBASE_PICK)
2046 return MAKEINTRESOURCE(IDS_PATHACTIONS_PICK);
2047 if(action & CTGitPath::LOGACTIONS_REBASE_SKIP)
2048 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIP);
2050 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN);
2052 CString CTGitPath::GetActionName()
2054 return GetActionName(m_Action);
2057 int CTGitPathList::GetAction()
2059 return m_Action;