Fixed issue #507: Help Spell error and push wrongly link to sync
[TortoiseGit.git] / src / Git / TGitPath.cpp
blob0f59c32f0528b19741e3f35f70070c095185c7f1
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;
61 CTGitPath::~CTGitPath(void)
64 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
65 CTGitPath::CTGitPath(const CString& sUnknownPath) :
66 m_bDirectoryKnown(false),
67 m_bIsDirectory(false),
68 m_bIsURL(false),
69 m_bURLKnown(false),
70 m_bHasAdminDirKnown(false),
71 m_bHasAdminDir(false),
72 m_bIsValidOnWindowsKnown(false),
73 m_bIsReadOnly(false),
74 m_bIsAdminDirKnown(false),
75 m_bIsAdminDir(false),
76 m_bExists(false),
77 m_bExistsKnown(false),
78 m_bLastWriteTimeKnown(0),
79 m_lastWriteTime(0),
80 m_customData(NULL),
81 m_bIsSpecialDirectoryKnown(false),
82 m_bIsSpecialDirectory(false)
84 SetFromUnknown(sUnknownPath);
85 m_Action=0;
86 m_Stage=0;
89 int CTGitPath::ParserAction(BYTE action)
91 //action=action.TrimLeft();
92 //TCHAR c=action.GetAt(0);
93 if(action == 'M')
94 m_Action|= LOGACTIONS_MODIFIED;
95 if(action == 'R')
96 m_Action|= LOGACTIONS_REPLACED;
97 if(action == 'A')
98 m_Action|= LOGACTIONS_ADDED;
99 if(action == 'D')
100 m_Action|= LOGACTIONS_DELETED;
101 if(action == 'U')
102 m_Action|= LOGACTIONS_UNMERGED;
103 if(action == 'K')
104 m_Action|= LOGACTIONS_DELETED;
105 if(action == 'H')
106 m_Action|= LOGACTIONS_CACHE;
107 if(action == 'C' )
108 m_Action|= LOGACTIONS_COPY;
110 return m_Action;
112 void CTGitPath::SetFromGit(const char* pPath)
114 Reset();
115 if (pPath == NULL)
116 return;
117 int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);
118 if (len)
120 len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, m_sFwdslashPath.GetBuffer(len+1), len+1);
121 m_sFwdslashPath.ReleaseBuffer(len-1);
123 SanitizeRootPath(m_sFwdslashPath, true);
126 void CTGitPath::SetFromGit(const char* pPath, bool bIsDirectory)
128 SetFromGit(pPath);
129 m_bDirectoryKnown = true;
130 m_bIsDirectory = bIsDirectory;
133 void CTGitPath::SetFromGit(const TCHAR* pPath, bool bIsDirectory)
135 Reset();
136 if (pPath)
138 m_sFwdslashPath = pPath;
139 SanitizeRootPath(m_sFwdslashPath, true);
141 m_bDirectoryKnown = true;
142 m_bIsDirectory = bIsDirectory;
145 void CTGitPath::SetFromGit(const CString& sPath,CString *oldpath)
147 Reset();
148 m_sFwdslashPath = sPath;
149 SanitizeRootPath(m_sFwdslashPath, true);
150 if(oldpath)
151 m_sOldFwdslashPath = *oldpath;
154 void CTGitPath::SetFromWin(LPCTSTR pPath)
156 Reset();
157 m_sBackslashPath = pPath;
158 SanitizeRootPath(m_sBackslashPath, false);
159 ATLASSERT(m_sBackslashPath.Find('/')<0);
161 void CTGitPath::SetFromWin(const CString& sPath)
163 Reset();
164 m_sBackslashPath = sPath;
165 SanitizeRootPath(m_sBackslashPath, false);
167 void CTGitPath::SetFromWin(const CString& sPath, bool bIsDirectory)
169 Reset();
170 m_sBackslashPath = sPath;
171 m_bIsDirectory = bIsDirectory;
172 m_bDirectoryKnown = true;
173 SanitizeRootPath(m_sBackslashPath, false);
175 void CTGitPath::SetFromUnknown(const CString& sPath)
177 Reset();
178 // Just set whichever path we think is most likely to be used
179 // GitAdminDir admin;
180 // CString p;
181 // if(admin.HasAdminDir(sPath,&p))
182 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
183 // else
184 SetFwdslashPath(sPath);
187 LPCTSTR CTGitPath::GetWinPath() const
189 if(IsEmpty())
191 return _T("");
193 if(m_sBackslashPath.IsEmpty())
195 SetBackslashPath(m_sFwdslashPath);
197 return m_sBackslashPath;
199 // This is a temporary function, to be used during the migration to
200 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
201 const CString& CTGitPath::GetWinPathString() const
203 if(m_sBackslashPath.IsEmpty())
205 SetBackslashPath(m_sFwdslashPath);
207 return m_sBackslashPath;
210 const CString& CTGitPath::GetGitPathString() const
212 if(m_sFwdslashPath.IsEmpty())
214 SetFwdslashPath(m_sBackslashPath);
216 return m_sFwdslashPath;
219 const CString &CTGitPath::GetGitOldPathString() const
221 return m_sOldFwdslashPath;
223 #if 0
224 const char* CTGitPath::GetGitApiPath(apr_pool_t *pool) const
226 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
227 // each call to GetGitApiPath returns a different pointer value.
228 // If you made multiple calls to GetGitApiPath on the same string, only the last
229 // one would give you a valid pointer to an empty string, because each
230 // call would invalidate the previous call's return.
231 if(IsEmpty())
233 return "";
235 if(m_sFwdslashPath.IsEmpty())
237 SetFwdslashPath(m_sBackslashPath);
239 if(m_sUTF8FwdslashPath.IsEmpty())
241 SetUTF8FwdslashPath(m_sFwdslashPath);
243 if (svn_path_is_url(m_sUTF8FwdslashPath))
245 m_sUTF8FwdslashPathEscaped = CPathUtils::PathEscape(m_sUTF8FwdslashPath);
246 m_sUTF8FwdslashPathEscaped.Replace("file:////", "file:///\\");
247 m_sUTF8FwdslashPathEscaped = svn_path_canonicalize(m_sUTF8FwdslashPathEscaped, pool);
248 return m_sUTF8FwdslashPathEscaped;
250 m_sUTF8FwdslashPath = svn_path_canonicalize(m_sUTF8FwdslashPath, pool);
252 return m_sUTF8FwdslashPath;
254 #endif
256 const CString& CTGitPath::GetUIPathString() const
258 if (m_sUIPath.IsEmpty())
260 #if defined(_MFC_VER)
261 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
262 if (IsUrl())
264 m_sUIPath = CPathUtils::PathUnescape(GetGitPathString());
265 m_sUIPath.Replace(_T("file:////"), _T("file:///\\"));
268 else
269 #endif
271 m_sUIPath = GetWinPathString();
274 return m_sUIPath;
277 void CTGitPath::SetFwdslashPath(const CString& sPath) const
279 m_sFwdslashPath = sPath;
280 m_sFwdslashPath.Replace('\\', '/');
282 // We don't leave a trailing /
283 m_sFwdslashPath.TrimRight('/');
285 SanitizeRootPath(m_sFwdslashPath, true);
287 m_sFwdslashPath.Replace(_T("file:////"), _T("file:///\\"));
289 m_sUTF8FwdslashPath.Empty();
292 void CTGitPath::SetBackslashPath(const CString& sPath) const
294 m_sBackslashPath = sPath;
295 m_sBackslashPath.Replace('/', '\\');
296 m_sBackslashPath.TrimRight('\\');
297 SanitizeRootPath(m_sBackslashPath, false);
300 void CTGitPath::SetUTF8FwdslashPath(const CString& sPath) const
302 m_sUTF8FwdslashPath = CUnicodeUtils::GetUTF8(sPath);
305 void CTGitPath::SanitizeRootPath(CString& sPath, bool bIsForwardPath) const
307 // Make sure to add the trailing slash to root paths such as 'C:'
308 if (sPath.GetLength() == 2 && sPath[1] == ':')
310 sPath += (bIsForwardPath) ? _T("/") : _T("\\");
314 bool CTGitPath::IsUrl() const
316 #if 0
317 if (!m_bURLKnown)
319 EnsureFwdslashPathSet();
320 if(m_sUTF8FwdslashPath.IsEmpty())
322 SetUTF8FwdslashPath(m_sFwdslashPath);
324 m_bIsURL = !!svn_path_is_url(m_sUTF8FwdslashPath);
325 m_bURLKnown = true;
327 return m_bIsURL;
328 #endif
329 return false;
332 bool CTGitPath::IsDirectory() const
334 if(!m_bDirectoryKnown)
336 UpdateAttributes();
338 return m_bIsDirectory;
341 bool CTGitPath::Exists() const
343 if (!m_bExistsKnown)
345 UpdateAttributes();
347 return m_bExists;
350 bool CTGitPath::Delete(bool bTrash) const
352 EnsureBackslashPathSet();
353 ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);
354 bool bRet = false;
355 if (Exists())
357 if ((bTrash)||(IsDirectory()))
359 TCHAR * buf = new TCHAR[m_sBackslashPath.GetLength()+2];
360 _tcscpy_s(buf, m_sBackslashPath.GetLength()+2, m_sBackslashPath);
361 buf[m_sBackslashPath.GetLength()] = 0;
362 buf[m_sBackslashPath.GetLength()+1] = 0;
363 SHFILEOPSTRUCT shop = {0};
364 shop.wFunc = FO_DELETE;
365 shop.pFrom = buf;
366 shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
367 if (bTrash)
368 shop.fFlags |= FOF_ALLOWUNDO;
369 bRet = (SHFileOperation(&shop) == 0);
370 delete [] buf;
372 else
374 bRet = !!::DeleteFile(m_sBackslashPath);
377 m_bExists = false;
378 m_bExistsKnown = true;
379 return bRet;
382 __int64 CTGitPath::GetLastWriteTime() const
384 if(!m_bLastWriteTimeKnown)
386 UpdateAttributes();
388 return m_lastWriteTime;
391 bool CTGitPath::IsReadOnly() const
393 if(!m_bLastWriteTimeKnown)
395 UpdateAttributes();
397 return m_bIsReadOnly;
400 void CTGitPath::UpdateAttributes() const
402 EnsureBackslashPathSet();
403 WIN32_FILE_ATTRIBUTE_DATA attribs;
404 if(GetFileAttributesEx(m_sBackslashPath, GetFileExInfoStandard, &attribs))
406 m_bIsDirectory = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
407 m_lastWriteTime = *(__int64*)&attribs.ftLastWriteTime;
408 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
409 m_bExists = true;
411 else
413 DWORD err = GetLastError();
414 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))
416 m_bIsDirectory = false;
417 m_lastWriteTime = 0;
418 m_bExists = false;
420 else
422 m_bIsDirectory = false;
423 m_lastWriteTime = 0;
424 m_bExists = true;
425 return;
428 m_bDirectoryKnown = true;
429 m_bLastWriteTimeKnown = true;
430 m_bExistsKnown = true;
433 CTGitPath CTGitPath::GetSubPath(CTGitPath &root)
435 CTGitPath path;
437 if(GetWinPathString().Left(root.GetWinPathString().GetLength()) == root.GetWinPathString())
439 CString str=GetWinPathString();
440 path.SetFromWin(str.Right(str.GetLength()-root.GetWinPathString().GetLength()-1));
442 return path;
445 void CTGitPath::EnsureBackslashPathSet() const
447 if(m_sBackslashPath.IsEmpty())
449 SetBackslashPath(m_sFwdslashPath);
450 ATLASSERT(IsEmpty() || !m_sBackslashPath.IsEmpty());
453 void CTGitPath::EnsureFwdslashPathSet() const
455 if(m_sFwdslashPath.IsEmpty())
457 SetFwdslashPath(m_sBackslashPath);
458 ATLASSERT(IsEmpty() || !m_sFwdslashPath.IsEmpty());
463 // Reset all the caches
464 void CTGitPath::Reset()
466 m_bDirectoryKnown = false;
467 m_bURLKnown = false;
468 m_bLastWriteTimeKnown = false;
469 m_bHasAdminDirKnown = false;
470 m_bIsValidOnWindowsKnown = false;
471 m_bIsAdminDirKnown = false;
472 m_bExistsKnown = false;
473 m_bIsSpecialDirectoryKnown = false;
474 m_bIsSpecialDirectory = false;
476 m_sBackslashPath.Empty();
477 m_sFwdslashPath.Empty();
478 m_sUTF8FwdslashPath.Empty();
479 this->m_Action=0;
480 this->m_StatAdd=_T("");
481 this->m_StatDel=_T("");
482 ATLASSERT(IsEmpty());
485 CTGitPath CTGitPath::GetDirectory() const
487 if ((IsDirectory())||(!Exists()))
489 return *this;
491 return GetContainingDirectory();
494 CTGitPath CTGitPath::GetContainingDirectory() const
496 EnsureBackslashPathSet();
498 CString sDirName = m_sBackslashPath.Left(m_sBackslashPath.ReverseFind('\\'));
499 if(sDirName.GetLength() == 2 && sDirName[1] == ':')
501 // This is a root directory, which needs a trailing slash
502 sDirName += '\\';
503 if(sDirName == m_sBackslashPath)
505 // We were clearly provided with a root path to start with - we should return nothing now
506 sDirName.Empty();
509 if(sDirName.GetLength() == 1 && sDirName[0] == '\\')
511 // We have an UNC path and we already are the root
512 sDirName.Empty();
514 CTGitPath retVal;
515 retVal.SetFromWin(sDirName);
516 return retVal;
519 CString CTGitPath::GetRootPathString() const
521 EnsureBackslashPathSet();
522 CString workingPath = m_sBackslashPath;
523 LPTSTR pPath = workingPath.GetBuffer(MAX_PATH); // MAX_PATH ok here.
524 ATLVERIFY(::PathStripToRoot(pPath));
525 workingPath.ReleaseBuffer();
526 return workingPath;
530 CString CTGitPath::GetFilename() const
532 //ATLASSERT(!IsDirectory());
533 return GetFileOrDirectoryName();
536 CString CTGitPath::GetFileOrDirectoryName() const
538 EnsureBackslashPathSet();
539 return m_sBackslashPath.Mid(m_sBackslashPath.ReverseFind('\\')+1);
542 CString CTGitPath::GetUIFileOrDirectoryName() const
544 GetUIPathString();
545 return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);
548 CString CTGitPath::GetFileExtension() const
550 if(!IsDirectory())
552 EnsureBackslashPathSet();
553 int dotPos = m_sBackslashPath.ReverseFind('.');
554 int slashPos = m_sBackslashPath.ReverseFind('\\');
555 if (dotPos > slashPos)
556 return m_sBackslashPath.Mid(dotPos);
558 return CString();
560 CString CTGitPath::GetBaseFilename() const
562 int dot;
563 CString filename=GetFilename();
564 dot = filename.ReverseFind(_T('.'));
565 if(dot>0)
566 return filename.Left(dot);
567 else
568 return filename;
571 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)
573 int length = sP1.GetLength();
574 if(length != sP2.GetLength())
576 // Different lengths
577 return false;
579 // We work from the end of the strings, because path differences
580 // are more likely to occur at the far end of a string
581 LPCTSTR pP1Start = sP1;
582 LPCTSTR pP1 = pP1Start+(length-1);
583 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
584 while(length-- > 0)
586 if(_totlower(*pP1--) != _totlower(*pP2--))
588 return false;
591 return true;
594 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)
596 int length = sP1.GetLength();
597 if(length != sP2.GetLength())
599 // Different lengths
600 return false;
602 // We work from the end of the strings, because path differences
603 // are more likely to occur at the far end of a string
604 LPCTSTR pP1Start = sP1;
605 LPCTSTR pP1 = pP1Start+(length-1);
606 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
607 while(length-- > 0)
609 if((*pP1--) != (*pP2--))
611 return false;
614 return true;
617 bool CTGitPath::IsEmpty() const
619 // Check the backward slash path first, since the chance that this
620 // one is set is higher. In case of a 'false' return value it's a little
621 // bit faster.
622 return m_sBackslashPath.IsEmpty() && m_sFwdslashPath.IsEmpty();
625 // Test if both paths refer to the same item
626 // Ignores case and slash direction
627 bool CTGitPath::IsEquivalentTo(const CTGitPath& rhs) const
629 // Try and find a slash direction which avoids having to convert
630 // both filenames
631 if(!m_sBackslashPath.IsEmpty())
633 // *We've* got a \ path - make sure that the RHS also has a \ path
634 rhs.EnsureBackslashPathSet();
635 return ArePathStringsEqualWithCase(m_sBackslashPath, rhs.m_sBackslashPath);
637 else
639 // Assume we've got a fwdslash path and make sure that the RHS has one
640 rhs.EnsureFwdslashPathSet();
641 return ArePathStringsEqualWithCase(m_sFwdslashPath, rhs.m_sFwdslashPath);
645 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath& rhs) const
647 // Try and find a slash direction which avoids having to convert
648 // both filenames
649 if(!m_sBackslashPath.IsEmpty())
651 // *We've* got a \ path - make sure that the RHS also has a \ path
652 rhs.EnsureBackslashPathSet();
653 return ArePathStringsEqual(m_sBackslashPath, rhs.m_sBackslashPath);
655 else
657 // Assume we've got a fwdslash path and make sure that the RHS has one
658 rhs.EnsureFwdslashPathSet();
659 return ArePathStringsEqual(m_sFwdslashPath, rhs.m_sFwdslashPath);
663 bool CTGitPath::IsAncestorOf(const CTGitPath& possibleDescendant) const
665 possibleDescendant.EnsureBackslashPathSet();
666 EnsureBackslashPathSet();
668 bool bPathStringsEqual = ArePathStringsEqual(m_sBackslashPath, possibleDescendant.m_sBackslashPath.Left(m_sBackslashPath.GetLength()));
669 if (m_sBackslashPath.GetLength() >= possibleDescendant.GetWinPathString().GetLength())
671 return bPathStringsEqual;
674 return (bPathStringsEqual &&
675 ((possibleDescendant.m_sBackslashPath[m_sBackslashPath.GetLength()] == '\\')||
676 (m_sBackslashPath.GetLength()==3 && m_sBackslashPath[1]==':')));
679 // Get a string representing the file path, optionally with a base
680 // section stripped off the front.
681 CString CTGitPath::GetDisplayString(const CTGitPath* pOptionalBasePath /* = NULL*/) const
683 EnsureFwdslashPathSet();
684 if(pOptionalBasePath != NULL)
686 // Find the length of the base-path without having to do an 'ensure' on it
687 int baseLength = max(pOptionalBasePath->m_sBackslashPath.GetLength(), pOptionalBasePath->m_sFwdslashPath.GetLength());
689 // Now, chop that baseLength of the front of the path
690 return m_sFwdslashPath.Mid(baseLength).TrimLeft('/');
692 return m_sFwdslashPath;
695 int CTGitPath::Compare(const CTGitPath& left, const CTGitPath& right)
697 left.EnsureBackslashPathSet();
698 right.EnsureBackslashPathSet();
699 return left.m_sBackslashPath.CompareNoCase(right.m_sBackslashPath);
702 bool operator<(const CTGitPath& left, const CTGitPath& right)
704 return CTGitPath::Compare(left, right) < 0;
707 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right)
709 return left.IsEquivalentTo(right);
712 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right)
714 if (left.IsAdminDir() && right.IsAdminDir())
716 CTGitPath l = left;
717 CTGitPath r = right;
720 l = l.GetContainingDirectory();
721 } while(l.HasAdminDir());
724 r = r.GetContainingDirectory();
725 } while(r.HasAdminDir());
726 return l.GetContainingDirectory().IsEquivalentTo(r.GetContainingDirectory());
728 return left.GetDirectory().IsEquivalentTo(right.GetDirectory());
731 bool CTGitPath::CheckChild(const CTGitPath &parent, const CTGitPath& child)
733 return parent.IsAncestorOf(child);
736 void CTGitPath::AppendRawString(const CString& sAppend)
738 EnsureFwdslashPathSet();
739 CString strCopy = m_sFwdslashPath += sAppend;
740 SetFromUnknown(strCopy);
743 void CTGitPath::AppendPathString(const CString& sAppend)
745 EnsureBackslashPathSet();
746 CString cleanAppend(sAppend);
747 cleanAppend.Replace('/', '\\');
748 cleanAppend.TrimLeft('\\');
749 m_sBackslashPath.TrimRight('\\');
750 CString strCopy = m_sBackslashPath + _T("\\") + cleanAppend;
751 SetFromWin(strCopy);
754 bool CTGitPath::HasAdminDir() const
756 if (m_bHasAdminDirKnown)
757 return m_bHasAdminDir;
759 EnsureBackslashPathSet();
760 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
761 m_bHasAdminDirKnown = true;
762 return m_bHasAdminDir;
765 bool CTGitPath::HasSubmodules() const
767 return !g_GitAdminDir.GetSuperProjectRoot(GetWinPathString()).IsEmpty();
770 int CTGitPath::GetAdminDirMask() const
772 int status = 0;
773 CString topdir,path;
774 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
776 return status;
779 status |= ITEMIS_INSVN|ITEMIS_FOLDERINSVN;
781 path=topdir;
782 path+=_T("\\");
783 path+=g_GitAdminDir.GetAdminDirName();
784 path+=_T("\\refs\\stash");
785 if( PathFileExists(path) )
786 status |= ITEMIS_STASH;
788 path=topdir;
789 path+=_T("\\");
790 path+=g_GitAdminDir.GetAdminDirName();
791 path+=_T("\\svn");
792 if( PathFileExists(path) )
793 status |= ITEMIS_GITSVN;
795 path=topdir;
796 path+=_T("\\.gitmodules");
797 if( PathFileExists(path) )
798 status |= ITEMIS_SUBMODULE;
800 return status;
803 bool CTGitPath::HasStashDir() const
805 CString topdir;
806 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
808 return false;
810 topdir+=_T("\\");
811 topdir+=g_GitAdminDir.GetAdminDirName();
812 topdir+=_T("\\refs\stash");
813 return PathFileExists(topdir);
815 bool CTGitPath::HasGitSVNDir() const
817 CString topdir;
818 if(!g_GitAdminDir.HasAdminDir(GetWinPathString(),&topdir))
820 return false;
822 topdir+=_T("\\");
823 topdir+=g_GitAdminDir.GetAdminDirName();
824 topdir+=_T("\\svn");
825 return PathFileExists(topdir);
827 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const
829 if (m_bHasAdminDirKnown)
831 if (ProjectTopDir)
832 *ProjectTopDir = m_sProjectRoot;
833 return m_bHasAdminDir;
836 EnsureBackslashPathSet();
837 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
838 m_bHasAdminDirKnown = true;
839 if (ProjectTopDir)
840 *ProjectTopDir = m_sProjectRoot;
841 return m_bHasAdminDir;
844 bool CTGitPath::IsAdminDir() const
846 if (m_bIsAdminDirKnown)
847 return m_bIsAdminDir;
849 EnsureBackslashPathSet();
850 m_bIsAdminDir = g_GitAdminDir.IsAdminDirPath(m_sBackslashPath);
851 m_bIsAdminDirKnown = true;
852 return m_bIsAdminDir;
855 bool CTGitPath::IsValidOnWindows() const
857 if (m_bIsValidOnWindowsKnown)
858 return m_bIsValidOnWindows;
860 m_bIsValidOnWindows = false;
861 EnsureBackslashPathSet();
862 CString sMatch = m_sBackslashPath + _T("\r\n");
863 wstring sPattern;
864 // the 'file://' URL is just a normal windows path:
865 if (sMatch.Left(7).CompareNoCase(_T("file:\\\\"))==0)
867 sMatch = sMatch.Mid(7);
868 sMatch.TrimLeft(_T("\\"));
869 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
871 else if (IsUrl())
873 sPattern = _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
875 else
877 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
882 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
883 tr1::wsmatch match;
885 wstring rmatch = wstring((LPCTSTR)sMatch);
886 if (tr1::regex_match(rmatch, match, rx))
888 if (wstring(match[0]).compare(sMatch)==0)
889 m_bIsValidOnWindows = true;
891 if (m_bIsValidOnWindows)
893 // now check for illegal filenames
894 tr1::wregex rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
895 rmatch = m_sBackslashPath;
896 if (tr1::regex_search(rmatch, rx2, tr1::regex_constants::match_default))
897 m_bIsValidOnWindows = false;
900 catch (exception) {}
902 m_bIsValidOnWindowsKnown = true;
903 return m_bIsValidOnWindows;
906 bool CTGitPath::IsSpecialDirectory() const
908 if (m_bIsSpecialDirectoryKnown)
909 return m_bIsSpecialDirectory;
911 static LPCTSTR specialDirectories[]
912 = { _T("trunk"), _T("tags"), _T("branches") };
914 for (int i=0 ; i<(sizeof(specialDirectories) / sizeof(specialDirectories[0])) ; ++i)
916 CString name = GetFileOrDirectoryName();
917 if (0 == name.CompareNoCase(specialDirectories[i]))
919 m_bIsSpecialDirectory = true;
920 break;
924 m_bIsSpecialDirectoryKnown = true;
926 return m_bIsSpecialDirectory;
929 //////////////////////////////////////////////////////////////////////////
931 CTGitPathList::CTGitPathList()
936 // A constructor which allows a path list to be easily built which one initial entry in
937 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)
939 AddPath(firstEntry);
941 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool staged)
943 int pos=0;
944 CString one;
945 CTGitPath path;
946 CString part;
947 this->Clear();
949 while(pos>=0 && pos<out.size())
951 one.Empty();
952 path.Reset();
954 g_Git.StringAppend(&one,&out[pos],CP_ACP);
955 int tabstart=0;
956 path.m_Action=path.ParserAction(out[pos]);
957 one.Tokenize(_T("\t"),tabstart);
959 if(tabstart>=0)
960 path.SetFromGit(one.Right(one.GetLength()-tabstart));
962 tabstart=0;
964 part=one.Tokenize(_T(" "),tabstart); //Tag
966 part=one.Tokenize(_T(" "),tabstart); //Mode
968 part=one.Tokenize(_T(" "),tabstart); //Hash
970 part=one.Tokenize(_T("\t"),tabstart); //Stage
972 path.m_Stage=_ttol(part);
974 this->AddPath(path);
976 pos=out.findNextString(pos);
978 return pos;
980 int CTGitPathList::FillUnRev(int action,CTGitPathList *list)
982 int pos=0;
983 this->Clear();
984 CTGitPath path;
986 int count;
987 if(list==NULL)
988 count=1;
989 else
990 count=list->GetCount();
991 for(int i=0;i<count;i++)
993 CString cmd;
994 pos=0;
996 CString ignored;
997 if(action & CTGitPath::LOGACTIONS_IGNORE)
998 ignored= _T(" --ignored");
1000 if(list==NULL)
1002 cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");
1003 cmd+=ignored;
1006 else
1007 { cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z %s-- \"%s\""),
1008 ignored,
1009 (*list)[i].GetWinPathString());
1012 BYTE_VECTOR out;
1013 out.clear();
1014 g_Git.Run(cmd,&out);
1016 pos=0;
1017 CString one;
1018 while( pos>=0 && pos<out.size())
1020 one.Empty();
1021 g_Git.StringAppend(&one,&out[pos],CP_ACP);
1022 if(!one.IsEmpty())
1024 //SetFromGit will clear all status
1025 path.SetFromGit(one);
1026 path.m_Action=action;
1027 AddPath(path);
1029 pos=out.findNextString(pos);
1033 return 0;
1035 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log)
1037 this->Clear();
1038 int pos=0;
1039 //BYTE *p=&log[0];
1040 //CString one;
1041 CTGitPath path;
1042 m_Action=0;
1043 while( pos>=0 && pos<log.size())
1045 //one=log.Tokenize(_T("\n"),pos);
1046 path.Reset();
1047 if(log[pos]=='\n')
1048 pos++;
1050 if(log[pos]==':')
1052 bool merged=false;
1053 if(log[pos+1] ==':')
1055 merged=true;
1057 int end=log.find(0,pos);
1058 int actionstart=-1;
1059 int numfile=1;
1060 int file1=-1,file2=-1;
1061 if( end>0 )
1063 actionstart=log.find(' ',end-6);
1064 pos=actionstart;
1066 if( actionstart>0 )
1068 actionstart++;
1070 file1 = log.find(0,actionstart);
1071 if( file1>=0 )
1073 file1++;
1074 pos=file1;
1076 if( log[actionstart] == 'C' || log[actionstart] == 'R' )
1078 file2=file1;
1079 numfile=2;
1080 file1 = log.find(0,file1);
1081 if(file1>=0 )
1083 file1++;
1084 pos=file1;
1090 CString pathname1;
1091 CString pathname2;
1093 if( file1>=0 )
1094 g_Git.StringAppend(&pathname1,&log[file1],CP_ACP);
1095 if( file2>=0 )
1096 g_Git.StringAppend(&pathname2,&log[file2],CP_ACP);
1098 CTGitPath *GitPath=LookForGitPath(pathname1);
1100 if(GitPath)
1102 GitPath->ParserAction( log[actionstart] );
1104 if(merged)
1106 GitPath->m_Action |= CTGitPath::LOGACTIONS_MERGED;
1107 GitPath->m_Action &= ~CTGitPath::LOGACTIONS_FORWORD;
1109 m_Action |=GitPath->m_Action;
1111 }else
1113 int ac=path.ParserAction(log[actionstart] );
1114 ac |= merged?CTGitPath::LOGACTIONS_MERGED:0;
1116 path.SetFromGit(pathname1,&pathname2);
1117 path.m_Action=ac;
1118 //action must be set after setfromgit. SetFromGit will clear all status.
1119 this->m_Action|=ac;
1121 AddPath(path);
1125 }else
1127 int tabstart=0;
1128 path.Reset();
1129 CString StatAdd;
1130 CString StatDel;
1131 CString file1;
1132 CString file2;
1134 tabstart=log.find('\t',pos);
1135 if(tabstart >=0)
1137 log[tabstart]=0;
1138 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);
1139 pos=tabstart+1;
1142 tabstart=log.find('\t',pos);
1143 if(tabstart >=0)
1145 log[tabstart]=0;
1147 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);
1148 pos=tabstart+1;
1151 if(log[pos] == 0) //rename
1153 pos++;
1154 g_Git.StringAppend(&file2,&log[pos],CP_ACP);
1155 int sec=log.find(0,pos);
1156 if(sec>=0)
1158 sec++;
1159 g_Git.StringAppend(&file1,&log[sec],CP_ACP);
1161 pos=sec;
1163 }else
1165 g_Git.StringAppend(&file1,&log[pos],CP_ACP);
1167 path.SetFromGit(file1,&file2);
1169 CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());
1170 if(GitPath)
1172 GitPath->m_StatAdd=StatAdd;
1173 GitPath->m_StatDel=StatDel;
1174 }else
1176 //path.SetFromGit(pathname);
1177 path.m_StatAdd=StatAdd;
1178 path.m_StatDel=StatDel;
1179 path.m_Action |= CTGitPath::LOGACTIONS_FORWORD;
1180 AddPath(path);
1184 pos=log.findNextString(pos);
1186 return pos;
1189 void CTGitPathList::AddPath(const CTGitPath& newPath)
1191 m_paths.push_back(newPath);
1192 m_commonBaseDirectory.Reset();
1194 int CTGitPathList::GetCount() const
1196 return (int)m_paths.size();
1198 void CTGitPathList::Clear()
1200 m_paths.clear();
1201 m_commonBaseDirectory.Reset();
1204 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const
1206 ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());
1207 return m_paths[index];
1210 bool CTGitPathList::AreAllPathsFiles() const
1212 // Look through the vector for any directories - if we find them, return false
1213 return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();
1217 #if defined(_MFC_VER)
1219 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)
1221 Clear();
1224 CString strLine;
1225 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
1227 // for every selected file/folder
1228 CTGitPath path;
1229 while (file.ReadString(strLine))
1231 path.SetFromUnknown(strLine);
1232 AddPath(path);
1234 file.Close();
1236 catch (CFileException* pE)
1238 TRACE("CFileException loading target file list\n");
1239 TCHAR error[10000] = {0};
1240 pE->GetErrorMessage(error, 10000);
1241 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1242 pE->Delete();
1243 return false;
1245 return true;
1248 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const
1252 if (bANSI)
1254 CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
1255 PathVector::const_iterator it;
1256 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1258 CStringA line = CStringA(it->GetGitPathString()) + '\n';
1259 file.Write(line, line.GetLength());
1261 file.Close();
1263 else
1265 CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);
1266 PathVector::const_iterator it;
1267 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1269 file.WriteString(it->GetGitPathString()+_T("\n"));
1271 file.Close();
1274 catch (CFileException* pE)
1276 TRACE("CFileException in writing temp file\n");
1277 pE->Delete();
1278 return false;
1280 return true;
1284 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)
1286 int pos = 0;
1287 CString temp;
1288 for(;;)
1290 temp = sPathString.Tokenize(_T("*"),pos);
1291 if(temp.IsEmpty())
1293 break;
1295 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));
1299 CString CTGitPathList::CreateAsteriskSeparatedString() const
1301 CString sRet;
1302 PathVector::const_iterator it;
1303 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1305 if (!sRet.IsEmpty())
1306 sRet += _T("*");
1307 sRet += it->GetWinPathString();
1309 return sRet;
1311 #endif // _MFC_VER
1313 bool
1314 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1316 // Check if all the paths are files and in the same directory
1317 PathVector::const_iterator it;
1318 m_commonBaseDirectory.Reset();
1319 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1321 if(it->IsDirectory())
1323 return false;
1325 const CTGitPath& baseDirectory = it->GetDirectory();
1326 if(m_commonBaseDirectory.IsEmpty())
1328 m_commonBaseDirectory = baseDirectory;
1330 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1332 // Different path
1333 m_commonBaseDirectory.Reset();
1334 return false;
1337 return true;
1340 CTGitPath CTGitPathList::GetCommonDirectory() const
1342 if (m_commonBaseDirectory.IsEmpty())
1344 PathVector::const_iterator it;
1345 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1347 const CTGitPath& baseDirectory = it->GetDirectory();
1348 if(m_commonBaseDirectory.IsEmpty())
1350 m_commonBaseDirectory = baseDirectory;
1352 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1354 // Different path
1355 m_commonBaseDirectory.Reset();
1356 break;
1360 // since we only checked strings, not paths,
1361 // we have to make sure now that we really return a *path* here
1362 PathVector::const_iterator iter;
1363 for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)
1365 if (!m_commonBaseDirectory.IsAncestorOf(*iter))
1367 m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();
1368 break;
1371 return m_commonBaseDirectory;
1374 CTGitPath CTGitPathList::GetCommonRoot() const
1376 PathVector::const_iterator it;
1377 CString sRoot, sTempRoot;
1378 bool bEqual = true;
1380 if (GetCount() == 1)
1381 return m_paths[0];
1383 int backSlashPos = 0;
1384 int searchStartPos = 0;
1385 while (bEqual)
1387 if(m_paths.empty())
1388 break;
1390 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1392 if (backSlashPos == 0)
1394 backSlashPos = it->GetWinPathString().Find('\\', searchStartPos+1);
1395 if ((backSlashPos < 0)&&(searchStartPos != it->GetWinPathString().GetLength()))
1396 backSlashPos = it->GetWinPathString().GetLength();
1398 else if (it->GetWinPathString().Find('\\', searchStartPos+1) != backSlashPos)
1400 if (it->GetWinPathString().Find('\\', searchStartPos+1) < 0)
1402 if (it->GetWinPathString().GetLength() != backSlashPos)
1404 bEqual = false;
1405 break;
1408 else
1410 bEqual = false;
1411 break;
1414 if (backSlashPos < 0)
1416 bEqual = false;
1417 break;
1420 if (bEqual == false)
1422 if (searchStartPos)
1423 sRoot = m_paths[0].GetWinPathString().Left(searchStartPos+1);
1425 else
1427 searchStartPos = backSlashPos;
1429 backSlashPos = 0;
1432 return CTGitPath(sRoot.TrimRight('\\'));
1435 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)
1437 std::sort(m_paths.begin(), m_paths.end());
1438 if (bReverse)
1439 std::reverse(m_paths.begin(), m_paths.end());
1442 void CTGitPathList::DeleteAllFiles(bool bTrash)
1444 PathVector::const_iterator it;
1445 if (bTrash)
1447 SortByPathname();
1448 CString sPaths;
1449 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1451 if ((it->Exists())&&(!it->IsDirectory()))
1453 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1454 sPaths += it->GetWinPath();
1455 sPaths += '\0';
1458 sPaths += '\0';
1459 sPaths += '\0';
1460 SHFILEOPSTRUCT shop = {0};
1461 shop.wFunc = FO_DELETE;
1462 shop.pFrom = (LPCTSTR)sPaths;
1463 shop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
1464 SHFileOperation(&shop);
1466 else
1468 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1470 if (!it->IsDirectory())
1472 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1473 ::DeleteFile(it->GetWinPath());
1477 Clear();
1480 void CTGitPathList::RemoveDuplicates()
1482 SortByPathname();
1483 // Remove the duplicates
1484 // (Unique moves them to the end of the vector, then erase chops them off)
1485 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());
1488 void CTGitPathList::RemoveAdminPaths()
1490 PathVector::iterator it;
1491 for(it = m_paths.begin(); it != m_paths.end(); )
1493 if (it->IsAdminDir())
1495 m_paths.erase(it);
1496 it = m_paths.begin();
1498 else
1499 ++it;
1503 void CTGitPathList::RemovePath(const CTGitPath& path)
1505 PathVector::iterator it;
1506 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1508 if (it->IsEquivalentTo(path))
1510 m_paths.erase(it);
1511 return;
1516 void CTGitPathList::RemoveItem(CTGitPath & path)
1518 PathVector::iterator it;
1519 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1521 if (it->GetGitPathString()==path.GetGitPathString())
1523 m_paths.erase(it);
1524 return;
1528 void CTGitPathList::RemoveChildren()
1530 SortByPathname();
1531 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());
1534 bool CTGitPathList::IsEqual(const CTGitPathList& list)
1536 if (list.GetCount() != GetCount())
1537 return false;
1538 for (int i=0; i<list.GetCount(); ++i)
1540 if (!list[i].IsEquivalentTo(m_paths[i]))
1541 return false;
1543 return true;
1546 //////////////////////////////////////////////////////////////////////////
1547 #if 0
1548 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const
1550 apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));
1552 for(int nItem = 0; nItem < GetCount(); nItem++)
1554 const char * target = m_paths[nItem].GetGitApiPath(pool);
1555 (*((const char **) apr_array_push (targets))) = target;
1558 return targets;
1560 #endif
1561 //////////////////////////////////////////////////////////////////////////
1563 #if 0
1564 #if defined(_DEBUG)
1565 // Some test cases for these classes
1566 static class CTGitPathTests
1568 public:
1569 CTGitPathTests()
1571 apr_initialize();
1572 pool = svn_pool_create(NULL);
1573 GetDirectoryTest();
1574 AdminDirTest();
1575 SortTest();
1576 RawAppendTest();
1577 PathAppendTest();
1578 RemoveDuplicatesTest();
1579 RemoveChildrenTest();
1580 ContainingDirectoryTest();
1581 AncestorTest();
1582 SubversionPathTest();
1583 GetCommonRootTest();
1584 #if defined(_MFC_VER)
1585 ValidPathAndUrlTest();
1586 ListLoadingTest();
1587 #endif
1588 apr_terminate();
1591 private:
1592 // apr_pool_t * pool;
1593 void GetDirectoryTest()
1595 // Bit tricky, this test, because we need to know something about the file
1596 // layout on the machine which is running the test
1597 TCHAR winDir[MAX_PATH+1];
1598 GetWindowsDirectory(winDir, MAX_PATH);
1599 CString sWinDir(winDir);
1601 CTGitPath testPath;
1602 // This is a file which we know will always be there
1603 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));
1604 ATLASSERT(!testPath.IsDirectory());
1605 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1606 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);
1608 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1609 // but we know it must be different to the directory itself
1610 testPath.SetFromUnknown(sWinDir);
1611 ATLASSERT(testPath.IsDirectory());
1612 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1613 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);
1614 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());
1616 // Try a root path
1617 testPath.SetFromUnknown(_T("C:\\"));
1618 ATLASSERT(testPath.IsDirectory());
1619 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1620 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1621 // Try a root UNC path
1622 testPath.SetFromUnknown(_T("\\MYSTATION"));
1623 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1626 void AdminDirTest()
1628 CTGitPath testPath;
1629 testPath.SetFromUnknown(_T("c:\\.svndir"));
1630 ATLASSERT(!testPath.IsAdminDir());
1631 testPath.SetFromUnknown(_T("c:\\test.svn"));
1632 ATLASSERT(!testPath.IsAdminDir());
1633 testPath.SetFromUnknown(_T("c:\\.svn"));
1634 ATLASSERT(testPath.IsAdminDir());
1635 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));
1636 ATLASSERT(!testPath.IsAdminDir());
1637 testPath.SetFromUnknown(_T("c:\\.svn\\test"));
1638 ATLASSERT(testPath.IsAdminDir());
1640 CTGitPathList pathList;
1641 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));
1642 pathList.AddPath(CTGitPath(_T("c:\\.svn")));
1643 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1644 pathList.AddPath(CTGitPath(_T("c:\\test")));
1645 pathList.RemoveAdminPaths();
1646 ATLASSERT(pathList.GetCount()==2);
1647 pathList.Clear();
1648 pathList.AddPath(CTGitPath(_T("c:\\test")));
1649 pathList.RemoveAdminPaths();
1650 ATLASSERT(pathList.GetCount()==1);
1653 void SortTest()
1655 CTGitPathList testList;
1656 CTGitPath testPath;
1657 testPath.SetFromUnknown(_T("c:/Z"));
1658 testList.AddPath(testPath);
1659 testPath.SetFromUnknown(_T("c:/B"));
1660 testList.AddPath(testPath);
1661 testPath.SetFromUnknown(_T("c:\\a"));
1662 testList.AddPath(testPath);
1663 testPath.SetFromUnknown(_T("c:/Test"));
1664 testList.AddPath(testPath);
1666 testList.SortByPathname();
1668 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));
1669 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));
1670 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));
1671 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));
1674 void RawAppendTest()
1676 CTGitPath testPath(_T("c:/test/"));
1677 testPath.AppendRawString(_T("/Hello"));
1678 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1680 testPath.AppendRawString(_T("\\T2"));
1681 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1683 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1684 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1685 testBasePath.AppendRawString(testFilePath.GetFileExtension());
1686 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1689 void PathAppendTest()
1691 CTGitPath testPath(_T("c:/test/"));
1692 testPath.AppendPathString(_T("/Hello"));
1693 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1695 testPath.AppendPathString(_T("T2"));
1696 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1698 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1699 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1700 // You wouldn't want to do this in real life - you'd use append-raw
1701 testBasePath.AppendPathString(testFilePath.GetFileExtension());
1702 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1705 void RemoveDuplicatesTest()
1707 CTGitPathList list;
1708 list.AddPath(CTGitPath(_T("Z")));
1709 list.AddPath(CTGitPath(_T("A")));
1710 list.AddPath(CTGitPath(_T("E")));
1711 list.AddPath(CTGitPath(_T("E")));
1713 ATLASSERT(list[2].IsEquivalentTo(list[3]));
1714 ATLASSERT(list[2]==list[3]);
1716 ATLASSERT(list.GetCount() == 4);
1718 list.RemoveDuplicates();
1720 ATLASSERT(list.GetCount() == 3);
1722 ATLASSERT(list[0].GetWinPathString() == _T("A"));
1723 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);
1724 ATLASSERT(list[2].GetWinPathString() == _T("Z"));
1727 void RemoveChildrenTest()
1729 CTGitPathList list;
1730 list.AddPath(CTGitPath(_T("c:\\test")));
1731 list.AddPath(CTGitPath(_T("c:\\test\\file")));
1732 list.AddPath(CTGitPath(_T("c:\\testfile")));
1733 list.AddPath(CTGitPath(_T("c:\\parent")));
1734 list.AddPath(CTGitPath(_T("c:\\parent\\child")));
1735 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1736 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1738 ATLASSERT(list.GetCount() == 7);
1740 list.RemoveChildren();
1742 ATLTRACE("count = %d\n", list.GetCount());
1743 ATLASSERT(list.GetCount() == 3);
1745 list.SortByPathname();
1747 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1748 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1749 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1752 #if defined(_MFC_VER)
1753 void ListLoadingTest()
1755 TCHAR buf[MAX_PATH];
1756 GetCurrentDirectory(MAX_PATH, buf);
1757 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1758 CTGitPathList testList;
1759 testList.LoadFromAsteriskSeparatedString(sPathList);
1761 ATLASSERT(testList.GetCount() == 3);
1762 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));
1763 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1764 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));
1766 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));
1767 testList.Clear();
1768 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1769 testList.LoadFromAsteriskSeparatedString(sPathList);
1770 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1772 #endif
1774 void ContainingDirectoryTest()
1777 CTGitPath testPath;
1778 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1779 CTGitPath dir;
1780 dir = testPath.GetContainingDirectory();
1781 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1782 dir = dir.GetContainingDirectory();
1783 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));
1784 dir = dir.GetContainingDirectory();
1785 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));
1786 dir = dir.GetContainingDirectory();
1787 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));
1788 dir = dir.GetContainingDirectory();
1789 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));
1790 dir = dir.GetContainingDirectory();
1791 ATLASSERT(dir.IsEmpty());
1792 ATLASSERT(dir.GetWinPathString() == _T(""));
1795 void AncestorTest()
1797 CTGitPath testPath;
1798 testPath.SetFromWin(_T("c:\\windows"));
1799 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1800 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1801 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1802 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1803 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1806 void SubversionPathTest()
1808 CTGitPath testPath;
1809 testPath.SetFromWin(_T("c:\\"));
1810 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);
1811 testPath.SetFromWin(_T("c:\\folder"));
1812 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);
1813 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1814 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);
1815 testPath.SetFromUnknown(_T("http://testing/"));
1816 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);
1817 testPath.SetFromGit(NULL);
1818 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);
1819 #if defined(_MFC_VER)
1820 testPath.SetFromUnknown(_T("http://testing again"));
1821 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1822 testPath.SetFromUnknown(_T("http://testing%20again"));
1823 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1824 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1825 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1826 #endif
1829 void GetCommonRootTest()
1831 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));
1832 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));
1833 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1835 CTGitPathList list;
1836 list.AddPath(pathA);
1837 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1838 list.AddPath(pathB);
1839 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1840 list.AddPath(pathC);
1841 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1842 #ifdef _MFC_VER
1843 list.Clear();
1844 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");
1845 list.LoadFromAsteriskSeparatedString(sPathList);
1846 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1848 list.Clear();
1849 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");
1850 list.LoadFromAsteriskSeparatedString(sPathList);
1851 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1853 list.Clear();
1854 sPathList = _T("c:\\windows\\*c:\\windows");
1855 list.LoadFromAsteriskSeparatedString(sPathList);
1856 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1858 list.Clear();
1859 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");
1860 list.LoadFromAsteriskSeparatedString(sPathList);
1861 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1863 list.Clear();
1864 sPathList = _T("c:\\windowsdummy*c:\\windows");
1865 list.LoadFromAsteriskSeparatedString(sPathList);
1866 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1867 #endif
1870 void ValidPathAndUrlTest()
1872 CTGitPath testPath;
1873 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1874 ATLASSERT(testPath.IsValidOnWindows());
1875 testPath.SetFromWin(_T("c:\\"));
1876 ATLASSERT(testPath.IsValidOnWindows());
1877 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1878 ATLASSERT(testPath.IsValidOnWindows());
1879 testPath.SetFromWin(_T("c"));
1880 ATLASSERT(testPath.IsValidOnWindows());
1881 testPath.SetFromWin(_T("c:\\test folder\\file"));
1882 ATLASSERT(testPath.IsValidOnWindows());
1883 testPath.SetFromWin(_T("c:\\folder\\"));
1884 ATLASSERT(testPath.IsValidOnWindows());
1885 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1886 ATLASSERT(testPath.IsValidOnWindows());
1887 testPath.SetFromWin(_T("c:\\.svn"));
1888 ATLASSERT(testPath.IsValidOnWindows());
1889 testPath.SetFromWin(_T("c:\\com\\file"));
1890 ATLASSERT(testPath.IsValidOnWindows());
1891 testPath.SetFromWin(_T("c:\\test\\conf"));
1892 ATLASSERT(testPath.IsValidOnWindows());
1893 testPath.SetFromWin(_T("c:\\LPT"));
1894 ATLASSERT(testPath.IsValidOnWindows());
1895 testPath.SetFromWin(_T("c:\\test\\LPT"));
1896 ATLASSERT(testPath.IsValidOnWindows());
1897 testPath.SetFromWin(_T("c:\\com1test"));
1898 ATLASSERT(testPath.IsValidOnWindows());
1899 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1900 ATLASSERT(testPath.IsValidOnWindows());
1902 testPath.SetFromWin(_T("\\\\Share\\filename"));
1903 ATLASSERT(testPath.IsValidOnWindows());
1904 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));
1905 ATLASSERT(testPath.IsValidOnWindows());
1906 testPath.SetFromWin(_T("\\\\Share\\.svn"));
1907 ATLASSERT(testPath.IsValidOnWindows());
1909 // now the negative tests
1910 testPath.SetFromWin(_T("c:\\test:folder"));
1911 ATLASSERT(!testPath.IsValidOnWindows());
1912 testPath.SetFromWin(_T("c:\\file<name"));
1913 ATLASSERT(!testPath.IsValidOnWindows());
1914 testPath.SetFromWin(_T("c:\\something*else"));
1915 ATLASSERT(!testPath.IsValidOnWindows());
1916 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));
1917 ATLASSERT(!testPath.IsValidOnWindows());
1918 testPath.SetFromWin(_T("c:\\ext.>ension"));
1919 ATLASSERT(!testPath.IsValidOnWindows());
1920 testPath.SetFromWin(_T("c:\\com1\\filename"));
1921 ATLASSERT(!testPath.IsValidOnWindows());
1922 testPath.SetFromWin(_T("c:\\com1"));
1923 ATLASSERT(!testPath.IsValidOnWindows());
1924 testPath.SetFromWin(_T("c:\\com1\\AuX"));
1925 ATLASSERT(!testPath.IsValidOnWindows());
1927 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1928 ATLASSERT(!testPath.IsValidOnWindows());
1929 testPath.SetFromWin(_T("\\\\Share\\prn"));
1930 ATLASSERT(!testPath.IsValidOnWindows());
1931 testPath.SetFromWin(_T("\\\\Share\\NUL"));
1932 ATLASSERT(!testPath.IsValidOnWindows());
1934 // now come some URL tests
1935 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));
1936 ATLASSERT(testPath.IsValidOnWindows());
1937 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1938 ATLASSERT(testPath.IsValidOnWindows());
1939 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1940 ATLASSERT(testPath.IsValidOnWindows());
1941 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1942 ATLASSERT(testPath.IsValidOnWindows());
1943 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));
1944 ATLASSERT(testPath.IsValidOnWindows());
1945 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1946 ATLASSERT(testPath.IsValidOnWindows());
1947 // and some negative URL tests
1948 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1949 ATLASSERT(!testPath.IsValidOnWindows());
1950 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1951 ATLASSERT(!testPath.IsValidOnWindows());
1952 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1953 ATLASSERT(!testPath.IsValidOnWindows());
1954 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
1955 ATLASSERT(!testPath.IsValidOnWindows());
1956 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
1957 ATLASSERT(!testPath.IsValidOnWindows());
1961 } TGitPathTestobject;
1962 #endif
1963 #endif
1965 CTGitPath * CTGitPathList::LookForGitPath(CString path)
1967 int i=0;
1968 for(i=0;i<this->GetCount();i++)
1970 if((*this)[i].GetGitPathString() == path )
1971 return (CTGitPath*)&(*this)[i];
1973 return NULL;
1975 CString CTGitPath::GetActionName(int action)
1977 if(action & CTGitPath::LOGACTIONS_UNMERGED)
1978 return _T("Conflict");
1979 if(action & CTGitPath::LOGACTIONS_ADDED)
1980 return _T("Added");
1981 if(action & CTGitPath::LOGACTIONS_DELETED)
1982 return _T("Deleted");
1983 if(action & CTGitPath::LOGACTIONS_MERGED )
1984 return _T("Merged");
1986 if(action & CTGitPath::LOGACTIONS_MODIFIED)
1987 return _T("Modified");
1988 if(action & CTGitPath::LOGACTIONS_REPLACED)
1989 return _T("Rename");
1990 if(action & CTGitPath::LOGACTIONS_COPY)
1991 return _T("Copy");
1993 if(action & CTGitPath::LOGACTIONS_FORWORD )
1994 return _T("Forward");
1996 if(action & CTGitPath::LOGACTIONS_REBASE_EDIT)
1997 return _T("Edit");
1998 if(action & CTGitPath::LOGACTIONS_REBASE_SQUASH)
1999 return _T("Squash");
2000 if(action & CTGitPath::LOGACTIONS_REBASE_PICK)
2001 return _T("Pick");
2002 if(action & CTGitPath::LOGACTIONS_REBASE_SKIP)
2003 return _T("Skip");
2005 return _T("Unknown");
2007 CString CTGitPath::GetActionName()
2009 return GetActionName(m_Action);
2012 int CTGitPathList::GetAction()
2014 return m_Action;