Show "Merged" status for merged file.
[TortoiseGit.git] / src / Git / TGitPath.cpp
blobc9dc54c50f722b1f88b7a42c92f6aaeef282a4df
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "TGitPath.h"
21 #include "UnicodeUtils.h"
22 #include "GitAdminDir.h"
23 #include "PathUtils.h"
24 #include <regex>
25 #include "git.h"
26 #if defined(_MFC_VER)
27 //#include "MessageBox.h"
28 //#include "AppUtils.h"
29 #endif
31 #ifndef ASSERT
32 #define ASSERT()
33 #endif
34 using namespace std;
35 extern CGit g_Git;
37 CTGitPath::CTGitPath(void) :
38 m_bDirectoryKnown(false),
39 m_bIsDirectory(false),
40 m_bIsURL(false),
41 m_bURLKnown(false),
42 m_bHasAdminDirKnown(false),
43 m_bHasAdminDir(false),
44 m_bIsValidOnWindowsKnown(false),
45 m_bIsReadOnly(false),
46 m_bIsAdminDirKnown(false),
47 m_bIsAdminDir(false),
48 m_bExists(false),
49 m_bExistsKnown(false),
50 m_bLastWriteTimeKnown(0),
51 m_lastWriteTime(0),
52 m_customData(NULL),
53 m_bIsSpecialDirectoryKnown(false),
54 m_bIsSpecialDirectory(false)
56 m_Action=0;
59 CTGitPath::~CTGitPath(void)
62 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
63 CTGitPath::CTGitPath(const CString& sUnknownPath) :
64 m_bDirectoryKnown(false),
65 m_bIsDirectory(false),
66 m_bIsURL(false),
67 m_bURLKnown(false),
68 m_bHasAdminDirKnown(false),
69 m_bHasAdminDir(false),
70 m_bIsValidOnWindowsKnown(false),
71 m_bIsReadOnly(false),
72 m_bIsAdminDirKnown(false),
73 m_bIsAdminDir(false),
74 m_bExists(false),
75 m_bExistsKnown(false),
76 m_bLastWriteTimeKnown(0),
77 m_lastWriteTime(0),
78 m_customData(NULL),
79 m_bIsSpecialDirectoryKnown(false),
80 m_bIsSpecialDirectory(false)
82 SetFromUnknown(sUnknownPath);
83 m_Action=0;
84 m_Stage=0;
87 int CTGitPath::ParserAction(BYTE action)
89 //action=action.TrimLeft();
90 //TCHAR c=action.GetAt(0);
91 if(action == 'M')
92 m_Action|= LOGACTIONS_MODIFIED;
93 if(action == 'R')
94 m_Action|= LOGACTIONS_REPLACED;
95 if(action == 'A')
96 m_Action|= LOGACTIONS_ADDED;
97 if(action == 'D')
98 m_Action|= LOGACTIONS_DELETED;
99 if(action == 'U')
100 m_Action|= LOGACTIONS_UNMERGED;
101 if(action == 'K')
102 m_Action|= LOGACTIONS_DELETED;
103 if(action == 'H')
104 m_Action|= LOGACTIONS_CACHE;
105 if(action == 'C' )
106 m_Action|= LOGACTIONS_COPY;
108 return m_Action;
110 void CTGitPath::SetFromGit(const char* pPath)
112 Reset();
113 if (pPath == NULL)
114 return;
115 int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);
116 if (len)
118 len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, m_sFwdslashPath.GetBuffer(len+1), len+1);
119 m_sFwdslashPath.ReleaseBuffer(len-1);
121 SanitizeRootPath(m_sFwdslashPath, true);
124 void CTGitPath::SetFromGit(const char* pPath, bool bIsDirectory)
126 SetFromGit(pPath);
127 m_bDirectoryKnown = true;
128 m_bIsDirectory = bIsDirectory;
131 void CTGitPath::SetFromGit(const TCHAR* pPath, bool bIsDirectory)
133 Reset();
134 if (pPath)
136 m_sFwdslashPath = pPath;
137 SanitizeRootPath(m_sFwdslashPath, true);
139 m_bDirectoryKnown = true;
140 m_bIsDirectory = bIsDirectory;
143 void CTGitPath::SetFromGit(const CString& sPath,CString *oldpath)
145 Reset();
146 m_sFwdslashPath = sPath;
147 SanitizeRootPath(m_sFwdslashPath, true);
148 if(oldpath)
149 m_sOldFwdslashPath = *oldpath;
152 void CTGitPath::SetFromWin(LPCTSTR pPath)
154 Reset();
155 m_sBackslashPath = pPath;
156 SanitizeRootPath(m_sBackslashPath, false);
157 ATLASSERT(m_sBackslashPath.Find('/')<0);
159 void CTGitPath::SetFromWin(const CString& sPath)
161 Reset();
162 m_sBackslashPath = sPath;
163 SanitizeRootPath(m_sBackslashPath, false);
165 void CTGitPath::SetFromWin(const CString& sPath, bool bIsDirectory)
167 Reset();
168 m_sBackslashPath = sPath;
169 m_bIsDirectory = bIsDirectory;
170 m_bDirectoryKnown = true;
171 SanitizeRootPath(m_sBackslashPath, false);
173 void CTGitPath::SetFromUnknown(const CString& sPath)
175 Reset();
176 // Just set whichever path we think is most likely to be used
177 // GitAdminDir admin;
178 // CString p;
179 // if(admin.HasAdminDir(sPath,&p))
180 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
181 // else
182 SetFwdslashPath(sPath);
185 LPCTSTR CTGitPath::GetWinPath() const
187 if(IsEmpty())
189 return _T("");
191 if(m_sBackslashPath.IsEmpty())
193 SetBackslashPath(m_sFwdslashPath);
195 return m_sBackslashPath;
197 // This is a temporary function, to be used during the migration to
198 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
199 const CString& CTGitPath::GetWinPathString() const
201 if(m_sBackslashPath.IsEmpty())
203 SetBackslashPath(m_sFwdslashPath);
205 return m_sBackslashPath;
208 const CString& CTGitPath::GetGitPathString() const
210 if(m_sFwdslashPath.IsEmpty())
212 SetFwdslashPath(m_sBackslashPath);
214 return m_sFwdslashPath;
217 const CString &CTGitPath::GetGitOldPathString() const
219 return m_sOldFwdslashPath;
221 #if 0
222 const char* CTGitPath::GetGitApiPath(apr_pool_t *pool) const
224 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
225 // each call to GetGitApiPath returns a different pointer value.
226 // If you made multiple calls to GetGitApiPath on the same string, only the last
227 // one would give you a valid pointer to an empty string, because each
228 // call would invalidate the previous call's return.
229 if(IsEmpty())
231 return "";
233 if(m_sFwdslashPath.IsEmpty())
235 SetFwdslashPath(m_sBackslashPath);
237 if(m_sUTF8FwdslashPath.IsEmpty())
239 SetUTF8FwdslashPath(m_sFwdslashPath);
241 if (svn_path_is_url(m_sUTF8FwdslashPath))
243 m_sUTF8FwdslashPathEscaped = CPathUtils::PathEscape(m_sUTF8FwdslashPath);
244 m_sUTF8FwdslashPathEscaped.Replace("file:////", "file:///\\");
245 m_sUTF8FwdslashPathEscaped = svn_path_canonicalize(m_sUTF8FwdslashPathEscaped, pool);
246 return m_sUTF8FwdslashPathEscaped;
248 m_sUTF8FwdslashPath = svn_path_canonicalize(m_sUTF8FwdslashPath, pool);
250 return m_sUTF8FwdslashPath;
252 #endif
254 const CString& CTGitPath::GetUIPathString() const
256 if (m_sUIPath.IsEmpty())
258 #if defined(_MFC_VER)
259 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
260 if (IsUrl())
262 m_sUIPath = CPathUtils::PathUnescape(GetGitPathString());
263 m_sUIPath.Replace(_T("file:////"), _T("file:///\\"));
266 else
267 #endif
269 m_sUIPath = GetWinPathString();
272 return m_sUIPath;
275 void CTGitPath::SetFwdslashPath(const CString& sPath) const
277 m_sFwdslashPath = sPath;
278 m_sFwdslashPath.Replace('\\', '/');
280 // We don't leave a trailing /
281 m_sFwdslashPath.TrimRight('/');
283 SanitizeRootPath(m_sFwdslashPath, true);
285 m_sFwdslashPath.Replace(_T("file:////"), _T("file:///\\"));
287 m_sUTF8FwdslashPath.Empty();
290 void CTGitPath::SetBackslashPath(const CString& sPath) const
292 m_sBackslashPath = sPath;
293 m_sBackslashPath.Replace('/', '\\');
294 m_sBackslashPath.TrimRight('\\');
295 SanitizeRootPath(m_sBackslashPath, false);
298 void CTGitPath::SetUTF8FwdslashPath(const CString& sPath) const
300 m_sUTF8FwdslashPath = CUnicodeUtils::GetUTF8(sPath);
303 void CTGitPath::SanitizeRootPath(CString& sPath, bool bIsForwardPath) const
305 // Make sure to add the trailing slash to root paths such as 'C:'
306 if (sPath.GetLength() == 2 && sPath[1] == ':')
308 sPath += (bIsForwardPath) ? _T("/") : _T("\\");
312 bool CTGitPath::IsUrl() const
314 #if 0
315 if (!m_bURLKnown)
317 EnsureFwdslashPathSet();
318 if(m_sUTF8FwdslashPath.IsEmpty())
320 SetUTF8FwdslashPath(m_sFwdslashPath);
322 m_bIsURL = !!svn_path_is_url(m_sUTF8FwdslashPath);
323 m_bURLKnown = true;
325 return m_bIsURL;
326 #endif
327 return false;
330 bool CTGitPath::IsDirectory() const
332 if(!m_bDirectoryKnown)
334 UpdateAttributes();
336 return m_bIsDirectory;
339 bool CTGitPath::Exists() const
341 if (!m_bExistsKnown)
343 UpdateAttributes();
345 return m_bExists;
348 bool CTGitPath::Delete(bool bTrash) const
350 EnsureBackslashPathSet();
351 ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);
352 bool bRet = false;
353 if (Exists())
355 if ((bTrash)||(IsDirectory()))
357 TCHAR * buf = new TCHAR[m_sBackslashPath.GetLength()+2];
358 _tcscpy_s(buf, m_sBackslashPath.GetLength()+2, m_sBackslashPath);
359 buf[m_sBackslashPath.GetLength()] = 0;
360 buf[m_sBackslashPath.GetLength()+1] = 0;
361 SHFILEOPSTRUCT shop = {0};
362 shop.wFunc = FO_DELETE;
363 shop.pFrom = buf;
364 shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
365 if (bTrash)
366 shop.fFlags |= FOF_ALLOWUNDO;
367 bRet = (SHFileOperation(&shop) == 0);
368 delete [] buf;
370 else
372 bRet = !!::DeleteFile(m_sBackslashPath);
375 m_bExists = false;
376 m_bExistsKnown = true;
377 return bRet;
380 __int64 CTGitPath::GetLastWriteTime() const
382 if(!m_bLastWriteTimeKnown)
384 UpdateAttributes();
386 return m_lastWriteTime;
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(GetFileAttributesEx(m_sBackslashPath, GetFileExInfoStandard, &attribs))
404 m_bIsDirectory = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
405 m_lastWriteTime = *(__int64*)&attribs.ftLastWriteTime;
406 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
407 m_bExists = true;
409 else
411 DWORD err = GetLastError();
412 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))
414 m_bIsDirectory = false;
415 m_lastWriteTime = 0;
416 m_bExists = false;
418 else
420 m_bIsDirectory = false;
421 m_lastWriteTime = 0;
422 m_bExists = true;
423 return;
426 m_bDirectoryKnown = true;
427 m_bLastWriteTimeKnown = true;
428 m_bExistsKnown = true;
431 CTGitPath CTGitPath::GetSubPath(CTGitPath &root)
433 CTGitPath path;
435 if(GetWinPathString().Left(root.GetWinPathString().GetLength()) == root.GetWinPathString())
437 CString str=GetWinPathString();
438 path.SetFromWin(str.Right(str.GetLength()-root.GetWinPathString().GetLength()-1));
440 return path;
443 void CTGitPath::EnsureBackslashPathSet() const
445 if(m_sBackslashPath.IsEmpty())
447 SetBackslashPath(m_sFwdslashPath);
448 ATLASSERT(IsEmpty() || !m_sBackslashPath.IsEmpty());
451 void CTGitPath::EnsureFwdslashPathSet() const
453 if(m_sFwdslashPath.IsEmpty())
455 SetFwdslashPath(m_sBackslashPath);
456 ATLASSERT(IsEmpty() || !m_sFwdslashPath.IsEmpty());
461 // Reset all the caches
462 void CTGitPath::Reset()
464 m_bDirectoryKnown = false;
465 m_bURLKnown = false;
466 m_bLastWriteTimeKnown = false;
467 m_bHasAdminDirKnown = false;
468 m_bIsValidOnWindowsKnown = false;
469 m_bIsAdminDirKnown = false;
470 m_bExistsKnown = false;
471 m_bIsSpecialDirectoryKnown = false;
472 m_bIsSpecialDirectory = false;
474 m_sBackslashPath.Empty();
475 m_sFwdslashPath.Empty();
476 m_sUTF8FwdslashPath.Empty();
477 this->m_Action=0;
478 this->m_StatAdd=_T("");
479 this->m_StatDel=_T("");
480 ATLASSERT(IsEmpty());
483 CTGitPath CTGitPath::GetDirectory() const
485 if ((IsDirectory())||(!Exists()))
487 return *this;
489 return GetContainingDirectory();
492 CTGitPath CTGitPath::GetContainingDirectory() const
494 EnsureBackslashPathSet();
496 CString sDirName = m_sBackslashPath.Left(m_sBackslashPath.ReverseFind('\\'));
497 if(sDirName.GetLength() == 2 && sDirName[1] == ':')
499 // This is a root directory, which needs a trailing slash
500 sDirName += '\\';
501 if(sDirName == m_sBackslashPath)
503 // We were clearly provided with a root path to start with - we should return nothing now
504 sDirName.Empty();
507 if(sDirName.GetLength() == 1 && sDirName[0] == '\\')
509 // We have an UNC path and we already are the root
510 sDirName.Empty();
512 CTGitPath retVal;
513 retVal.SetFromWin(sDirName);
514 return retVal;
517 CString CTGitPath::GetRootPathString() const
519 EnsureBackslashPathSet();
520 CString workingPath = m_sBackslashPath;
521 LPTSTR pPath = workingPath.GetBuffer(MAX_PATH); // MAX_PATH ok here.
522 ATLVERIFY(::PathStripToRoot(pPath));
523 workingPath.ReleaseBuffer();
524 return workingPath;
528 CString CTGitPath::GetFilename() const
530 ATLASSERT(!IsDirectory());
531 return GetFileOrDirectoryName();
534 CString CTGitPath::GetFileOrDirectoryName() const
536 EnsureBackslashPathSet();
537 return m_sBackslashPath.Mid(m_sBackslashPath.ReverseFind('\\')+1);
540 CString CTGitPath::GetUIFileOrDirectoryName() const
542 GetUIPathString();
543 return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);
546 CString CTGitPath::GetFileExtension() const
548 if(!IsDirectory())
550 EnsureBackslashPathSet();
551 int dotPos = m_sBackslashPath.ReverseFind('.');
552 int slashPos = m_sBackslashPath.ReverseFind('\\');
553 if (dotPos > slashPos)
554 return m_sBackslashPath.Mid(dotPos);
556 return CString();
558 CString CTGitPath::GetBaseFilename() const
560 int dot;
561 CString filename=GetFilename();
562 dot = filename.ReverseFind(_T('.'));
563 if(dot>0)
564 return filename.Left(dot);
565 else
566 return filename;
569 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)
571 int length = sP1.GetLength();
572 if(length != sP2.GetLength())
574 // Different lengths
575 return false;
577 // We work from the end of the strings, because path differences
578 // are more likely to occur at the far end of a string
579 LPCTSTR pP1Start = sP1;
580 LPCTSTR pP1 = pP1Start+(length-1);
581 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
582 while(length-- > 0)
584 if(_totlower(*pP1--) != _totlower(*pP2--))
586 return false;
589 return true;
592 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)
594 int length = sP1.GetLength();
595 if(length != sP2.GetLength())
597 // Different lengths
598 return false;
600 // We work from the end of the strings, because path differences
601 // are more likely to occur at the far end of a string
602 LPCTSTR pP1Start = sP1;
603 LPCTSTR pP1 = pP1Start+(length-1);
604 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
605 while(length-- > 0)
607 if((*pP1--) != (*pP2--))
609 return false;
612 return true;
615 bool CTGitPath::IsEmpty() const
617 // Check the backward slash path first, since the chance that this
618 // one is set is higher. In case of a 'false' return value it's a little
619 // bit faster.
620 return m_sBackslashPath.IsEmpty() && m_sFwdslashPath.IsEmpty();
623 // Test if both paths refer to the same item
624 // Ignores case and slash direction
625 bool CTGitPath::IsEquivalentTo(const CTGitPath& rhs) const
627 // Try and find a slash direction which avoids having to convert
628 // both filenames
629 if(!m_sBackslashPath.IsEmpty())
631 // *We've* got a \ path - make sure that the RHS also has a \ path
632 rhs.EnsureBackslashPathSet();
633 return ArePathStringsEqualWithCase(m_sBackslashPath, rhs.m_sBackslashPath);
635 else
637 // Assume we've got a fwdslash path and make sure that the RHS has one
638 rhs.EnsureFwdslashPathSet();
639 return ArePathStringsEqualWithCase(m_sFwdslashPath, rhs.m_sFwdslashPath);
643 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath& rhs) const
645 // Try and find a slash direction which avoids having to convert
646 // both filenames
647 if(!m_sBackslashPath.IsEmpty())
649 // *We've* got a \ path - make sure that the RHS also has a \ path
650 rhs.EnsureBackslashPathSet();
651 return ArePathStringsEqual(m_sBackslashPath, rhs.m_sBackslashPath);
653 else
655 // Assume we've got a fwdslash path and make sure that the RHS has one
656 rhs.EnsureFwdslashPathSet();
657 return ArePathStringsEqual(m_sFwdslashPath, rhs.m_sFwdslashPath);
661 bool CTGitPath::IsAncestorOf(const CTGitPath& possibleDescendant) const
663 possibleDescendant.EnsureBackslashPathSet();
664 EnsureBackslashPathSet();
666 bool bPathStringsEqual = ArePathStringsEqual(m_sBackslashPath, possibleDescendant.m_sBackslashPath.Left(m_sBackslashPath.GetLength()));
667 if (m_sBackslashPath.GetLength() >= possibleDescendant.GetWinPathString().GetLength())
669 return bPathStringsEqual;
672 return (bPathStringsEqual &&
673 ((possibleDescendant.m_sBackslashPath[m_sBackslashPath.GetLength()] == '\\')||
674 (m_sBackslashPath.GetLength()==3 && m_sBackslashPath[1]==':')));
677 // Get a string representing the file path, optionally with a base
678 // section stripped off the front.
679 CString CTGitPath::GetDisplayString(const CTGitPath* pOptionalBasePath /* = NULL*/) const
681 EnsureFwdslashPathSet();
682 if(pOptionalBasePath != NULL)
684 // Find the length of the base-path without having to do an 'ensure' on it
685 int baseLength = max(pOptionalBasePath->m_sBackslashPath.GetLength(), pOptionalBasePath->m_sFwdslashPath.GetLength());
687 // Now, chop that baseLength of the front of the path
688 return m_sFwdslashPath.Mid(baseLength).TrimLeft('/');
690 return m_sFwdslashPath;
693 int CTGitPath::Compare(const CTGitPath& left, const CTGitPath& right)
695 left.EnsureBackslashPathSet();
696 right.EnsureBackslashPathSet();
697 return left.m_sBackslashPath.CompareNoCase(right.m_sBackslashPath);
700 bool operator<(const CTGitPath& left, const CTGitPath& right)
702 return CTGitPath::Compare(left, right) < 0;
705 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right)
707 return left.IsEquivalentTo(right);
710 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right)
712 if (left.IsAdminDir() && right.IsAdminDir())
714 CTGitPath l = left;
715 CTGitPath r = right;
718 l = l.GetContainingDirectory();
719 } while(l.HasAdminDir());
722 r = r.GetContainingDirectory();
723 } while(r.HasAdminDir());
724 return l.GetContainingDirectory().IsEquivalentTo(r.GetContainingDirectory());
726 return left.GetDirectory().IsEquivalentTo(right.GetDirectory());
729 bool CTGitPath::CheckChild(const CTGitPath &parent, const CTGitPath& child)
731 return parent.IsAncestorOf(child);
734 void CTGitPath::AppendRawString(const CString& sAppend)
736 EnsureFwdslashPathSet();
737 CString strCopy = m_sFwdslashPath += sAppend;
738 SetFromUnknown(strCopy);
741 void CTGitPath::AppendPathString(const CString& sAppend)
743 EnsureBackslashPathSet();
744 CString cleanAppend(sAppend);
745 cleanAppend.Replace('/', '\\');
746 cleanAppend.TrimLeft('\\');
747 m_sBackslashPath.TrimRight('\\');
748 CString strCopy = m_sBackslashPath + _T("\\") + cleanAppend;
749 SetFromWin(strCopy);
752 bool CTGitPath::HasAdminDir() const
754 if (m_bHasAdminDirKnown)
755 return m_bHasAdminDir;
757 EnsureBackslashPathSet();
758 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
759 m_bHasAdminDirKnown = true;
760 return m_bHasAdminDir;
763 bool CTGitPath::HasSubmodules() const
765 return !g_GitAdminDir.GetSuperProjectRoot(GetWinPathString()).IsEmpty();
768 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const
770 if (m_bHasAdminDirKnown)
772 if (ProjectTopDir)
773 *ProjectTopDir = m_sProjectRoot;
774 return m_bHasAdminDir;
777 EnsureBackslashPathSet();
778 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
779 m_bHasAdminDirKnown = true;
780 if (ProjectTopDir)
781 *ProjectTopDir = m_sProjectRoot;
782 return m_bHasAdminDir;
785 bool CTGitPath::IsAdminDir() const
787 if (m_bIsAdminDirKnown)
788 return m_bIsAdminDir;
790 EnsureBackslashPathSet();
791 m_bIsAdminDir = g_GitAdminDir.IsAdminDirPath(m_sBackslashPath);
792 m_bIsAdminDirKnown = true;
793 return m_bIsAdminDir;
796 bool CTGitPath::IsValidOnWindows() const
798 if (m_bIsValidOnWindowsKnown)
799 return m_bIsValidOnWindows;
801 m_bIsValidOnWindows = false;
802 EnsureBackslashPathSet();
803 CString sMatch = m_sBackslashPath + _T("\r\n");
804 wstring sPattern;
805 // the 'file://' URL is just a normal windows path:
806 if (sMatch.Left(7).CompareNoCase(_T("file:\\\\"))==0)
808 sMatch = sMatch.Mid(7);
809 sMatch.TrimLeft(_T("\\"));
810 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
812 else if (IsUrl())
814 sPattern = _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
816 else
818 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
823 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
824 tr1::wsmatch match;
826 wstring rmatch = wstring((LPCTSTR)sMatch);
827 if (tr1::regex_match(rmatch, match, rx))
829 if (wstring(match[0]).compare(sMatch)==0)
830 m_bIsValidOnWindows = true;
832 if (m_bIsValidOnWindows)
834 // now check for illegal filenames
835 tr1::wregex rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
836 rmatch = m_sBackslashPath;
837 if (tr1::regex_search(rmatch, rx2, tr1::regex_constants::match_default))
838 m_bIsValidOnWindows = false;
841 catch (exception) {}
843 m_bIsValidOnWindowsKnown = true;
844 return m_bIsValidOnWindows;
847 bool CTGitPath::IsSpecialDirectory() const
849 if (m_bIsSpecialDirectoryKnown)
850 return m_bIsSpecialDirectory;
852 static LPCTSTR specialDirectories[]
853 = { _T("trunk"), _T("tags"), _T("branches") };
855 for (int i=0 ; i<(sizeof(specialDirectories) / sizeof(specialDirectories[0])) ; ++i)
857 CString name = GetFileOrDirectoryName();
858 if (0 == name.CompareNoCase(specialDirectories[i]))
860 m_bIsSpecialDirectory = true;
861 break;
865 m_bIsSpecialDirectoryKnown = true;
867 return m_bIsSpecialDirectory;
870 //////////////////////////////////////////////////////////////////////////
872 CTGitPathList::CTGitPathList()
877 // A constructor which allows a path list to be easily built which one initial entry in
878 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)
880 AddPath(firstEntry);
882 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool staged)
884 int pos=0;
885 CString one;
886 CTGitPath path;
887 CString part;
888 while(pos>=0 && pos<out.size())
890 one.Empty();
891 path.Reset();
893 g_Git.StringAppend(&one,&out[pos],CP_ACP);
894 int tabstart=0;
895 path.m_Action=path.ParserAction(out[pos]);
896 one.Tokenize(_T("\t"),tabstart);
898 if(tabstart>=0)
899 path.SetFromGit(one.Right(one.GetLength()-tabstart));
901 tabstart=0;
903 part=one.Tokenize(_T(" "),tabstart); //Tag
905 part=one.Tokenize(_T(" "),tabstart); //Mode
907 part=one.Tokenize(_T(" "),tabstart); //Hash
909 part=one.Tokenize(_T("\t"),tabstart); //Stage
911 path.m_Stage=_ttol(part);
913 this->AddPath(path);
915 pos=out.findNextString(pos);
917 return pos;
919 int CTGitPathList::FillUnRev(int action,CTGitPathList *list)
921 int pos=0;
922 this->Clear();
923 CTGitPath path;
925 int count;
926 if(list==NULL)
927 count=1;
928 else
929 count=list->GetCount();
930 for(int i=0;i<count;i++)
932 CString cmd;
933 pos=0;
935 CString ignored;
936 if(action & CTGitPath::LOGACTIONS_IGNORE)
937 ignored= _T(" --ignored");
939 if(list==NULL)
941 cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");
942 cmd+=ignored;
945 else
946 { cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z %s-- \"%s\""),
947 ignored,
948 (*list)[i].GetWinPathString());
951 BYTE_VECTOR out;
952 out.clear();
953 g_Git.Run(cmd,&out);
955 pos=0;
956 CString one;
957 while( pos>=0 && pos<out.size())
959 one.Empty();
960 g_Git.StringAppend(&one,&out[pos],CP_ACP);
961 if(!one.IsEmpty())
963 //SetFromGit will clear all status
964 path.SetFromGit(one);
965 path.m_Action=action;
966 AddPath(path);
968 pos=out.findNextString(pos);
972 return 0;
974 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log)
976 this->Clear();
977 int pos=0;
978 //BYTE *p=&log[0];
979 //CString one;
980 CTGitPath path;
981 m_Action=0;
982 while( pos>=0 && pos<log.size())
984 //one=log.Tokenize(_T("\n"),pos);
985 path.Reset();
986 if(log[pos]=='\n')
987 pos++;
989 if(log[pos]==':')
991 bool merged=false;
992 if(log[pos+1] ==':')
994 merged=true;
996 int end=log.find(0,pos);
997 int actionstart=-1;
998 int numfile=1;
999 int file1=-1,file2=-1;
1000 if( end>0 )
1002 actionstart=log.find(' ',end-6);
1003 pos=actionstart;
1005 if( actionstart>0 )
1007 actionstart++;
1009 file1 = log.find(0,actionstart);
1010 if( file1>=0 )
1012 file1++;
1013 pos=file1;
1015 if( log[actionstart] == 'C' || log[actionstart] == 'R' )
1017 file2=file1;
1018 numfile=2;
1019 file1 = log.find(0,file1);
1020 if(file1>=0 )
1022 file1++;
1023 pos=file1;
1029 CString pathname1;
1030 CString pathname2;
1032 if( file1>=0 )
1033 g_Git.StringAppend(&pathname1,&log[file1],CP_ACP);
1034 if( file2>=0 )
1035 g_Git.StringAppend(&pathname2,&log[file2],CP_ACP);
1037 CTGitPath *GitPath=LookForGitPath(pathname1);
1039 if(GitPath)
1041 GitPath->ParserAction( log[actionstart] );
1042 GitPath->m_Action |=merged?CTGitPath::LOGACTIONS_MERGED:0;
1043 m_Action |=GitPath->m_Action;
1045 }else
1047 int ac=path.ParserAction(log[actionstart] );
1048 ac |= merged?CTGitPath::LOGACTIONS_MERGED:0;
1050 path.SetFromGit(pathname1,&pathname2);
1051 path.m_Action=ac;
1052 //action must be set after setfromgit. SetFromGit will clear all status.
1053 this->m_Action|=ac;
1055 AddPath(path);
1059 }else
1061 int tabstart=0;
1062 path.Reset();
1063 CString StatAdd;
1064 CString StatDel;
1065 CString file1;
1066 CString file2;
1068 tabstart=log.find('\t',pos);
1069 if(tabstart >=0)
1071 log[tabstart]=0;
1072 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);
1073 pos=tabstart+1;
1076 tabstart=log.find('\t',pos);
1077 if(tabstart >=0)
1079 log[tabstart]=0;
1081 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);
1082 pos=tabstart+1;
1085 if(log[pos] == 0) //rename
1087 pos++;
1088 g_Git.StringAppend(&file2,&log[pos],CP_ACP);
1089 int sec=log.find(0,pos);
1090 if(sec>=0)
1092 sec++;
1093 g_Git.StringAppend(&file1,&log[sec],CP_ACP);
1095 pos=sec;
1097 }else
1099 g_Git.StringAppend(&file1,&log[pos],CP_ACP);
1101 path.SetFromGit(file1,&file2);
1103 CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());
1104 if(GitPath)
1106 GitPath->m_StatAdd=StatAdd;
1107 GitPath->m_StatDel=StatDel;
1108 }else
1110 //path.SetFromGit(pathname);
1111 path.m_StatAdd=StatAdd;
1112 path.m_StatDel=StatDel;
1113 AddPath(path);
1117 pos=log.findNextString(pos);
1119 return pos;
1122 void CTGitPathList::AddPath(const CTGitPath& newPath)
1124 m_paths.push_back(newPath);
1125 m_commonBaseDirectory.Reset();
1127 int CTGitPathList::GetCount() const
1129 return (int)m_paths.size();
1131 void CTGitPathList::Clear()
1133 m_paths.clear();
1134 m_commonBaseDirectory.Reset();
1137 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const
1139 ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());
1140 return m_paths[index];
1143 bool CTGitPathList::AreAllPathsFiles() const
1145 // Look through the vector for any directories - if we find them, return false
1146 return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();
1150 #if defined(_MFC_VER)
1152 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)
1154 Clear();
1157 CString strLine;
1158 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
1160 // for every selected file/folder
1161 CTGitPath path;
1162 while (file.ReadString(strLine))
1164 path.SetFromUnknown(strLine);
1165 AddPath(path);
1167 file.Close();
1169 catch (CFileException* pE)
1171 TRACE("CFileException loading target file list\n");
1172 TCHAR error[10000] = {0};
1173 pE->GetErrorMessage(error, 10000);
1174 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1175 pE->Delete();
1176 return false;
1178 return true;
1181 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const
1185 if (bANSI)
1187 CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
1188 PathVector::const_iterator it;
1189 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1191 CStringA line = CStringA(it->GetGitPathString()) + '\n';
1192 file.Write(line, line.GetLength());
1194 file.Close();
1196 else
1198 CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);
1199 PathVector::const_iterator it;
1200 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1202 file.WriteString(it->GetGitPathString()+_T("\n"));
1204 file.Close();
1207 catch (CFileException* pE)
1209 TRACE("CFileException in writing temp file\n");
1210 pE->Delete();
1211 return false;
1213 return true;
1217 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)
1219 int pos = 0;
1220 CString temp;
1221 for(;;)
1223 temp = sPathString.Tokenize(_T("*"),pos);
1224 if(temp.IsEmpty())
1226 break;
1228 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));
1232 CString CTGitPathList::CreateAsteriskSeparatedString() const
1234 CString sRet;
1235 PathVector::const_iterator it;
1236 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1238 if (!sRet.IsEmpty())
1239 sRet += _T("*");
1240 sRet += it->GetWinPathString();
1242 return sRet;
1244 #endif // _MFC_VER
1246 bool
1247 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1249 // Check if all the paths are files and in the same directory
1250 PathVector::const_iterator it;
1251 m_commonBaseDirectory.Reset();
1252 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1254 if(it->IsDirectory())
1256 return false;
1258 const CTGitPath& baseDirectory = it->GetDirectory();
1259 if(m_commonBaseDirectory.IsEmpty())
1261 m_commonBaseDirectory = baseDirectory;
1263 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1265 // Different path
1266 m_commonBaseDirectory.Reset();
1267 return false;
1270 return true;
1273 CTGitPath CTGitPathList::GetCommonDirectory() const
1275 if (m_commonBaseDirectory.IsEmpty())
1277 PathVector::const_iterator it;
1278 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1280 const CTGitPath& baseDirectory = it->GetDirectory();
1281 if(m_commonBaseDirectory.IsEmpty())
1283 m_commonBaseDirectory = baseDirectory;
1285 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1287 // Different path
1288 m_commonBaseDirectory.Reset();
1289 break;
1293 // since we only checked strings, not paths,
1294 // we have to make sure now that we really return a *path* here
1295 PathVector::const_iterator iter;
1296 for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)
1298 if (!m_commonBaseDirectory.IsAncestorOf(*iter))
1300 m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();
1301 break;
1304 return m_commonBaseDirectory;
1307 CTGitPath CTGitPathList::GetCommonRoot() const
1309 PathVector::const_iterator it;
1310 CString sRoot, sTempRoot;
1311 bool bEqual = true;
1313 if (GetCount() == 1)
1314 return m_paths[0];
1316 int backSlashPos = 0;
1317 int searchStartPos = 0;
1318 while (bEqual)
1320 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1322 if (backSlashPos == 0)
1324 backSlashPos = it->GetWinPathString().Find('\\', searchStartPos+1);
1325 if ((backSlashPos < 0)&&(searchStartPos != it->GetWinPathString().GetLength()))
1326 backSlashPos = it->GetWinPathString().GetLength();
1328 else if (it->GetWinPathString().Find('\\', searchStartPos+1) != backSlashPos)
1330 if (it->GetWinPathString().Find('\\', searchStartPos+1) < 0)
1332 if (it->GetWinPathString().GetLength() != backSlashPos)
1334 bEqual = false;
1335 break;
1338 else
1340 bEqual = false;
1341 break;
1344 if (backSlashPos < 0)
1346 bEqual = false;
1347 break;
1350 if (bEqual == false)
1352 if (searchStartPos)
1353 sRoot = m_paths[0].GetWinPathString().Left(searchStartPos+1);
1355 else
1357 searchStartPos = backSlashPos;
1359 backSlashPos = 0;
1362 return CTGitPath(sRoot.TrimRight('\\'));
1365 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)
1367 std::sort(m_paths.begin(), m_paths.end());
1368 if (bReverse)
1369 std::reverse(m_paths.begin(), m_paths.end());
1372 void CTGitPathList::DeleteAllFiles(bool bTrash)
1374 PathVector::const_iterator it;
1375 if (bTrash)
1377 SortByPathname();
1378 CString sPaths;
1379 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1381 if ((it->Exists())&&(!it->IsDirectory()))
1383 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1384 sPaths += it->GetWinPath();
1385 sPaths += '\0';
1388 sPaths += '\0';
1389 sPaths += '\0';
1390 SHFILEOPSTRUCT shop = {0};
1391 shop.wFunc = FO_DELETE;
1392 shop.pFrom = (LPCTSTR)sPaths;
1393 shop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
1394 SHFileOperation(&shop);
1396 else
1398 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1400 if (!it->IsDirectory())
1402 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1403 ::DeleteFile(it->GetWinPath());
1407 Clear();
1410 void CTGitPathList::RemoveDuplicates()
1412 SortByPathname();
1413 // Remove the duplicates
1414 // (Unique moves them to the end of the vector, then erase chops them off)
1415 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());
1418 void CTGitPathList::RemoveAdminPaths()
1420 PathVector::iterator it;
1421 for(it = m_paths.begin(); it != m_paths.end(); )
1423 if (it->IsAdminDir())
1425 m_paths.erase(it);
1426 it = m_paths.begin();
1428 else
1429 ++it;
1433 void CTGitPathList::RemovePath(const CTGitPath& path)
1435 PathVector::iterator it;
1436 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1438 if (it->IsEquivalentTo(path))
1440 m_paths.erase(it);
1441 return;
1446 void CTGitPathList::RemoveItem(CTGitPath & path)
1448 PathVector::iterator it;
1449 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1451 if (it->GetGitPathString()==path.GetGitPathString())
1453 m_paths.erase(it);
1454 return;
1458 void CTGitPathList::RemoveChildren()
1460 SortByPathname();
1461 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());
1464 bool CTGitPathList::IsEqual(const CTGitPathList& list)
1466 if (list.GetCount() != GetCount())
1467 return false;
1468 for (int i=0; i<list.GetCount(); ++i)
1470 if (!list[i].IsEquivalentTo(m_paths[i]))
1471 return false;
1473 return true;
1476 //////////////////////////////////////////////////////////////////////////
1477 #if 0
1478 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const
1480 apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));
1482 for(int nItem = 0; nItem < GetCount(); nItem++)
1484 const char * target = m_paths[nItem].GetGitApiPath(pool);
1485 (*((const char **) apr_array_push (targets))) = target;
1488 return targets;
1490 #endif
1491 //////////////////////////////////////////////////////////////////////////
1493 #if 0
1494 #if defined(_DEBUG)
1495 // Some test cases for these classes
1496 static class CTGitPathTests
1498 public:
1499 CTGitPathTests()
1501 apr_initialize();
1502 pool = svn_pool_create(NULL);
1503 GetDirectoryTest();
1504 AdminDirTest();
1505 SortTest();
1506 RawAppendTest();
1507 PathAppendTest();
1508 RemoveDuplicatesTest();
1509 RemoveChildrenTest();
1510 ContainingDirectoryTest();
1511 AncestorTest();
1512 SubversionPathTest();
1513 GetCommonRootTest();
1514 #if defined(_MFC_VER)
1515 ValidPathAndUrlTest();
1516 ListLoadingTest();
1517 #endif
1518 apr_terminate();
1521 private:
1522 // apr_pool_t * pool;
1523 void GetDirectoryTest()
1525 // Bit tricky, this test, because we need to know something about the file
1526 // layout on the machine which is running the test
1527 TCHAR winDir[MAX_PATH+1];
1528 GetWindowsDirectory(winDir, MAX_PATH);
1529 CString sWinDir(winDir);
1531 CTGitPath testPath;
1532 // This is a file which we know will always be there
1533 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));
1534 ATLASSERT(!testPath.IsDirectory());
1535 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1536 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);
1538 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1539 // but we know it must be different to the directory itself
1540 testPath.SetFromUnknown(sWinDir);
1541 ATLASSERT(testPath.IsDirectory());
1542 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1543 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);
1544 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());
1546 // Try a root path
1547 testPath.SetFromUnknown(_T("C:\\"));
1548 ATLASSERT(testPath.IsDirectory());
1549 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1550 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1551 // Try a root UNC path
1552 testPath.SetFromUnknown(_T("\\MYSTATION"));
1553 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1556 void AdminDirTest()
1558 CTGitPath testPath;
1559 testPath.SetFromUnknown(_T("c:\\.svndir"));
1560 ATLASSERT(!testPath.IsAdminDir());
1561 testPath.SetFromUnknown(_T("c:\\test.svn"));
1562 ATLASSERT(!testPath.IsAdminDir());
1563 testPath.SetFromUnknown(_T("c:\\.svn"));
1564 ATLASSERT(testPath.IsAdminDir());
1565 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));
1566 ATLASSERT(!testPath.IsAdminDir());
1567 testPath.SetFromUnknown(_T("c:\\.svn\\test"));
1568 ATLASSERT(testPath.IsAdminDir());
1570 CTGitPathList pathList;
1571 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));
1572 pathList.AddPath(CTGitPath(_T("c:\\.svn")));
1573 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1574 pathList.AddPath(CTGitPath(_T("c:\\test")));
1575 pathList.RemoveAdminPaths();
1576 ATLASSERT(pathList.GetCount()==2);
1577 pathList.Clear();
1578 pathList.AddPath(CTGitPath(_T("c:\\test")));
1579 pathList.RemoveAdminPaths();
1580 ATLASSERT(pathList.GetCount()==1);
1583 void SortTest()
1585 CTGitPathList testList;
1586 CTGitPath testPath;
1587 testPath.SetFromUnknown(_T("c:/Z"));
1588 testList.AddPath(testPath);
1589 testPath.SetFromUnknown(_T("c:/B"));
1590 testList.AddPath(testPath);
1591 testPath.SetFromUnknown(_T("c:\\a"));
1592 testList.AddPath(testPath);
1593 testPath.SetFromUnknown(_T("c:/Test"));
1594 testList.AddPath(testPath);
1596 testList.SortByPathname();
1598 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));
1599 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));
1600 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));
1601 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));
1604 void RawAppendTest()
1606 CTGitPath testPath(_T("c:/test/"));
1607 testPath.AppendRawString(_T("/Hello"));
1608 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1610 testPath.AppendRawString(_T("\\T2"));
1611 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1613 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1614 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1615 testBasePath.AppendRawString(testFilePath.GetFileExtension());
1616 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1619 void PathAppendTest()
1621 CTGitPath testPath(_T("c:/test/"));
1622 testPath.AppendPathString(_T("/Hello"));
1623 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1625 testPath.AppendPathString(_T("T2"));
1626 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1628 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1629 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1630 // You wouldn't want to do this in real life - you'd use append-raw
1631 testBasePath.AppendPathString(testFilePath.GetFileExtension());
1632 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1635 void RemoveDuplicatesTest()
1637 CTGitPathList list;
1638 list.AddPath(CTGitPath(_T("Z")));
1639 list.AddPath(CTGitPath(_T("A")));
1640 list.AddPath(CTGitPath(_T("E")));
1641 list.AddPath(CTGitPath(_T("E")));
1643 ATLASSERT(list[2].IsEquivalentTo(list[3]));
1644 ATLASSERT(list[2]==list[3]);
1646 ATLASSERT(list.GetCount() == 4);
1648 list.RemoveDuplicates();
1650 ATLASSERT(list.GetCount() == 3);
1652 ATLASSERT(list[0].GetWinPathString() == _T("A"));
1653 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);
1654 ATLASSERT(list[2].GetWinPathString() == _T("Z"));
1657 void RemoveChildrenTest()
1659 CTGitPathList list;
1660 list.AddPath(CTGitPath(_T("c:\\test")));
1661 list.AddPath(CTGitPath(_T("c:\\test\\file")));
1662 list.AddPath(CTGitPath(_T("c:\\testfile")));
1663 list.AddPath(CTGitPath(_T("c:\\parent")));
1664 list.AddPath(CTGitPath(_T("c:\\parent\\child")));
1665 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1666 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1668 ATLASSERT(list.GetCount() == 7);
1670 list.RemoveChildren();
1672 ATLTRACE("count = %d\n", list.GetCount());
1673 ATLASSERT(list.GetCount() == 3);
1675 list.SortByPathname();
1677 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1678 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1679 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1682 #if defined(_MFC_VER)
1683 void ListLoadingTest()
1685 TCHAR buf[MAX_PATH];
1686 GetCurrentDirectory(MAX_PATH, buf);
1687 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1688 CTGitPathList testList;
1689 testList.LoadFromAsteriskSeparatedString(sPathList);
1691 ATLASSERT(testList.GetCount() == 3);
1692 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));
1693 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1694 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));
1696 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));
1697 testList.Clear();
1698 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1699 testList.LoadFromAsteriskSeparatedString(sPathList);
1700 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1702 #endif
1704 void ContainingDirectoryTest()
1707 CTGitPath testPath;
1708 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1709 CTGitPath dir;
1710 dir = testPath.GetContainingDirectory();
1711 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1712 dir = dir.GetContainingDirectory();
1713 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));
1714 dir = dir.GetContainingDirectory();
1715 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));
1716 dir = dir.GetContainingDirectory();
1717 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));
1718 dir = dir.GetContainingDirectory();
1719 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));
1720 dir = dir.GetContainingDirectory();
1721 ATLASSERT(dir.IsEmpty());
1722 ATLASSERT(dir.GetWinPathString() == _T(""));
1725 void AncestorTest()
1727 CTGitPath testPath;
1728 testPath.SetFromWin(_T("c:\\windows"));
1729 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1730 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1731 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1732 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1733 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1736 void SubversionPathTest()
1738 CTGitPath testPath;
1739 testPath.SetFromWin(_T("c:\\"));
1740 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);
1741 testPath.SetFromWin(_T("c:\\folder"));
1742 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);
1743 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1744 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);
1745 testPath.SetFromUnknown(_T("http://testing/"));
1746 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);
1747 testPath.SetFromGit(NULL);
1748 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);
1749 #if defined(_MFC_VER)
1750 testPath.SetFromUnknown(_T("http://testing again"));
1751 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1752 testPath.SetFromUnknown(_T("http://testing%20again"));
1753 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1754 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1755 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1756 #endif
1759 void GetCommonRootTest()
1761 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));
1762 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));
1763 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1765 CTGitPathList list;
1766 list.AddPath(pathA);
1767 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1768 list.AddPath(pathB);
1769 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1770 list.AddPath(pathC);
1771 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1772 #ifdef _MFC_VER
1773 list.Clear();
1774 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");
1775 list.LoadFromAsteriskSeparatedString(sPathList);
1776 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1778 list.Clear();
1779 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");
1780 list.LoadFromAsteriskSeparatedString(sPathList);
1781 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1783 list.Clear();
1784 sPathList = _T("c:\\windows\\*c:\\windows");
1785 list.LoadFromAsteriskSeparatedString(sPathList);
1786 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1788 list.Clear();
1789 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");
1790 list.LoadFromAsteriskSeparatedString(sPathList);
1791 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1793 list.Clear();
1794 sPathList = _T("c:\\windowsdummy*c:\\windows");
1795 list.LoadFromAsteriskSeparatedString(sPathList);
1796 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1797 #endif
1800 void ValidPathAndUrlTest()
1802 CTGitPath testPath;
1803 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1804 ATLASSERT(testPath.IsValidOnWindows());
1805 testPath.SetFromWin(_T("c:\\"));
1806 ATLASSERT(testPath.IsValidOnWindows());
1807 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1808 ATLASSERT(testPath.IsValidOnWindows());
1809 testPath.SetFromWin(_T("c"));
1810 ATLASSERT(testPath.IsValidOnWindows());
1811 testPath.SetFromWin(_T("c:\\test folder\\file"));
1812 ATLASSERT(testPath.IsValidOnWindows());
1813 testPath.SetFromWin(_T("c:\\folder\\"));
1814 ATLASSERT(testPath.IsValidOnWindows());
1815 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1816 ATLASSERT(testPath.IsValidOnWindows());
1817 testPath.SetFromWin(_T("c:\\.svn"));
1818 ATLASSERT(testPath.IsValidOnWindows());
1819 testPath.SetFromWin(_T("c:\\com\\file"));
1820 ATLASSERT(testPath.IsValidOnWindows());
1821 testPath.SetFromWin(_T("c:\\test\\conf"));
1822 ATLASSERT(testPath.IsValidOnWindows());
1823 testPath.SetFromWin(_T("c:\\LPT"));
1824 ATLASSERT(testPath.IsValidOnWindows());
1825 testPath.SetFromWin(_T("c:\\test\\LPT"));
1826 ATLASSERT(testPath.IsValidOnWindows());
1827 testPath.SetFromWin(_T("c:\\com1test"));
1828 ATLASSERT(testPath.IsValidOnWindows());
1829 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1830 ATLASSERT(testPath.IsValidOnWindows());
1832 testPath.SetFromWin(_T("\\\\Share\\filename"));
1833 ATLASSERT(testPath.IsValidOnWindows());
1834 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));
1835 ATLASSERT(testPath.IsValidOnWindows());
1836 testPath.SetFromWin(_T("\\\\Share\\.svn"));
1837 ATLASSERT(testPath.IsValidOnWindows());
1839 // now the negative tests
1840 testPath.SetFromWin(_T("c:\\test:folder"));
1841 ATLASSERT(!testPath.IsValidOnWindows());
1842 testPath.SetFromWin(_T("c:\\file<name"));
1843 ATLASSERT(!testPath.IsValidOnWindows());
1844 testPath.SetFromWin(_T("c:\\something*else"));
1845 ATLASSERT(!testPath.IsValidOnWindows());
1846 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));
1847 ATLASSERT(!testPath.IsValidOnWindows());
1848 testPath.SetFromWin(_T("c:\\ext.>ension"));
1849 ATLASSERT(!testPath.IsValidOnWindows());
1850 testPath.SetFromWin(_T("c:\\com1\\filename"));
1851 ATLASSERT(!testPath.IsValidOnWindows());
1852 testPath.SetFromWin(_T("c:\\com1"));
1853 ATLASSERT(!testPath.IsValidOnWindows());
1854 testPath.SetFromWin(_T("c:\\com1\\AuX"));
1855 ATLASSERT(!testPath.IsValidOnWindows());
1857 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1858 ATLASSERT(!testPath.IsValidOnWindows());
1859 testPath.SetFromWin(_T("\\\\Share\\prn"));
1860 ATLASSERT(!testPath.IsValidOnWindows());
1861 testPath.SetFromWin(_T("\\\\Share\\NUL"));
1862 ATLASSERT(!testPath.IsValidOnWindows());
1864 // now come some URL tests
1865 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));
1866 ATLASSERT(testPath.IsValidOnWindows());
1867 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1868 ATLASSERT(testPath.IsValidOnWindows());
1869 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1870 ATLASSERT(testPath.IsValidOnWindows());
1871 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1872 ATLASSERT(testPath.IsValidOnWindows());
1873 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));
1874 ATLASSERT(testPath.IsValidOnWindows());
1875 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1876 ATLASSERT(testPath.IsValidOnWindows());
1877 // and some negative URL tests
1878 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1879 ATLASSERT(!testPath.IsValidOnWindows());
1880 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1881 ATLASSERT(!testPath.IsValidOnWindows());
1882 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1883 ATLASSERT(!testPath.IsValidOnWindows());
1884 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
1885 ATLASSERT(!testPath.IsValidOnWindows());
1886 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
1887 ATLASSERT(!testPath.IsValidOnWindows());
1891 } TGitPathTestobject;
1892 #endif
1893 #endif
1895 CTGitPath * CTGitPathList::LookForGitPath(CString path)
1897 int i=0;
1898 for(i=0;i<this->GetCount();i++)
1900 if((*this)[i].GetGitPathString() == path )
1901 return (CTGitPath*)&(*this)[i];
1903 return NULL;
1905 CString CTGitPath::GetActionName(int action)
1907 if(action & CTGitPath::LOGACTIONS_UNMERGED)
1908 return _T("Conflict");
1909 if(action & CTGitPath::LOGACTIONS_ADDED)
1910 return _T("Added");
1911 if(action & CTGitPath::LOGACTIONS_DELETED)
1912 return _T("Deleted");
1913 if(action & CTGitPath::LOGACTIONS_MERGED )
1914 return _T("Merged");
1916 if(action & CTGitPath::LOGACTIONS_MODIFIED)
1917 return _T("Modified");
1918 if(action & CTGitPath::LOGACTIONS_REPLACED)
1919 return _T("Rename");
1920 if(action & CTGitPath::LOGACTIONS_COPY)
1921 return _T("Copy");
1922 if(action & CTGitPath::LOGACTIONS_REBASE_EDIT)
1923 return _T("Edit");
1924 if(action & CTGitPath::LOGACTIONS_REBASE_SQUASH)
1925 return _T("Squash");
1926 if(action & CTGitPath::LOGACTIONS_REBASE_PICK)
1927 return _T("Pick");
1928 if(action & CTGitPath::LOGACTIONS_REBASE_SKIP)
1929 return _T("Skip");
1931 return _T("Unknown");
1933 CString CTGitPath::GetActionName()
1935 return GetActionName(m_Action);
1938 int CTGitPathList::GetAction()
1940 return m_Action;