Clean up warning
[TortoiseGit.git] / src / Git / TGitPath.cpp
blob7fab1c883b8eca496df23cc55c8ef1f0abfa1f0f
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 #include "Globals.h"
28 #if defined(_MFC_VER)
29 //#include "MessageBox.h"
30 //#include "AppUtils.h"
31 #endif
33 #ifndef ASSERT
34 #define ASSERT()
35 #endif
36 using namespace std;
37 extern CGit g_Git;
39 CTGitPath::CTGitPath(void) :
40 m_bDirectoryKnown(false),
41 m_bIsDirectory(false),
42 m_bIsURL(false),
43 m_bURLKnown(false),
44 m_bHasAdminDirKnown(false),
45 m_bHasAdminDir(false),
46 m_bIsValidOnWindowsKnown(false),
47 m_bIsReadOnly(false),
48 m_bIsAdminDirKnown(false),
49 m_bIsAdminDir(false),
50 m_bExists(false),
51 m_bExistsKnown(false),
52 m_bLastWriteTimeKnown(0),
53 m_lastWriteTime(0),
54 m_customData(NULL),
55 m_bIsSpecialDirectoryKnown(false),
56 m_bIsSpecialDirectory(false)
58 m_Action=0;
59 m_ParentNo=0;
62 CTGitPath::~CTGitPath(void)
65 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
66 CTGitPath::CTGitPath(const CString& sUnknownPath) :
67 m_bDirectoryKnown(false),
68 m_bIsDirectory(false),
69 m_bIsURL(false),
70 m_bURLKnown(false),
71 m_bHasAdminDirKnown(false),
72 m_bHasAdminDir(false),
73 m_bIsValidOnWindowsKnown(false),
74 m_bIsReadOnly(false),
75 m_bIsAdminDirKnown(false),
76 m_bIsAdminDir(false),
77 m_bExists(false),
78 m_bExistsKnown(false),
79 m_bLastWriteTimeKnown(0),
80 m_lastWriteTime(0),
81 m_customData(NULL),
82 m_bIsSpecialDirectoryKnown(false),
83 m_bIsSpecialDirectory(false)
85 SetFromUnknown(sUnknownPath);
86 m_Action=0;
87 m_Stage=0;
88 m_ParentNo=0;
91 int CTGitPath::ParserAction(BYTE action)
93 //action=action.TrimLeft();
94 //TCHAR c=action.GetAt(0);
95 if(action == 'M')
96 m_Action|= LOGACTIONS_MODIFIED;
97 if(action == 'R')
98 m_Action|= LOGACTIONS_REPLACED;
99 if(action == 'A')
100 m_Action|= LOGACTIONS_ADDED;
101 if(action == 'D')
102 m_Action|= LOGACTIONS_DELETED;
103 if(action == 'U')
104 m_Action|= LOGACTIONS_UNMERGED;
105 if(action == 'K')
106 m_Action|= LOGACTIONS_DELETED;
107 if(action == 'H')
108 m_Action|= LOGACTIONS_CACHE;
109 if(action == 'C' )
110 m_Action|= LOGACTIONS_COPY;
112 return m_Action;
114 void CTGitPath::SetFromGit(const char* pPath)
116 Reset();
117 if (pPath == NULL)
118 return;
119 int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);
120 if (len)
122 len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, m_sFwdslashPath.GetBuffer(len+1), len+1);
123 m_sFwdslashPath.ReleaseBuffer(len-1);
125 SanitizeRootPath(m_sFwdslashPath, true);
128 void CTGitPath::SetFromGit(const char* pPath, bool bIsDirectory)
130 SetFromGit(pPath);
131 m_bDirectoryKnown = true;
132 m_bIsDirectory = bIsDirectory;
135 void CTGitPath::SetFromGit(const TCHAR* pPath, bool bIsDirectory)
137 Reset();
138 if (pPath)
140 m_sFwdslashPath = pPath;
141 SanitizeRootPath(m_sFwdslashPath, true);
143 m_bDirectoryKnown = true;
144 m_bIsDirectory = bIsDirectory;
147 void CTGitPath::SetFromGit(const CString& sPath,CString *oldpath)
149 Reset();
150 m_sFwdslashPath = sPath;
151 SanitizeRootPath(m_sFwdslashPath, true);
152 if(oldpath)
153 m_sOldFwdslashPath = *oldpath;
156 void CTGitPath::SetFromWin(LPCTSTR pPath)
158 Reset();
159 m_sBackslashPath = pPath;
160 SanitizeRootPath(m_sBackslashPath, false);
161 ATLASSERT(m_sBackslashPath.Find('/')<0);
163 void CTGitPath::SetFromWin(const CString& sPath)
165 Reset();
166 m_sBackslashPath = sPath;
167 SanitizeRootPath(m_sBackslashPath, false);
169 void CTGitPath::SetFromWin(const CString& sPath, bool bIsDirectory)
171 Reset();
172 m_sBackslashPath = sPath;
173 m_bIsDirectory = bIsDirectory;
174 m_bDirectoryKnown = true;
175 SanitizeRootPath(m_sBackslashPath, false);
177 void CTGitPath::SetFromUnknown(const CString& sPath)
179 Reset();
180 // Just set whichever path we think is most likely to be used
181 // GitAdminDir admin;
182 // CString p;
183 // if(admin.HasAdminDir(sPath,&p))
184 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
185 // else
186 SetFwdslashPath(sPath);
189 LPCTSTR CTGitPath::GetWinPath() const
191 if(IsEmpty())
193 return _T("");
195 if(m_sBackslashPath.IsEmpty())
197 SetBackslashPath(m_sFwdslashPath);
199 return m_sBackslashPath;
201 // This is a temporary function, to be used during the migration to
202 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
203 const CString& CTGitPath::GetWinPathString() const
205 if(m_sBackslashPath.IsEmpty())
207 SetBackslashPath(m_sFwdslashPath);
209 return m_sBackslashPath;
212 const CString& CTGitPath::GetGitPathString() const
214 if(m_sFwdslashPath.IsEmpty())
216 SetFwdslashPath(m_sBackslashPath);
218 return m_sFwdslashPath;
221 const CString &CTGitPath::GetGitOldPathString() const
223 return m_sOldFwdslashPath;
225 #if 0
226 const char* CTGitPath::GetGitApiPath(apr_pool_t *pool) const
228 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
229 // each call to GetGitApiPath returns a different pointer value.
230 // If you made multiple calls to GetGitApiPath on the same string, only the last
231 // one would give you a valid pointer to an empty string, because each
232 // call would invalidate the previous call's return.
233 if(IsEmpty())
235 return "";
237 if(m_sFwdslashPath.IsEmpty())
239 SetFwdslashPath(m_sBackslashPath);
241 if(m_sUTF8FwdslashPath.IsEmpty())
243 SetUTF8FwdslashPath(m_sFwdslashPath);
245 if (svn_path_is_url(m_sUTF8FwdslashPath))
247 m_sUTF8FwdslashPathEscaped = CPathUtils::PathEscape(m_sUTF8FwdslashPath);
248 m_sUTF8FwdslashPathEscaped.Replace("file:////", "file:///\\");
249 m_sUTF8FwdslashPathEscaped = svn_path_canonicalize(m_sUTF8FwdslashPathEscaped, pool);
250 return m_sUTF8FwdslashPathEscaped;
252 m_sUTF8FwdslashPath = svn_path_canonicalize(m_sUTF8FwdslashPath, pool);
254 return m_sUTF8FwdslashPath;
256 #endif
258 const CString& CTGitPath::GetUIPathString() const
260 if (m_sUIPath.IsEmpty())
262 #if defined(_MFC_VER)
263 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
264 if (IsUrl())
266 m_sUIPath = CPathUtils::PathUnescape(GetGitPathString());
267 m_sUIPath.Replace(_T("file:////"), _T("file:///\\"));
270 else
271 #endif
273 m_sUIPath = GetWinPathString();
276 return m_sUIPath;
279 void CTGitPath::SetFwdslashPath(const CString& sPath) const
281 m_sFwdslashPath = sPath;
282 m_sFwdslashPath.Replace('\\', '/');
284 // We don't leave a trailing /
285 m_sFwdslashPath.TrimRight('/');
287 SanitizeRootPath(m_sFwdslashPath, true);
289 m_sFwdslashPath.Replace(_T("file:////"), _T("file:///\\"));
291 m_sUTF8FwdslashPath.Empty();
294 void CTGitPath::SetBackslashPath(const CString& sPath) const
296 m_sBackslashPath = sPath;
297 m_sBackslashPath.Replace('/', '\\');
298 m_sBackslashPath.TrimRight('\\');
299 SanitizeRootPath(m_sBackslashPath, false);
302 void CTGitPath::SetUTF8FwdslashPath(const CString& sPath) const
304 m_sUTF8FwdslashPath = CUnicodeUtils::GetUTF8(sPath);
307 void CTGitPath::SanitizeRootPath(CString& sPath, bool bIsForwardPath) const
309 // Make sure to add the trailing slash to root paths such as 'C:'
310 if (sPath.GetLength() == 2 && sPath[1] == ':')
312 sPath += (bIsForwardPath) ? _T("/") : _T("\\");
316 bool CTGitPath::IsUrl() const
318 #if 0
319 if (!m_bURLKnown)
321 EnsureFwdslashPathSet();
322 if(m_sUTF8FwdslashPath.IsEmpty())
324 SetUTF8FwdslashPath(m_sFwdslashPath);
326 m_bIsURL = !!svn_path_is_url(m_sUTF8FwdslashPath);
327 m_bURLKnown = true;
329 return m_bIsURL;
330 #endif
331 return false;
334 bool CTGitPath::IsDirectory() const
336 if(!m_bDirectoryKnown)
338 UpdateAttributes();
340 return m_bIsDirectory;
343 bool CTGitPath::Exists() const
345 if (!m_bExistsKnown)
347 UpdateAttributes();
349 return m_bExists;
352 bool CTGitPath::Delete(bool bTrash) const
354 EnsureBackslashPathSet();
355 ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);
356 bool bRet = false;
357 if (Exists())
359 if ((bTrash)||(IsDirectory()))
361 TCHAR * buf = new TCHAR[m_sBackslashPath.GetLength()+2];
362 _tcscpy_s(buf, m_sBackslashPath.GetLength()+2, m_sBackslashPath);
363 buf[m_sBackslashPath.GetLength()] = 0;
364 buf[m_sBackslashPath.GetLength()+1] = 0;
365 SHFILEOPSTRUCT shop = {0};
366 shop.wFunc = FO_DELETE;
367 shop.pFrom = buf;
368 shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
369 if (bTrash)
370 shop.fFlags |= FOF_ALLOWUNDO;
371 bRet = (SHFileOperation(&shop) == 0);
372 delete [] buf;
374 else
376 bRet = !!::DeleteFile(m_sBackslashPath);
379 m_bExists = false;
380 m_bExistsKnown = true;
381 return bRet;
384 __int64 CTGitPath::GetLastWriteTime() const
386 if(!m_bLastWriteTimeKnown)
388 UpdateAttributes();
390 return m_lastWriteTime;
393 bool CTGitPath::IsReadOnly() const
395 if(!m_bLastWriteTimeKnown)
397 UpdateAttributes();
399 return m_bIsReadOnly;
402 void CTGitPath::UpdateAttributes() const
404 EnsureBackslashPathSet();
405 WIN32_FILE_ATTRIBUTE_DATA attribs;
406 if(GetFileAttributesEx(m_sBackslashPath, GetFileExInfoStandard, &attribs))
408 m_bIsDirectory = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
409 m_lastWriteTime = *(__int64*)&attribs.ftLastWriteTime;
410 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
411 m_bExists = true;
413 else
415 DWORD err = GetLastError();
416 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))
418 m_bIsDirectory = false;
419 m_lastWriteTime = 0;
420 m_bExists = false;
422 else
424 m_bIsDirectory = false;
425 m_lastWriteTime = 0;
426 m_bExists = true;
427 return;
430 m_bDirectoryKnown = true;
431 m_bLastWriteTimeKnown = true;
432 m_bExistsKnown = true;
435 CTGitPath CTGitPath::GetSubPath(const CTGitPath &root)
437 CTGitPath path;
439 if(GetWinPathString().Left(root.GetWinPathString().GetLength()) == root.GetWinPathString())
441 CString str=GetWinPathString();
442 path.SetFromWin(str.Right(str.GetLength()-root.GetWinPathString().GetLength()-1));
444 return path;
447 void CTGitPath::EnsureBackslashPathSet() const
449 if(m_sBackslashPath.IsEmpty())
451 SetBackslashPath(m_sFwdslashPath);
452 ATLASSERT(IsEmpty() || !m_sBackslashPath.IsEmpty());
455 void CTGitPath::EnsureFwdslashPathSet() const
457 if(m_sFwdslashPath.IsEmpty())
459 SetFwdslashPath(m_sBackslashPath);
460 ATLASSERT(IsEmpty() || !m_sFwdslashPath.IsEmpty());
465 // Reset all the caches
466 void CTGitPath::Reset()
468 m_bDirectoryKnown = false;
469 m_bURLKnown = false;
470 m_bLastWriteTimeKnown = false;
471 m_bHasAdminDirKnown = false;
472 m_bIsValidOnWindowsKnown = false;
473 m_bIsAdminDirKnown = false;
474 m_bExistsKnown = false;
475 m_bIsSpecialDirectoryKnown = false;
476 m_bIsSpecialDirectory = false;
478 m_sBackslashPath.Empty();
479 m_sFwdslashPath.Empty();
480 m_sUTF8FwdslashPath.Empty();
481 this->m_Action=0;
482 this->m_StatAdd=_T("");
483 this->m_StatDel=_T("");
484 m_ParentNo=0;
485 ATLASSERT(IsEmpty());
488 CTGitPath CTGitPath::GetDirectory() const
490 if ((IsDirectory())||(!Exists()))
492 return *this;
494 return GetContainingDirectory();
497 CTGitPath CTGitPath::GetContainingDirectory() const
499 EnsureBackslashPathSet();
501 CString sDirName = m_sBackslashPath.Left(m_sBackslashPath.ReverseFind('\\'));
502 if(sDirName.GetLength() == 2 && sDirName[1] == ':')
504 // This is a root directory, which needs a trailing slash
505 sDirName += '\\';
506 if(sDirName == m_sBackslashPath)
508 // We were clearly provided with a root path to start with - we should return nothing now
509 sDirName.Empty();
512 if(sDirName.GetLength() == 1 && sDirName[0] == '\\')
514 // We have an UNC path and we already are the root
515 sDirName.Empty();
517 CTGitPath retVal;
518 retVal.SetFromWin(sDirName);
519 return retVal;
522 CString CTGitPath::GetRootPathString() const
524 EnsureBackslashPathSet();
525 CString workingPath = m_sBackslashPath;
526 LPTSTR pPath = workingPath.GetBuffer(MAX_PATH); // MAX_PATH ok here.
527 ATLVERIFY(::PathStripToRoot(pPath));
528 workingPath.ReleaseBuffer();
529 return workingPath;
533 CString CTGitPath::GetFilename() const
535 //ATLASSERT(!IsDirectory());
536 return GetFileOrDirectoryName();
539 CString CTGitPath::GetFileOrDirectoryName() const
541 EnsureBackslashPathSet();
542 return m_sBackslashPath.Mid(m_sBackslashPath.ReverseFind('\\')+1);
545 CString CTGitPath::GetUIFileOrDirectoryName() const
547 GetUIPathString();
548 return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);
551 CString CTGitPath::GetFileExtension() const
553 if(!IsDirectory())
555 EnsureBackslashPathSet();
556 int dotPos = m_sBackslashPath.ReverseFind('.');
557 int slashPos = m_sBackslashPath.ReverseFind('\\');
558 if (dotPos > slashPos)
559 return m_sBackslashPath.Mid(dotPos);
561 return CString();
563 CString CTGitPath::GetBaseFilename() const
565 int dot;
566 CString filename=GetFilename();
567 dot = filename.ReverseFind(_T('.'));
568 if(dot>0)
569 return filename.Left(dot);
570 else
571 return filename;
574 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)
576 int length = sP1.GetLength();
577 if(length != sP2.GetLength())
579 // Different lengths
580 return false;
582 // We work from the end of the strings, because path differences
583 // are more likely to occur at the far end of a string
584 LPCTSTR pP1Start = sP1;
585 LPCTSTR pP1 = pP1Start+(length-1);
586 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
587 while(length-- > 0)
589 if(_totlower(*pP1--) != _totlower(*pP2--))
591 return false;
594 return true;
597 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)
599 int length = sP1.GetLength();
600 if(length != sP2.GetLength())
602 // Different lengths
603 return false;
605 // We work from the end of the strings, because path differences
606 // are more likely to occur at the far end of a string
607 LPCTSTR pP1Start = sP1;
608 LPCTSTR pP1 = pP1Start+(length-1);
609 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
610 while(length-- > 0)
612 if((*pP1--) != (*pP2--))
614 return false;
617 return true;
620 bool CTGitPath::IsEmpty() const
622 // Check the backward slash path first, since the chance that this
623 // one is set is higher. In case of a 'false' return value it's a little
624 // bit faster.
625 return m_sBackslashPath.IsEmpty() && m_sFwdslashPath.IsEmpty();
628 // Test if both paths refer to the same item
629 // Ignores case and slash direction
630 bool CTGitPath::IsEquivalentTo(const CTGitPath& rhs) const
632 // Try and find a slash direction which avoids having to convert
633 // both filenames
634 if(!m_sBackslashPath.IsEmpty())
636 // *We've* got a \ path - make sure that the RHS also has a \ path
637 rhs.EnsureBackslashPathSet();
638 return ArePathStringsEqualWithCase(m_sBackslashPath, rhs.m_sBackslashPath);
640 else
642 // Assume we've got a fwdslash path and make sure that the RHS has one
643 rhs.EnsureFwdslashPathSet();
644 return ArePathStringsEqualWithCase(m_sFwdslashPath, rhs.m_sFwdslashPath);
648 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath& rhs) const
650 // Try and find a slash direction which avoids having to convert
651 // both filenames
652 if(!m_sBackslashPath.IsEmpty())
654 // *We've* got a \ path - make sure that the RHS also has a \ path
655 rhs.EnsureBackslashPathSet();
656 return ArePathStringsEqual(m_sBackslashPath, rhs.m_sBackslashPath);
658 else
660 // Assume we've got a fwdslash path and make sure that the RHS has one
661 rhs.EnsureFwdslashPathSet();
662 return ArePathStringsEqual(m_sFwdslashPath, rhs.m_sFwdslashPath);
666 bool CTGitPath::IsAncestorOf(const CTGitPath& possibleDescendant) const
668 possibleDescendant.EnsureBackslashPathSet();
669 EnsureBackslashPathSet();
671 bool bPathStringsEqual = ArePathStringsEqual(m_sBackslashPath, possibleDescendant.m_sBackslashPath.Left(m_sBackslashPath.GetLength()));
672 if (m_sBackslashPath.GetLength() >= possibleDescendant.GetWinPathString().GetLength())
674 return bPathStringsEqual;
677 return (bPathStringsEqual &&
678 ((possibleDescendant.m_sBackslashPath[m_sBackslashPath.GetLength()] == '\\')||
679 (m_sBackslashPath.GetLength()==3 && m_sBackslashPath[1]==':')));
682 // Get a string representing the file path, optionally with a base
683 // section stripped off the front.
684 CString CTGitPath::GetDisplayString(const CTGitPath* pOptionalBasePath /* = NULL*/) const
686 EnsureFwdslashPathSet();
687 if(pOptionalBasePath != NULL)
689 // Find the length of the base-path without having to do an 'ensure' on it
690 int baseLength = max(pOptionalBasePath->m_sBackslashPath.GetLength(), pOptionalBasePath->m_sFwdslashPath.GetLength());
692 // Now, chop that baseLength of the front of the path
693 return m_sFwdslashPath.Mid(baseLength).TrimLeft('/');
695 return m_sFwdslashPath;
698 int CTGitPath::Compare(const CTGitPath& left, const CTGitPath& right)
700 left.EnsureBackslashPathSet();
701 right.EnsureBackslashPathSet();
702 return left.m_sBackslashPath.CompareNoCase(right.m_sBackslashPath);
705 bool operator<(const CTGitPath& left, const CTGitPath& right)
707 return CTGitPath::Compare(left, right) < 0;
710 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right)
712 return left.IsEquivalentTo(right);
715 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right)
717 if (left.IsAdminDir() && right.IsAdminDir())
719 CTGitPath l = left;
720 CTGitPath r = right;
723 l = l.GetContainingDirectory();
724 } while(l.HasAdminDir());
727 r = r.GetContainingDirectory();
728 } while(r.HasAdminDir());
729 return l.GetContainingDirectory().IsEquivalentTo(r.GetContainingDirectory());
731 return left.GetDirectory().IsEquivalentTo(right.GetDirectory());
734 bool CTGitPath::CheckChild(const CTGitPath &parent, const CTGitPath& child)
736 return parent.IsAncestorOf(child);
739 void CTGitPath::AppendRawString(const CString& sAppend)
741 EnsureFwdslashPathSet();
742 CString strCopy = m_sFwdslashPath += sAppend;
743 SetFromUnknown(strCopy);
746 void CTGitPath::AppendPathString(const CString& sAppend)
748 EnsureBackslashPathSet();
749 CString cleanAppend(sAppend);
750 cleanAppend.Replace('/', '\\');
751 cleanAppend.TrimLeft('\\');
752 m_sBackslashPath.TrimRight('\\');
753 CString strCopy = m_sBackslashPath + _T("\\") + cleanAppend;
754 SetFromWin(strCopy);
757 bool CTGitPath::HasAdminDir() const
759 if (m_bHasAdminDirKnown)
760 return m_bHasAdminDir;
762 EnsureBackslashPathSet();
763 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
764 m_bHasAdminDirKnown = true;
765 return m_bHasAdminDir;
768 bool CTGitPath::HasSubmodules() const
770 return !g_GitAdminDir.GetSuperProjectRoot(GetWinPathString()).IsEmpty();
773 int CTGitPath::GetAdminDirMask() const
775 int status = 0;
776 CString topdir,path;
777 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
779 return status;
782 status |= ITEMIS_INSVN|ITEMIS_FOLDERINSVN|ITEMIS_INVERSIONEDFOLDER;
784 path=topdir;
785 path+=_T("\\");
786 path+=g_GitAdminDir.GetAdminDirName();
787 path+=_T("\\refs\\stash");
788 if( PathFileExists(path) )
789 status |= ITEMIS_STASH;
791 path=topdir;
792 path+=_T("\\");
793 path+=g_GitAdminDir.GetAdminDirName();
794 path+=_T("\\svn");
795 if( PathFileExists(path) )
796 status |= ITEMIS_GITSVN;
798 path=topdir;
799 path+=_T("\\.gitmodules");
800 if( PathFileExists(path) )
801 status |= ITEMIS_SUBMODULE;
803 return status;
806 bool CTGitPath::HasStashDir() const
808 CString topdir;
809 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
811 return false;
813 topdir+=_T("\\");
814 topdir+=g_GitAdminDir.GetAdminDirName();
815 topdir+=_T("\\refs\\stash");
816 return !!PathFileExists(topdir);
818 bool CTGitPath::HasGitSVNDir() const
820 CString topdir;
821 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
823 return false;
825 topdir+=_T("\\");
826 topdir+=g_GitAdminDir.GetAdminDirName();
827 topdir+=_T("\\svn");
828 return !!PathFileExists(topdir);
831 bool CTGitPath::HasRebaseApply() const
833 CString topdir;
834 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
836 return false;
838 topdir+=_T("\\");
839 topdir+=g_GitAdminDir.GetAdminDirName();
840 topdir+=_T("\\rebase-apply");
841 return !!PathFileExists(topdir);
844 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const
846 if (m_bHasAdminDirKnown)
848 if (ProjectTopDir)
849 *ProjectTopDir = m_sProjectRoot;
850 return m_bHasAdminDir;
853 EnsureBackslashPathSet();
854 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
855 m_bHasAdminDirKnown = true;
856 if (ProjectTopDir)
857 *ProjectTopDir = m_sProjectRoot;
858 return m_bHasAdminDir;
861 bool CTGitPath::IsAdminDir() const
863 if (m_bIsAdminDirKnown)
864 return m_bIsAdminDir;
866 EnsureBackslashPathSet();
867 m_bIsAdminDir = g_GitAdminDir.IsAdminDirPath(m_sBackslashPath);
868 m_bIsAdminDirKnown = true;
869 return m_bIsAdminDir;
872 bool CTGitPath::IsValidOnWindows() const
874 if (m_bIsValidOnWindowsKnown)
875 return m_bIsValidOnWindows;
877 m_bIsValidOnWindows = false;
878 EnsureBackslashPathSet();
879 CString sMatch = m_sBackslashPath + _T("\r\n");
880 wstring sPattern;
881 // the 'file://' URL is just a normal windows path:
882 if (sMatch.Left(7).CompareNoCase(_T("file:\\\\"))==0)
884 sMatch = sMatch.Mid(7);
885 sMatch.TrimLeft(_T("\\"));
886 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
888 else if (IsUrl())
890 sPattern = _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
892 else
894 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
899 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
900 tr1::wsmatch match;
902 wstring rmatch = wstring((LPCTSTR)sMatch);
903 if (tr1::regex_match(rmatch, match, rx))
905 if (wstring(match[0]).compare(sMatch)==0)
906 m_bIsValidOnWindows = true;
908 if (m_bIsValidOnWindows)
910 // now check for illegal filenames
911 tr1::wregex rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
912 rmatch = m_sBackslashPath;
913 if (tr1::regex_search(rmatch, rx2, tr1::regex_constants::match_default))
914 m_bIsValidOnWindows = false;
917 catch (exception) {}
919 m_bIsValidOnWindowsKnown = true;
920 return m_bIsValidOnWindows;
923 bool CTGitPath::IsSpecialDirectory() const
925 if (m_bIsSpecialDirectoryKnown)
926 return m_bIsSpecialDirectory;
928 static LPCTSTR specialDirectories[]
929 = { _T("trunk"), _T("tags"), _T("branches") };
931 for (int i=0 ; i<(sizeof(specialDirectories) / sizeof(specialDirectories[0])) ; ++i)
933 CString name = GetFileOrDirectoryName();
934 if (0 == name.CompareNoCase(specialDirectories[i]))
936 m_bIsSpecialDirectory = true;
937 break;
941 m_bIsSpecialDirectoryKnown = true;
943 return m_bIsSpecialDirectory;
946 //////////////////////////////////////////////////////////////////////////
948 CTGitPathList::CTGitPathList()
953 // A constructor which allows a path list to be easily built which one initial entry in
954 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)
956 AddPath(firstEntry);
958 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool /*staged*/)
960 unsigned int pos=0;
961 CString one;
962 CTGitPath path;
963 CString part;
964 this->Clear();
966 while(pos>=0 && pos<out.size())
968 one.Empty();
969 path.Reset();
971 g_Git.StringAppend(&one,&out[pos],CP_ACP);
972 int tabstart=0;
973 path.m_Action=path.ParserAction(out[pos]);
974 one.Tokenize(_T("\t"),tabstart);
976 if(tabstart>=0)
977 path.SetFromGit(one.Right(one.GetLength()-tabstart));
979 tabstart=0;
981 part=one.Tokenize(_T(" "),tabstart); //Tag
983 part=one.Tokenize(_T(" "),tabstart); //Mode
985 part=one.Tokenize(_T(" "),tabstart); //Hash
987 part=one.Tokenize(_T("\t"),tabstart); //Stage
989 path.m_Stage=_ttol(part);
991 this->AddPath(path);
993 pos=out.findNextString(pos);
995 return pos;
997 int CTGitPathList::FillUnRev(int action,CTGitPathList *list)
999 int pos=0;
1000 this->Clear();
1001 CTGitPath path;
1003 int count;
1004 if(list==NULL)
1005 count=1;
1006 else
1007 count=list->GetCount();
1008 for(int i=0;i<count;i++)
1010 CString cmd;
1011 pos=0;
1013 CString ignored;
1014 if(action & CTGitPath::LOGACTIONS_IGNORE)
1015 ignored= _T(" --ignored");
1017 if(list==NULL)
1019 cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1020 cmd+=ignored;
1023 else
1024 { cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z %s-- \"%s\""),
1025 ignored,
1026 (*list)[i].GetWinPathString());
1029 BYTE_VECTOR out;
1030 out.clear();
1031 g_Git.Run(cmd,&out);
1033 pos=0;
1034 CString one;
1035 while( pos>=0 && pos<out.size())
1037 one.Empty();
1038 g_Git.StringAppend(&one,&out[pos],CP_ACP);
1039 if(!one.IsEmpty())
1041 //SetFromGit will clear all status
1042 path.SetFromGit(one);
1043 path.m_Action=action;
1044 AddPath(path);
1046 pos=out.findNextString(pos);
1050 return 0;
1052 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log)
1054 this->Clear();
1055 int pos=0;
1056 //BYTE *p=&log[0];
1057 //CString one;
1058 CTGitPath path;
1059 m_Action=0;
1060 while( pos>=0 && pos<log.size())
1062 //one=log.Tokenize(_T("\n"),pos);
1063 path.Reset();
1064 if(log[pos]=='\n')
1065 pos++;
1067 if(log[pos]==':')
1069 bool merged=false;
1070 if(log[pos+1] ==':')
1072 merged=true;
1074 int end=log.find(0,pos);
1075 int actionstart=-1;
1076 int numfile=1;
1077 int file1=-1,file2=-1;
1078 if( end>0 )
1080 actionstart=log.find(' ',end-6);
1081 pos=actionstart;
1083 if( actionstart>0 )
1085 actionstart++;
1087 file1 = log.find(0,actionstart);
1088 if( file1>=0 )
1090 file1++;
1091 pos=file1;
1093 if( log[actionstart] == 'C' || log[actionstart] == 'R' )
1095 file2=file1;
1096 numfile=2;
1097 file1 = log.find(0,file1);
1098 if(file1>=0 )
1100 file1++;
1101 pos=file1;
1107 CString pathname1;
1108 CString pathname2;
1110 if( file1>=0 )
1111 g_Git.StringAppend(&pathname1,&log[file1],CP_ACP);
1112 if( file2>=0 )
1113 g_Git.StringAppend(&pathname2,&log[file2],CP_ACP);
1115 CTGitPath *GitPath=LookForGitPath(pathname1);
1117 if(GitPath)
1119 GitPath->ParserAction( log[actionstart] );
1121 if(merged)
1123 GitPath->m_Action |= CTGitPath::LOGACTIONS_MERGED;
1124 GitPath->m_Action &= ~CTGitPath::LOGACTIONS_FORWORD;
1126 m_Action |=GitPath->m_Action;
1128 }else
1130 int ac=path.ParserAction(log[actionstart] );
1131 ac |= merged?CTGitPath::LOGACTIONS_MERGED:0;
1133 path.SetFromGit(pathname1,&pathname2);
1134 path.m_Action=ac;
1135 //action must be set after setfromgit. SetFromGit will clear all status.
1136 this->m_Action|=ac;
1138 AddPath(path);
1142 }else
1144 int tabstart=0;
1145 path.Reset();
1146 CString StatAdd;
1147 CString StatDel;
1148 CString file1;
1149 CString file2;
1151 tabstart=log.find('\t',pos);
1152 if(tabstart >=0)
1154 log[tabstart]=0;
1155 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);
1156 pos=tabstart+1;
1159 tabstart=log.find('\t',pos);
1160 if(tabstart >=0)
1162 log[tabstart]=0;
1164 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);
1165 pos=tabstart+1;
1168 if(log[pos] == 0) //rename
1170 pos++;
1171 g_Git.StringAppend(&file2,&log[pos],CP_ACP);
1172 int sec=log.find(0,pos);
1173 if(sec>=0)
1175 sec++;
1176 g_Git.StringAppend(&file1,&log[sec],CP_ACP);
1178 pos=sec;
1180 }else
1182 g_Git.StringAppend(&file1,&log[pos],CP_ACP);
1184 path.SetFromGit(file1,&file2);
1186 CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());
1187 if(GitPath)
1189 GitPath->m_StatAdd=StatAdd;
1190 GitPath->m_StatDel=StatDel;
1191 }else
1193 //path.SetFromGit(pathname);
1194 path.m_StatAdd=StatAdd;
1195 path.m_StatDel=StatDel;
1196 path.m_Action |= CTGitPath::LOGACTIONS_FORWORD;
1197 AddPath(path);
1201 pos=log.findNextString(pos);
1203 return pos;
1206 void CTGitPathList::AddPath(const CTGitPath& newPath)
1208 m_paths.push_back(newPath);
1209 m_commonBaseDirectory.Reset();
1211 int CTGitPathList::GetCount() const
1213 return (int)m_paths.size();
1215 void CTGitPathList::Clear()
1217 m_paths.clear();
1218 m_commonBaseDirectory.Reset();
1221 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const
1223 ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());
1224 return m_paths[index];
1227 bool CTGitPathList::AreAllPathsFiles() const
1229 // Look through the vector for any directories - if we find them, return false
1230 return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();
1234 #if defined(_MFC_VER)
1236 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)
1238 Clear();
1241 CString strLine;
1242 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
1244 // for every selected file/folder
1245 CTGitPath path;
1246 while (file.ReadString(strLine))
1248 path.SetFromUnknown(strLine);
1249 AddPath(path);
1251 file.Close();
1253 catch (CFileException* pE)
1255 TRACE("CFileException loading target file list\n");
1256 TCHAR error[10000] = {0};
1257 pE->GetErrorMessage(error, 10000);
1258 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1259 pE->Delete();
1260 return false;
1262 return true;
1265 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const
1269 if (bANSI)
1271 CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
1272 PathVector::const_iterator it;
1273 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1275 CStringA line = CStringA(it->GetGitPathString()) + '\n';
1276 file.Write(line, line.GetLength());
1278 file.Close();
1280 else
1282 CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);
1283 PathVector::const_iterator it;
1284 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1286 file.WriteString(it->GetGitPathString()+_T("\n"));
1288 file.Close();
1291 catch (CFileException* pE)
1293 TRACE("CFileException in writing temp file\n");
1294 pE->Delete();
1295 return false;
1297 return true;
1301 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)
1303 int pos = 0;
1304 CString temp;
1305 for(;;)
1307 temp = sPathString.Tokenize(_T("*"),pos);
1308 if(temp.IsEmpty())
1310 break;
1312 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));
1316 CString CTGitPathList::CreateAsteriskSeparatedString() const
1318 CString sRet;
1319 PathVector::const_iterator it;
1320 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1322 if (!sRet.IsEmpty())
1323 sRet += _T("*");
1324 sRet += it->GetWinPathString();
1326 return sRet;
1328 #endif // _MFC_VER
1330 bool
1331 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1333 // Check if all the paths are files and in the same directory
1334 PathVector::const_iterator it;
1335 m_commonBaseDirectory.Reset();
1336 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1338 if(it->IsDirectory())
1340 return false;
1342 const CTGitPath& baseDirectory = it->GetDirectory();
1343 if(m_commonBaseDirectory.IsEmpty())
1345 m_commonBaseDirectory = baseDirectory;
1347 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1349 // Different path
1350 m_commonBaseDirectory.Reset();
1351 return false;
1354 return true;
1357 CTGitPath CTGitPathList::GetCommonDirectory() const
1359 if (m_commonBaseDirectory.IsEmpty())
1361 PathVector::const_iterator it;
1362 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1364 const CTGitPath& baseDirectory = it->GetDirectory();
1365 if(m_commonBaseDirectory.IsEmpty())
1367 m_commonBaseDirectory = baseDirectory;
1369 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1371 // Different path
1372 m_commonBaseDirectory.Reset();
1373 break;
1377 // since we only checked strings, not paths,
1378 // we have to make sure now that we really return a *path* here
1379 PathVector::const_iterator iter;
1380 for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)
1382 if (!m_commonBaseDirectory.IsAncestorOf(*iter))
1384 m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();
1385 break;
1388 return m_commonBaseDirectory;
1391 CTGitPath CTGitPathList::GetCommonRoot() const
1393 PathVector::const_iterator it;
1394 CString sRoot, sTempRoot;
1395 bool bEqual = true;
1397 if (GetCount() == 1)
1398 return m_paths[0];
1400 int backSlashPos = 0;
1401 int searchStartPos = 0;
1402 while (bEqual)
1404 if(m_paths.empty())
1405 break;
1407 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1409 if (backSlashPos == 0)
1411 backSlashPos = it->GetWinPathString().Find('\\', searchStartPos+1);
1412 if ((backSlashPos < 0)&&(searchStartPos != it->GetWinPathString().GetLength()))
1413 backSlashPos = it->GetWinPathString().GetLength();
1414 sTempRoot = it->GetWinPathString().Left(backSlashPos+1);
1416 else if (it->GetWinPathString().Find('\\', searchStartPos+1) != backSlashPos || it->GetWinPathString().Left(backSlashPos+1) != sTempRoot.Left(backSlashPos+1))
1418 if (it->GetWinPathString().Find('\\', searchStartPos+1) < 0)
1420 if (it->GetWinPathString().GetLength() != backSlashPos || it->GetWinPathString().Left(backSlashPos+1) != sTempRoot.Left(backSlashPos+1))
1422 bEqual = false;
1423 break;
1426 else
1428 bEqual = false;
1429 break;
1432 if (backSlashPos < 0)
1434 bEqual = false;
1435 break;
1438 if (bEqual == false)
1440 if (searchStartPos)
1441 sRoot = m_paths[0].GetWinPathString().Left(searchStartPos+1);
1443 else
1445 searchStartPos = backSlashPos;
1447 backSlashPos = 0;
1450 return CTGitPath(sRoot.TrimRight('\\'));
1453 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)
1455 std::sort(m_paths.begin(), m_paths.end());
1456 if (bReverse)
1457 std::reverse(m_paths.begin(), m_paths.end());
1460 void CTGitPathList::DeleteAllFiles(bool bTrash)
1462 PathVector::const_iterator it;
1463 if (bTrash)
1465 SortByPathname();
1466 CString sPaths;
1467 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1469 if ((it->Exists())&&(!it->IsDirectory()))
1471 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1472 sPaths += it->GetWinPath();
1473 sPaths += '\0';
1476 sPaths += '\0';
1477 sPaths += '\0';
1478 SHFILEOPSTRUCT shop = {0};
1479 shop.wFunc = FO_DELETE;
1480 shop.pFrom = (LPCTSTR)sPaths;
1481 shop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
1482 SHFileOperation(&shop);
1484 else
1486 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1488 if (!it->IsDirectory())
1490 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1491 ::DeleteFile(it->GetWinPath());
1495 Clear();
1498 void CTGitPathList::RemoveDuplicates()
1500 SortByPathname();
1501 // Remove the duplicates
1502 // (Unique moves them to the end of the vector, then erase chops them off)
1503 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());
1506 void CTGitPathList::RemoveAdminPaths()
1508 PathVector::iterator it;
1509 for(it = m_paths.begin(); it != m_paths.end(); )
1511 if (it->IsAdminDir())
1513 m_paths.erase(it);
1514 it = m_paths.begin();
1516 else
1517 ++it;
1521 void CTGitPathList::RemovePath(const CTGitPath& path)
1523 PathVector::iterator it;
1524 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1526 if (it->IsEquivalentTo(path))
1528 m_paths.erase(it);
1529 return;
1534 void CTGitPathList::RemoveItem(CTGitPath & path)
1536 PathVector::iterator it;
1537 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1539 if (it->GetGitPathString()==path.GetGitPathString())
1541 m_paths.erase(it);
1542 return;
1546 void CTGitPathList::RemoveChildren()
1548 SortByPathname();
1549 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());
1552 bool CTGitPathList::IsEqual(const CTGitPathList& list)
1554 if (list.GetCount() != GetCount())
1555 return false;
1556 for (int i=0; i<list.GetCount(); ++i)
1558 if (!list[i].IsEquivalentTo(m_paths[i]))
1559 return false;
1561 return true;
1564 //////////////////////////////////////////////////////////////////////////
1565 #if 0
1566 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const
1568 apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));
1570 for(int nItem = 0; nItem < GetCount(); nItem++)
1572 const char * target = m_paths[nItem].GetGitApiPath(pool);
1573 (*((const char **) apr_array_push (targets))) = target;
1576 return targets;
1578 #endif
1579 //////////////////////////////////////////////////////////////////////////
1581 #if 0
1582 #if defined(_DEBUG)
1583 // Some test cases for these classes
1584 static class CTGitPathTests
1586 public:
1587 CTGitPathTests()
1589 apr_initialize();
1590 pool = svn_pool_create(NULL);
1591 GetDirectoryTest();
1592 AdminDirTest();
1593 SortTest();
1594 RawAppendTest();
1595 PathAppendTest();
1596 RemoveDuplicatesTest();
1597 RemoveChildrenTest();
1598 ContainingDirectoryTest();
1599 AncestorTest();
1600 SubversionPathTest();
1601 GetCommonRootTest();
1602 #if defined(_MFC_VER)
1603 ValidPathAndUrlTest();
1604 ListLoadingTest();
1605 #endif
1606 apr_terminate();
1609 private:
1610 // apr_pool_t * pool;
1611 void GetDirectoryTest()
1613 // Bit tricky, this test, because we need to know something about the file
1614 // layout on the machine which is running the test
1615 TCHAR winDir[MAX_PATH+1];
1616 GetWindowsDirectory(winDir, MAX_PATH);
1617 CString sWinDir(winDir);
1619 CTGitPath testPath;
1620 // This is a file which we know will always be there
1621 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));
1622 ATLASSERT(!testPath.IsDirectory());
1623 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1624 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);
1626 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1627 // but we know it must be different to the directory itself
1628 testPath.SetFromUnknown(sWinDir);
1629 ATLASSERT(testPath.IsDirectory());
1630 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1631 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);
1632 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());
1634 // Try a root path
1635 testPath.SetFromUnknown(_T("C:\\"));
1636 ATLASSERT(testPath.IsDirectory());
1637 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1638 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1639 // Try a root UNC path
1640 testPath.SetFromUnknown(_T("\\MYSTATION"));
1641 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1644 void AdminDirTest()
1646 CTGitPath testPath;
1647 testPath.SetFromUnknown(_T("c:\\.svndir"));
1648 ATLASSERT(!testPath.IsAdminDir());
1649 testPath.SetFromUnknown(_T("c:\\test.svn"));
1650 ATLASSERT(!testPath.IsAdminDir());
1651 testPath.SetFromUnknown(_T("c:\\.svn"));
1652 ATLASSERT(testPath.IsAdminDir());
1653 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));
1654 ATLASSERT(!testPath.IsAdminDir());
1655 testPath.SetFromUnknown(_T("c:\\.svn\\test"));
1656 ATLASSERT(testPath.IsAdminDir());
1658 CTGitPathList pathList;
1659 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));
1660 pathList.AddPath(CTGitPath(_T("c:\\.svn")));
1661 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1662 pathList.AddPath(CTGitPath(_T("c:\\test")));
1663 pathList.RemoveAdminPaths();
1664 ATLASSERT(pathList.GetCount()==2);
1665 pathList.Clear();
1666 pathList.AddPath(CTGitPath(_T("c:\\test")));
1667 pathList.RemoveAdminPaths();
1668 ATLASSERT(pathList.GetCount()==1);
1671 void SortTest()
1673 CTGitPathList testList;
1674 CTGitPath testPath;
1675 testPath.SetFromUnknown(_T("c:/Z"));
1676 testList.AddPath(testPath);
1677 testPath.SetFromUnknown(_T("c:/B"));
1678 testList.AddPath(testPath);
1679 testPath.SetFromUnknown(_T("c:\\a"));
1680 testList.AddPath(testPath);
1681 testPath.SetFromUnknown(_T("c:/Test"));
1682 testList.AddPath(testPath);
1684 testList.SortByPathname();
1686 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));
1687 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));
1688 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));
1689 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));
1692 void RawAppendTest()
1694 CTGitPath testPath(_T("c:/test/"));
1695 testPath.AppendRawString(_T("/Hello"));
1696 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1698 testPath.AppendRawString(_T("\\T2"));
1699 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1701 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1702 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1703 testBasePath.AppendRawString(testFilePath.GetFileExtension());
1704 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1707 void PathAppendTest()
1709 CTGitPath testPath(_T("c:/test/"));
1710 testPath.AppendPathString(_T("/Hello"));
1711 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1713 testPath.AppendPathString(_T("T2"));
1714 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1716 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1717 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1718 // You wouldn't want to do this in real life - you'd use append-raw
1719 testBasePath.AppendPathString(testFilePath.GetFileExtension());
1720 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1723 void RemoveDuplicatesTest()
1725 CTGitPathList list;
1726 list.AddPath(CTGitPath(_T("Z")));
1727 list.AddPath(CTGitPath(_T("A")));
1728 list.AddPath(CTGitPath(_T("E")));
1729 list.AddPath(CTGitPath(_T("E")));
1731 ATLASSERT(list[2].IsEquivalentTo(list[3]));
1732 ATLASSERT(list[2]==list[3]);
1734 ATLASSERT(list.GetCount() == 4);
1736 list.RemoveDuplicates();
1738 ATLASSERT(list.GetCount() == 3);
1740 ATLASSERT(list[0].GetWinPathString() == _T("A"));
1741 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);
1742 ATLASSERT(list[2].GetWinPathString() == _T("Z"));
1745 void RemoveChildrenTest()
1747 CTGitPathList list;
1748 list.AddPath(CTGitPath(_T("c:\\test")));
1749 list.AddPath(CTGitPath(_T("c:\\test\\file")));
1750 list.AddPath(CTGitPath(_T("c:\\testfile")));
1751 list.AddPath(CTGitPath(_T("c:\\parent")));
1752 list.AddPath(CTGitPath(_T("c:\\parent\\child")));
1753 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1754 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1756 ATLASSERT(list.GetCount() == 7);
1758 list.RemoveChildren();
1760 ATLTRACE("count = %d\n", list.GetCount());
1761 ATLASSERT(list.GetCount() == 3);
1763 list.SortByPathname();
1765 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1766 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1767 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1770 #if defined(_MFC_VER)
1771 void ListLoadingTest()
1773 TCHAR buf[MAX_PATH];
1774 GetCurrentDirectory(MAX_PATH, buf);
1775 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1776 CTGitPathList testList;
1777 testList.LoadFromAsteriskSeparatedString(sPathList);
1779 ATLASSERT(testList.GetCount() == 3);
1780 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));
1781 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1782 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));
1784 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));
1785 testList.Clear();
1786 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1787 testList.LoadFromAsteriskSeparatedString(sPathList);
1788 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1790 #endif
1792 void ContainingDirectoryTest()
1795 CTGitPath testPath;
1796 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1797 CTGitPath dir;
1798 dir = testPath.GetContainingDirectory();
1799 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1800 dir = dir.GetContainingDirectory();
1801 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));
1802 dir = dir.GetContainingDirectory();
1803 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));
1804 dir = dir.GetContainingDirectory();
1805 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));
1806 dir = dir.GetContainingDirectory();
1807 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));
1808 dir = dir.GetContainingDirectory();
1809 ATLASSERT(dir.IsEmpty());
1810 ATLASSERT(dir.GetWinPathString() == _T(""));
1813 void AncestorTest()
1815 CTGitPath testPath;
1816 testPath.SetFromWin(_T("c:\\windows"));
1817 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1818 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1819 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1820 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1821 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1824 void SubversionPathTest()
1826 CTGitPath testPath;
1827 testPath.SetFromWin(_T("c:\\"));
1828 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);
1829 testPath.SetFromWin(_T("c:\\folder"));
1830 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);
1831 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1832 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);
1833 testPath.SetFromUnknown(_T("http://testing/"));
1834 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);
1835 testPath.SetFromGit(NULL);
1836 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);
1837 #if defined(_MFC_VER)
1838 testPath.SetFromUnknown(_T("http://testing again"));
1839 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1840 testPath.SetFromUnknown(_T("http://testing%20again"));
1841 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1842 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1843 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1844 #endif
1847 void GetCommonRootTest()
1849 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));
1850 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));
1851 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1853 CTGitPathList list;
1854 list.AddPath(pathA);
1855 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1856 list.AddPath(pathB);
1857 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1858 list.AddPath(pathC);
1859 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1860 #ifdef _MFC_VER
1861 list.Clear();
1862 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");
1863 list.LoadFromAsteriskSeparatedString(sPathList);
1864 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1866 list.Clear();
1867 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");
1868 list.LoadFromAsteriskSeparatedString(sPathList);
1869 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1871 list.Clear();
1872 sPathList = _T("c:\\windows\\*c:\\windows");
1873 list.LoadFromAsteriskSeparatedString(sPathList);
1874 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1876 list.Clear();
1877 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");
1878 list.LoadFromAsteriskSeparatedString(sPathList);
1879 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1881 list.Clear();
1882 sPathList = _T("c:\\windowsdummy*c:\\windows");
1883 list.LoadFromAsteriskSeparatedString(sPathList);
1884 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1885 #endif
1888 void ValidPathAndUrlTest()
1890 CTGitPath testPath;
1891 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1892 ATLASSERT(testPath.IsValidOnWindows());
1893 testPath.SetFromWin(_T("c:\\"));
1894 ATLASSERT(testPath.IsValidOnWindows());
1895 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1896 ATLASSERT(testPath.IsValidOnWindows());
1897 testPath.SetFromWin(_T("c"));
1898 ATLASSERT(testPath.IsValidOnWindows());
1899 testPath.SetFromWin(_T("c:\\test folder\\file"));
1900 ATLASSERT(testPath.IsValidOnWindows());
1901 testPath.SetFromWin(_T("c:\\folder\\"));
1902 ATLASSERT(testPath.IsValidOnWindows());
1903 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1904 ATLASSERT(testPath.IsValidOnWindows());
1905 testPath.SetFromWin(_T("c:\\.svn"));
1906 ATLASSERT(testPath.IsValidOnWindows());
1907 testPath.SetFromWin(_T("c:\\com\\file"));
1908 ATLASSERT(testPath.IsValidOnWindows());
1909 testPath.SetFromWin(_T("c:\\test\\conf"));
1910 ATLASSERT(testPath.IsValidOnWindows());
1911 testPath.SetFromWin(_T("c:\\LPT"));
1912 ATLASSERT(testPath.IsValidOnWindows());
1913 testPath.SetFromWin(_T("c:\\test\\LPT"));
1914 ATLASSERT(testPath.IsValidOnWindows());
1915 testPath.SetFromWin(_T("c:\\com1test"));
1916 ATLASSERT(testPath.IsValidOnWindows());
1917 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1918 ATLASSERT(testPath.IsValidOnWindows());
1920 testPath.SetFromWin(_T("\\\\Share\\filename"));
1921 ATLASSERT(testPath.IsValidOnWindows());
1922 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));
1923 ATLASSERT(testPath.IsValidOnWindows());
1924 testPath.SetFromWin(_T("\\\\Share\\.svn"));
1925 ATLASSERT(testPath.IsValidOnWindows());
1927 // now the negative tests
1928 testPath.SetFromWin(_T("c:\\test:folder"));
1929 ATLASSERT(!testPath.IsValidOnWindows());
1930 testPath.SetFromWin(_T("c:\\file<name"));
1931 ATLASSERT(!testPath.IsValidOnWindows());
1932 testPath.SetFromWin(_T("c:\\something*else"));
1933 ATLASSERT(!testPath.IsValidOnWindows());
1934 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));
1935 ATLASSERT(!testPath.IsValidOnWindows());
1936 testPath.SetFromWin(_T("c:\\ext.>ension"));
1937 ATLASSERT(!testPath.IsValidOnWindows());
1938 testPath.SetFromWin(_T("c:\\com1\\filename"));
1939 ATLASSERT(!testPath.IsValidOnWindows());
1940 testPath.SetFromWin(_T("c:\\com1"));
1941 ATLASSERT(!testPath.IsValidOnWindows());
1942 testPath.SetFromWin(_T("c:\\com1\\AuX"));
1943 ATLASSERT(!testPath.IsValidOnWindows());
1945 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1946 ATLASSERT(!testPath.IsValidOnWindows());
1947 testPath.SetFromWin(_T("\\\\Share\\prn"));
1948 ATLASSERT(!testPath.IsValidOnWindows());
1949 testPath.SetFromWin(_T("\\\\Share\\NUL"));
1950 ATLASSERT(!testPath.IsValidOnWindows());
1952 // now come some URL tests
1953 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));
1954 ATLASSERT(testPath.IsValidOnWindows());
1955 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1956 ATLASSERT(testPath.IsValidOnWindows());
1957 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1958 ATLASSERT(testPath.IsValidOnWindows());
1959 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1960 ATLASSERT(testPath.IsValidOnWindows());
1961 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));
1962 ATLASSERT(testPath.IsValidOnWindows());
1963 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1964 ATLASSERT(testPath.IsValidOnWindows());
1965 // and some negative URL tests
1966 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1967 ATLASSERT(!testPath.IsValidOnWindows());
1968 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1969 ATLASSERT(!testPath.IsValidOnWindows());
1970 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1971 ATLASSERT(!testPath.IsValidOnWindows());
1972 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
1973 ATLASSERT(!testPath.IsValidOnWindows());
1974 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
1975 ATLASSERT(!testPath.IsValidOnWindows());
1979 } TGitPathTestobject;
1980 #endif
1981 #endif
1983 CTGitPath * CTGitPathList::LookForGitPath(CString path)
1985 int i=0;
1986 for(i=0;i<this->GetCount();i++)
1988 if((*this)[i].GetGitPathString() == path )
1989 return (CTGitPath*)&(*this)[i];
1991 return NULL;
1993 CString CTGitPath::GetActionName(int action)
1995 if(action & CTGitPath::LOGACTIONS_UNMERGED)
1996 return _T("Conflict");
1997 if(action & CTGitPath::LOGACTIONS_ADDED)
1998 return _T("Added");
1999 if(action & CTGitPath::LOGACTIONS_DELETED)
2000 return _T("Deleted");
2001 if(action & CTGitPath::LOGACTIONS_MERGED )
2002 return _T("Merged");
2004 if(action & CTGitPath::LOGACTIONS_MODIFIED)
2005 return _T("Modified");
2006 if(action & CTGitPath::LOGACTIONS_REPLACED)
2007 return _T("Rename");
2008 if(action & CTGitPath::LOGACTIONS_COPY)
2009 return _T("Copy");
2011 if(action & CTGitPath::LOGACTIONS_FORWORD )
2012 return _T("Forward");
2014 if(action & CTGitPath::LOGACTIONS_REBASE_EDIT)
2015 return _T("Edit");
2016 if(action & CTGitPath::LOGACTIONS_REBASE_SQUASH)
2017 return _T("Squash");
2018 if(action & CTGitPath::LOGACTIONS_REBASE_PICK)
2019 return _T("Pick");
2020 if(action & CTGitPath::LOGACTIONS_REBASE_SKIP)
2021 return _T("Skip");
2023 return _T("Unknown");
2025 CString CTGitPath::GetActionName()
2027 return GetActionName(m_Action);
2030 int CTGitPathList::GetAction()
2032 return m_Action;