Merge branch 'RevisionGraph'
[TortoiseGit.git] / src / Git / TGitPath.cpp
blob4f0a1352548c9c2343508ab14b045b22819d6a94
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
4 // Copyright (C) 2010-2012 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"
32 #if defined(_MFC_VER)
33 //#include "MessageBox.h"
34 //#include "AppUtils.h"
35 #endif
37 #ifndef ASSERT
38 #define ASSERT()
39 #endif
40 using namespace std;
41 extern CGit g_Git;
43 CTGitPath::CTGitPath(void)
44 : m_bDirectoryKnown(false)
45 , m_bIsDirectory(false)
46 , m_bURLKnown(false)
47 , m_bHasAdminDirKnown(false)
48 , m_bHasAdminDir(false)
49 , m_bIsValidOnWindowsKnown(false)
50 , m_bIsValidOnWindows(false)
51 , m_bIsReadOnly(false)
52 , m_bIsAdminDirKnown(false)
53 , m_bIsAdminDir(false)
54 , m_bExists(false)
55 , m_bExistsKnown(false)
56 , m_bLastWriteTimeKnown(0)
57 , m_lastWriteTime(0)
58 , m_customData(NULL)
59 , m_bIsSpecialDirectoryKnown(false)
60 , m_bIsSpecialDirectory(false)
61 , m_bIsWCRootKnown(false)
62 , m_bIsWCRoot(false)
63 , m_fileSize(0)
65 m_Action=0;
66 m_ParentNo=0;
69 CTGitPath::~CTGitPath(void)
72 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
73 CTGitPath::CTGitPath(const CString& sUnknownPath) :
74 m_bDirectoryKnown(false)
75 , m_bIsDirectory(false)
76 , m_bURLKnown(false)
77 , m_bHasAdminDirKnown(false)
78 , m_bHasAdminDir(false)
79 , m_bIsValidOnWindowsKnown(false)
80 , m_bIsValidOnWindows(false)
81 , m_bIsReadOnly(false)
82 , m_bIsAdminDirKnown(false)
83 , m_bIsAdminDir(false)
84 , m_bExists(false)
85 , m_bExistsKnown(false)
86 , m_bLastWriteTimeKnown(0)
87 , m_lastWriteTime(0)
88 , m_customData(NULL)
89 , m_bIsSpecialDirectoryKnown(false)
90 , m_bIsSpecialDirectory(false)
91 , m_bIsWCRootKnown(false)
92 , m_bIsWCRoot(false)
93 , m_fileSize(0)
95 SetFromUnknown(sUnknownPath);
96 m_Action=0;
97 m_Stage=0;
98 m_ParentNo=0;
101 int CTGitPath::ParserAction(BYTE action)
103 //action=action.TrimLeft();
104 //TCHAR c=action.GetAt(0);
105 if(action == 'M')
106 m_Action|= LOGACTIONS_MODIFIED;
107 if(action == 'R')
108 m_Action|= LOGACTIONS_REPLACED;
109 if(action == 'A')
110 m_Action|= LOGACTIONS_ADDED;
111 if(action == 'D')
112 m_Action|= LOGACTIONS_DELETED;
113 if(action == 'U')
114 m_Action|= LOGACTIONS_UNMERGED;
115 if(action == 'K')
116 m_Action|= LOGACTIONS_DELETED;
117 if(action == 'H')
118 m_Action|= LOGACTIONS_CACHE;
119 if(action == 'C' )
120 m_Action|= LOGACTIONS_COPY;
122 return m_Action;
124 void CTGitPath::SetFromGit(const char* pPath)
126 Reset();
127 if (pPath == NULL)
128 return;
129 int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);
130 if (len)
132 len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, m_sFwdslashPath.GetBuffer(len+1), len+1);
133 m_sFwdslashPath.ReleaseBuffer(len-1);
135 SanitizeRootPath(m_sFwdslashPath, true);
138 void CTGitPath::SetFromGit(const char* pPath, bool bIsDirectory)
140 SetFromGit(pPath);
141 m_bDirectoryKnown = true;
142 m_bIsDirectory = bIsDirectory;
145 void CTGitPath::SetFromGit(const TCHAR* pPath, bool bIsDirectory)
147 Reset();
148 if (pPath)
150 m_sFwdslashPath = pPath;
151 SanitizeRootPath(m_sFwdslashPath, true);
153 m_bDirectoryKnown = true;
154 m_bIsDirectory = bIsDirectory;
157 void CTGitPath::SetFromGit(const CString& sPath,CString *oldpath)
159 Reset();
160 m_sFwdslashPath = sPath;
161 SanitizeRootPath(m_sFwdslashPath, true);
162 if(oldpath)
163 m_sOldFwdslashPath = *oldpath;
166 void CTGitPath::SetFromWin(LPCTSTR pPath)
168 Reset();
169 m_sBackslashPath = pPath;
170 m_sBackslashPath.Replace(L"\\\\?\\", L"");
171 SanitizeRootPath(m_sBackslashPath, false);
172 ATLASSERT(m_sBackslashPath.Find('/')<0);
174 void CTGitPath::SetFromWin(const CString& sPath)
176 Reset();
177 m_sBackslashPath = sPath;
178 m_sBackslashPath.Replace(L"\\\\?\\", L"");
179 SanitizeRootPath(m_sBackslashPath, false);
181 void CTGitPath::SetFromWin(LPCTSTR pPath, bool bIsDirectory)
183 Reset();
184 m_sBackslashPath = pPath;
185 m_bIsDirectory = bIsDirectory;
186 m_bDirectoryKnown = true;
187 SanitizeRootPath(m_sBackslashPath, false);
189 void CTGitPath::SetFromWin(const CString& sPath, bool bIsDirectory)
191 Reset();
192 m_sBackslashPath = sPath;
193 m_bIsDirectory = bIsDirectory;
194 m_bDirectoryKnown = true;
195 SanitizeRootPath(m_sBackslashPath, false);
197 void CTGitPath::SetFromUnknown(const CString& sPath)
199 Reset();
200 // Just set whichever path we think is most likely to be used
201 // GitAdminDir admin;
202 // CString p;
203 // if(admin.HasAdminDir(sPath,&p))
204 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
205 // else
206 SetFwdslashPath(sPath);
209 LPCTSTR CTGitPath::GetWinPath() const
211 if(IsEmpty())
213 return _T("");
215 if(m_sBackslashPath.IsEmpty())
217 SetBackslashPath(m_sFwdslashPath);
219 return m_sBackslashPath;
221 // This is a temporary function, to be used during the migration to
222 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
223 const CString& CTGitPath::GetWinPathString() const
225 if(m_sBackslashPath.IsEmpty())
227 SetBackslashPath(m_sFwdslashPath);
229 return m_sBackslashPath;
232 const CString& CTGitPath::GetGitPathString() const
234 if(m_sFwdslashPath.IsEmpty())
236 SetFwdslashPath(m_sBackslashPath);
238 return m_sFwdslashPath;
241 const CString &CTGitPath::GetGitOldPathString() const
243 return m_sOldFwdslashPath;
245 #if 0
246 const char* CTGitPath::GetGitApiPath(apr_pool_t *pool) const
248 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
249 // each call to GetGitApiPath returns a different pointer value.
250 // If you made multiple calls to GetGitApiPath on the same string, only the last
251 // one would give you a valid pointer to an empty string, because each
252 // call would invalidate the previous call's return.
253 if(IsEmpty())
255 return "";
257 if(m_sFwdslashPath.IsEmpty())
259 SetFwdslashPath(m_sBackslashPath);
261 if(m_sUTF8FwdslashPath.IsEmpty())
263 SetUTF8FwdslashPath(m_sFwdslashPath);
265 if (svn_path_is_url(m_sUTF8FwdslashPath))
267 m_sUTF8FwdslashPathEscaped = CPathUtils::PathEscape(m_sUTF8FwdslashPath);
268 m_sUTF8FwdslashPathEscaped.Replace("file:////", "file:///\\");
269 m_sUTF8FwdslashPathEscaped = svn_path_canonicalize(m_sUTF8FwdslashPathEscaped, pool);
270 return m_sUTF8FwdslashPathEscaped;
272 m_sUTF8FwdslashPath = svn_path_canonicalize(m_sUTF8FwdslashPath, pool);
274 return m_sUTF8FwdslashPath;
276 #endif
278 const CString& CTGitPath::GetUIPathString() const
280 if (m_sUIPath.IsEmpty())
282 m_sUIPath = GetWinPathString();
284 return m_sUIPath;
287 void CTGitPath::SetFwdslashPath(const CString& sPath) const
289 m_sFwdslashPath = sPath;
290 m_sFwdslashPath.Replace('\\', '/');
292 // We don't leave a trailing /
293 m_sFwdslashPath.TrimRight('/');
294 m_sFwdslashPath.Replace(L"//?/", L"");
296 SanitizeRootPath(m_sFwdslashPath, true);
298 m_sFwdslashPath.Replace(_T("file:////"), _T("file://"));
300 m_sUTF8FwdslashPath.Empty();
303 void CTGitPath::SetBackslashPath(const CString& sPath) const
305 m_sBackslashPath = sPath;
306 m_sBackslashPath.Replace('/', '\\');
307 m_sBackslashPath.TrimRight('\\');
308 SanitizeRootPath(m_sBackslashPath, false);
311 void CTGitPath::SetUTF8FwdslashPath(const CString& sPath) const
313 m_sUTF8FwdslashPath = CUnicodeUtils::GetUTF8(sPath);
316 void CTGitPath::SanitizeRootPath(CString& sPath, bool bIsForwardPath) const
318 // Make sure to add the trailing slash to root paths such as 'C:'
319 if (sPath.GetLength() == 2 && sPath[1] == ':')
321 sPath += (bIsForwardPath) ? _T("/") : _T("\\");
325 bool CTGitPath::IsDirectory() const
327 if(!m_bDirectoryKnown)
329 UpdateAttributes();
331 return m_bIsDirectory;
334 bool CTGitPath::Exists() const
336 if (!m_bExistsKnown)
338 UpdateAttributes();
340 return m_bExists;
343 bool CTGitPath::Delete(bool bTrash) const
345 EnsureBackslashPathSet();
346 ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);
347 bool bRet = false;
348 if (Exists())
350 if ((bTrash)||(IsDirectory()))
352 TCHAR * buf = new TCHAR[m_sBackslashPath.GetLength()+2];
353 _tcscpy_s(buf, m_sBackslashPath.GetLength()+2, m_sBackslashPath);
354 buf[m_sBackslashPath.GetLength()] = 0;
355 buf[m_sBackslashPath.GetLength()+1] = 0;
356 bRet = CTGitPathList::DeleteViaShell(buf, bTrash);
357 delete [] buf;
359 else
361 bRet = !!::DeleteFile(m_sBackslashPath);
364 m_bExists = false;
365 m_bExistsKnown = true;
366 return bRet;
369 __int64 CTGitPath::GetLastWriteTime() const
371 if(!m_bLastWriteTimeKnown)
373 UpdateAttributes();
375 return m_lastWriteTime;
378 __int64 CTGitPath::GetFileSize() const
380 if(!m_bDirectoryKnown)
382 UpdateAttributes();
384 return m_fileSize;
387 bool CTGitPath::IsReadOnly() const
389 if(!m_bLastWriteTimeKnown)
391 UpdateAttributes();
393 return m_bIsReadOnly;
396 void CTGitPath::UpdateAttributes() const
398 EnsureBackslashPathSet();
399 WIN32_FILE_ATTRIBUTE_DATA attribs;
400 if (m_sBackslashPath.GetLength() >= 248)
401 m_sLongBackslashPath = _T("\\\\?\\") + m_sBackslashPath;
402 if(GetFileAttributesEx(m_sBackslashPath.GetLength() >= 248 ? m_sLongBackslashPath : m_sBackslashPath, GetFileExInfoStandard, &attribs))
404 m_bIsDirectory = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
405 m_lastWriteTime = *(__int64*)&attribs.ftLastWriteTime;
406 if (m_bIsDirectory)
408 m_fileSize = 0;
410 else
412 m_fileSize = ((INT64)( (DWORD)(attribs.nFileSizeLow) ) | ( (INT64)( (DWORD)(attribs.nFileSizeHigh) )<<32 ));
414 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
415 m_bExists = true;
417 else
419 m_bIsDirectory = false;
420 m_lastWriteTime = 0;
421 m_fileSize = 0;
422 DWORD err = GetLastError();
423 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))
425 m_bExists = false;
427 else
429 m_bExists = true;
430 return;
433 m_bDirectoryKnown = true;
434 m_bLastWriteTimeKnown = true;
435 m_bExistsKnown = true;
438 CTGitPath CTGitPath::GetSubPath(const CTGitPath &root)
440 CTGitPath path;
442 if(GetWinPathString().Left(root.GetWinPathString().GetLength()) == root.GetWinPathString())
444 CString str=GetWinPathString();
445 path.SetFromWin(str.Right(str.GetLength()-root.GetWinPathString().GetLength()-1));
447 return path;
450 void CTGitPath::EnsureBackslashPathSet() const
452 if(m_sBackslashPath.IsEmpty())
454 SetBackslashPath(m_sFwdslashPath);
455 ATLASSERT(IsEmpty() || !m_sBackslashPath.IsEmpty());
458 void CTGitPath::EnsureFwdslashPathSet() const
460 if(m_sFwdslashPath.IsEmpty())
462 SetFwdslashPath(m_sBackslashPath);
463 ATLASSERT(IsEmpty() || !m_sFwdslashPath.IsEmpty());
468 // Reset all the caches
469 void CTGitPath::Reset()
471 m_bDirectoryKnown = false;
472 m_bURLKnown = false;
473 m_bLastWriteTimeKnown = false;
474 m_bHasAdminDirKnown = false;
475 m_bIsValidOnWindowsKnown = false;
476 m_bIsAdminDirKnown = false;
477 m_bExistsKnown = false;
478 m_bIsSpecialDirectoryKnown = false;
479 m_bIsSpecialDirectory = false;
481 m_sBackslashPath.Empty();
482 m_sFwdslashPath.Empty();
483 m_sUTF8FwdslashPath.Empty();
484 this->m_Action=0;
485 this->m_StatAdd=_T("");
486 this->m_StatDel=_T("");
487 m_ParentNo=0;
488 ATLASSERT(IsEmpty());
491 CTGitPath CTGitPath::GetDirectory() const
493 if ((IsDirectory())||(!Exists()))
495 return *this;
497 return GetContainingDirectory();
500 CTGitPath CTGitPath::GetContainingDirectory() const
502 EnsureBackslashPathSet();
504 CString sDirName = m_sBackslashPath.Left(m_sBackslashPath.ReverseFind('\\'));
505 if(sDirName.GetLength() == 2 && sDirName[1] == ':')
507 // This is a root directory, which needs a trailing slash
508 sDirName += '\\';
509 if(sDirName == m_sBackslashPath)
511 // We were clearly provided with a root path to start with - we should return nothing now
512 sDirName.Empty();
515 if(sDirName.GetLength() == 1 && sDirName[0] == '\\')
517 // We have an UNC path and we already are the root
518 sDirName.Empty();
520 CTGitPath retVal;
521 retVal.SetFromWin(sDirName);
522 return retVal;
525 CString CTGitPath::GetRootPathString() const
527 EnsureBackslashPathSet();
528 CString workingPath = m_sBackslashPath;
529 LPTSTR pPath = workingPath.GetBuffer(MAX_PATH); // MAX_PATH ok here.
530 ATLVERIFY(::PathStripToRoot(pPath));
531 workingPath.ReleaseBuffer();
532 return workingPath;
536 CString CTGitPath::GetFilename() const
538 //ATLASSERT(!IsDirectory());
539 return GetFileOrDirectoryName();
542 CString CTGitPath::GetFileOrDirectoryName() const
544 EnsureBackslashPathSet();
545 return m_sBackslashPath.Mid(m_sBackslashPath.ReverseFind('\\')+1);
548 CString CTGitPath::GetUIFileOrDirectoryName() const
550 GetUIPathString();
551 return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);
554 CString CTGitPath::GetFileExtension() const
556 if(!IsDirectory())
558 EnsureBackslashPathSet();
559 int dotPos = m_sBackslashPath.ReverseFind('.');
560 int slashPos = m_sBackslashPath.ReverseFind('\\');
561 if (dotPos > slashPos)
562 return m_sBackslashPath.Mid(dotPos);
564 return CString();
566 CString CTGitPath::GetBaseFilename() const
568 int dot;
569 CString filename=GetFilename();
570 dot = filename.ReverseFind(_T('.'));
571 if(dot>0)
572 return filename.Left(dot);
573 else
574 return filename;
577 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)
579 int length = sP1.GetLength();
580 if(length != sP2.GetLength())
582 // Different lengths
583 return false;
585 // We work from the end of the strings, because path differences
586 // are more likely to occur at the far end of a string
587 LPCTSTR pP1Start = sP1;
588 LPCTSTR pP1 = pP1Start+(length-1);
589 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
590 while(length-- > 0)
592 if(_totlower(*pP1--) != _totlower(*pP2--))
594 return false;
597 return true;
600 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)
602 int length = sP1.GetLength();
603 if(length != sP2.GetLength())
605 // Different lengths
606 return false;
608 // We work from the end of the strings, because path differences
609 // are more likely to occur at the far end of a string
610 LPCTSTR pP1Start = sP1;
611 LPCTSTR pP1 = pP1Start+(length-1);
612 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
613 while(length-- > 0)
615 if((*pP1--) != (*pP2--))
617 return false;
620 return true;
623 bool CTGitPath::IsEmpty() const
625 // Check the backward slash path first, since the chance that this
626 // one is set is higher. In case of a 'false' return value it's a little
627 // bit faster.
628 return m_sBackslashPath.IsEmpty() && m_sFwdslashPath.IsEmpty();
631 // Test if both paths refer to the same item
632 // Ignores case and slash direction
633 bool CTGitPath::IsEquivalentTo(const CTGitPath& rhs) const
635 // Try and find a slash direction which avoids having to convert
636 // both filenames
637 if(!m_sBackslashPath.IsEmpty())
639 // *We've* got a \ path - make sure that the RHS also has a \ path
640 rhs.EnsureBackslashPathSet();
641 return ArePathStringsEqualWithCase(m_sBackslashPath, rhs.m_sBackslashPath);
643 else
645 // Assume we've got a fwdslash path and make sure that the RHS has one
646 rhs.EnsureFwdslashPathSet();
647 return ArePathStringsEqualWithCase(m_sFwdslashPath, rhs.m_sFwdslashPath);
651 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath& rhs) const
653 // Try and find a slash direction which avoids having to convert
654 // both filenames
655 if(!m_sBackslashPath.IsEmpty())
657 // *We've* got a \ path - make sure that the RHS also has a \ path
658 rhs.EnsureBackslashPathSet();
659 return ArePathStringsEqual(m_sBackslashPath, rhs.m_sBackslashPath);
661 else
663 // Assume we've got a fwdslash path and make sure that the RHS has one
664 rhs.EnsureFwdslashPathSet();
665 return ArePathStringsEqual(m_sFwdslashPath, rhs.m_sFwdslashPath);
669 bool CTGitPath::IsAncestorOf(const CTGitPath& possibleDescendant) const
671 possibleDescendant.EnsureBackslashPathSet();
672 EnsureBackslashPathSet();
674 bool bPathStringsEqual = ArePathStringsEqual(m_sBackslashPath, possibleDescendant.m_sBackslashPath.Left(m_sBackslashPath.GetLength()));
675 if (m_sBackslashPath.GetLength() >= possibleDescendant.GetWinPathString().GetLength())
677 return bPathStringsEqual;
680 return (bPathStringsEqual &&
681 ((possibleDescendant.m_sBackslashPath[m_sBackslashPath.GetLength()] == '\\')||
682 (m_sBackslashPath.GetLength()==3 && m_sBackslashPath[1]==':')));
685 // Get a string representing the file path, optionally with a base
686 // section stripped off the front.
687 CString CTGitPath::GetDisplayString(const CTGitPath* pOptionalBasePath /* = NULL*/) const
689 EnsureFwdslashPathSet();
690 if(pOptionalBasePath != NULL)
692 // Find the length of the base-path without having to do an 'ensure' on it
693 int baseLength = max(pOptionalBasePath->m_sBackslashPath.GetLength(), pOptionalBasePath->m_sFwdslashPath.GetLength());
695 // Now, chop that baseLength of the front of the path
696 return m_sFwdslashPath.Mid(baseLength).TrimLeft('/');
698 return m_sFwdslashPath;
701 int CTGitPath::Compare(const CTGitPath& left, const CTGitPath& right)
703 left.EnsureBackslashPathSet();
704 right.EnsureBackslashPathSet();
705 return left.m_sBackslashPath.CompareNoCase(right.m_sBackslashPath);
708 bool operator<(const CTGitPath& left, const CTGitPath& right)
710 return CTGitPath::Compare(left, right) < 0;
713 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right)
715 return left.IsEquivalentTo(right);
718 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right)
720 if (left.IsAdminDir() && right.IsAdminDir())
722 CTGitPath l = left;
723 CTGitPath r = right;
726 l = l.GetContainingDirectory();
727 } while(l.HasAdminDir());
730 r = r.GetContainingDirectory();
731 } while(r.HasAdminDir());
732 return l.GetContainingDirectory().IsEquivalentTo(r.GetContainingDirectory());
734 return left.GetDirectory().IsEquivalentTo(right.GetDirectory());
737 bool CTGitPath::CheckChild(const CTGitPath &parent, const CTGitPath& child)
739 return parent.IsAncestorOf(child);
742 void CTGitPath::AppendRawString(const CString& sAppend)
744 EnsureFwdslashPathSet();
745 CString strCopy = m_sFwdslashPath += sAppend;
746 SetFromUnknown(strCopy);
749 void CTGitPath::AppendPathString(const CString& sAppend)
751 EnsureBackslashPathSet();
752 CString cleanAppend(sAppend);
753 cleanAppend.Replace('/', '\\');
754 cleanAppend.TrimLeft('\\');
755 m_sBackslashPath.TrimRight('\\');
756 CString strCopy = m_sBackslashPath + _T("\\") + cleanAppend;
757 SetFromWin(strCopy);
760 bool CTGitPath::IsWCRoot() const
762 if (m_bIsWCRootKnown)
763 return m_bIsWCRoot;
765 m_bIsWCRootKnown = true;
766 m_bIsWCRoot = false;
768 CString topDirectory;
769 if (!IsDirectory() || !HasAdminDir(&topDirectory))
771 return m_bIsWCRoot;
774 if (IsEquivalentToWithoutCase(topDirectory))
776 m_bIsWCRoot = true;
779 return m_bIsWCRoot;
782 bool CTGitPath::HasAdminDir() const
784 if (m_bHasAdminDirKnown)
785 return m_bHasAdminDir;
787 EnsureBackslashPathSet();
788 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
789 m_bHasAdminDirKnown = true;
790 return m_bHasAdminDir;
793 bool CTGitPath::HasSubmodules() const
795 if (HasAdminDir())
797 CString path = m_sProjectRoot;
798 path += _T("\\.gitmodules");
799 if( PathFileExists(path) )
800 return true;
802 return false;
805 int CTGitPath::GetAdminDirMask() const
807 int status = 0;
808 CString topdir;
809 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
811 return status;
814 // ITEMIS_INGIT will be revoked if necessary in TortoiseShell/ContextMenu.cpp
815 status |= ITEMIS_INGIT|ITEMIS_INVERSIONEDFOLDER;
817 if (IsDirectory())
819 status |= ITEMIS_FOLDERINGIT;
820 if (IsWCRoot())
822 status |= ITEMIS_WCROOT;
824 if (g_GitAdminDir.HasAdminDir(GetWinPathString(), false))
825 status |= ITEMIS_SUBMODULE;
829 CString dotGitPath;
830 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
832 if (PathFileExists(dotGitPath + _T("BISECT_START")))
833 status |= ITEMIS_BISECT;
835 if (PathFileExists(dotGitPath + _T("refs\\stash")))
836 status |= ITEMIS_STASH;
838 if (PathFileExists(dotGitPath + _T("svn")))
839 status |= ITEMIS_GITSVN;
841 if (PathFileExists(topdir + _T("\\.gitmodules")))
842 status |= ITEMIS_SUBMODULECONTAINER;
844 return status;
847 bool CTGitPath::HasStashDir() const
849 CString topdir;
850 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
852 return false;
855 CString dotGitPath;
856 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
858 return !!PathFileExists(dotGitPath + _T("\\refs\\stash"));
860 bool CTGitPath::HasGitSVNDir() const
862 CString topdir;
863 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
865 return false;
868 CString dotGitPath;
869 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
871 return !!PathFileExists(dotGitPath + _T("svn"));
873 bool CTGitPath::IsBisectActive() const
875 CString topdir;
876 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
878 return false;
881 CString dotGitPath;
882 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
884 return !!PathFileExists(dotGitPath + _T("BISECT_START"));
886 bool CTGitPath::IsMergeActive() const
888 CString topdir;
889 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
891 return false;
894 CString dotGitPath;
895 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
897 return !!PathFileExists(dotGitPath + _T("MERGE_HEAD"));
899 bool CTGitPath::HasRebaseApply() const
901 CString topdir;
902 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
904 return false;
907 CString dotGitPath;
908 g_GitAdminDir.GetAdminDirPath(topdir, dotGitPath);
910 return !!PathFileExists(dotGitPath + _T("rebase-apply"));
913 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const
915 if (m_bHasAdminDirKnown)
917 if (ProjectTopDir)
918 *ProjectTopDir = m_sProjectRoot;
919 return m_bHasAdminDir;
922 EnsureBackslashPathSet();
923 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
924 m_bHasAdminDirKnown = true;
925 if (ProjectTopDir)
926 *ProjectTopDir = m_sProjectRoot;
927 return m_bHasAdminDir;
930 bool CTGitPath::IsAdminDir() const
932 if (m_bIsAdminDirKnown)
933 return m_bIsAdminDir;
935 EnsureBackslashPathSet();
936 m_bIsAdminDir = g_GitAdminDir.IsAdminDirPath(m_sBackslashPath);
937 m_bIsAdminDirKnown = true;
938 return m_bIsAdminDir;
941 bool CTGitPath::IsValidOnWindows() const
943 if (m_bIsValidOnWindowsKnown)
944 return m_bIsValidOnWindows;
946 m_bIsValidOnWindows = false;
947 EnsureBackslashPathSet();
948 CString sMatch = m_sBackslashPath + _T("\r\n");
949 wstring sPattern;
950 // the 'file://' URL is just a normal windows path:
951 if (sMatch.Left(7).CompareNoCase(_T("file:\\\\"))==0)
953 sMatch = sMatch.Mid(7);
954 sMatch.TrimLeft(_T("\\"));
955 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
957 else
959 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
964 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
965 tr1::wsmatch match;
967 wstring rmatch = wstring((LPCTSTR)sMatch);
968 if (tr1::regex_match(rmatch, match, rx))
970 if (wstring(match[0]).compare(sMatch)==0)
971 m_bIsValidOnWindows = true;
973 if (m_bIsValidOnWindows)
975 // now check for illegal filenames
976 tr1::wregex rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
977 rmatch = m_sBackslashPath;
978 if (tr1::regex_search(rmatch, rx2, tr1::regex_constants::match_default))
979 m_bIsValidOnWindows = false;
982 catch (exception) {}
984 m_bIsValidOnWindowsKnown = true;
985 return m_bIsValidOnWindows;
988 //////////////////////////////////////////////////////////////////////////
990 CTGitPathList::CTGitPathList()
995 // A constructor which allows a path list to be easily built which one initial entry in
996 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)
998 AddPath(firstEntry);
1000 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool /*staged*/)
1002 unsigned int pos=0;
1003 CString one;
1004 CTGitPath path;
1005 CString part;
1006 this->Clear();
1008 while(pos>=0 && pos<out.size())
1010 one.Empty();
1011 path.Reset();
1013 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1014 int tabstart=0;
1015 path.m_Action=path.ParserAction(out[pos]);
1016 one.Tokenize(_T("\t"),tabstart);
1018 if(tabstart>=0)
1019 path.SetFromGit(one.Right(one.GetLength()-tabstart));
1021 tabstart=0;
1023 part=one.Tokenize(_T(" "),tabstart); //Tag
1025 part=one.Tokenize(_T(" "),tabstart); //Mode
1027 part=one.Tokenize(_T(" "),tabstart); //Hash
1029 part=one.Tokenize(_T("\t"),tabstart); //Stage
1031 path.m_Stage=_ttol(part);
1033 this->AddPath(path);
1035 pos=out.findNextString(pos);
1037 return pos;
1039 int CTGitPathList::FillUnRev(unsigned int action,CTGitPathList *list)
1041 int pos=0;
1042 this->Clear();
1043 CTGitPath path;
1045 int count;
1046 if(list==NULL)
1047 count=1;
1048 else
1049 count=list->GetCount();
1050 for (int i = 0; i < count; i++)
1052 CString cmd;
1053 pos=0;
1055 CString ignored;
1056 if(action & CTGitPath::LOGACTIONS_IGNORE)
1057 ignored= _T(" -i");
1059 if(list==NULL)
1061 cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1062 cmd+=ignored;
1065 else
1066 { cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z%s -- \"%s\""),
1067 ignored,
1068 (*list)[i].GetWinPathString());
1071 BYTE_VECTOR out;
1072 out.clear();
1073 g_Git.Run(cmd, &out);
1075 pos=0;
1076 CString one;
1077 while (pos >= 0 && pos < (int)out.size())
1079 one.Empty();
1080 g_Git.StringAppend(&one, &out[pos], CP_UTF8);
1081 if(!one.IsEmpty())
1083 //SetFromGit will clear all status
1084 path.SetFromGit(one);
1085 path.m_Action=action;
1086 AddPath(path);
1088 pos=out.findNextString(pos);
1092 return 0;
1094 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log, bool parseDeletes /*false*/)
1096 this->Clear();
1097 int pos=0;
1098 //BYTE *p=&log[0];
1099 //CString one;
1100 CTGitPath path;
1101 m_Action=0;
1102 while (pos >= 0 && pos < (int)log.size())
1104 //one=log.Tokenize(_T("\n"),pos);
1105 path.Reset();
1106 if(log[pos]=='\n')
1107 pos++;
1109 if(log[pos]==':')
1111 bool merged=false;
1112 if(log[pos+1] ==':')
1114 merged=true;
1116 int end=log.find(0,pos);
1117 int actionstart=-1;
1118 int numfile=1;
1119 int file1=-1,file2=-1;
1120 if( end>0 )
1122 actionstart=log.find(' ',end-6);
1123 pos=actionstart;
1125 if( actionstart>0 )
1127 actionstart++;
1129 file1 = log.find(0,actionstart);
1130 if( file1>=0 )
1132 file1++;
1133 pos=file1;
1135 if( log[actionstart] == 'C' || log[actionstart] == 'R' )
1137 file2=file1;
1138 numfile=2;
1139 file1 = log.find(0,file1);
1140 if(file1>=0 )
1142 file1++;
1143 pos=file1;
1149 CString pathname1;
1150 CString pathname2;
1152 if( file1>=0 )
1153 g_Git.StringAppend(&pathname1, &log[file1], CP_UTF8);
1154 if( file2>=0 )
1155 g_Git.StringAppend(&pathname2, &log[file2], CP_UTF8);
1157 CTGitPath *GitPath=LookForGitPath(pathname1);
1159 if(GitPath)
1161 GitPath->ParserAction( log[actionstart] );
1163 if(merged)
1165 GitPath->m_Action |= CTGitPath::LOGACTIONS_MERGED;
1166 GitPath->m_Action &= ~CTGitPath::LOGACTIONS_FORWORD;
1168 m_Action |=GitPath->m_Action;
1171 else
1173 int ac=path.ParserAction(log[actionstart] );
1174 ac |= merged?CTGitPath::LOGACTIONS_MERGED:0;
1176 path.SetFromGit(pathname1,&pathname2);
1177 path.m_Action=ac;
1178 //action must be set after setfromgit. SetFromGit will clear all status.
1179 this->m_Action|=ac;
1181 AddPath(path);
1186 else
1188 int tabstart=0;
1189 path.Reset();
1190 CString StatAdd;
1191 CString StatDel;
1192 CString file1;
1193 CString file2;
1195 tabstart=log.find('\t',pos);
1196 if(tabstart >=0)
1198 log[tabstart]=0;
1199 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);
1200 pos=tabstart+1;
1203 tabstart=log.find('\t',pos);
1204 if(tabstart >=0)
1206 log[tabstart]=0;
1208 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);
1209 pos=tabstart+1;
1212 if(log[pos] == 0) //rename
1214 pos++;
1215 g_Git.StringAppend(&file2, &log[pos], CP_UTF8);
1216 int sec=log.find(0,pos);
1217 if(sec>=0)
1219 sec++;
1220 g_Git.StringAppend(&file1, &log[sec], CP_UTF8);
1222 pos=sec;
1225 else
1227 g_Git.StringAppend(&file1, &log[pos], CP_UTF8);
1229 path.SetFromGit(file1,&file2);
1231 CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());
1232 if(GitPath)
1234 GitPath->m_StatAdd=StatAdd;
1235 GitPath->m_StatDel=StatDel;
1237 else
1239 //path.SetFromGit(pathname);
1240 if (parseDeletes)
1242 path.m_StatAdd=_T("0");
1243 path.m_StatDel=_T("0");
1244 path.m_Action |= CTGitPath::LOGACTIONS_DELETED;
1246 else
1248 path.m_StatAdd=StatAdd;
1249 path.m_StatDel=StatDel;
1250 path.m_Action |= CTGitPath::LOGACTIONS_FORWORD;
1252 AddPath(path);
1256 pos=log.findNextString(pos);
1258 return pos;
1261 void CTGitPathList::AddPath(const CTGitPath& newPath)
1263 m_paths.push_back(newPath);
1264 m_commonBaseDirectory.Reset();
1266 int CTGitPathList::GetCount() const
1268 return (int)m_paths.size();
1270 void CTGitPathList::Clear()
1272 m_paths.clear();
1273 m_commonBaseDirectory.Reset();
1276 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const
1278 ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());
1279 return m_paths[index];
1282 bool CTGitPathList::AreAllPathsFiles() const
1284 // Look through the vector for any directories - if we find them, return false
1285 return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();
1289 #if defined(_MFC_VER)
1291 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)
1293 Clear();
1296 CString strLine;
1297 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
1299 // for every selected file/folder
1300 CTGitPath path;
1301 while (file.ReadString(strLine))
1303 path.SetFromUnknown(strLine);
1304 AddPath(path);
1306 file.Close();
1308 catch (CFileException* pE)
1310 TRACE("CFileException loading target file list\n");
1311 TCHAR error[10000] = {0};
1312 pE->GetErrorMessage(error, 10000);
1313 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1314 pE->Delete();
1315 return false;
1317 return true;
1320 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const
1324 if (bANSI)
1326 CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
1327 PathVector::const_iterator it;
1328 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1330 CStringA line = CStringA(it->GetGitPathString()) + '\n';
1331 file.Write(line, line.GetLength());
1333 file.Close();
1335 else
1337 CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);
1338 PathVector::const_iterator it;
1339 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1341 file.WriteString(it->GetGitPathString()+_T("\n"));
1343 file.Close();
1346 catch (CFileException* pE)
1348 TRACE("CFileException in writing temp file\n");
1349 pE->Delete();
1350 return false;
1352 return true;
1356 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)
1358 int pos = 0;
1359 CString temp;
1360 for(;;)
1362 temp = sPathString.Tokenize(_T("*"),pos);
1363 if(temp.IsEmpty())
1365 break;
1367 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));
1371 CString CTGitPathList::CreateAsteriskSeparatedString() const
1373 CString sRet;
1374 PathVector::const_iterator it;
1375 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1377 if (!sRet.IsEmpty())
1378 sRet += _T("*");
1379 sRet += it->GetWinPathString();
1381 return sRet;
1383 #endif // _MFC_VER
1385 bool
1386 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1388 // Check if all the paths are files and in the same directory
1389 PathVector::const_iterator it;
1390 m_commonBaseDirectory.Reset();
1391 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1393 if(it->IsDirectory())
1395 return false;
1397 const CTGitPath& baseDirectory = it->GetDirectory();
1398 if(m_commonBaseDirectory.IsEmpty())
1400 m_commonBaseDirectory = baseDirectory;
1402 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1404 // Different path
1405 m_commonBaseDirectory.Reset();
1406 return false;
1409 return true;
1412 CTGitPath CTGitPathList::GetCommonDirectory() const
1414 if (m_commonBaseDirectory.IsEmpty())
1416 PathVector::const_iterator it;
1417 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1419 const CTGitPath& baseDirectory = it->GetDirectory();
1420 if(m_commonBaseDirectory.IsEmpty())
1422 m_commonBaseDirectory = baseDirectory;
1424 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1426 // Different path
1427 m_commonBaseDirectory.Reset();
1428 break;
1432 // since we only checked strings, not paths,
1433 // we have to make sure now that we really return a *path* here
1434 PathVector::const_iterator iter;
1435 for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)
1437 if (!m_commonBaseDirectory.IsAncestorOf(*iter))
1439 m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();
1440 break;
1443 return m_commonBaseDirectory;
1446 CTGitPath CTGitPathList::GetCommonRoot() const
1448 if (GetCount() == 0)
1449 return CTGitPath();
1451 if (GetCount() == 1)
1452 return m_paths[0];
1454 // first entry is common root for itself
1455 // (add trailing '\\' to detect partial matches of the last path element)
1456 CString root = m_paths[0].GetWinPathString() + _T('\\');
1457 int rootLength = root.GetLength();
1459 // determine common path string prefix
1460 for (PathVector::const_iterator it = m_paths.begin() + 1; it != m_paths.end(); ++it)
1462 CString path = it->GetWinPathString() + _T('\\');
1464 int newLength = CStringUtils::GetMatchingLength(root, path);
1465 if (newLength != rootLength)
1467 root.Delete(newLength, rootLength);
1468 rootLength = newLength;
1472 // remove the last (partial) path element
1473 if (rootLength > 0)
1474 root.Delete(root.ReverseFind(_T('\\')), rootLength);
1476 // done
1477 return CTGitPath(root);
1480 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)
1482 std::sort(m_paths.begin(), m_paths.end());
1483 if (bReverse)
1484 std::reverse(m_paths.begin(), m_paths.end());
1487 void CTGitPathList::DeleteAllFiles(bool bTrash, bool bFilesOnly)
1489 PathVector::const_iterator it;
1490 SortByPathname(true); // nested ones first
1492 CString sPaths;
1493 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1495 if ((it->Exists()) && ((it->IsDirectory() != bFilesOnly) || !bFilesOnly))
1497 if (!it->IsDirectory())
1498 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1500 sPaths += it->GetWinPath();
1501 sPaths += '\0';
1504 sPaths += '\0';
1505 sPaths += '\0';
1506 DeleteViaShell((LPCTSTR)sPaths, bTrash);
1507 Clear();
1510 bool CTGitPathList::DeleteViaShell(LPCTSTR path, bool bTrash)
1512 SHFILEOPSTRUCT shop = {0};
1513 shop.wFunc = FO_DELETE;
1514 shop.pFrom = path;
1515 shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT|FOF_NO_CONNECTED_ELEMENTS;
1516 if (bTrash)
1517 shop.fFlags |= FOF_ALLOWUNDO;
1518 const bool bRet = (SHFileOperation(&shop) == 0);
1519 return bRet;
1522 void CTGitPathList::RemoveDuplicates()
1524 SortByPathname();
1525 // Remove the duplicates
1526 // (Unique moves them to the end of the vector, then erase chops them off)
1527 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());
1530 void CTGitPathList::RemoveAdminPaths()
1532 PathVector::iterator it;
1533 for(it = m_paths.begin(); it != m_paths.end(); )
1535 if (it->IsAdminDir())
1537 m_paths.erase(it);
1538 it = m_paths.begin();
1540 else
1541 ++it;
1545 void CTGitPathList::RemovePath(const CTGitPath& path)
1547 PathVector::iterator it;
1548 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1550 if (it->IsEquivalentTo(path))
1552 m_paths.erase(it);
1553 return;
1558 void CTGitPathList::RemoveItem(CTGitPath & path)
1560 PathVector::iterator it;
1561 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1563 if (it->GetGitPathString()==path.GetGitPathString())
1565 m_paths.erase(it);
1566 return;
1570 void CTGitPathList::RemoveChildren()
1572 SortByPathname();
1573 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());
1576 bool CTGitPathList::IsEqual(const CTGitPathList& list)
1578 if (list.GetCount() != GetCount())
1579 return false;
1580 for (int i=0; i<list.GetCount(); ++i)
1582 if (!list[i].IsEquivalentTo(m_paths[i]))
1583 return false;
1585 return true;
1588 //////////////////////////////////////////////////////////////////////////
1589 #if 0
1590 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const
1592 apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));
1594 for(int nItem = 0; nItem < GetCount(); nItem++)
1596 const char * target = m_paths[nItem].GetGitApiPath(pool);
1597 (*((const char **) apr_array_push (targets))) = target;
1600 return targets;
1602 #endif
1603 //////////////////////////////////////////////////////////////////////////
1605 #if 0
1606 #if defined(_DEBUG)
1607 // Some test cases for these classes
1608 static class CTGitPathTests
1610 public:
1611 CTGitPathTests()
1613 apr_initialize();
1614 pool = svn_pool_create(NULL);
1615 GetDirectoryTest();
1616 AdminDirTest();
1617 SortTest();
1618 RawAppendTest();
1619 PathAppendTest();
1620 RemoveDuplicatesTest();
1621 RemoveChildrenTest();
1622 ContainingDirectoryTest();
1623 AncestorTest();
1624 SubversionPathTest();
1625 GetCommonRootTest();
1626 #if defined(_MFC_VER)
1627 ValidPathAndUrlTest();
1628 ListLoadingTest();
1629 #endif
1630 apr_terminate();
1633 private:
1634 // apr_pool_t * pool;
1635 void GetDirectoryTest()
1637 // Bit tricky, this test, because we need to know something about the file
1638 // layout on the machine which is running the test
1639 TCHAR winDir[MAX_PATH+1];
1640 GetWindowsDirectory(winDir, MAX_PATH);
1641 CString sWinDir(winDir);
1643 CTGitPath testPath;
1644 // This is a file which we know will always be there
1645 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));
1646 ATLASSERT(!testPath.IsDirectory());
1647 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1648 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);
1650 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1651 // but we know it must be different to the directory itself
1652 testPath.SetFromUnknown(sWinDir);
1653 ATLASSERT(testPath.IsDirectory());
1654 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1655 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);
1656 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());
1658 // Try a root path
1659 testPath.SetFromUnknown(_T("C:\\"));
1660 ATLASSERT(testPath.IsDirectory());
1661 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1662 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1663 // Try a root UNC path
1664 testPath.SetFromUnknown(_T("\\MYSTATION"));
1665 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1668 void AdminDirTest()
1670 CTGitPath testPath;
1671 testPath.SetFromUnknown(_T("c:\\.svndir"));
1672 ATLASSERT(!testPath.IsAdminDir());
1673 testPath.SetFromUnknown(_T("c:\\test.svn"));
1674 ATLASSERT(!testPath.IsAdminDir());
1675 testPath.SetFromUnknown(_T("c:\\.svn"));
1676 ATLASSERT(testPath.IsAdminDir());
1677 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));
1678 ATLASSERT(!testPath.IsAdminDir());
1679 testPath.SetFromUnknown(_T("c:\\.svn\\test"));
1680 ATLASSERT(testPath.IsAdminDir());
1682 CTGitPathList pathList;
1683 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));
1684 pathList.AddPath(CTGitPath(_T("c:\\.svn")));
1685 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1686 pathList.AddPath(CTGitPath(_T("c:\\test")));
1687 pathList.RemoveAdminPaths();
1688 ATLASSERT(pathList.GetCount()==2);
1689 pathList.Clear();
1690 pathList.AddPath(CTGitPath(_T("c:\\test")));
1691 pathList.RemoveAdminPaths();
1692 ATLASSERT(pathList.GetCount()==1);
1695 void SortTest()
1697 CTGitPathList testList;
1698 CTGitPath testPath;
1699 testPath.SetFromUnknown(_T("c:/Z"));
1700 testList.AddPath(testPath);
1701 testPath.SetFromUnknown(_T("c:/B"));
1702 testList.AddPath(testPath);
1703 testPath.SetFromUnknown(_T("c:\\a"));
1704 testList.AddPath(testPath);
1705 testPath.SetFromUnknown(_T("c:/Test"));
1706 testList.AddPath(testPath);
1708 testList.SortByPathname();
1710 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));
1711 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));
1712 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));
1713 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));
1716 void RawAppendTest()
1718 CTGitPath testPath(_T("c:/test/"));
1719 testPath.AppendRawString(_T("/Hello"));
1720 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1722 testPath.AppendRawString(_T("\\T2"));
1723 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1725 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1726 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1727 testBasePath.AppendRawString(testFilePath.GetFileExtension());
1728 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1731 void PathAppendTest()
1733 CTGitPath testPath(_T("c:/test/"));
1734 testPath.AppendPathString(_T("/Hello"));
1735 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1737 testPath.AppendPathString(_T("T2"));
1738 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1740 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1741 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1742 // You wouldn't want to do this in real life - you'd use append-raw
1743 testBasePath.AppendPathString(testFilePath.GetFileExtension());
1744 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1747 void RemoveDuplicatesTest()
1749 CTGitPathList list;
1750 list.AddPath(CTGitPath(_T("Z")));
1751 list.AddPath(CTGitPath(_T("A")));
1752 list.AddPath(CTGitPath(_T("E")));
1753 list.AddPath(CTGitPath(_T("E")));
1755 ATLASSERT(list[2].IsEquivalentTo(list[3]));
1756 ATLASSERT(list[2]==list[3]);
1758 ATLASSERT(list.GetCount() == 4);
1760 list.RemoveDuplicates();
1762 ATLASSERT(list.GetCount() == 3);
1764 ATLASSERT(list[0].GetWinPathString() == _T("A"));
1765 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);
1766 ATLASSERT(list[2].GetWinPathString() == _T("Z"));
1769 void RemoveChildrenTest()
1771 CTGitPathList list;
1772 list.AddPath(CTGitPath(_T("c:\\test")));
1773 list.AddPath(CTGitPath(_T("c:\\test\\file")));
1774 list.AddPath(CTGitPath(_T("c:\\testfile")));
1775 list.AddPath(CTGitPath(_T("c:\\parent")));
1776 list.AddPath(CTGitPath(_T("c:\\parent\\child")));
1777 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1778 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1780 ATLASSERT(list.GetCount() == 7);
1782 list.RemoveChildren();
1784 ATLTRACE("count = %d\n", list.GetCount());
1785 ATLASSERT(list.GetCount() == 3);
1787 list.SortByPathname();
1789 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1790 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1791 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1794 #if defined(_MFC_VER)
1795 void ListLoadingTest()
1797 TCHAR buf[MAX_PATH];
1798 GetCurrentDirectory(MAX_PATH, buf);
1799 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1800 CTGitPathList testList;
1801 testList.LoadFromAsteriskSeparatedString(sPathList);
1803 ATLASSERT(testList.GetCount() == 3);
1804 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));
1805 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1806 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));
1808 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));
1809 testList.Clear();
1810 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1811 testList.LoadFromAsteriskSeparatedString(sPathList);
1812 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1814 #endif
1816 void ContainingDirectoryTest()
1819 CTGitPath testPath;
1820 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1821 CTGitPath dir;
1822 dir = testPath.GetContainingDirectory();
1823 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1824 dir = dir.GetContainingDirectory();
1825 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));
1826 dir = dir.GetContainingDirectory();
1827 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));
1828 dir = dir.GetContainingDirectory();
1829 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));
1830 dir = dir.GetContainingDirectory();
1831 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));
1832 dir = dir.GetContainingDirectory();
1833 ATLASSERT(dir.IsEmpty());
1834 ATLASSERT(dir.GetWinPathString() == _T(""));
1837 void AncestorTest()
1839 CTGitPath testPath;
1840 testPath.SetFromWin(_T("c:\\windows"));
1841 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1842 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1843 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1844 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1845 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1848 void SubversionPathTest()
1850 CTGitPath testPath;
1851 testPath.SetFromWin(_T("c:\\"));
1852 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);
1853 testPath.SetFromWin(_T("c:\\folder"));
1854 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);
1855 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1856 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);
1857 testPath.SetFromUnknown(_T("http://testing/"));
1858 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);
1859 testPath.SetFromGit(NULL);
1860 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);
1861 #if defined(_MFC_VER)
1862 testPath.SetFromUnknown(_T("http://testing again"));
1863 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1864 testPath.SetFromUnknown(_T("http://testing%20again"));
1865 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1866 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1867 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1868 #endif
1871 void GetCommonRootTest()
1873 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));
1874 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));
1875 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1877 CTGitPathList list;
1878 list.AddPath(pathA);
1879 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1880 list.AddPath(pathB);
1881 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1882 list.AddPath(pathC);
1883 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1884 #ifdef _MFC_VER
1885 list.Clear();
1886 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");
1887 list.LoadFromAsteriskSeparatedString(sPathList);
1888 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1890 list.Clear();
1891 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");
1892 list.LoadFromAsteriskSeparatedString(sPathList);
1893 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1895 list.Clear();
1896 sPathList = _T("c:\\windows\\*c:\\windows");
1897 list.LoadFromAsteriskSeparatedString(sPathList);
1898 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1900 list.Clear();
1901 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");
1902 list.LoadFromAsteriskSeparatedString(sPathList);
1903 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1905 list.Clear();
1906 sPathList = _T("c:\\windowsdummy*c:\\windows");
1907 list.LoadFromAsteriskSeparatedString(sPathList);
1908 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1909 #endif
1912 void ValidPathAndUrlTest()
1914 CTGitPath testPath;
1915 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1916 ATLASSERT(testPath.IsValidOnWindows());
1917 testPath.SetFromWin(_T("c:\\"));
1918 ATLASSERT(testPath.IsValidOnWindows());
1919 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1920 ATLASSERT(testPath.IsValidOnWindows());
1921 testPath.SetFromWin(_T("c"));
1922 ATLASSERT(testPath.IsValidOnWindows());
1923 testPath.SetFromWin(_T("c:\\test folder\\file"));
1924 ATLASSERT(testPath.IsValidOnWindows());
1925 testPath.SetFromWin(_T("c:\\folder\\"));
1926 ATLASSERT(testPath.IsValidOnWindows());
1927 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1928 ATLASSERT(testPath.IsValidOnWindows());
1929 testPath.SetFromWin(_T("c:\\.svn"));
1930 ATLASSERT(testPath.IsValidOnWindows());
1931 testPath.SetFromWin(_T("c:\\com\\file"));
1932 ATLASSERT(testPath.IsValidOnWindows());
1933 testPath.SetFromWin(_T("c:\\test\\conf"));
1934 ATLASSERT(testPath.IsValidOnWindows());
1935 testPath.SetFromWin(_T("c:\\LPT"));
1936 ATLASSERT(testPath.IsValidOnWindows());
1937 testPath.SetFromWin(_T("c:\\test\\LPT"));
1938 ATLASSERT(testPath.IsValidOnWindows());
1939 testPath.SetFromWin(_T("c:\\com1test"));
1940 ATLASSERT(testPath.IsValidOnWindows());
1941 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1942 ATLASSERT(testPath.IsValidOnWindows());
1944 testPath.SetFromWin(_T("\\\\Share\\filename"));
1945 ATLASSERT(testPath.IsValidOnWindows());
1946 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));
1947 ATLASSERT(testPath.IsValidOnWindows());
1948 testPath.SetFromWin(_T("\\\\Share\\.svn"));
1949 ATLASSERT(testPath.IsValidOnWindows());
1951 // now the negative tests
1952 testPath.SetFromWin(_T("c:\\test:folder"));
1953 ATLASSERT(!testPath.IsValidOnWindows());
1954 testPath.SetFromWin(_T("c:\\file<name"));
1955 ATLASSERT(!testPath.IsValidOnWindows());
1956 testPath.SetFromWin(_T("c:\\something*else"));
1957 ATLASSERT(!testPath.IsValidOnWindows());
1958 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));
1959 ATLASSERT(!testPath.IsValidOnWindows());
1960 testPath.SetFromWin(_T("c:\\ext.>ension"));
1961 ATLASSERT(!testPath.IsValidOnWindows());
1962 testPath.SetFromWin(_T("c:\\com1\\filename"));
1963 ATLASSERT(!testPath.IsValidOnWindows());
1964 testPath.SetFromWin(_T("c:\\com1"));
1965 ATLASSERT(!testPath.IsValidOnWindows());
1966 testPath.SetFromWin(_T("c:\\com1\\AuX"));
1967 ATLASSERT(!testPath.IsValidOnWindows());
1969 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1970 ATLASSERT(!testPath.IsValidOnWindows());
1971 testPath.SetFromWin(_T("\\\\Share\\prn"));
1972 ATLASSERT(!testPath.IsValidOnWindows());
1973 testPath.SetFromWin(_T("\\\\Share\\NUL"));
1974 ATLASSERT(!testPath.IsValidOnWindows());
1976 // now come some URL tests
1977 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));
1978 ATLASSERT(testPath.IsValidOnWindows());
1979 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1980 ATLASSERT(testPath.IsValidOnWindows());
1981 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1982 ATLASSERT(testPath.IsValidOnWindows());
1983 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1984 ATLASSERT(testPath.IsValidOnWindows());
1985 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));
1986 ATLASSERT(testPath.IsValidOnWindows());
1987 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1988 ATLASSERT(testPath.IsValidOnWindows());
1989 // and some negative URL tests
1990 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1991 ATLASSERT(!testPath.IsValidOnWindows());
1992 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1993 ATLASSERT(!testPath.IsValidOnWindows());
1994 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1995 ATLASSERT(!testPath.IsValidOnWindows());
1996 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
1997 ATLASSERT(!testPath.IsValidOnWindows());
1998 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
1999 ATLASSERT(!testPath.IsValidOnWindows());
2003 } TGitPathTestobject;
2004 #endif
2005 #endif
2007 CTGitPath * CTGitPathList::LookForGitPath(CString path)
2009 int i=0;
2010 for(i=0;i<this->GetCount();i++)
2012 if((*this)[i].GetGitPathString() == path )
2013 return (CTGitPath*)&(*this)[i];
2015 return NULL;
2017 CString CTGitPath::GetActionName(int action)
2019 if(action & CTGitPath::LOGACTIONS_UNMERGED)
2020 return MAKEINTRESOURCE(IDS_PATHACTIONS_CONFLICT);
2021 if(action & CTGitPath::LOGACTIONS_ADDED)
2022 return MAKEINTRESOURCE(IDS_PATHACTIONS_ADD);
2023 if(action & CTGitPath::LOGACTIONS_DELETED)
2024 return MAKEINTRESOURCE(IDS_PATHACTIONS_DELETE);
2025 if(action & CTGitPath::LOGACTIONS_MERGED )
2026 return MAKEINTRESOURCE(IDS_PATHACTIONS_MERGED);
2028 if(action & CTGitPath::LOGACTIONS_MODIFIED)
2029 return MAKEINTRESOURCE(IDS_PATHACTIONS_MODIFIED);
2030 if(action & CTGitPath::LOGACTIONS_REPLACED)
2031 return MAKEINTRESOURCE(IDS_PATHACTIONS_RENAME);
2032 if(action & CTGitPath::LOGACTIONS_COPY)
2033 return MAKEINTRESOURCE(IDS_PATHACTIONS_COPY);
2035 if(action & CTGitPath::LOGACTIONS_FORWORD )
2036 return MAKEINTRESOURCE(IDS_PATHACTIONS_FORWARD);
2038 if(action & CTGitPath::LOGACTIONS_REBASE_EDIT)
2039 return MAKEINTRESOURCE(IDS_PATHACTIONS_EDIT);
2040 if(action & CTGitPath::LOGACTIONS_REBASE_SQUASH)
2041 return MAKEINTRESOURCE(IDS_PATHACTIONS_SQUASH);
2042 if(action & CTGitPath::LOGACTIONS_REBASE_PICK)
2043 return MAKEINTRESOURCE(IDS_PATHACTIONS_PICK);
2044 if(action & CTGitPath::LOGACTIONS_REBASE_SKIP)
2045 return MAKEINTRESOURCE(IDS_PATHACTIONS_SKIP);
2047 return MAKEINTRESOURCE(IDS_PATHACTIONS_UNKNOWN);
2049 CString CTGitPath::GetActionName()
2051 return GetActionName(m_Action);
2054 int CTGitPathList::GetAction()
2056 return m_Action;