1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - 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.
22 #include "..\TortoiseShell\resource.h"
23 #include "GitStatus.h"
24 #include "UnicodeUtils.h"
27 #include "ShellCache.h"
30 extern CGitAdminDirMap g_AdminDirMap
;
31 extern CGitIndexFileMap g_IndexFileMap
;
32 CGitHeadFileMap g_HeadFileMap
;
33 CGitIgnoreList g_IgnoreList
;
35 GitStatus::GitStatus()
38 m_status
.assumeValid
= m_status
.skipWorktree
= false;
39 m_status
.prop_status
= m_status
.text_status
= git_wc_status_none
;
42 GitStatus::~GitStatus(void)
48 git_wc_status_kind
GitStatus::GetAllStatus(const CTGitPath
& path
, git_depth_t depth
, bool * assumeValid
, bool * skipWorktree
)
50 git_wc_status_kind statuskind
;
55 isDir
= path
.IsDirectory();
56 if (!path
.HasAdminDir(&sProjectRoot
))
57 return git_wc_status_none
;
59 // rev.kind = git_opt_revision_unspecified;
60 statuskind
= git_wc_status_none
;
62 const BOOL bIsRecursive
= (depth
== git_depth_infinity
|| depth
== git_depth_unknown
); // taken from SVN source
65 CString s
= path
.GetWinPathString();
66 if (s
.GetLength() > sProjectRoot
.GetLength())
68 if (sProjectRoot
.GetLength() == 3 && sProjectRoot
[1] == _T(':'))
69 sSubPath
= s
.Right(s
.GetLength() - sProjectRoot
.GetLength());
71 sSubPath
= s
.Right(s
.GetLength() - sProjectRoot
.GetLength() - 1/*otherwise it gets initial slash*/);
74 bool isfull
= ((DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\CacheType"),
75 GetSystemMetrics(SM_REMOTESESSION
) ? ShellCache::dll
: ShellCache::exe
) == ShellCache::dllFull
);
79 err
= GetDirStatus(sProjectRoot
, sSubPath
, &statuskind
, isfull
, bIsRecursive
, isfull
);
80 // folders must not be displayed as added or deleted only as modified (this is for Shell Overlay-Modes)
81 if (statuskind
== git_wc_status_unversioned
&& sSubPath
.IsEmpty())
82 statuskind
= git_wc_status_normal
;
83 else if (statuskind
== git_wc_status_deleted
|| statuskind
== git_wc_status_added
)
84 statuskind
= git_wc_status_modified
;
88 err
= GetFileStatus(sProjectRoot
, sSubPath
, &statuskind
, isfull
, false, isfull
, NULL
, NULL
, assumeValid
, skipWorktree
);
96 git_wc_status_kind
GitStatus::GetMoreImportant(git_wc_status_kind status1
, git_wc_status_kind status2
)
98 if (GetStatusRanking(status1
) >= GetStatusRanking(status2
))
102 // static private method
103 int GitStatus::GetStatusRanking(git_wc_status_kind status
)
107 case git_wc_status_none
:
109 case git_wc_status_unversioned
:
111 case git_wc_status_ignored
:
113 case git_wc_status_incomplete
:
115 case git_wc_status_normal
:
116 case git_wc_status_external
:
118 case git_wc_status_added
:
120 case git_wc_status_missing
:
122 case git_wc_status_deleted
:
124 case git_wc_status_replaced
:
126 case git_wc_status_modified
:
128 case git_wc_status_merged
:
130 case git_wc_status_conflicted
:
132 case git_wc_status_obstructed
:
139 void GitStatus::GetStatus(const CTGitPath
& path
, bool /*update*/ /* = false */, bool noignore
/* = false */, bool /*noexternals*/ /* = false */)
141 // NOTE: unlike the SVN version this one does not cache the enumerated files, because in practice no code in all of
142 // Tortoise uses this, all places that call GetStatus create a temp GitStatus object which gets destroyed right
143 // after the call again
145 CString sProjectRoot
;
146 if ( !path
.HasAdminDir(&sProjectRoot
) )
149 bool isfull
= ((DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\CacheType"),
150 GetSystemMetrics(SM_REMOTESESSION
) ? ShellCache::dll
: ShellCache::exe
) == ShellCache::dllFull
);
154 LPCTSTR lpszSubPath
= NULL
;
156 CString s
= path
.GetWinPathString();
157 if (s
.GetLength() > sProjectRoot
.GetLength())
159 sSubPath
= s
.Right(s
.GetLength() - sProjectRoot
.GetLength());
160 lpszSubPath
= sSubPath
;
161 // skip initial slash if necessary
162 if (*lpszSubPath
== _T('\\'))
166 m_status
.prop_status
= m_status
.text_status
= git_wc_status_none
;
167 m_status
.assumeValid
= false;
168 m_status
.skipWorktree
= false;
170 if (path
.IsDirectory())
172 err
= GetDirStatus(sProjectRoot
, lpszSubPath
, &m_status
.text_status
, isfull
, false, !noignore
);
173 if (m_status
.text_status
== git_wc_status_added
|| m_status
.text_status
== git_wc_status_deleted
) // fix for issue #1769; a folder is either modified, conflicted or normal
174 m_status
.text_status
= git_wc_status_modified
;
177 err
= GetFileStatus(sProjectRoot
, lpszSubPath
, &m_status
.text_status
, isfull
, false, !noignore
, nullptr, nullptr, &m_status
.assumeValid
, &m_status
.skipWorktree
);
179 // Error present if function is not under version control
190 typedef CComCritSecLock
<CComCriticalSection
> CAutoLocker
;
192 int GitStatus::GetFileStatus(const CString
& gitdir
, CString path
, git_wc_status_kind
* status
, BOOL IsFull
, BOOL
/*IsRecursive*/, BOOL IsIgnore
, FILL_STATUS_CALLBACK callback
, void* pData
, bool* assumeValid
, bool* skipWorktree
)
197 path
.Replace(_T('\\'), _T('/'));
199 CString lowcasepath
= path
;
200 lowcasepath
.MakeLower();
202 git_wc_status_kind st
= git_wc_status_none
;
205 g_IndexFileMap
.GetFileStatus(gitdir
, path
, &st
, IsFull
, false, callback
, pData
, &hash
, true, assumeValid
, skipWorktree
);
207 if (st
== git_wc_status_conflicted
)
210 if (callback
&& assumeValid
&& skipWorktree
)
211 callback(CombinePath(gitdir
, path
), st
, false, pData
, *assumeValid
, *skipWorktree
);
215 if (st
== git_wc_status_unversioned
)
219 *status
= git_wc_status_unversioned
;
220 if (callback
&& assumeValid
&& skipWorktree
)
221 callback(CombinePath(gitdir
, path
), *status
, false, pData
, *assumeValid
, *skipWorktree
);
225 if (g_IgnoreList
.CheckIgnoreChanged(gitdir
, path
, false))
226 g_IgnoreList
.LoadAllIgnoreFile(gitdir
, path
, false);
227 if (g_IgnoreList
.IsIgnore(path
, gitdir
, false))
228 st
= git_wc_status_ignored
;
231 if (callback
&& assumeValid
&& skipWorktree
)
232 callback(CombinePath(gitdir
, path
), st
, false, pData
, *assumeValid
, *skipWorktree
);
237 if ((st
== git_wc_status_normal
|| st
== git_wc_status_modified
) && IsFull
)
239 g_HeadFileMap
.CheckHeadAndUpdate(gitdir
);
241 // Check Head Tree Hash
242 SHARED_TREE_PTR treeptr
= g_HeadFileMap
.SafeGet(gitdir
);
245 int start
= SearchInSortVector(*treeptr
, lowcasepath
, -1);
248 *status
= st
= git_wc_status_added
;
249 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": File miss in head tree %s"), (LPCTSTR
)path
);
250 if (callback
&& assumeValid
&& skipWorktree
)
251 callback(CombinePath(gitdir
, path
), st
, false, pData
, *assumeValid
, *skipWorktree
);
255 // staged and not commit
256 if (treeptr
->at(start
).m_Hash
!= hash
)
258 *status
= st
= git_wc_status_modified
;
259 if (callback
&& assumeValid
&& skipWorktree
)
260 callback(CombinePath(gitdir
, path
), st
, false, pData
, *assumeValid
, *skipWorktree
);
265 if (callback
&& assumeValid
&& skipWorktree
)
266 callback(CombinePath(gitdir
, path
), st
, false, pData
, *assumeValid
, *skipWorktree
);
271 bool GitStatus::HasIgnoreFilesChanged(const CString
&gitdir
, const CString
&subpaths
, bool isDir
)
273 return g_IgnoreList
.CheckIgnoreChanged(gitdir
, subpaths
, isDir
);
276 int GitStatus::LoadIgnoreFile(const CString
&gitdir
, const CString
&subpaths
, bool isDir
)
278 return g_IgnoreList
.LoadAllIgnoreFile(gitdir
, subpaths
, isDir
);
280 int GitStatus::IsUnderVersionControl(const CString
&gitdir
, const CString
&path
, bool isDir
,bool *isVersion
)
282 if (g_IndexFileMap
.IsUnderVersionControl(gitdir
, path
, isDir
, isVersion
))
285 return g_HeadFileMap
.IsUnderVersionControl(gitdir
, path
, isDir
, isVersion
);
289 int GitStatus::IsIgnore(const CString
&gitdir
, const CString
&path
, bool *isIgnore
, bool isDir
)
291 if (g_IgnoreList
.CheckIgnoreChanged(gitdir
, path
, isDir
))
292 g_IgnoreList
.LoadAllIgnoreFile(gitdir
, path
, isDir
);
294 *isIgnore
= g_IgnoreList
.IsIgnore(path
, gitdir
, isDir
);
299 int GitStatus::GetFileList(CString path
, std::vector
<CGitFileName
> &list
)
302 WIN32_FIND_DATA data
;
303 HANDLE handle
= ::FindFirstFileEx(path
, SysInfo::Instance().IsWin7OrLater() ? FindExInfoBasic
: FindExInfoStandard
, &data
, FindExSearchNameMatch
, nullptr, SysInfo::Instance().IsWin7OrLater() ? FIND_FIRST_EX_LARGE_FETCH
: 0);
306 if(_tcscmp(data
.cFileName
, _T(".git")) == 0)
309 if(_tcscmp(data
.cFileName
, _T(".")) == 0)
312 if(_tcscmp(data
.cFileName
, _T("..")) == 0)
315 CGitFileName filename
;
317 filename
.m_CaseFileName
= filename
.m_FileName
= data
.cFileName
;
318 filename
.m_FileName
.MakeLower();
320 if(data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
322 filename
.m_FileName
+= _T('/');
325 list
.push_back(filename
);
327 }while(::FindNextFile(handle
, &data
));
331 std::sort(list
.begin(), list
.end(), SortCGitFileName
);
335 int GitStatus::EnumDirStatus(const CString
&gitdir
, const CString
&subpath
, git_wc_status_kind
* status
,BOOL IsFul
, BOOL IsRecursive
, BOOL IsIgnore
, FILL_STATUS_CALLBACK callback
, void *pData
)
340 CString path
= subpath
;
342 path
.Replace(_T('\\'), _T('/'));
343 if (!path
.IsEmpty() && path
[path
.GetLength() - 1] != _T('/'))
344 path
+= _T('/'); // Add trail / to show it is directory, not file name.
346 std::vector
<CGitFileName
> filelist
;
347 GetFileList(CombinePath(gitdir
, subpath
), filelist
);
349 g_IndexFileMap
.CheckAndUpdate(gitdir
,true);
351 g_HeadFileMap
.CheckHeadAndUpdate(gitdir
);
353 SHARED_INDEX_PTR indexptr
= g_IndexFileMap
.SafeGet(gitdir
);
354 SHARED_TREE_PTR treeptr
= g_HeadFileMap
.SafeGet(gitdir
);
356 // new git working tree has no index file
359 for (auto it
= filelist
.cbegin(); it
!= filelist
.cend(); ++it
)
361 CString casepath
= path
;
362 casepath
+= it
->m_CaseFileName
;
365 if (it
->m_FileName
.GetLength() > 0 && it
->m_FileName
[it
->m_FileName
.GetLength() - 1] == _T('/'))
370 if (g_IgnoreList
.CheckIgnoreChanged(gitdir
, casepath
, bIsDir
))
371 g_IgnoreList
.LoadAllIgnoreFile(gitdir
, casepath
, bIsDir
);
373 if (g_IgnoreList
.IsIgnore(casepath
, gitdir
, bIsDir
))
374 *status
= git_wc_status_ignored
;
378 *status
= git_wc_status_unversioned
;
383 *status
= git_wc_status_unversioned
;
386 callback(CombinePath(gitdir
, casepath
), *status
, bIsDir
, pData
, false, false);
391 CString lowcasepath
= path
;
392 lowcasepath
.MakeLower();
396 for (auto it
= filelist
.cbegin(), itend
= filelist
.cend(); it
!= itend
; ++it
)
398 casepath
= onepath
= lowcasepath
;
399 onepath
+= it
->m_FileName
;
400 casepath
+= it
->m_CaseFileName
;
403 if (!onepath
.IsEmpty() && onepath
[onepath
.GetLength() - 1] == _T('/'))
406 int matchLength
= -1;
408 matchLength
= onepath
.GetLength();
409 int pos
= SearchInSortVector(*indexptr
, onepath
, matchLength
);
410 int posintree
= SearchInSortVector(*treeptr
, onepath
, matchLength
);
412 if (pos
< 0 && posintree
< 0)
414 if (onepath
.IsEmpty())
419 *status
= git_wc_status_unversioned
;
421 callback(CombinePath(gitdir
, casepath
), *status
, bIsDir
, pData
, false, false);
425 if (g_IgnoreList
.CheckIgnoreChanged(gitdir
, casepath
, bIsDir
))
426 g_IgnoreList
.LoadAllIgnoreFile(gitdir
, casepath
, bIsDir
);
428 if (g_IgnoreList
.IsIgnore(casepath
, gitdir
, bIsDir
))
429 *status
= git_wc_status_ignored
;
431 *status
= git_wc_status_unversioned
;
434 callback(CombinePath(gitdir
, casepath
), *status
, bIsDir
, pData
, false, false);
436 else if (pos
< 0 && posintree
>= 0) /* check if file delete in index */
438 *status
= git_wc_status_deleted
;
440 callback(CombinePath(gitdir
, casepath
), *status
, bIsDir
, pData
, false, false);
442 else if (pos
>= 0 && posintree
< 0) /* Check if file added */
444 *status
= git_wc_status_added
;
445 if (indexptr
->at(pos
).m_Flags
& GIT_IDXENTRY_STAGEMASK
)
446 *status
= git_wc_status_conflicted
;
448 callback(CombinePath(gitdir
, casepath
), *status
, bIsDir
, pData
, false, false);
452 if (onepath
.IsEmpty())
457 *status
= git_wc_status_normal
;
459 callback(CombinePath(gitdir
, casepath
), *status
, bIsDir
, pData
, false, false);
463 bool assumeValid
= false;
464 bool skipWorktree
= false;
465 git_wc_status_kind filestatus
;
466 GetFileStatus(gitdir
, casepath
, &filestatus
, IsFul
, IsRecursive
, IsIgnore
, callback
, pData
, &assumeValid
, &skipWorktree
);
471 /* Check deleted file in system */
472 int start
= 0, end
= 0;
473 int pos
= SearchInSortVector(*indexptr
, lowcasepath
, lowcasepath
.GetLength()); // match path prefix, (sub)folders end with slash
474 std::map
<CString
, bool> skipWorktreeMap
;
476 if (GetRangeInSortVector(*indexptr
, lowcasepath
, lowcasepath
.GetLength(), &start
, &end
, pos
) == 0)
479 for (auto it
= indexptr
->cbegin() + start
, itlast
= indexptr
->cbegin() + end
; it
<= itlast
; ++it
)
481 int commonPrefixLength
= lowcasepath
.GetLength();
482 int index
= (*it
).m_FileName
.Find(_T('/'), commonPrefixLength
);
484 index
= (*it
).m_FileName
.GetLength();
486 ++index
; // include slash at the end for subfolders, so that we do not match files by mistake
488 CString filename
= (*it
).m_FileName
.Mid(commonPrefixLength
, index
- commonPrefixLength
);
489 if (oldstring
!= filename
)
491 oldstring
= filename
;
492 if (SearchInSortVector(filelist
, filename
, filename
.GetLength()) < 0)
494 bool skipWorktree
= false;
495 *status
= git_wc_status_deleted
;
496 if (((*it
).m_Flags
& GIT_IDXENTRY_SKIP_WORKTREE
) != 0)
498 skipWorktreeMap
[filename
] = true;
500 *status
= git_wc_status_normal
;
503 callback(CombinePath(gitdir
, (*it
).m_FileName
), *status
, false, pData
, false, skipWorktree
);
510 pos
= SearchInSortVector(*treeptr
, lowcasepath
, lowcasepath
.GetLength()); // match path prefix, (sub)folders end with slash
511 if (GetRangeInSortVector(*treeptr
, lowcasepath
, lowcasepath
.GetLength(), &start
, &end
, pos
) == 0)
514 for (auto it
= treeptr
->cbegin() + start
, itlast
= treeptr
->cbegin() + end
; it
<= itlast
; ++it
)
516 int commonPrefixLength
= lowcasepath
.GetLength();
517 int index
= (*it
).m_FileName
.Find(_T('/'), commonPrefixLength
);
519 index
= (*it
).m_FileName
.GetLength();
521 ++index
; // include slash at the end for subfolders, so that we do not match files by mistake
523 CString filename
= (*it
).m_FileName
.Mid(commonPrefixLength
, index
- commonPrefixLength
);
524 if (oldstring
!= filename
&& skipWorktreeMap
[filename
] != true)
526 oldstring
= filename
;
527 if (SearchInSortVector(filelist
, filename
, filename
.GetLength()) < 0)
529 *status
= git_wc_status_deleted
;
531 callback(CombinePath(gitdir
, (*it
).m_FileName
), *status
, false, pData
, false, false);
541 int GitStatus::GetDirStatus(const CString
& gitdir
, const CString
& subpath
, git_wc_status_kind
* status
, BOOL IsFul
, BOOL IsRecursive
, BOOL IsIgnore
)
546 CString path
= subpath
;
548 path
.Replace(_T('\\'), _T('/'));
549 if (!path
.IsEmpty() && path
[path
.GetLength() - 1] != _T('/'))
550 path
+= _T('/'); //Add trail / to show it is directory, not file name.
552 g_IndexFileMap
.CheckAndUpdate(gitdir
, true);
554 SHARED_INDEX_PTR indexptr
= g_IndexFileMap
.SafeGet(gitdir
);
558 *status
= git_wc_status_unversioned
;
562 CString lowcasepath
= path
;
563 lowcasepath
.MakeLower();
565 int pos
= SearchInSortVector(*indexptr
, lowcasepath
, lowcasepath
.GetLength());
567 // Not In Version Contorl
572 *status
= git_wc_status_unversioned
;
576 // Check ignore always.
577 if (g_IgnoreList
.CheckIgnoreChanged(gitdir
, path
, true))
578 g_IgnoreList
.LoadAllIgnoreFile(gitdir
, path
, true);
580 if (g_IgnoreList
.IsIgnore(path
, gitdir
, true))
581 *status
= git_wc_status_ignored
;
583 *status
= git_wc_status_unversioned
;
585 g_HeadFileMap
.CheckHeadAndUpdate(gitdir
);
587 SHARED_TREE_PTR treeptr
= g_HeadFileMap
.SafeGet(gitdir
);
588 // Check init repository
589 if (treeptr
->HeadIsEmpty() && path
.IsEmpty())
590 *status
= git_wc_status_normal
;
591 // check if only one file in repository is deleted in index
592 else if (path
.IsEmpty() && !treeptr
->empty())
593 *status
= git_wc_status_deleted
;
598 // In version control
599 *status
= git_wc_status_normal
;
604 end
= (int)indexptr
->size() - 1;
606 GetRangeInSortVector(*indexptr
, lowcasepath
, lowcasepath
.GetLength(), &start
, &end
, pos
);
609 for (auto it
= indexptr
->cbegin() + start
, itlast
= indexptr
->cbegin() + end
; it
<= itlast
; ++it
)
611 if (((*it
).m_Flags
& GIT_IDXENTRY_STAGEMASK
) != 0)
613 *status
= git_wc_status_conflicted
;
618 if (IsFul
&& (*status
!= git_wc_status_conflicted
))
620 *status
= git_wc_status_normal
;
622 g_HeadFileMap
.CheckHeadAndUpdate(gitdir
);
626 // Check if new init repository
627 SHARED_TREE_PTR treeptr
= g_HeadFileMap
.SafeGet(gitdir
);
629 if (!treeptr
->empty() || treeptr
->HeadIsEmpty())
631 for (auto it
= indexptr
->cbegin() + start
, itlast
= indexptr
->cbegin() + end
; it
<= itlast
; ++it
)
633 pos
= SearchInSortVector(*treeptr
, (*it
).m_FileName
, -1);
637 *status
= max(git_wc_status_added
, *status
); // added file found
641 if (pos
>= 0 && treeptr
->at(pos
).m_Hash
!= (*it
).m_IndexHash
)
643 *status
= max(git_wc_status_modified
, *status
); // modified file found
649 if (*status
== git_wc_status_normal
)
651 pos
= SearchInSortVector(*treeptr
, lowcasepath
, lowcasepath
.GetLength());
653 *status
= max(git_wc_status_added
, *status
); // added file found
657 GetRangeInSortVector(*treeptr
, lowcasepath
, lowcasepath
.GetLength(), &hstart
, &hend
, pos
);
658 for (auto hit
= treeptr
->cbegin() + hstart
, lastElement
= treeptr
->cbegin() + hend
; hit
<= lastElement
; ++hit
)
660 if (SearchInSortVector(*indexptr
, (*hit
).m_FileName
, -1) < 0)
662 *status
= max(git_wc_status_deleted
, *status
); // deleted file found
672 // When status == git_wc_status_conflicted, needn't check each file status
673 // because git_wc_status_conflicted is highest.s
674 if (*status
== git_wc_status_conflicted
)
677 for (auto it
= indexptr
->cbegin() + start
, itlast
= indexptr
->cbegin() + end
; it
<= itlast
; ++it
)
679 //skip child directory
680 if (!IsRecursive
&& (*it
).m_FileName
.Find(_T('/'), path
.GetLength()) > 0)
683 git_wc_status_kind filestatus
= git_wc_status_none
;
684 bool assumeValid
= false;
685 bool skipWorktree
= false;
686 GetFileStatus(gitdir
, (*it
).m_FileName
, &filestatus
, IsFul
, IsRecursive
, IsIgnore
, nullptr, nullptr, &assumeValid
, &skipWorktree
);
689 case git_wc_status_added
:
690 case git_wc_status_modified
:
691 case git_wc_status_deleted
:
692 case git_wc_status_conflicted
:
693 *status
= GetMoreImportant(filestatus
, *status
);
702 bool GitStatus::IsExistIndexLockFile(CString sDirName
)
704 if (!PathIsDirectory(sDirName
))
706 int x
= sDirName
.ReverseFind(_T('\\'));
710 sDirName
= sDirName
.Left(x
);
715 if (PathFileExists(CombinePath(sDirName
, _T(".git"))))
717 if (PathFileExists(g_AdminDirMap
.GetAdminDirConcat(sDirName
, _T("index.lock"))))
723 int x
= sDirName
.ReverseFind(_T('\\'));
727 sDirName
= sDirName
.Left(x
);
732 bool GitStatus::ReleasePath(const CString
&gitdir
)
734 g_IndexFileMap
.SafeClear(gitdir
);
738 bool GitStatus::ReleasePathsRecursively(const CString
&rootpath
)
740 g_IndexFileMap
.SafeClearRecursively(rootpath
);