initial icon overlay support using the wingit lib
[TortoiseGit.git] / src / Git / TGitPath.cpp
blob759d811b6caaca5cb3bc6a77fbea5d6fd274e3d2
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "TGitPath.h"
21 #include "UnicodeUtils.h"
22 #include "GitAdminDir.h"
23 #include "PathUtils.h"
24 #include <regex>
25 #include "git.h"
26 #if defined(_MFC_VER)
27 //#include "MessageBox.h"
28 //#include "AppUtils.h"
29 #endif
31 #ifndef ASSERT
32 #define ASSERT()
33 #endif
34 using namespace std;
35 extern CGit g_Git;
37 CTGitPath::CTGitPath(void) :
38 m_bDirectoryKnown(false),
39 m_bIsDirectory(false),
40 m_bIsURL(false),
41 m_bURLKnown(false),
42 m_bHasAdminDirKnown(false),
43 m_bHasAdminDir(false),
44 m_bIsValidOnWindowsKnown(false),
45 m_bIsReadOnly(false),
46 m_bIsAdminDirKnown(false),
47 m_bIsAdminDir(false),
48 m_bExists(false),
49 m_bExistsKnown(false),
50 m_bLastWriteTimeKnown(0),
51 m_lastWriteTime(0),
52 m_customData(NULL),
53 m_bIsSpecialDirectoryKnown(false),
54 m_bIsSpecialDirectory(false)
56 m_Action=0;
59 CTGitPath::~CTGitPath(void)
62 // Create a TGitPath object from an unknown path type (same as using SetFromUnknown)
63 CTGitPath::CTGitPath(const CString& sUnknownPath) :
64 m_bDirectoryKnown(false),
65 m_bIsDirectory(false),
66 m_bIsURL(false),
67 m_bURLKnown(false),
68 m_bHasAdminDirKnown(false),
69 m_bHasAdminDir(false),
70 m_bIsValidOnWindowsKnown(false),
71 m_bIsReadOnly(false),
72 m_bIsAdminDirKnown(false),
73 m_bIsAdminDir(false),
74 m_bExists(false),
75 m_bExistsKnown(false),
76 m_bLastWriteTimeKnown(0),
77 m_lastWriteTime(0),
78 m_customData(NULL),
79 m_bIsSpecialDirectoryKnown(false),
80 m_bIsSpecialDirectory(false)
82 SetFromUnknown(sUnknownPath);
83 m_Action=0;
84 m_Stage=0;
87 int CTGitPath::ParserAction(BYTE action)
89 //action=action.TrimLeft();
90 //TCHAR c=action.GetAt(0);
91 if(action == 'M')
92 m_Action|= LOGACTIONS_MODIFIED;
93 if(action == 'R')
94 m_Action|= LOGACTIONS_REPLACED;
95 if(action == 'A')
96 m_Action|= LOGACTIONS_ADDED;
97 if(action == 'D')
98 m_Action|= LOGACTIONS_DELETED;
99 if(action == 'U')
100 m_Action|= LOGACTIONS_UNMERGED;
101 if(action == 'K')
102 m_Action|= LOGACTIONS_DELETED;
103 if(action == 'H')
104 m_Action|= LOGACTIONS_CACHE;
105 return m_Action;
107 void CTGitPath::SetFromGit(const char* pPath)
109 Reset();
110 if (pPath == NULL)
111 return;
112 int len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, NULL, 0);
113 if (len)
115 len = MultiByteToWideChar(CP_UTF8, 0, pPath, -1, m_sFwdslashPath.GetBuffer(len+1), len+1);
116 m_sFwdslashPath.ReleaseBuffer(len-1);
118 SanitizeRootPath(m_sFwdslashPath, true);
121 void CTGitPath::SetFromGit(const char* pPath, bool bIsDirectory)
123 SetFromGit(pPath);
124 m_bDirectoryKnown = true;
125 m_bIsDirectory = bIsDirectory;
128 void CTGitPath::SetFromGit(const CString& sPath,CString *oldpath)
130 Reset();
131 m_sFwdslashPath = sPath;
132 SanitizeRootPath(m_sFwdslashPath, true);
133 if(oldpath)
134 m_sOldFwdslashPath = *oldpath;
137 void CTGitPath::SetFromWin(LPCTSTR pPath)
139 Reset();
140 m_sBackslashPath = pPath;
141 SanitizeRootPath(m_sBackslashPath, false);
142 ATLASSERT(m_sBackslashPath.Find('/')<0);
144 void CTGitPath::SetFromWin(const CString& sPath)
146 Reset();
147 m_sBackslashPath = sPath;
148 SanitizeRootPath(m_sBackslashPath, false);
150 void CTGitPath::SetFromWin(const CString& sPath, bool bIsDirectory)
152 Reset();
153 m_sBackslashPath = sPath;
154 m_bIsDirectory = bIsDirectory;
155 m_bDirectoryKnown = true;
156 SanitizeRootPath(m_sBackslashPath, false);
158 void CTGitPath::SetFromUnknown(const CString& sPath)
160 Reset();
161 // Just set whichever path we think is most likely to be used
162 // GitAdminDir admin;
163 // CString p;
164 // if(admin.HasAdminDir(sPath,&p))
165 // SetFwdslashPath(sPath.Right(sPath.GetLength()-p.GetLength()));
166 // else
167 SetFwdslashPath(sPath);
170 LPCTSTR CTGitPath::GetWinPath() const
172 if(IsEmpty())
174 return _T("");
176 if(m_sBackslashPath.IsEmpty())
178 SetBackslashPath(m_sFwdslashPath);
180 return m_sBackslashPath;
182 // This is a temporary function, to be used during the migration to
183 // the path class. Ultimately, functions consuming paths should take a CTGitPath&, not a CString
184 const CString& CTGitPath::GetWinPathString() const
186 if(m_sBackslashPath.IsEmpty())
188 SetBackslashPath(m_sFwdslashPath);
190 return m_sBackslashPath;
193 const CString& CTGitPath::GetGitPathString() const
195 if(m_sFwdslashPath.IsEmpty())
197 SetFwdslashPath(m_sBackslashPath);
199 return m_sFwdslashPath;
202 const CString &CTGitPath::GetGitOldPathString() const
204 return m_sOldFwdslashPath;
206 #if 0
207 const char* CTGitPath::GetGitApiPath(apr_pool_t *pool) const
209 // This funny-looking 'if' is to avoid a subtle problem with empty paths, whereby
210 // each call to GetGitApiPath returns a different pointer value.
211 // If you made multiple calls to GetGitApiPath on the same string, only the last
212 // one would give you a valid pointer to an empty string, because each
213 // call would invalidate the previous call's return.
214 if(IsEmpty())
216 return "";
218 if(m_sFwdslashPath.IsEmpty())
220 SetFwdslashPath(m_sBackslashPath);
222 if(m_sUTF8FwdslashPath.IsEmpty())
224 SetUTF8FwdslashPath(m_sFwdslashPath);
226 if (svn_path_is_url(m_sUTF8FwdslashPath))
228 m_sUTF8FwdslashPathEscaped = CPathUtils::PathEscape(m_sUTF8FwdslashPath);
229 m_sUTF8FwdslashPathEscaped.Replace("file:////", "file:///\\");
230 m_sUTF8FwdslashPathEscaped = svn_path_canonicalize(m_sUTF8FwdslashPathEscaped, pool);
231 return m_sUTF8FwdslashPathEscaped;
233 m_sUTF8FwdslashPath = svn_path_canonicalize(m_sUTF8FwdslashPath, pool);
235 return m_sUTF8FwdslashPath;
237 #endif
239 const CString& CTGitPath::GetUIPathString() const
241 if (m_sUIPath.IsEmpty())
243 #if defined(_MFC_VER)
244 //BUGBUG HORRIBLE!!! - CPathUtils::IsEscaped doesn't need to be MFC-only
245 if (IsUrl())
247 m_sUIPath = CPathUtils::PathUnescape(GetGitPathString());
248 m_sUIPath.Replace(_T("file:////"), _T("file:///\\"));
251 else
252 #endif
254 m_sUIPath = GetWinPathString();
257 return m_sUIPath;
260 void CTGitPath::SetFwdslashPath(const CString& sPath) const
262 m_sFwdslashPath = sPath;
263 m_sFwdslashPath.Replace('\\', '/');
265 // We don't leave a trailing /
266 m_sFwdslashPath.TrimRight('/');
268 SanitizeRootPath(m_sFwdslashPath, true);
270 m_sFwdslashPath.Replace(_T("file:////"), _T("file:///\\"));
272 m_sUTF8FwdslashPath.Empty();
275 void CTGitPath::SetBackslashPath(const CString& sPath) const
277 m_sBackslashPath = sPath;
278 m_sBackslashPath.Replace('/', '\\');
279 m_sBackslashPath.TrimRight('\\');
280 SanitizeRootPath(m_sBackslashPath, false);
283 void CTGitPath::SetUTF8FwdslashPath(const CString& sPath) const
285 m_sUTF8FwdslashPath = CUnicodeUtils::GetUTF8(sPath);
288 void CTGitPath::SanitizeRootPath(CString& sPath, bool bIsForwardPath) const
290 // Make sure to add the trailing slash to root paths such as 'C:'
291 if (sPath.GetLength() == 2 && sPath[1] == ':')
293 sPath += (bIsForwardPath) ? _T("/") : _T("\\");
297 bool CTGitPath::IsUrl() const
299 #if 0
300 if (!m_bURLKnown)
302 EnsureFwdslashPathSet();
303 if(m_sUTF8FwdslashPath.IsEmpty())
305 SetUTF8FwdslashPath(m_sFwdslashPath);
307 m_bIsURL = !!svn_path_is_url(m_sUTF8FwdslashPath);
308 m_bURLKnown = true;
310 return m_bIsURL;
311 #endif
312 return false;
315 bool CTGitPath::IsDirectory() const
317 if(!m_bDirectoryKnown)
319 UpdateAttributes();
321 return m_bIsDirectory;
324 bool CTGitPath::Exists() const
326 if (!m_bExistsKnown)
328 UpdateAttributes();
330 return m_bExists;
333 bool CTGitPath::Delete(bool bTrash) const
335 EnsureBackslashPathSet();
336 ::SetFileAttributes(m_sBackslashPath, FILE_ATTRIBUTE_NORMAL);
337 bool bRet = false;
338 if (Exists())
340 if ((bTrash)||(IsDirectory()))
342 TCHAR * buf = new TCHAR[m_sBackslashPath.GetLength()+2];
343 _tcscpy_s(buf, m_sBackslashPath.GetLength()+2, m_sBackslashPath);
344 buf[m_sBackslashPath.GetLength()] = 0;
345 buf[m_sBackslashPath.GetLength()+1] = 0;
346 SHFILEOPSTRUCT shop = {0};
347 shop.wFunc = FO_DELETE;
348 shop.pFrom = buf;
349 shop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
350 if (bTrash)
351 shop.fFlags |= FOF_ALLOWUNDO;
352 bRet = (SHFileOperation(&shop) == 0);
353 delete [] buf;
355 else
357 bRet = !!::DeleteFile(m_sBackslashPath);
360 m_bExists = false;
361 m_bExistsKnown = true;
362 return bRet;
365 __int64 CTGitPath::GetLastWriteTime() const
367 if(!m_bLastWriteTimeKnown)
369 UpdateAttributes();
371 return m_lastWriteTime;
374 bool CTGitPath::IsReadOnly() const
376 if(!m_bLastWriteTimeKnown)
378 UpdateAttributes();
380 return m_bIsReadOnly;
383 void CTGitPath::UpdateAttributes() const
385 EnsureBackslashPathSet();
386 WIN32_FILE_ATTRIBUTE_DATA attribs;
387 if(GetFileAttributesEx(m_sBackslashPath, GetFileExInfoStandard, &attribs))
389 m_bIsDirectory = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
390 m_lastWriteTime = *(__int64*)&attribs.ftLastWriteTime;
391 m_bIsReadOnly = !!(attribs.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
392 m_bExists = true;
394 else
396 DWORD err = GetLastError();
397 if ((err == ERROR_FILE_NOT_FOUND)||(err == ERROR_PATH_NOT_FOUND)||(err == ERROR_INVALID_NAME))
399 m_bIsDirectory = false;
400 m_lastWriteTime = 0;
401 m_bExists = false;
403 else
405 m_bIsDirectory = false;
406 m_lastWriteTime = 0;
407 m_bExists = true;
408 return;
411 m_bDirectoryKnown = true;
412 m_bLastWriteTimeKnown = true;
413 m_bExistsKnown = true;
417 void CTGitPath::EnsureBackslashPathSet() const
419 if(m_sBackslashPath.IsEmpty())
421 SetBackslashPath(m_sFwdslashPath);
422 ATLASSERT(IsEmpty() || !m_sBackslashPath.IsEmpty());
425 void CTGitPath::EnsureFwdslashPathSet() const
427 if(m_sFwdslashPath.IsEmpty())
429 SetFwdslashPath(m_sBackslashPath);
430 ATLASSERT(IsEmpty() || !m_sFwdslashPath.IsEmpty());
435 // Reset all the caches
436 void CTGitPath::Reset()
438 m_bDirectoryKnown = false;
439 m_bURLKnown = false;
440 m_bLastWriteTimeKnown = false;
441 m_bHasAdminDirKnown = false;
442 m_bIsValidOnWindowsKnown = false;
443 m_bIsAdminDirKnown = false;
444 m_bExistsKnown = false;
445 m_bIsSpecialDirectoryKnown = false;
446 m_bIsSpecialDirectory = false;
448 m_sBackslashPath.Empty();
449 m_sFwdslashPath.Empty();
450 m_sUTF8FwdslashPath.Empty();
451 this->m_Action=0;
452 this->m_StatAdd=_T("");
453 this->m_StatDel=_T("");
454 ATLASSERT(IsEmpty());
457 CTGitPath CTGitPath::GetDirectory() const
459 if ((IsDirectory())||(!Exists()))
461 return *this;
463 return GetContainingDirectory();
466 CTGitPath CTGitPath::GetContainingDirectory() const
468 EnsureBackslashPathSet();
470 CString sDirName = m_sBackslashPath.Left(m_sBackslashPath.ReverseFind('\\'));
471 if(sDirName.GetLength() == 2 && sDirName[1] == ':')
473 // This is a root directory, which needs a trailing slash
474 sDirName += '\\';
475 if(sDirName == m_sBackslashPath)
477 // We were clearly provided with a root path to start with - we should return nothing now
478 sDirName.Empty();
481 if(sDirName.GetLength() == 1 && sDirName[0] == '\\')
483 // We have an UNC path and we already are the root
484 sDirName.Empty();
486 CTGitPath retVal;
487 retVal.SetFromWin(sDirName);
488 return retVal;
491 CString CTGitPath::GetRootPathString() const
493 EnsureBackslashPathSet();
494 CString workingPath = m_sBackslashPath;
495 LPTSTR pPath = workingPath.GetBuffer(MAX_PATH); // MAX_PATH ok here.
496 ATLVERIFY(::PathStripToRoot(pPath));
497 workingPath.ReleaseBuffer();
498 return workingPath;
502 CString CTGitPath::GetFilename() const
504 ATLASSERT(!IsDirectory());
505 return GetFileOrDirectoryName();
508 CString CTGitPath::GetFileOrDirectoryName() const
510 EnsureBackslashPathSet();
511 return m_sBackslashPath.Mid(m_sBackslashPath.ReverseFind('\\')+1);
514 CString CTGitPath::GetUIFileOrDirectoryName() const
516 GetUIPathString();
517 return m_sUIPath.Mid(m_sUIPath.ReverseFind('\\')+1);
520 CString CTGitPath::GetFileExtension() const
522 if(!IsDirectory())
524 EnsureBackslashPathSet();
525 int dotPos = m_sBackslashPath.ReverseFind('.');
526 int slashPos = m_sBackslashPath.ReverseFind('\\');
527 if (dotPos > slashPos)
528 return m_sBackslashPath.Mid(dotPos);
530 return CString();
532 CString CTGitPath::GetBaseFilename() const
534 int dot;
535 CString filename=GetFilename();
536 dot = filename.ReverseFind(_T('.'));
537 if(dot>0)
538 return filename.Left(dot-1);
539 else
540 return filename;
543 bool CTGitPath::ArePathStringsEqual(const CString& sP1, const CString& sP2)
545 int length = sP1.GetLength();
546 if(length != sP2.GetLength())
548 // Different lengths
549 return false;
551 // We work from the end of the strings, because path differences
552 // are more likely to occur at the far end of a string
553 LPCTSTR pP1Start = sP1;
554 LPCTSTR pP1 = pP1Start+(length-1);
555 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
556 while(length-- > 0)
558 if(_totlower(*pP1--) != _totlower(*pP2--))
560 return false;
563 return true;
566 bool CTGitPath::ArePathStringsEqualWithCase(const CString& sP1, const CString& sP2)
568 int length = sP1.GetLength();
569 if(length != sP2.GetLength())
571 // Different lengths
572 return false;
574 // We work from the end of the strings, because path differences
575 // are more likely to occur at the far end of a string
576 LPCTSTR pP1Start = sP1;
577 LPCTSTR pP1 = pP1Start+(length-1);
578 LPCTSTR pP2 = ((LPCTSTR)sP2)+(length-1);
579 while(length-- > 0)
581 if((*pP1--) != (*pP2--))
583 return false;
586 return true;
589 bool CTGitPath::IsEmpty() const
591 // Check the backward slash path first, since the chance that this
592 // one is set is higher. In case of a 'false' return value it's a little
593 // bit faster.
594 return m_sBackslashPath.IsEmpty() && m_sFwdslashPath.IsEmpty();
597 // Test if both paths refer to the same item
598 // Ignores case and slash direction
599 bool CTGitPath::IsEquivalentTo(const CTGitPath& rhs) const
601 // Try and find a slash direction which avoids having to convert
602 // both filenames
603 if(!m_sBackslashPath.IsEmpty())
605 // *We've* got a \ path - make sure that the RHS also has a \ path
606 rhs.EnsureBackslashPathSet();
607 return ArePathStringsEqualWithCase(m_sBackslashPath, rhs.m_sBackslashPath);
609 else
611 // Assume we've got a fwdslash path and make sure that the RHS has one
612 rhs.EnsureFwdslashPathSet();
613 return ArePathStringsEqualWithCase(m_sFwdslashPath, rhs.m_sFwdslashPath);
617 bool CTGitPath::IsEquivalentToWithoutCase(const CTGitPath& rhs) const
619 // Try and find a slash direction which avoids having to convert
620 // both filenames
621 if(!m_sBackslashPath.IsEmpty())
623 // *We've* got a \ path - make sure that the RHS also has a \ path
624 rhs.EnsureBackslashPathSet();
625 return ArePathStringsEqual(m_sBackslashPath, rhs.m_sBackslashPath);
627 else
629 // Assume we've got a fwdslash path and make sure that the RHS has one
630 rhs.EnsureFwdslashPathSet();
631 return ArePathStringsEqual(m_sFwdslashPath, rhs.m_sFwdslashPath);
635 bool CTGitPath::IsAncestorOf(const CTGitPath& possibleDescendant) const
637 possibleDescendant.EnsureBackslashPathSet();
638 EnsureBackslashPathSet();
640 bool bPathStringsEqual = ArePathStringsEqual(m_sBackslashPath, possibleDescendant.m_sBackslashPath.Left(m_sBackslashPath.GetLength()));
641 if (m_sBackslashPath.GetLength() >= possibleDescendant.GetWinPathString().GetLength())
643 return bPathStringsEqual;
646 return (bPathStringsEqual &&
647 ((possibleDescendant.m_sBackslashPath[m_sBackslashPath.GetLength()] == '\\')||
648 (m_sBackslashPath.GetLength()==3 && m_sBackslashPath[1]==':')));
651 // Get a string representing the file path, optionally with a base
652 // section stripped off the front.
653 CString CTGitPath::GetDisplayString(const CTGitPath* pOptionalBasePath /* = NULL*/) const
655 EnsureFwdslashPathSet();
656 if(pOptionalBasePath != NULL)
658 // Find the length of the base-path without having to do an 'ensure' on it
659 int baseLength = max(pOptionalBasePath->m_sBackslashPath.GetLength(), pOptionalBasePath->m_sFwdslashPath.GetLength());
661 // Now, chop that baseLength of the front of the path
662 return m_sFwdslashPath.Mid(baseLength).TrimLeft('/');
664 return m_sFwdslashPath;
667 int CTGitPath::Compare(const CTGitPath& left, const CTGitPath& right)
669 left.EnsureBackslashPathSet();
670 right.EnsureBackslashPathSet();
671 return left.m_sBackslashPath.CompareNoCase(right.m_sBackslashPath);
674 bool operator<(const CTGitPath& left, const CTGitPath& right)
676 return CTGitPath::Compare(left, right) < 0;
679 bool CTGitPath::PredLeftEquivalentToRight(const CTGitPath& left, const CTGitPath& right)
681 return left.IsEquivalentTo(right);
684 bool CTGitPath::PredLeftSameWCPathAsRight(const CTGitPath& left, const CTGitPath& right)
686 if (left.IsAdminDir() && right.IsAdminDir())
688 CTGitPath l = left;
689 CTGitPath r = right;
692 l = l.GetContainingDirectory();
693 } while(l.HasAdminDir());
696 r = r.GetContainingDirectory();
697 } while(r.HasAdminDir());
698 return l.GetContainingDirectory().IsEquivalentTo(r.GetContainingDirectory());
700 return left.GetDirectory().IsEquivalentTo(right.GetDirectory());
703 bool CTGitPath::CheckChild(const CTGitPath &parent, const CTGitPath& child)
705 return parent.IsAncestorOf(child);
708 void CTGitPath::AppendRawString(const CString& sAppend)
710 EnsureFwdslashPathSet();
711 CString strCopy = m_sFwdslashPath += sAppend;
712 SetFromUnknown(strCopy);
715 void CTGitPath::AppendPathString(const CString& sAppend)
717 EnsureBackslashPathSet();
718 CString cleanAppend(sAppend);
719 cleanAppend.Replace('/', '\\');
720 cleanAppend.TrimLeft('\\');
721 m_sBackslashPath.TrimRight('\\');
722 CString strCopy = m_sBackslashPath + _T("\\") + cleanAppend;
723 SetFromWin(strCopy);
726 bool CTGitPath::HasAdminDir() const
728 if (m_bHasAdminDirKnown)
729 return m_bHasAdminDir;
731 EnsureBackslashPathSet();
732 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
733 m_bHasAdminDirKnown = true;
734 return m_bHasAdminDir;
737 bool CTGitPath::HasAdminDir(CString *ProjectTopDir) const
739 if (m_bHasAdminDirKnown)
741 if (ProjectTopDir)
742 *ProjectTopDir = m_sProjectRoot;
743 return m_bHasAdminDir;
746 EnsureBackslashPathSet();
747 m_bHasAdminDir = g_GitAdminDir.HasAdminDir(m_sBackslashPath, IsDirectory(), &m_sProjectRoot);
748 m_bHasAdminDirKnown = true;
749 if (ProjectTopDir)
750 *ProjectTopDir = m_sProjectRoot;
751 return m_bHasAdminDir;
754 bool CTGitPath::IsAdminDir() const
756 if (m_bIsAdminDirKnown)
757 return m_bIsAdminDir;
759 EnsureBackslashPathSet();
760 m_bIsAdminDir = g_GitAdminDir.IsAdminDirPath(m_sBackslashPath);
761 m_bIsAdminDirKnown = true;
762 return m_bIsAdminDir;
765 bool CTGitPath::IsValidOnWindows() const
767 if (m_bIsValidOnWindowsKnown)
768 return m_bIsValidOnWindows;
770 m_bIsValidOnWindows = false;
771 EnsureBackslashPathSet();
772 CString sMatch = m_sBackslashPath + _T("\r\n");
773 wstring sPattern;
774 // the 'file://' URL is just a normal windows path:
775 if (sMatch.Left(7).CompareNoCase(_T("file:\\\\"))==0)
777 sMatch = sMatch.Mid(7);
778 sMatch.TrimLeft(_T("\\"));
779 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
781 else if (IsUrl())
783 sPattern = _T("^((http|https|svn|svn\\+ssh|file)\\:\\\\+([^\\\\@\\:]+\\:[^\\\\@\\:]+@)?\\\\[^\\\\]+(\\:\\d+)?)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<>\\. ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
785 else
787 sPattern = _T("^(\\\\\\\\\\?\\\\)?(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?\"\\|<> ](([^\\\\/:\\*\\?\"\\|<>\\. ])|([^\\\\/:\\*\\?\"\\|<>]*[^\\\\/:\\*\\?\"\\|<>\\. ]))?$");
792 tr1::wregex rx(sPattern, tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
793 tr1::wsmatch match;
795 wstring rmatch = wstring((LPCTSTR)sMatch);
796 if (tr1::regex_match(rmatch, match, rx))
798 if (wstring(match[0]).compare(sMatch)==0)
799 m_bIsValidOnWindows = true;
801 if (m_bIsValidOnWindows)
803 // now check for illegal filenames
804 tr1::wregex rx2(_T("\\\\(lpt\\d|com\\d|aux|nul|prn|con)(\\\\|$)"), tr1::regex_constants::icase | tr1::regex_constants::ECMAScript);
805 rmatch = m_sBackslashPath;
806 if (tr1::regex_search(rmatch, rx2, tr1::regex_constants::match_default))
807 m_bIsValidOnWindows = false;
810 catch (exception) {}
812 m_bIsValidOnWindowsKnown = true;
813 return m_bIsValidOnWindows;
816 bool CTGitPath::IsSpecialDirectory() const
818 if (m_bIsSpecialDirectoryKnown)
819 return m_bIsSpecialDirectory;
821 static LPCTSTR specialDirectories[]
822 = { _T("trunk"), _T("tags"), _T("branches") };
824 for (int i=0 ; i<(sizeof(specialDirectories) / sizeof(specialDirectories[0])) ; ++i)
826 CString name = GetFileOrDirectoryName();
827 if (0 == name.CompareNoCase(specialDirectories[i]))
829 m_bIsSpecialDirectory = true;
830 break;
834 m_bIsSpecialDirectoryKnown = true;
836 return m_bIsSpecialDirectory;
839 //////////////////////////////////////////////////////////////////////////
841 CTGitPathList::CTGitPathList()
846 // A constructor which allows a path list to be easily built which one initial entry in
847 CTGitPathList::CTGitPathList(const CTGitPath& firstEntry)
849 AddPath(firstEntry);
851 int CTGitPathList::ParserFromLsFile(BYTE_VECTOR &out,bool staged)
853 int pos=0;
854 CString one;
855 CTGitPath path;
856 CString part;
857 while(pos>=0 && pos<out.size())
859 one.Empty();
860 path.Reset();
862 g_Git.StringAppend(&one,&out[pos],CP_OEMCP);
863 int tabstart=0;
864 path.m_Action=path.ParserAction(out[pos]);
865 one.Tokenize(_T("\t"),tabstart);
867 if(tabstart>=0)
868 path.SetFromGit(one.Right(one.GetLength()-tabstart));
870 tabstart=0;
872 part=one.Tokenize(_T(" "),tabstart); //Tag
874 part=one.Tokenize(_T(" "),tabstart); //Mode
876 part=one.Tokenize(_T(" "),tabstart); //Hash
878 part=one.Tokenize(_T("\t"),tabstart); //Stage
880 path.m_Stage=_ttol(part);
882 this->AddPath(path);
884 pos=out.findNextString(pos);
886 return pos;
888 int CTGitPathList::FillUnRev(int action,CTGitPathList *list)
890 int pos=0;
891 this->Clear();
892 CTGitPath path;
894 int count;
895 if(list==NULL)
896 count=1;
897 else
898 count=list->GetCount();
899 for(int i=0;i<count;i++)
901 CString cmd;
902 pos=0;
904 CString ignored;
905 if(action & CTGitPath::LOGACTIONS_IGNORE)
906 ignored= _T(" --ignored");
908 if(list==NULL)
910 cmd=_T("git.exe ls-files --exclude-standard --full-name --others -z");
911 cmd+=ignored;
914 else
915 { cmd.Format(_T("git.exe ls-files --exclude-standard --full-name --others -z %s-- \"%s\""),
916 ignored,
917 (*list)[i].GetWinPathString());
920 BYTE_VECTOR out;
921 out.clear();
922 g_Git.Run(cmd,&out);
924 pos=0;
925 CString one;
926 while( pos>=0 && pos<out.size())
928 g_Git.StringAppend(&one,&out[pos],CP_OEMCP);
929 if(!one.IsEmpty())
931 //SetFromGit will clear all status
932 path.SetFromGit(one);
933 path.m_Action=action;
934 AddPath(path);
936 pos=out.findNextString(pos);
940 return 0;
942 int CTGitPathList::ParserFromLog(BYTE_VECTOR &log)
944 this->Clear();
945 int pos=0;
946 //BYTE *p=&log[0];
947 //CString one;
948 CTGitPath path;
949 m_Action=0;
950 while( pos>=0 && pos<log.size())
952 //one=log.Tokenize(_T("\n"),pos);
953 path.Reset();
954 if(log[pos]=='\n')
955 pos++;
957 if(log[pos]==':')
959 int end=log.find(0,pos);
960 int actionstart=-1;
961 int numfile=1;
962 int file1=-1,file2=-1;
963 if( end>0 )
965 actionstart=log.find(' ',end-6);
966 pos=actionstart;
968 if( actionstart>0 )
970 actionstart++;
972 file1 = log.find(0,actionstart);
973 if( file1>=0 )
975 file1++;
976 pos=file1;
978 if( log[actionstart] == 'C' || log[actionstart] == 'R' )
980 file2=file1;
981 numfile=2;
982 file1 = log.find(0,file1);
983 if(file1>=0 )
985 file1++;
986 pos=file1;
992 CString pathname1;
993 CString pathname2;
995 if( file1>=0 )
996 g_Git.StringAppend(&pathname1,&log[file1],CP_OEMCP);
997 if( file2>=0 )
998 g_Git.StringAppend(&pathname2,&log[file2],CP_OEMCP);
1000 CTGitPath *GitPath=LookForGitPath(pathname1);
1002 if(GitPath)
1004 this->m_Action|=GitPath->ParserAction( log[actionstart] );
1006 }else
1008 int ac=path.ParserAction(log[actionstart] );
1010 path.SetFromGit(pathname1,&pathname2);
1011 path.m_Action=ac;
1012 //action must be set after setfromgit. SetFromGit will clear all status.
1013 this->m_Action|=ac;
1014 AddPath(path);
1018 }else
1020 int tabstart=0;
1021 path.Reset();
1022 CString StatAdd;
1023 CString StatDel;
1024 CString file1;
1025 CString file2;
1027 tabstart=log.find('\t',pos);
1028 if(tabstart >=0)
1030 log[tabstart]=0;
1031 g_Git.StringAppend(&StatAdd,&log[pos],CP_UTF8);
1032 pos=tabstart+1;
1035 tabstart=log.find('\t',pos);
1036 if(tabstart >=0)
1038 log[tabstart]=0;
1040 g_Git.StringAppend(&StatDel,&log[pos],CP_UTF8);
1041 pos=tabstart+1;
1044 if(log[pos] == 0) //rename
1046 pos++;
1047 g_Git.StringAppend(&file2,&log[pos],CP_OEMCP);
1048 int sec=log.find(0,pos);
1049 if(sec>=0)
1051 sec++;
1052 g_Git.StringAppend(&file1,&log[sec],CP_OEMCP);
1055 }else
1057 g_Git.StringAppend(&file1,&log[pos],CP_OEMCP);
1059 path.SetFromGit(file1,&file2);
1061 CTGitPath *GitPath=LookForGitPath(path.GetGitPathString());
1062 if(GitPath)
1064 GitPath->m_StatAdd=StatAdd;
1065 GitPath->m_StatDel=StatDel;
1066 }else
1068 //path.SetFromGit(pathname);
1069 path.m_StatAdd=StatAdd;
1070 path.m_StatDel=StatDel;
1071 AddPath(path);
1075 pos=log.findNextString(pos);
1077 return pos;
1080 void CTGitPathList::AddPath(const CTGitPath& newPath)
1082 m_paths.push_back(newPath);
1083 m_commonBaseDirectory.Reset();
1085 int CTGitPathList::GetCount() const
1087 return (int)m_paths.size();
1089 void CTGitPathList::Clear()
1091 m_paths.clear();
1092 m_commonBaseDirectory.Reset();
1095 const CTGitPath& CTGitPathList::operator[](INT_PTR index) const
1097 ATLASSERT(index >= 0 && index < (INT_PTR)m_paths.size());
1098 return m_paths[index];
1101 bool CTGitPathList::AreAllPathsFiles() const
1103 // Look through the vector for any directories - if we find them, return false
1104 return std::find_if(m_paths.begin(), m_paths.end(), std::mem_fun_ref(&CTGitPath::IsDirectory)) == m_paths.end();
1108 #if defined(_MFC_VER)
1110 bool CTGitPathList::LoadFromFile(const CTGitPath& filename)
1112 Clear();
1115 CString strLine;
1116 CStdioFile file(filename.GetWinPath(), CFile::typeBinary | CFile::modeRead | CFile::shareDenyWrite);
1118 // for every selected file/folder
1119 CTGitPath path;
1120 while (file.ReadString(strLine))
1122 path.SetFromUnknown(strLine);
1123 AddPath(path);
1125 file.Close();
1127 catch (CFileException* pE)
1129 TRACE("CFileException loading target file list\n");
1130 TCHAR error[10000] = {0};
1131 pE->GetErrorMessage(error, 10000);
1132 // CMessageBox::Show(NULL, error, _T("TortoiseGit"), MB_ICONERROR);
1133 pE->Delete();
1134 return false;
1136 return true;
1139 bool CTGitPathList::WriteToFile(const CString& sFilename, bool bANSI /* = false */) const
1143 if (bANSI)
1145 CStdioFile file(sFilename, CFile::typeText | CFile::modeReadWrite | CFile::modeCreate);
1146 PathVector::const_iterator it;
1147 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1149 CStringA line = CStringA(it->GetGitPathString()) + '\n';
1150 file.Write(line, line.GetLength());
1152 file.Close();
1154 else
1156 CStdioFile file(sFilename, CFile::typeBinary | CFile::modeReadWrite | CFile::modeCreate);
1157 PathVector::const_iterator it;
1158 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1160 file.WriteString(it->GetGitPathString()+_T("\n"));
1162 file.Close();
1165 catch (CFileException* pE)
1167 TRACE("CFileException in writing temp file\n");
1168 pE->Delete();
1169 return false;
1171 return true;
1175 void CTGitPathList::LoadFromAsteriskSeparatedString(const CString& sPathString)
1177 int pos = 0;
1178 CString temp;
1179 for(;;)
1181 temp = sPathString.Tokenize(_T("*"),pos);
1182 if(temp.IsEmpty())
1184 break;
1186 AddPath(CTGitPath(CPathUtils::GetLongPathname(temp)));
1190 CString CTGitPathList::CreateAsteriskSeparatedString() const
1192 CString sRet;
1193 PathVector::const_iterator it;
1194 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1196 if (!sRet.IsEmpty())
1197 sRet += _T("*");
1198 sRet += it->GetWinPathString();
1200 return sRet;
1202 #endif // _MFC_VER
1204 bool
1205 CTGitPathList::AreAllPathsFilesInOneDirectory() const
1207 // Check if all the paths are files and in the same directory
1208 PathVector::const_iterator it;
1209 m_commonBaseDirectory.Reset();
1210 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1212 if(it->IsDirectory())
1214 return false;
1216 const CTGitPath& baseDirectory = it->GetDirectory();
1217 if(m_commonBaseDirectory.IsEmpty())
1219 m_commonBaseDirectory = baseDirectory;
1221 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1223 // Different path
1224 m_commonBaseDirectory.Reset();
1225 return false;
1228 return true;
1231 CTGitPath CTGitPathList::GetCommonDirectory() const
1233 if (m_commonBaseDirectory.IsEmpty())
1235 PathVector::const_iterator it;
1236 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1238 const CTGitPath& baseDirectory = it->GetDirectory();
1239 if(m_commonBaseDirectory.IsEmpty())
1241 m_commonBaseDirectory = baseDirectory;
1243 else if(!m_commonBaseDirectory.IsEquivalentTo(baseDirectory))
1245 // Different path
1246 m_commonBaseDirectory.Reset();
1247 break;
1251 // since we only checked strings, not paths,
1252 // we have to make sure now that we really return a *path* here
1253 PathVector::const_iterator iter;
1254 for(iter = m_paths.begin(); iter != m_paths.end(); ++iter)
1256 if (!m_commonBaseDirectory.IsAncestorOf(*iter))
1258 m_commonBaseDirectory = m_commonBaseDirectory.GetContainingDirectory();
1259 break;
1262 return m_commonBaseDirectory;
1265 CTGitPath CTGitPathList::GetCommonRoot() const
1267 PathVector::const_iterator it;
1268 CString sRoot, sTempRoot;
1269 bool bEqual = true;
1271 if (GetCount() == 1)
1272 return m_paths[0];
1274 int backSlashPos = 0;
1275 int searchStartPos = 0;
1276 while (bEqual)
1278 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1280 if (backSlashPos == 0)
1282 backSlashPos = it->GetWinPathString().Find('\\', searchStartPos+1);
1283 if ((backSlashPos < 0)&&(searchStartPos != it->GetWinPathString().GetLength()))
1284 backSlashPos = it->GetWinPathString().GetLength();
1286 else if (it->GetWinPathString().Find('\\', searchStartPos+1) != backSlashPos)
1288 if (it->GetWinPathString().Find('\\', searchStartPos+1) < 0)
1290 if (it->GetWinPathString().GetLength() != backSlashPos)
1292 bEqual = false;
1293 break;
1296 else
1298 bEqual = false;
1299 break;
1302 if (backSlashPos < 0)
1304 bEqual = false;
1305 break;
1308 if (bEqual == false)
1310 if (searchStartPos)
1311 sRoot = m_paths[0].GetWinPathString().Left(searchStartPos+1);
1313 else
1315 searchStartPos = backSlashPos;
1317 backSlashPos = 0;
1320 return CTGitPath(sRoot.TrimRight('\\'));
1323 void CTGitPathList::SortByPathname(bool bReverse /*= false*/)
1325 std::sort(m_paths.begin(), m_paths.end());
1326 if (bReverse)
1327 std::reverse(m_paths.begin(), m_paths.end());
1330 void CTGitPathList::DeleteAllFiles(bool bTrash)
1332 PathVector::const_iterator it;
1333 if (bTrash)
1335 SortByPathname();
1336 CString sPaths;
1337 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1339 if ((it->Exists())&&(!it->IsDirectory()))
1341 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1342 sPaths += it->GetWinPath();
1343 sPaths += '\0';
1346 sPaths += '\0';
1347 sPaths += '\0';
1348 SHFILEOPSTRUCT shop = {0};
1349 shop.wFunc = FO_DELETE;
1350 shop.pFrom = (LPCTSTR)sPaths;
1351 shop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
1352 SHFileOperation(&shop);
1354 else
1356 for (it = m_paths.begin(); it != m_paths.end(); ++it)
1358 if (!it->IsDirectory())
1360 ::SetFileAttributes(it->GetWinPath(), FILE_ATTRIBUTE_NORMAL);
1361 ::DeleteFile(it->GetWinPath());
1365 Clear();
1368 void CTGitPathList::RemoveDuplicates()
1370 SortByPathname();
1371 // Remove the duplicates
1372 // (Unique moves them to the end of the vector, then erase chops them off)
1373 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::PredLeftEquivalentToRight), m_paths.end());
1376 void CTGitPathList::RemoveAdminPaths()
1378 PathVector::iterator it;
1379 for(it = m_paths.begin(); it != m_paths.end(); )
1381 if (it->IsAdminDir())
1383 m_paths.erase(it);
1384 it = m_paths.begin();
1386 else
1387 ++it;
1391 void CTGitPathList::RemovePath(const CTGitPath& path)
1393 PathVector::iterator it;
1394 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1396 if (it->IsEquivalentTo(path))
1398 m_paths.erase(it);
1399 return;
1404 void CTGitPathList::RemoveItem(CTGitPath & path)
1406 PathVector::iterator it;
1407 for(it = m_paths.begin(); it != m_paths.end(); ++it)
1409 if (it->GetGitPathString()==path.GetGitPathString())
1411 m_paths.erase(it);
1412 return;
1416 void CTGitPathList::RemoveChildren()
1418 SortByPathname();
1419 m_paths.erase(std::unique(m_paths.begin(), m_paths.end(), &CTGitPath::CheckChild), m_paths.end());
1422 bool CTGitPathList::IsEqual(const CTGitPathList& list)
1424 if (list.GetCount() != GetCount())
1425 return false;
1426 for (int i=0; i<list.GetCount(); ++i)
1428 if (!list[i].IsEquivalentTo(m_paths[i]))
1429 return false;
1431 return true;
1434 //////////////////////////////////////////////////////////////////////////
1435 #if 0
1436 apr_array_header_t * CTGitPathList::MakePathArray (apr_pool_t *pool) const
1438 apr_array_header_t *targets = apr_array_make (pool, GetCount(), sizeof(const char *));
1440 for(int nItem = 0; nItem < GetCount(); nItem++)
1442 const char * target = m_paths[nItem].GetGitApiPath(pool);
1443 (*((const char **) apr_array_push (targets))) = target;
1446 return targets;
1448 #endif
1449 //////////////////////////////////////////////////////////////////////////
1451 #if 0
1452 #if defined(_DEBUG)
1453 // Some test cases for these classes
1454 static class CTGitPathTests
1456 public:
1457 CTGitPathTests()
1459 apr_initialize();
1460 pool = svn_pool_create(NULL);
1461 GetDirectoryTest();
1462 AdminDirTest();
1463 SortTest();
1464 RawAppendTest();
1465 PathAppendTest();
1466 RemoveDuplicatesTest();
1467 RemoveChildrenTest();
1468 ContainingDirectoryTest();
1469 AncestorTest();
1470 SubversionPathTest();
1471 GetCommonRootTest();
1472 #if defined(_MFC_VER)
1473 ValidPathAndUrlTest();
1474 ListLoadingTest();
1475 #endif
1476 apr_terminate();
1479 private:
1480 // apr_pool_t * pool;
1481 void GetDirectoryTest()
1483 // Bit tricky, this test, because we need to know something about the file
1484 // layout on the machine which is running the test
1485 TCHAR winDir[MAX_PATH+1];
1486 GetWindowsDirectory(winDir, MAX_PATH);
1487 CString sWinDir(winDir);
1489 CTGitPath testPath;
1490 // This is a file which we know will always be there
1491 testPath.SetFromUnknown(sWinDir + _T("\\win.ini"));
1492 ATLASSERT(!testPath.IsDirectory());
1493 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1494 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() == sWinDir);
1496 // Now do the test on the win directory itself - It's hard to be sure about the containing directory
1497 // but we know it must be different to the directory itself
1498 testPath.SetFromUnknown(sWinDir);
1499 ATLASSERT(testPath.IsDirectory());
1500 ATLASSERT(testPath.GetDirectory().GetWinPathString() == sWinDir);
1501 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString() != sWinDir);
1502 ATLASSERT(testPath.GetContainingDirectory().GetWinPathString().GetLength() < sWinDir.GetLength());
1504 // Try a root path
1505 testPath.SetFromUnknown(_T("C:\\"));
1506 ATLASSERT(testPath.IsDirectory());
1507 ATLASSERT(testPath.GetDirectory().GetWinPathString().CompareNoCase(_T("C:\\"))==0);
1508 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1509 // Try a root UNC path
1510 testPath.SetFromUnknown(_T("\\MYSTATION"));
1511 ATLASSERT(testPath.GetContainingDirectory().IsEmpty());
1514 void AdminDirTest()
1516 CTGitPath testPath;
1517 testPath.SetFromUnknown(_T("c:\\.svndir"));
1518 ATLASSERT(!testPath.IsAdminDir());
1519 testPath.SetFromUnknown(_T("c:\\test.svn"));
1520 ATLASSERT(!testPath.IsAdminDir());
1521 testPath.SetFromUnknown(_T("c:\\.svn"));
1522 ATLASSERT(testPath.IsAdminDir());
1523 testPath.SetFromUnknown(_T("c:\\.svndir\\test"));
1524 ATLASSERT(!testPath.IsAdminDir());
1525 testPath.SetFromUnknown(_T("c:\\.svn\\test"));
1526 ATLASSERT(testPath.IsAdminDir());
1528 CTGitPathList pathList;
1529 pathList.AddPath(CTGitPath(_T("c:\\.svndir")));
1530 pathList.AddPath(CTGitPath(_T("c:\\.svn")));
1531 pathList.AddPath(CTGitPath(_T("c:\\.svn\\test")));
1532 pathList.AddPath(CTGitPath(_T("c:\\test")));
1533 pathList.RemoveAdminPaths();
1534 ATLASSERT(pathList.GetCount()==2);
1535 pathList.Clear();
1536 pathList.AddPath(CTGitPath(_T("c:\\test")));
1537 pathList.RemoveAdminPaths();
1538 ATLASSERT(pathList.GetCount()==1);
1541 void SortTest()
1543 CTGitPathList testList;
1544 CTGitPath testPath;
1545 testPath.SetFromUnknown(_T("c:/Z"));
1546 testList.AddPath(testPath);
1547 testPath.SetFromUnknown(_T("c:/B"));
1548 testList.AddPath(testPath);
1549 testPath.SetFromUnknown(_T("c:\\a"));
1550 testList.AddPath(testPath);
1551 testPath.SetFromUnknown(_T("c:/Test"));
1552 testList.AddPath(testPath);
1554 testList.SortByPathname();
1556 ATLASSERT(testList[0].GetWinPathString() == _T("c:\\a"));
1557 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\B"));
1558 ATLASSERT(testList[2].GetWinPathString() == _T("c:\\Test"));
1559 ATLASSERT(testList[3].GetWinPathString() == _T("c:\\Z"));
1562 void RawAppendTest()
1564 CTGitPath testPath(_T("c:/test/"));
1565 testPath.AppendRawString(_T("/Hello"));
1566 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1568 testPath.AppendRawString(_T("\\T2"));
1569 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1571 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1572 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1573 testBasePath.AppendRawString(testFilePath.GetFileExtension());
1574 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt.ini"));
1577 void PathAppendTest()
1579 CTGitPath testPath(_T("c:/test/"));
1580 testPath.AppendPathString(_T("/Hello"));
1581 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello"));
1583 testPath.AppendPathString(_T("T2"));
1584 ATLASSERT(testPath.GetWinPathString() == _T("c:\\test\\Hello\\T2"));
1586 CTGitPath testFilePath(_T("C:\\windows\\win.ini"));
1587 CTGitPath testBasePath(_T("c:/temp/myfile.txt"));
1588 // You wouldn't want to do this in real life - you'd use append-raw
1589 testBasePath.AppendPathString(testFilePath.GetFileExtension());
1590 ATLASSERT(testBasePath.GetWinPathString() == _T("c:\\temp\\myfile.txt\\.ini"));
1593 void RemoveDuplicatesTest()
1595 CTGitPathList list;
1596 list.AddPath(CTGitPath(_T("Z")));
1597 list.AddPath(CTGitPath(_T("A")));
1598 list.AddPath(CTGitPath(_T("E")));
1599 list.AddPath(CTGitPath(_T("E")));
1601 ATLASSERT(list[2].IsEquivalentTo(list[3]));
1602 ATLASSERT(list[2]==list[3]);
1604 ATLASSERT(list.GetCount() == 4);
1606 list.RemoveDuplicates();
1608 ATLASSERT(list.GetCount() == 3);
1610 ATLASSERT(list[0].GetWinPathString() == _T("A"));
1611 ATLASSERT(list[1].GetWinPathString().Compare(_T("E")) == 0);
1612 ATLASSERT(list[2].GetWinPathString() == _T("Z"));
1615 void RemoveChildrenTest()
1617 CTGitPathList list;
1618 list.AddPath(CTGitPath(_T("c:\\test")));
1619 list.AddPath(CTGitPath(_T("c:\\test\\file")));
1620 list.AddPath(CTGitPath(_T("c:\\testfile")));
1621 list.AddPath(CTGitPath(_T("c:\\parent")));
1622 list.AddPath(CTGitPath(_T("c:\\parent\\child")));
1623 list.AddPath(CTGitPath(_T("c:\\parent\\child1")));
1624 list.AddPath(CTGitPath(_T("c:\\parent\\child2")));
1626 ATLASSERT(list.GetCount() == 7);
1628 list.RemoveChildren();
1630 ATLTRACE("count = %d\n", list.GetCount());
1631 ATLASSERT(list.GetCount() == 3);
1633 list.SortByPathname();
1635 ATLASSERT(list[0].GetWinPathString().Compare(_T("c:\\parent")) == 0);
1636 ATLASSERT(list[1].GetWinPathString().Compare(_T("c:\\test")) == 0);
1637 ATLASSERT(list[2].GetWinPathString().Compare(_T("c:\\testfile")) == 0);
1640 #if defined(_MFC_VER)
1641 void ListLoadingTest()
1643 TCHAR buf[MAX_PATH];
1644 GetCurrentDirectory(MAX_PATH, buf);
1645 CString sPathList(_T("Path1*c:\\path2 with spaces and stuff*\\funnypath\\*"));
1646 CTGitPathList testList;
1647 testList.LoadFromAsteriskSeparatedString(sPathList);
1649 ATLASSERT(testList.GetCount() == 3);
1650 ATLASSERT(testList[0].GetWinPathString() == CString(buf) + _T("\\Path1"));
1651 ATLASSERT(testList[1].GetWinPathString() == _T("c:\\path2 with spaces and stuff"));
1652 ATLASSERT(testList[2].GetWinPathString() == _T("\\funnypath"));
1654 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T(""));
1655 testList.Clear();
1656 sPathList = _T("c:\\path2 with spaces and stuff*c:\\funnypath\\*");
1657 testList.LoadFromAsteriskSeparatedString(sPathList);
1658 ATLASSERT(testList.GetCommonRoot().GetWinPathString() == _T("c:\\"));
1660 #endif
1662 void ContainingDirectoryTest()
1665 CTGitPath testPath;
1666 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1667 CTGitPath dir;
1668 dir = testPath.GetContainingDirectory();
1669 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c\\d"));
1670 dir = dir.GetContainingDirectory();
1671 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b\\c"));
1672 dir = dir.GetContainingDirectory();
1673 ATLASSERT(dir.GetWinPathString() == _T("c:\\a\\b"));
1674 dir = dir.GetContainingDirectory();
1675 ATLASSERT(dir.GetWinPathString() == _T("c:\\a"));
1676 dir = dir.GetContainingDirectory();
1677 ATLASSERT(dir.GetWinPathString() == _T("c:\\"));
1678 dir = dir.GetContainingDirectory();
1679 ATLASSERT(dir.IsEmpty());
1680 ATLASSERT(dir.GetWinPathString() == _T(""));
1683 void AncestorTest()
1685 CTGitPath testPath;
1686 testPath.SetFromWin(_T("c:\\windows"));
1687 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\")))==false);
1688 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows"))));
1689 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windowsdummy")))==false);
1690 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\test.txt"))));
1691 ATLASSERT(testPath.IsAncestorOf(CTGitPath(_T("c:\\windows\\system32\\test.txt"))));
1694 void SubversionPathTest()
1696 CTGitPath testPath;
1697 testPath.SetFromWin(_T("c:\\"));
1698 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:") == 0);
1699 testPath.SetFromWin(_T("c:\\folder"));
1700 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/folder") == 0);
1701 testPath.SetFromWin(_T("c:\\a\\b\\c\\d\\e"));
1702 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "c:/a/b/c/d/e") == 0);
1703 testPath.SetFromUnknown(_T("http://testing/"));
1704 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing") == 0);
1705 testPath.SetFromGit(NULL);
1706 ATLASSERT(strlen(testPath.GetGitApiPath(pool))==0);
1707 #if defined(_MFC_VER)
1708 testPath.SetFromUnknown(_T("http://testing again"));
1709 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1710 testPath.SetFromUnknown(_T("http://testing%20again"));
1711 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20again") == 0);
1712 testPath.SetFromUnknown(_T("http://testing special chars \344\366\374"));
1713 ATLASSERT(strcmp(testPath.GetGitApiPath(pool), "http://testing%20special%20chars%20%c3%a4%c3%b6%c3%bc") == 0);
1714 #endif
1717 void GetCommonRootTest()
1719 CTGitPath pathA (_T("C:\\Development\\LogDlg.cpp"));
1720 CTGitPath pathB (_T("C:\\Development\\LogDlg.h"));
1721 CTGitPath pathC (_T("C:\\Development\\SomeDir\\LogDlg.h"));
1723 CTGitPathList list;
1724 list.AddPath(pathA);
1725 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development\\LogDlg.cpp"))==0);
1726 list.AddPath(pathB);
1727 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1728 list.AddPath(pathC);
1729 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("C:\\Development"))==0);
1730 #ifdef _MFC_VER
1731 list.Clear();
1732 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");
1733 list.LoadFromAsteriskSeparatedString(sPathList);
1734 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("D:\\Development\\StExBar"))==0);
1736 list.Clear();
1737 sPathList = _T("c:\\windows\\explorer.exe*c:\\windows");
1738 list.LoadFromAsteriskSeparatedString(sPathList);
1739 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1741 list.Clear();
1742 sPathList = _T("c:\\windows\\*c:\\windows");
1743 list.LoadFromAsteriskSeparatedString(sPathList);
1744 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1746 list.Clear();
1747 sPathList = _T("c:\\windows\\system32*c:\\windows\\system");
1748 list.LoadFromAsteriskSeparatedString(sPathList);
1749 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\windows"))==0);
1751 list.Clear();
1752 sPathList = _T("c:\\windowsdummy*c:\\windows");
1753 list.LoadFromAsteriskSeparatedString(sPathList);
1754 ATLASSERT(list.GetCommonRoot().GetWinPathString().CompareNoCase(_T("c:\\"))==0);
1755 #endif
1758 void ValidPathAndUrlTest()
1760 CTGitPath testPath;
1761 testPath.SetFromWin(_T("c:\\a\\b\\c.test.txt"));
1762 ATLASSERT(testPath.IsValidOnWindows());
1763 testPath.SetFromWin(_T("c:\\"));
1764 ATLASSERT(testPath.IsValidOnWindows());
1765 testPath.SetFromWin(_T("D:\\.Net\\SpindleSearch\\"));
1766 ATLASSERT(testPath.IsValidOnWindows());
1767 testPath.SetFromWin(_T("c"));
1768 ATLASSERT(testPath.IsValidOnWindows());
1769 testPath.SetFromWin(_T("c:\\test folder\\file"));
1770 ATLASSERT(testPath.IsValidOnWindows());
1771 testPath.SetFromWin(_T("c:\\folder\\"));
1772 ATLASSERT(testPath.IsValidOnWindows());
1773 testPath.SetFromWin(_T("c:\\ext.ext.ext\\ext.ext.ext.ext"));
1774 ATLASSERT(testPath.IsValidOnWindows());
1775 testPath.SetFromWin(_T("c:\\.svn"));
1776 ATLASSERT(testPath.IsValidOnWindows());
1777 testPath.SetFromWin(_T("c:\\com\\file"));
1778 ATLASSERT(testPath.IsValidOnWindows());
1779 testPath.SetFromWin(_T("c:\\test\\conf"));
1780 ATLASSERT(testPath.IsValidOnWindows());
1781 testPath.SetFromWin(_T("c:\\LPT"));
1782 ATLASSERT(testPath.IsValidOnWindows());
1783 testPath.SetFromWin(_T("c:\\test\\LPT"));
1784 ATLASSERT(testPath.IsValidOnWindows());
1785 testPath.SetFromWin(_T("c:\\com1test"));
1786 ATLASSERT(testPath.IsValidOnWindows());
1787 testPath.SetFromWin(_T("\\\\?\\c:\\test\\com1test"));
1788 ATLASSERT(testPath.IsValidOnWindows());
1790 testPath.SetFromWin(_T("\\\\Share\\filename"));
1791 ATLASSERT(testPath.IsValidOnWindows());
1792 testPath.SetFromWin(_T("\\\\Share\\filename.extension"));
1793 ATLASSERT(testPath.IsValidOnWindows());
1794 testPath.SetFromWin(_T("\\\\Share\\.svn"));
1795 ATLASSERT(testPath.IsValidOnWindows());
1797 // now the negative tests
1798 testPath.SetFromWin(_T("c:\\test:folder"));
1799 ATLASSERT(!testPath.IsValidOnWindows());
1800 testPath.SetFromWin(_T("c:\\file<name"));
1801 ATLASSERT(!testPath.IsValidOnWindows());
1802 testPath.SetFromWin(_T("c:\\something*else"));
1803 ATLASSERT(!testPath.IsValidOnWindows());
1804 testPath.SetFromWin(_T("c:\\folder\\file?nofile"));
1805 ATLASSERT(!testPath.IsValidOnWindows());
1806 testPath.SetFromWin(_T("c:\\ext.>ension"));
1807 ATLASSERT(!testPath.IsValidOnWindows());
1808 testPath.SetFromWin(_T("c:\\com1\\filename"));
1809 ATLASSERT(!testPath.IsValidOnWindows());
1810 testPath.SetFromWin(_T("c:\\com1"));
1811 ATLASSERT(!testPath.IsValidOnWindows());
1812 testPath.SetFromWin(_T("c:\\com1\\AuX"));
1813 ATLASSERT(!testPath.IsValidOnWindows());
1815 testPath.SetFromWin(_T("\\\\Share\\lpt9\\filename"));
1816 ATLASSERT(!testPath.IsValidOnWindows());
1817 testPath.SetFromWin(_T("\\\\Share\\prn"));
1818 ATLASSERT(!testPath.IsValidOnWindows());
1819 testPath.SetFromWin(_T("\\\\Share\\NUL"));
1820 ATLASSERT(!testPath.IsValidOnWindows());
1822 // now come some URL tests
1823 testPath.SetFromGit(_T("http://myserver.com/repos/trunk"));
1824 ATLASSERT(testPath.IsValidOnWindows());
1825 testPath.SetFromGit(_T("https://myserver.com/repos/trunk/file%20with%20spaces"));
1826 ATLASSERT(testPath.IsValidOnWindows());
1827 testPath.SetFromGit(_T("svn://myserver.com/repos/trunk/file with spaces"));
1828 ATLASSERT(testPath.IsValidOnWindows());
1829 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk"));
1830 ATLASSERT(testPath.IsValidOnWindows());
1831 testPath.SetFromGit(_T("http://localhost:90/repos/trunk"));
1832 ATLASSERT(testPath.IsValidOnWindows());
1833 testPath.SetFromGit(_T("file:///C:/GitRepos/Tester/Proj1/tags/t2"));
1834 ATLASSERT(testPath.IsValidOnWindows());
1835 // and some negative URL tests
1836 testPath.SetFromGit(_T("httpp://myserver.com/repos/trunk"));
1837 ATLASSERT(!testPath.IsValidOnWindows());
1838 testPath.SetFromGit(_T("https://myserver.com/rep:os/trunk/file%20with%20spaces"));
1839 ATLASSERT(!testPath.IsValidOnWindows());
1840 testPath.SetFromGit(_T("svn://myserver.com/rep<os/trunk/file with spaces"));
1841 ATLASSERT(!testPath.IsValidOnWindows());
1842 testPath.SetFromGit(_T("svn+ssh://www.myserver.com/repos/trunk/prn/"));
1843 ATLASSERT(!testPath.IsValidOnWindows());
1844 testPath.SetFromGit(_T("http://localhost:90/repos/trunk/com1"));
1845 ATLASSERT(!testPath.IsValidOnWindows());
1849 } TGitPathTestobject;
1850 #endif
1851 #endif
1853 CTGitPath * CTGitPathList::LookForGitPath(CString path)
1855 int i=0;
1856 for(i=0;i<this->GetCount();i++)
1858 if((*this)[i].GetGitPathString() == path )
1859 return (CTGitPath*)&(*this)[i];
1861 return NULL;
1864 CString CTGitPath::GetActionName()
1866 if(m_Action & CTGitPath::LOGACTIONS_ADDED)
1867 return _T("Added");
1868 if(m_Action & CTGitPath::LOGACTIONS_DELETED)
1869 return _T("Deleted");
1870 if(m_Action & CTGitPath::LOGACTIONS_UNMERGED)
1871 return _T("Conflict");
1872 if(m_Action & CTGitPath::LOGACTIONS_MODIFIED)
1873 return _T("Modified");
1874 if(m_Action & CTGitPath::LOGACTIONS_REPLACED)
1875 return _T("Rename");
1876 return _T("Unknown");
1879 int CTGitPathList::GetAction()
1881 return m_Action;