Report correct filenames of deleted files to cache
[TortoiseGit.git] / src / Git / GitStatus.cpp
bloba583cdc14e44ee98a23328b2b51269f2bc97dc28
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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.
20 #include "stdafx.h"
21 #include "registry.h"
22 #include "..\TortoiseShell\resource.h"
23 #include "GitStatus.h"
24 #include "UnicodeUtils.h"
25 #include "Git.h"
26 #include "GitIndex.h"
27 #include "ShellCache.h"
29 extern CGitAdminDirMap g_AdminDirMap;
30 CGitIndexFileMap g_IndexFileMap;
31 CGitHeadFileMap g_HeadFileMap;
32 CGitIgnoreList g_IgnoreList;
34 GitStatus::GitStatus()
35 : status(NULL)
36 , m_allstatus(git_wc_status_none)
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)
46 // static method
47 git_wc_status_kind GitStatus::GetAllStatus(const CTGitPath& path, git_depth_t depth, bool * assumeValid, bool * skipWorktree)
49 git_wc_status_kind statuskind;
50 BOOL err;
51 BOOL isDir;
52 CString sProjectRoot;
54 isDir = path.IsDirectory();
55 if (!path.HasAdminDir(&sProjectRoot))
56 return git_wc_status_none;
58 // rev.kind = git_opt_revision_unspecified;
59 statuskind = git_wc_status_none;
61 const BOOL bIsRecursive = (depth == git_depth_infinity || depth == git_depth_unknown); // taken from SVN source
63 CString sSubPath;
64 CString s = path.GetWinPathString();
65 if (s.GetLength() > sProjectRoot.GetLength())
67 if (sProjectRoot.GetLength() == 3 && sProjectRoot[1] == _T(':'))
68 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength());
69 else
70 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() - 1/*otherwise it gets initial slash*/);
73 bool isfull = ((DWORD)CRegStdDWORD(_T("Software\\TortoiseGit\\CacheType"),
74 GetSystemMetrics(SM_REMOTESESSION) ? ShellCache::dll : ShellCache::exe) == ShellCache::dllFull);
76 if(isDir)
78 err = GetDirStatus(sProjectRoot,sSubPath,&statuskind, isfull,bIsRecursive,isfull,NULL, NULL);
79 // folders must not be displayed as added or deleted only as modified (this is for Shell Overlay-Modes)
80 if (statuskind == git_wc_status_unversioned && sSubPath.IsEmpty())
81 statuskind = git_wc_status_normal;
82 else if (statuskind == git_wc_status_deleted || statuskind == git_wc_status_added)
83 statuskind = git_wc_status_modified;
85 else
87 err = GetFileStatus(sProjectRoot, sSubPath, &statuskind, isfull, false, isfull, NULL, NULL, assumeValid, skipWorktree);
90 return statuskind;
93 // static method
94 git_wc_status_kind GitStatus::GetAllStatusRecursive(const CTGitPath& path)
96 return GetAllStatus(path, git_depth_infinity);
99 // static method
100 git_wc_status_kind GitStatus::GetMoreImportant(git_wc_status_kind status1, git_wc_status_kind status2)
102 if (GetStatusRanking(status1) >= GetStatusRanking(status2))
103 return status1;
104 return status2;
106 // static private method
107 int GitStatus::GetStatusRanking(git_wc_status_kind status)
109 switch (status)
111 case git_wc_status_none:
112 return 0;
113 case git_wc_status_unversioned:
114 return 1;
115 case git_wc_status_ignored:
116 return 2;
117 case git_wc_status_incomplete:
118 return 4;
119 case git_wc_status_normal:
120 case git_wc_status_external:
121 return 5;
122 case git_wc_status_added:
123 return 6;
124 case git_wc_status_missing:
125 return 7;
126 case git_wc_status_deleted:
127 return 8;
128 case git_wc_status_replaced:
129 return 9;
130 case git_wc_status_modified:
131 return 10;
132 case git_wc_status_merged:
133 return 11;
134 case git_wc_status_conflicted:
135 return 12;
136 case git_wc_status_obstructed:
137 return 13;
139 return 0;
142 void GitStatus::GetStatus(const CTGitPath& path, bool /*update*/ /* = false */, bool noignore /* = false */, bool /*noexternals*/ /* = false */)
144 // NOTE: unlike the SVN version this one does not cache the enumerated files, because in practice no code in all of
145 // Tortoise uses this, all places that call GetStatus create a temp GitStatus object which gets destroyed right
146 // after the call again
148 CString sProjectRoot;
149 if ( !path.HasAdminDir(&sProjectRoot) )
150 return;
152 bool isfull = ((DWORD)CRegStdDWORD(_T("Software\\TortoiseGit\\CacheType"),
153 GetSystemMetrics(SM_REMOTESESSION) ? ShellCache::dll : ShellCache::exe) == ShellCache::dllFull);
155 int err = 0;
158 LPCTSTR lpszSubPath = NULL;
159 CString sSubPath;
160 CString s = path.GetWinPathString();
161 if (s.GetLength() > sProjectRoot.GetLength())
163 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength());
164 lpszSubPath = sSubPath;
165 // skip initial slash if necessary
166 if (*lpszSubPath == _T('\\'))
167 ++lpszSubPath;
170 m_status.prop_status = m_status.text_status = git_wc_status_none;
171 m_status.assumeValid = false;
172 m_status.skipWorktree = false;
174 if(path.IsDirectory())
176 err = GetDirStatus(sProjectRoot,CString(lpszSubPath),&m_status.text_status , isfull, false,!noignore, NULL, NULL);
177 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
178 m_status.text_status = git_wc_status_modified;
180 else
182 err = GetFileStatus(sProjectRoot, CString(lpszSubPath), &m_status.text_status ,isfull, false,!noignore, NULL,NULL, &m_status.assumeValid, &m_status.skipWorktree);
186 // Error present if function is not under version control
187 if (err)
189 status = NULL;
190 return;
193 status = &m_status;
196 void GitStatus::GetStatusString(git_wc_status_kind status, size_t buflen, TCHAR * string)
198 TCHAR * buf;
199 switch (status)
201 case git_wc_status_none:
202 buf = _T("none\0");
203 break;
204 case git_wc_status_unversioned:
205 buf = _T("unversioned\0");
206 break;
207 case git_wc_status_normal:
208 buf = _T("normal\0");
209 break;
210 case git_wc_status_added:
211 buf = _T("added\0");
212 break;
213 case git_wc_status_missing:
214 buf = _T("missing\0");
215 break;
216 case git_wc_status_deleted:
217 buf = _T("deleted\0");
218 break;
219 case git_wc_status_replaced:
220 buf = _T("replaced\0");
221 break;
222 case git_wc_status_modified:
223 buf = _T("modified\0");
224 break;
225 case git_wc_status_merged:
226 buf = _T("merged\0");
227 break;
228 case git_wc_status_conflicted:
229 buf = _T("conflicted\0");
230 break;
231 case git_wc_status_obstructed:
232 buf = _T("obstructed\0");
233 break;
234 case git_wc_status_ignored:
235 buf = _T("ignored");
236 break;
237 case git_wc_status_external:
238 buf = _T("external");
239 break;
240 case git_wc_status_incomplete:
241 buf = _T("incomplete\0");
242 break;
243 default:
244 buf = _T("\0");
245 break;
247 _stprintf_s(string, buflen, _T("%s"), buf);
250 void GitStatus::GetStatusString(HINSTANCE hInst, git_wc_status_kind status, TCHAR * string, int size, WORD lang)
252 switch (status)
254 case git_wc_status_none:
255 LoadStringEx(hInst, IDS_STATUSNONE, string, size, lang);
256 break;
257 case git_wc_status_unversioned:
258 LoadStringEx(hInst, IDS_STATUSUNVERSIONED, string, size, lang);
259 break;
260 case git_wc_status_normal:
261 LoadStringEx(hInst, IDS_STATUSNORMAL, string, size, lang);
262 break;
263 case git_wc_status_added:
264 LoadStringEx(hInst, IDS_STATUSADDED, string, size, lang);
265 break;
266 case git_wc_status_missing:
267 LoadStringEx(hInst, IDS_STATUSABSENT, string, size, lang);
268 break;
269 case git_wc_status_deleted:
270 LoadStringEx(hInst, IDS_STATUSDELETED, string, size, lang);
271 break;
272 case git_wc_status_replaced:
273 LoadStringEx(hInst, IDS_STATUSREPLACED, string, size, lang);
274 break;
275 case git_wc_status_modified:
276 LoadStringEx(hInst, IDS_STATUSMODIFIED, string, size, lang);
277 break;
278 case git_wc_status_merged:
279 LoadStringEx(hInst, IDS_STATUSMERGED, string, size, lang);
280 break;
281 case git_wc_status_conflicted:
282 LoadStringEx(hInst, IDS_STATUSCONFLICTED, string, size, lang);
283 break;
284 case git_wc_status_ignored:
285 LoadStringEx(hInst, IDS_STATUSIGNORED, string, size, lang);
286 break;
287 case git_wc_status_obstructed:
288 LoadStringEx(hInst, IDS_STATUSOBSTRUCTED, string, size, lang);
289 break;
290 case git_wc_status_external:
291 LoadStringEx(hInst, IDS_STATUSEXTERNAL, string, size, lang);
292 break;
293 case git_wc_status_incomplete:
294 LoadStringEx(hInst, IDS_STATUSINCOMPLETE, string, size, lang);
295 break;
296 default:
297 LoadStringEx(hInst, IDS_STATUSNONE, string, size, lang);
298 break;
302 int GitStatus::LoadStringEx(HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax, WORD wLanguage)
304 const STRINGRESOURCEIMAGE* pImage;
305 const STRINGRESOURCEIMAGE* pImageEnd;
306 ULONG nResourceSize;
307 HGLOBAL hGlobal;
308 UINT iIndex;
309 int ret;
311 HRSRC hResource = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE(((uID>>4)+1)), wLanguage);
312 if (!hResource)
314 // try the default language before giving up!
315 hResource = FindResource(hInstance, MAKEINTRESOURCE(((uID>>4)+1)), RT_STRING);
316 if (!hResource)
317 return 0;
319 hGlobal = LoadResource(hInstance, hResource);
320 if (!hGlobal)
321 return 0;
322 pImage = (const STRINGRESOURCEIMAGE*)::LockResource(hGlobal);
323 if(!pImage)
324 return 0;
326 nResourceSize = ::SizeofResource(hInstance, hResource);
327 pImageEnd = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+nResourceSize);
328 iIndex = uID&0x000f;
330 while ((iIndex > 0) && (pImage < pImageEnd))
332 pImage = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+(sizeof(STRINGRESOURCEIMAGE)+(pImage->nLength*sizeof(WCHAR))));
333 iIndex--;
335 if (pImage >= pImageEnd)
336 return 0;
337 if (pImage->nLength == 0)
338 return 0;
340 ret = pImage->nLength;
341 if (pImage->nLength > nBufferMax)
343 wcsncpy_s(lpBuffer, nBufferMax, pImage->achString, pImage->nLength-1);
344 lpBuffer[nBufferMax-1] = 0;
346 else
348 wcsncpy_s(lpBuffer, nBufferMax, pImage->achString, pImage->nLength);
349 lpBuffer[ret] = 0;
351 return ret;
354 typedef CComCritSecLock<CComCriticalSection> CAutoLocker;
356 int GitStatus::GetFileStatus(const CString &gitdir, const CString &pathParam, git_wc_status_kind * status,BOOL IsFull, BOOL /*IsRecursive*/,BOOL IsIgnore, FILL_STATUS_CALLBACK callback, void *pData, bool * assumeValid, bool * skipWorktree)
360 CString path = pathParam;
362 path.Replace(_T('\\'),_T('/'));
364 CString lowcasepath =path;
365 lowcasepath.MakeLower();
367 if(status)
369 git_wc_status_kind st = git_wc_status_none;
370 CGitHash hash;
372 g_IndexFileMap.GetFileStatus(gitdir, path, &st, IsFull, false, callback, pData, &hash, true, assumeValid, skipWorktree);
374 if( st == git_wc_status_conflicted )
376 *status =st;
377 if (callback && assumeValid && skipWorktree)
378 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
379 return 0;
382 if( st == git_wc_status_unversioned )
384 if(!IsIgnore)
386 *status = git_wc_status_unversioned;
387 if (callback && assumeValid && skipWorktree)
388 callback(gitdir + _T("/") + path, *status, false, pData, *assumeValid, *skipWorktree);
389 return 0;
392 if( g_IgnoreList.CheckIgnoreChanged(gitdir,path))
394 g_IgnoreList.LoadAllIgnoreFile(gitdir,path);
396 if( g_IgnoreList.IsIgnore(path, gitdir) )
398 st = git_wc_status_ignored;
400 *status = st;
401 if (callback && assumeValid && skipWorktree)
402 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
404 return 0;
407 if ((st == git_wc_status_normal || st == git_wc_status_modified) && IsFull)
410 g_HeadFileMap.CheckHeadAndUpdate(gitdir);
412 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
414 // Check Head Tree Hash;
416 //add item
418 int start =SearchInSortVector(*treeptr,lowcasepath.GetBuffer(),-1);
419 lowcasepath.ReleaseBuffer();
421 if(start<0)
423 *status =st=git_wc_status_added;
424 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": File miss in head tree %s"), path);
425 if (callback && assumeValid && skipWorktree)
426 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
427 return 0;
430 //staged and not commit
431 if( treeptr->at(start).m_Hash != hash )
433 *status =st=git_wc_status_modified;
434 if (callback && assumeValid && skipWorktree)
435 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
436 return 0;
441 *status =st;
442 if (callback && assumeValid && skipWorktree)
443 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
444 return 0;
447 catch(...)
449 if(status)
450 *status = git_wc_status_none;
451 return -1;
454 return 0;
458 bool GitStatus::HasIgnoreFilesChanged(const CString &gitdir, const CString &subpaths)
460 return g_IgnoreList.CheckIgnoreChanged(gitdir, subpaths);
463 int GitStatus::LoadIgnoreFile(const CString &gitdir,const CString &subpaths)
465 return g_IgnoreList.LoadAllIgnoreFile(gitdir,subpaths);
467 int GitStatus::IsUnderVersionControl(const CString &gitdir, const CString &path, bool isDir,bool *isVersion)
469 if (g_IndexFileMap.IsUnderVersionControl(gitdir, path, isDir, isVersion))
470 return 1;
471 if (!*isVersion)
472 return g_HeadFileMap.IsUnderVersionControl(gitdir, path, isDir, isVersion);
473 return 0;
476 __int64 GitStatus::GetIndexFileTime(const CString &gitdir)
478 SHARED_INDEX_PTR ptr=g_IndexFileMap.SafeGet(gitdir);
479 if(ptr.get() == NULL)
480 return 0;
482 return ptr->m_LastModifyTime;
485 int GitStatus::IsIgnore(const CString &gitdir, const CString &path, bool *isIgnore)
487 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,path))
488 g_IgnoreList.LoadAllIgnoreFile(gitdir,path);
490 *isIgnore = g_IgnoreList.IsIgnore(path,gitdir);
492 return 0;
495 static bool SortFileName(CGitFileName &Item1, CGitFileName &Item2)
497 return Item1.m_FileName.Compare(Item2.m_FileName)<0;
500 int GitStatus::GetFileList(const CString &gitdir, const CString &subpath, std::vector<CGitFileName> &list)
502 WIN32_FIND_DATA data;
503 HANDLE handle=::FindFirstFile(gitdir+_T("\\")+subpath+_T("\\*.*"), &data);
506 if(_tcscmp(data.cFileName, _T(".git")) == 0)
507 continue;
509 if(_tcscmp(data.cFileName, _T(".")) == 0)
510 continue;
512 if(_tcscmp(data.cFileName, _T("..")) == 0)
513 continue;
515 CGitFileName filename;
517 filename.m_CaseFileName = filename.m_FileName = data.cFileName;
518 filename.m_FileName.MakeLower();
520 if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
522 filename.m_FileName += _T('/');
525 list.push_back(filename);
527 }while(::FindNextFile(handle, &data));
529 FindClose(handle);
531 std::sort(list.begin(), list.end(), SortFileName);
532 return 0;
535 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)
539 CString path =subpath;
541 path.Replace(_T('\\'),_T('/'));
542 if(!path.IsEmpty())
543 if(path[path.GetLength()-1] != _T('/'))
544 path += _T('/'); //Add trail / to show it is directory, not file name.
546 CString lowcasepath = path;
547 lowcasepath.MakeLower();
549 std::vector<CGitFileName> filelist;
550 GetFileList(gitdir, subpath, filelist);
552 if(status)
554 g_IndexFileMap.CheckAndUpdate(gitdir,true);
556 g_HeadFileMap.CheckHeadAndUpdate(gitdir);
558 SHARED_INDEX_PTR indexptr = g_IndexFileMap.SafeGet(gitdir);
559 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
561 std::vector<CGitFileName>::iterator it;
563 // new git working tree has no index file
564 if (indexptr.get() == NULL)
566 for (it = filelist.begin(); it < filelist.end(); ++it)
568 CString casepath = path + it->m_CaseFileName;
570 bool bIsDir = false;
571 if (casepath.GetLength() > 0 && casepath[casepath.GetLength() - 1] == _T('/'))
572 bIsDir = true;
574 if (bIsDir) /*check if it is directory*/
576 if (::PathFileExists(gitdir + casepath + _T("\\.git")))
577 { /* That is git submodule */
578 *status = git_wc_status_unknown;
579 if (callback)
580 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
581 continue;
585 if (IsIgnore)
587 if (g_IgnoreList.CheckIgnoreChanged(gitdir, casepath))
588 g_IgnoreList.LoadAllIgnoreFile(gitdir, casepath);
590 if (g_IgnoreList.IsIgnore(casepath, gitdir))
591 *status = git_wc_status_ignored;
592 else if (bIsDir)
593 continue;
594 else
595 *status = git_wc_status_unversioned;
597 else if (bIsDir)
598 continue;
599 else
600 *status = git_wc_status_unversioned;
602 if(callback)
603 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
605 return 0;
608 CString onepath;
609 CString casepath;
610 for (it = filelist.begin(); it < filelist.end(); ++it)
612 casepath=onepath = path;
613 onepath.MakeLower();
614 onepath += it->m_FileName;
615 casepath += it->m_CaseFileName;
617 bool bIsDir = false;
618 if (!onepath.IsEmpty() && onepath[onepath.GetLength() - 1] == _T('/'))
619 bIsDir = true;
621 LPTSTR onepathBuffer = onepath.GetBuffer();
622 int matchLength = -1;
623 if (bIsDir)
624 matchLength = onepath.GetLength();
625 int pos = SearchInSortVector(*indexptr, onepathBuffer, matchLength);
626 int posintree = SearchInSortVector(*treeptr, onepathBuffer, matchLength);
627 onepath.ReleaseBuffer();
629 if(pos <0 && posintree<0)
631 if (onepath.IsEmpty())
632 continue;
634 if(bIsDir) /*check if it is directory*/
636 if(::PathFileExists(gitdir+onepath+_T("/.git")))
637 { /* That is git submodule */
638 *status = git_wc_status_unknown;
639 if(callback)
640 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
641 continue;
645 if(!IsIgnore)
647 *status = git_wc_status_unversioned;
648 if(callback)
649 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
650 continue;
653 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,casepath))
654 g_IgnoreList.LoadAllIgnoreFile(gitdir,casepath);
656 if(g_IgnoreList.IsIgnore(casepath,gitdir))
657 *status = git_wc_status_ignored;
658 else
659 *status = git_wc_status_unversioned;
661 if(callback)
662 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
665 else if(pos <0 && posintree>=0) /* check if file delete in index */
667 *status = git_wc_status_deleted;
668 if(callback)
669 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
672 else if(pos >=0 && posintree <0) /* Check if file added */
674 *status = git_wc_status_added;
675 if(callback)
676 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
678 else
680 if (onepath.IsEmpty())
681 continue;
683 if (bIsDir)
685 *status = git_wc_status_normal;
686 if(callback)
687 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
689 else
691 bool assumeValid = false;
692 bool skipWorktree = false;
693 git_wc_status_kind filestatus;
694 GetFileStatus(gitdir, casepath, &filestatus, IsFul, IsRecursive, IsIgnore, callback, pData, &assumeValid, &skipWorktree);
698 }/*End of For*/
700 /* Check deleted file in system */
701 LPTSTR lowcasepathBuffer = lowcasepath.GetBuffer();
702 int start=0, end=0;
703 int pos = SearchInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength()); // match path prefix, (sub)folders end with slash
704 std::map<CString, bool> skipWorktreeMap;
706 if (GetRangeInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos) == 0)
708 CGitIndexList::iterator it;
709 CString oldstring;
711 for (it = indexptr->begin() + start; it <= indexptr->begin() + end; ++it)
713 int commonPrefixLength = lowcasepath.GetLength();
714 int index = (*it).m_FileName.Find(_T('/'), commonPrefixLength);
715 if(index<0)
716 index = (*it).m_FileName.GetLength();
717 else
718 ++index; // include slash at the end for subfolders, so that we do not match files by mistake
720 CString filename = (*it).m_FileName.Mid(commonPrefixLength, index - commonPrefixLength);
721 if(oldstring != filename)
723 oldstring = filename;
724 if(SearchInSortVector(filelist, filename.GetBuffer(), filename.GetLength())<0)
726 bool skipWorktree = false;
727 *status = git_wc_status_deleted;
728 if (((*it).m_Flags & GIT_IDXENTRY_SKIP_WORKTREE) != 0)
730 skipWorktreeMap[filename] = true;
731 skipWorktree = true;
732 *status = git_wc_status_normal;
734 if(callback)
735 callback(gitdir + _T("/") + (*it).m_FileName, *status, false, pData, false, skipWorktree);
741 start = end =0;
742 pos = SearchInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength()); // match path prefix, (sub)folders end with slash
743 if (GetRangeInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos) == 0)
745 CGitHeadFileList::iterator it;
746 CString oldstring;
748 for (it = treeptr->begin() + start; it <= treeptr->begin() + end; ++it)
750 int commonPrefixLength = lowcasepath.GetLength();
751 int index = (*it).m_FileName.Find(_T('/'), commonPrefixLength);
752 if(index<0)
753 index = (*it).m_FileName.GetLength();
754 else
755 ++index; // include slash at the end for subfolders, so that we do not match files by mistake
757 CString filename = (*it).m_FileName.Mid(commonPrefixLength, index - commonPrefixLength);
758 if (oldstring != filename && skipWorktreeMap[filename] != true)
760 oldstring = filename;
761 if(SearchInSortVector(filelist, filename.GetBuffer(), filename.GetLength())<0)
763 *status = git_wc_status_deleted;
764 if(callback)
765 callback(gitdir + _T("/") + (*it).m_FileName, *status, false, pData, false, false);
770 lowcasepath.ReleaseBuffer();
772 }/*End of if status*/
773 }catch(...)
775 return -1;
777 return 0;
780 int GitStatus::GetDirStatus(const CString &gitdir, const CString &subpath, git_wc_status_kind * status, BOOL IsFul, BOOL IsRecursive, BOOL IsIgnore, FILL_STATUS_CALLBACK callback, void *pData)
784 CString path =subpath;
786 path.Replace(_T('\\'),_T('/'));
787 if(!path.IsEmpty())
788 if(path[path.GetLength()-1] != _T('/'))
789 path += _T('/'); //Add trail / to show it is directory, not file name.
791 CString lowcasepath = path;
792 lowcasepath.MakeLower();
794 if(status)
796 g_IndexFileMap.CheckAndUpdate(gitdir, true);
798 SHARED_INDEX_PTR indexptr = g_IndexFileMap.SafeGet(gitdir);
800 if (indexptr == NULL)
802 *status = git_wc_status_unversioned;
803 return 0;
806 int pos=SearchInSortVector(*indexptr,lowcasepath.GetBuffer(),lowcasepath.GetLength());
807 lowcasepath.ReleaseBuffer();
809 //Not In Version Contorl
810 if(pos<0)
812 if(!IsIgnore)
814 *status = git_wc_status_unversioned;
815 if(callback)
816 callback(gitdir + _T("/") + path, *status, false, pData, false, false);
817 return 0;
819 //Check ignore always.
821 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,path))
822 g_IgnoreList.LoadAllIgnoreFile(gitdir,path);
824 if(g_IgnoreList.IsIgnore(path,gitdir))
825 *status = git_wc_status_ignored;
826 else
827 *status = git_wc_status_unversioned;
829 g_HeadFileMap.CheckHeadAndUpdate(gitdir, false);
831 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
832 //Check init repository
833 if (treeptr->HeadIsEmpty() && path.IsEmpty())
834 *status = git_wc_status_normal;
838 else // In version control
840 *status = git_wc_status_normal;
842 int start=0;
843 int end=0;
844 if(path.IsEmpty())
846 start=0;
847 end = (int)indexptr->size() - 1;
849 LPTSTR lowcasepathBuffer = lowcasepath.GetBuffer();
850 GetRangeInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos);
851 lowcasepath.ReleaseBuffer();
852 CGitIndexList::iterator it;
854 it = indexptr->begin()+start;
856 // Check Conflict;
857 for (int i = start; i <= end; ++i)
859 if (((*it).m_Flags & GIT_IDXENTRY_STAGEMASK) !=0)
861 *status = git_wc_status_conflicted;
862 if(callback)
864 int dirpos = (*it).m_FileName.Find(_T('/'), path.GetLength());
865 if(dirpos<0 || IsRecursive)
866 callback(gitdir + _T("\\") + it->m_FileName, git_wc_status_conflicted, false, pData, false, false);
868 else
869 break;
871 ++it;
874 if( IsFul && (*status != git_wc_status_conflicted))
876 *status = git_wc_status_normal;
878 g_HeadFileMap.CheckHeadAndUpdate(gitdir);
880 //Check Add
881 it = indexptr->begin()+start;
885 //Check if new init repository
886 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
888 if (!treeptr->empty() || treeptr->HeadIsEmpty())
890 for (int i = start; i<= end; ++i)
892 pos =SearchInSortVector(*treeptr, (*it).m_FileName.GetBuffer(), -1);
893 (*it).m_FileName.ReleaseBuffer();
895 if(pos < 0)
897 *status = max(git_wc_status_added, *status); // added file found
898 if(callback)
900 int dirpos = (*it).m_FileName.Find(_T('/'), path.GetLength());
901 if(dirpos<0 || IsRecursive)
902 callback(gitdir + _T("\\") + it->m_FileName, git_wc_status_added, false, pData, false, false);
905 else
906 break;
909 if( pos>=0 && treeptr->at(pos).m_Hash != (*it).m_IndexHash)
911 *status = max(git_wc_status_modified, *status); // modified file found
912 if(callback)
914 int dirpos = (*it).m_FileName.Find(_T('/'), path.GetLength());
915 if(dirpos<0 || IsRecursive)
916 callback(gitdir + _T("\\") + it->m_FileName, git_wc_status_modified, false, pData, ((*it).m_Flags & GIT_IDXENTRY_VALID) && !((*it).m_Flags & GIT_IDXENTRY_SKIP_WORKTREE), ((*it).m_Flags & GIT_IDXENTRY_SKIP_WORKTREE) != 0);
919 else
920 break;
923 ++it;
926 //Check Delete
927 if( *status == git_wc_status_normal )
929 pos = SearchInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength());
930 if(pos <0)
932 *status = max(git_wc_status_added, *status); // added file found
935 else
937 int hstart,hend;
938 GetRangeInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength(), &hstart, &hend, pos);
939 CGitHeadFileList::iterator hit;
940 hit = treeptr->begin() + hstart;
941 CGitHeadFileList::iterator lastElement = treeptr->end();
942 for (int i = hstart; i <= hend && hit != lastElement; ++i)
944 if( SearchInSortVector(*indexptr,(*hit).m_FileName.GetBuffer(),-1) < 0)
946 (*hit).m_FileName.ReleaseBuffer();
947 *status = max(git_wc_status_deleted, *status); // deleted file found
948 break;
950 (*hit).m_FileName.ReleaseBuffer();
951 ++hit;
956 }/* End lock*/
958 lowcasepath.ReleaseBuffer();
959 // If define callback, it need update each file status.
960 // If not define callback, status == git_wc_status_conflicted, needn't check each file status
961 // because git_wc_status_conflicted is highest.s
962 if(callback || (*status != git_wc_status_conflicted))
964 //Check File Time;
965 //if(IsRecursive)
967 CString sub, currentPath;
968 it = indexptr->begin()+start;
969 for (int i = start; i <= end; ++i, ++it)
971 if( !IsRecursive )
973 //skip child directory
974 int pos = (*it).m_FileName.Find(_T('/'), path.GetLength());
976 if( pos > 0)
978 currentPath = (*it).m_FileName.Left(pos);
979 if( callback && (sub != currentPath) )
981 sub = currentPath;
982 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": index subdir %s\n"),sub);
983 if(callback) callback(gitdir + _T("\\") + sub,
984 git_wc_status_normal, true, pData, false, false);
986 continue;
990 git_wc_status_kind filestatus = git_wc_status_none;
991 bool assumeValid = false;
992 bool skipWorktree = false;
993 GetFileStatus(gitdir, (*it).m_FileName, &filestatus, IsFul, IsRecursive, IsIgnore, callback, pData, &assumeValid, &skipWorktree);
999 if(callback) callback(gitdir + _T("/") + subpath, *status, true, pData, false, false);
1002 }catch(...)
1004 if(status)
1005 *status = git_wc_status_none;
1006 return -1;
1009 return 0;
1012 bool GitStatus::IsExistIndexLockFile(const CString &gitdir)
1014 CString sDirName= gitdir;
1016 for (;;)
1018 if(PathFileExists(sDirName + _T("\\.git")))
1020 if(PathFileExists(g_AdminDirMap.GetAdminDir(sDirName) + _T("index.lock")))
1021 return true;
1022 else
1023 return false;
1026 int x = sDirName.ReverseFind(_T('\\'));
1027 if (x < 2)
1028 return false;
1030 sDirName = sDirName.Left(x);
1034 bool GitStatus::ReleasePath(const CString &gitdir)
1036 g_IndexFileMap.SafeClear(gitdir);
1037 return true;