Fixed typo: Store CGitHeadFileList after it was populated correctly
[TortoiseGit.git] / src / Git / GitStatus.cpp
blob98e55102ae2905b6cf950fcc0e60273b19e823dc
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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 "resource.h"
23 #include "..\TortoiseShell\resource.h"
24 //#include "git_config.h"
25 #include "GitStatus.h"
26 #include "UnicodeUtils.h"
27 //#include "GitGlobal.h"
28 //#include "GitHelpers.h"
29 #ifdef _MFC_VER
30 //# include "Git.h"
31 //# include "MessageBox.h"
32 //# include "registry.h"
33 //# include "TGitPath.h"
34 //# include "PathUtils.h"
35 #endif
36 #include "git.h"
37 #include "git2.h"
38 #include "gitindex.h"
39 #include "shellcache.h"
41 extern CGitAdminDirMap g_AdminDirMap;
42 CGitIndexFileMap g_IndexFileMap;
43 CGitHeadFileMap g_HeadFileMap;
44 CGitIgnoreList g_IgnoreList;
46 GitStatus::GitStatus()
47 : status(NULL)
51 GitStatus::~GitStatus(void)
55 // static method
56 git_wc_status_kind GitStatus::GetAllStatus(const CTGitPath& path, git_depth_t depth, bool * assumeValid, bool * skipWorktree)
58 git_wc_status_kind statuskind;
59 BOOL err;
60 BOOL isDir;
61 CString sProjectRoot;
63 isDir = path.IsDirectory();
64 if (!path.HasAdminDir(&sProjectRoot))
65 return git_wc_status_none;
67 // rev.kind = git_opt_revision_unspecified;
68 statuskind = git_wc_status_none;
70 const BOOL bIsRecursive = (depth == git_depth_infinity || depth == git_depth_unknown); // taken from SVN source
72 CString sSubPath;
73 CString s = path.GetWinPathString();
74 if (s.GetLength() > sProjectRoot.GetLength())
76 if (sProjectRoot.GetLength() == 3 && sProjectRoot[1] == _T(':'))
77 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength());
78 else
79 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() - 1/*otherwise it gets initial slash*/);
82 bool isfull = ((DWORD)CRegStdDWORD(_T("Software\\TortoiseGit\\CacheType"),
83 GetSystemMetrics(SM_REMOTESESSION) ? ShellCache::dll : ShellCache::exe) == ShellCache::dllFull);
85 if(isDir)
87 err = GetDirStatus(sProjectRoot,sSubPath,&statuskind, isfull,bIsRecursive,isfull,NULL, NULL);
88 // folders must not be displayed as added or deleted only as modified (this is for Shell Overlay-Modes)
89 if (statuskind == git_wc_status_unversioned && sSubPath.IsEmpty())
90 statuskind = git_wc_status_normal;
91 else if (statuskind == git_wc_status_deleted || statuskind == git_wc_status_added)
92 statuskind = git_wc_status_modified;
94 else
96 err = GetFileStatus(sProjectRoot, sSubPath, &statuskind, isfull, false, isfull, NULL, NULL, assumeValid, skipWorktree);
99 return statuskind;
102 // static method
103 git_wc_status_kind GitStatus::GetAllStatusRecursive(const CTGitPath& path)
105 return GetAllStatus(path, git_depth_infinity);
108 // static method
109 git_wc_status_kind GitStatus::GetMoreImportant(git_wc_status_kind status1, git_wc_status_kind status2)
111 if (GetStatusRanking(status1) >= GetStatusRanking(status2))
112 return status1;
113 return status2;
115 // static private method
116 int GitStatus::GetStatusRanking(git_wc_status_kind status)
118 switch (status)
120 case git_wc_status_none:
121 return 0;
122 case git_wc_status_unversioned:
123 return 1;
124 case git_wc_status_ignored:
125 return 2;
126 case git_wc_status_incomplete:
127 return 4;
128 case git_wc_status_normal:
129 case git_wc_status_external:
130 return 5;
131 case git_wc_status_added:
132 return 6;
133 case git_wc_status_missing:
134 return 7;
135 case git_wc_status_deleted:
136 return 8;
137 case git_wc_status_replaced:
138 return 9;
139 case git_wc_status_modified:
140 return 10;
141 case git_wc_status_merged:
142 return 11;
143 case git_wc_status_conflicted:
144 return 12;
145 case git_wc_status_obstructed:
146 return 13;
148 return 0;
151 void GitStatus::GetStatus(const CTGitPath& path, bool /*update*/ /* = false */, bool noignore /* = false */, bool /*noexternals*/ /* = false */)
153 // NOTE: unlike the SVN version this one does not cache the enumerated files, because in practice no code in all of
154 // Tortoise uses this, all places that call GetStatus create a temp GitStatus object which gets destroyed right
155 // after the call again
157 CString sProjectRoot;
158 if ( !path.HasAdminDir(&sProjectRoot) )
159 return;
161 bool isfull = ((DWORD)CRegStdDWORD(_T("Software\\TortoiseGit\\CacheType"),
162 GetSystemMetrics(SM_REMOTESESSION) ? ShellCache::dll : ShellCache::exe) == ShellCache::dllFull);
165 LPCTSTR lpszSubPath = NULL;
166 CString sSubPath;
167 CString s = path.GetWinPathString();
168 if (s.GetLength() > sProjectRoot.GetLength())
170 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength());
171 lpszSubPath = sSubPath;
172 // skip initial slash if necessary
173 if (*lpszSubPath == _T('\\'))
174 lpszSubPath++;
177 m_status.prop_status = m_status.text_status = git_wc_status_none;
178 m_status.assumeValid = false;
179 m_status.skipWorktree = false;
181 if(path.IsDirectory())
183 m_err = GetDirStatus(sProjectRoot,CString(lpszSubPath),&m_status.text_status , isfull, false,!noignore, NULL, NULL);
186 else
188 m_err = GetFileStatus(sProjectRoot, CString(lpszSubPath), &m_status.text_status ,isfull, false,!noignore, NULL,NULL, &m_status.assumeValid, &m_status.skipWorktree);
192 // Error present if function is not under version control
193 if (m_err)
195 status = NULL;
196 return;
199 status = &m_status;
202 void GitStatus::GetStatusString(git_wc_status_kind status, size_t buflen, TCHAR * string)
204 TCHAR * buf;
205 switch (status)
207 case git_wc_status_none:
208 buf = _T("none\0");
209 break;
210 case git_wc_status_unversioned:
211 buf = _T("unversioned\0");
212 break;
213 case git_wc_status_normal:
214 buf = _T("normal\0");
215 break;
216 case git_wc_status_added:
217 buf = _T("added\0");
218 break;
219 case git_wc_status_missing:
220 buf = _T("missing\0");
221 break;
222 case git_wc_status_deleted:
223 buf = _T("deleted\0");
224 break;
225 case git_wc_status_replaced:
226 buf = _T("replaced\0");
227 break;
228 case git_wc_status_modified:
229 buf = _T("modified\0");
230 break;
231 case git_wc_status_merged:
232 buf = _T("merged\0");
233 break;
234 case git_wc_status_conflicted:
235 buf = _T("conflicted\0");
236 break;
237 case git_wc_status_obstructed:
238 buf = _T("obstructed\0");
239 break;
240 case git_wc_status_ignored:
241 buf = _T("ignored");
242 break;
243 case git_wc_status_external:
244 buf = _T("external");
245 break;
246 case git_wc_status_incomplete:
247 buf = _T("incomplete\0");
248 break;
249 default:
250 buf = _T("\0");
251 break;
253 _stprintf_s(string, buflen, _T("%s"), buf);
256 void GitStatus::GetStatusString(HINSTANCE hInst, git_wc_status_kind status, TCHAR * string, int size, WORD lang)
258 switch (status)
260 case git_wc_status_none:
261 LoadStringEx(hInst, IDS_STATUSNONE, string, size, lang);
262 break;
263 case git_wc_status_unversioned:
264 LoadStringEx(hInst, IDS_STATUSUNVERSIONED, string, size, lang);
265 break;
266 case git_wc_status_normal:
267 LoadStringEx(hInst, IDS_STATUSNORMAL, string, size, lang);
268 break;
269 case git_wc_status_added:
270 LoadStringEx(hInst, IDS_STATUSADDED, string, size, lang);
271 break;
272 case git_wc_status_missing:
273 LoadStringEx(hInst, IDS_STATUSABSENT, string, size, lang);
274 break;
275 case git_wc_status_deleted:
276 LoadStringEx(hInst, IDS_STATUSDELETED, string, size, lang);
277 break;
278 case git_wc_status_replaced:
279 LoadStringEx(hInst, IDS_STATUSREPLACED, string, size, lang);
280 break;
281 case git_wc_status_modified:
282 LoadStringEx(hInst, IDS_STATUSMODIFIED, string, size, lang);
283 break;
284 case git_wc_status_merged:
285 LoadStringEx(hInst, IDS_STATUSMERGED, string, size, lang);
286 break;
287 case git_wc_status_conflicted:
288 LoadStringEx(hInst, IDS_STATUSCONFLICTED, string, size, lang);
289 break;
290 case git_wc_status_ignored:
291 LoadStringEx(hInst, IDS_STATUSIGNORED, string, size, lang);
292 break;
293 case git_wc_status_obstructed:
294 LoadStringEx(hInst, IDS_STATUSOBSTRUCTED, string, size, lang);
295 break;
296 case git_wc_status_external:
297 LoadStringEx(hInst, IDS_STATUSEXTERNAL, string, size, lang);
298 break;
299 case git_wc_status_incomplete:
300 LoadStringEx(hInst, IDS_STATUSINCOMPLETE, string, size, lang);
301 break;
302 default:
303 LoadStringEx(hInst, IDS_STATUSNONE, string, size, lang);
304 break;
308 int GitStatus::LoadStringEx(HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax, WORD wLanguage)
310 const STRINGRESOURCEIMAGE* pImage;
311 const STRINGRESOURCEIMAGE* pImageEnd;
312 ULONG nResourceSize;
313 HGLOBAL hGlobal;
314 UINT iIndex;
315 int ret;
317 HRSRC hResource = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE(((uID>>4)+1)), wLanguage);
318 if (!hResource)
320 // try the default language before giving up!
321 hResource = FindResource(hInstance, MAKEINTRESOURCE(((uID>>4)+1)), RT_STRING);
322 if (!hResource)
323 return 0;
325 hGlobal = LoadResource(hInstance, hResource);
326 if (!hGlobal)
327 return 0;
328 pImage = (const STRINGRESOURCEIMAGE*)::LockResource(hGlobal);
329 if(!pImage)
330 return 0;
332 nResourceSize = ::SizeofResource(hInstance, hResource);
333 pImageEnd = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+nResourceSize);
334 iIndex = uID&0x000f;
336 while ((iIndex > 0) && (pImage < pImageEnd))
338 pImage = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+(sizeof(STRINGRESOURCEIMAGE)+(pImage->nLength*sizeof(WCHAR))));
339 iIndex--;
341 if (pImage >= pImageEnd)
342 return 0;
343 if (pImage->nLength == 0)
344 return 0;
346 ret = pImage->nLength;
347 if (pImage->nLength > nBufferMax)
349 wcsncpy_s(lpBuffer, nBufferMax, pImage->achString, pImage->nLength-1);
350 lpBuffer[nBufferMax-1] = 0;
352 else
354 wcsncpy_s(lpBuffer, nBufferMax, pImage->achString, pImage->nLength);
355 lpBuffer[ret] = 0;
357 return ret;
360 typedef CComCritSecLock<CComCriticalSection> CAutoLocker;
362 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)
366 CString path = pathParam;
368 TCHAR oldpath[MAX_PATH+1];
369 memset(oldpath,0,MAX_PATH+1);
371 path.Replace(_T('\\'),_T('/'));
373 CString lowcasepath =path;
374 lowcasepath.MakeLower();
376 if(status)
378 git_wc_status_kind st = git_wc_status_none;
379 CGitHash hash;
381 g_IndexFileMap.GetFileStatus(gitdir, path, &st, IsFull, false, callback, pData, &hash, true, assumeValid, skipWorktree);
383 if( st == git_wc_status_conflicted )
385 *status =st;
386 if(callback)
387 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
388 return 0;
391 if( st == git_wc_status_unversioned )
393 if(!IsIgnore)
395 *status = git_wc_status_unversioned;
396 if(callback)
397 callback(gitdir + _T("/") + path, *status, false, pData, *assumeValid, *skipWorktree);
398 return 0;
401 if( g_IgnoreList.CheckIgnoreChanged(gitdir,path))
403 g_IgnoreList.LoadAllIgnoreFile(gitdir,path);
405 if( g_IgnoreList.IsIgnore(path, gitdir) )
407 st = git_wc_status_ignored;
409 *status = st;
410 if(callback)
411 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
413 return 0;
416 if( st == git_wc_status_normal && IsFull)
419 g_HeadFileMap.CheckHeadUpdate(gitdir);
421 SHARED_TREE_PTR treeptr;
423 treeptr=g_HeadFileMap.SafeGet(gitdir);
425 if (!treeptr->HeadHashEqualsTreeHash())
427 treeptr = SHARED_TREE_PTR(new CGitHeadFileList());
429 treeptr->ReadHeadHash(gitdir);
431 // Init Repository
432 if (treeptr->HeadFileIsEmpty())
434 *status =st=git_wc_status_added;
435 if(callback)
436 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
437 return 0;
439 if(treeptr->ReadTree())
441 //Check if init repository
442 *status = treeptr->HeadIsEmpty() ? git_wc_status_added : st;
443 if(callback)
444 callback(gitdir + _T("/") + path, *status, false, pData, *assumeValid, *skipWorktree);
445 return 0;
447 g_HeadFileMap.SafeSet(gitdir, treeptr);
450 // Check Head Tree Hash;
452 //add item
454 int start =SearchInSortVector(*treeptr,lowcasepath.GetBuffer(),-1);
455 lowcasepath.ReleaseBuffer();
457 if(start<0)
459 *status =st=git_wc_status_added;
460 ATLTRACE(_T("File miss in head tree %s"), path);
461 if(callback)
462 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
463 return 0;
466 //staged and not commit
467 if( treeptr->at(start).m_Hash != hash )
469 *status =st=git_wc_status_modified;
470 if(callback)
471 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
472 return 0;
477 *status =st;
478 if(callback)
479 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
480 return 0;
483 catch(...)
485 if(status)
486 *status = git_wc_status_none;
487 return -1;
490 return 0;
494 bool GitStatus::IsGitReposChanged(const CString &gitdir,const CString &subpaths, int mode)
496 if( mode & GIT_MODE_INDEX)
498 return g_IndexFileMap.CheckAndUpdate(gitdir, true);
501 if( mode & GIT_MODE_HEAD)
503 if(g_HeadFileMap.CheckHeadUpdate(gitdir))
504 return true;
507 if( mode & GIT_MODE_IGNORE)
509 if(g_IgnoreList.CheckIgnoreChanged(gitdir,subpaths))
510 return true;
512 return false;
515 int GitStatus::LoadIgnoreFile(const CString &gitdir,const CString &subpaths)
517 return g_IgnoreList.LoadAllIgnoreFile(gitdir,subpaths);
519 int GitStatus::IsUnderVersionControl(const CString &gitdir, const CString &path, bool isDir,bool *isVersion)
521 if (g_IndexFileMap.IsUnderVersionControl(gitdir, path, isDir, isVersion))
522 return 1;
523 if (!*isVersion)
524 return g_HeadFileMap.IsUnderVersionControl(gitdir, path, isDir, isVersion);
525 return 0;
528 __int64 GitStatus::GetIndexFileTime(const CString &gitdir)
530 SHARED_INDEX_PTR ptr=g_IndexFileMap.SafeGet(gitdir);
531 if(ptr.get() == NULL)
532 return 0;
534 return ptr->m_LastModifyTime;
537 int GitStatus::IsIgnore(const CString &gitdir, const CString &path, bool *isIgnore)
539 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,path))
540 g_IgnoreList.LoadAllIgnoreFile(gitdir,path);
542 *isIgnore = g_IgnoreList.IsIgnore(path,gitdir);
544 return 0;
547 static bool SortFileName(CGitFileName &Item1, CGitFileName &Item2)
549 return Item1.m_FileName.Compare(Item2.m_FileName)<0;
552 int GitStatus::GetFileList(const CString &gitdir, const CString &subpath, std::vector<CGitFileName> &list)
554 WIN32_FIND_DATA data;
555 HANDLE handle=::FindFirstFile(gitdir+_T("\\")+subpath+_T("\\*.*"), &data);
558 if(_tcscmp(data.cFileName, _T(".git")) == 0)
559 continue;
561 if(_tcscmp(data.cFileName, _T(".")) == 0)
562 continue;
564 if(_tcscmp(data.cFileName, _T("..")) == 0)
565 continue;
567 CGitFileName filename;
569 filename.m_CaseFileName = filename.m_FileName = data.cFileName;
570 filename.m_FileName.MakeLower();
572 if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
574 filename.m_FileName += _T('/');
577 list.push_back(filename);
579 }while(::FindNextFile(handle, &data));
581 FindClose(handle);
583 std::sort(list.begin(), list.end(), SortFileName);
584 return 0;
587 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)
591 TCHAR oldpath[MAX_PATH+1];
592 memset(oldpath,0,MAX_PATH+1);
594 CString path =subpath;
596 path.Replace(_T('\\'),_T('/'));
597 if(!path.IsEmpty())
598 if(path[path.GetLength()-1] != _T('/'))
599 path += _T('/'); //Add trail / to show it is directory, not file name.
601 CString lowcasepath = path;
602 lowcasepath.MakeLower();
604 std::vector<CGitFileName> filelist;
605 GetFileList(gitdir, subpath, filelist);
607 if(status)
609 g_IndexFileMap.CheckAndUpdate(gitdir,true);
611 if(g_HeadFileMap.CheckHeadUpdate(gitdir) || g_HeadFileMap.IsHashChanged(gitdir))
613 SHARED_TREE_PTR treeptr(new CGitHeadFileList());
614 treeptr->ReadHeadHash(gitdir);
615 if (treeptr->ReadTree())
617 g_HeadFileMap.SafeSet(gitdir, treeptr);
621 SHARED_INDEX_PTR indexptr = g_IndexFileMap.SafeGet(gitdir);
622 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
624 std::vector<CGitFileName>::iterator it;
626 // new git working tree has no index file
627 if (indexptr.get() == NULL)
629 for(it = filelist.begin(); it < filelist.end(); it++)
631 CString casepath = path + it->m_CaseFileName;
633 bool bIsDir = false;
634 if (casepath.GetLength() > 0 && casepath[casepath.GetLength() - 1] == _T('/'))
635 bIsDir = true;
637 if (bIsDir) /*check if it is directory*/
639 if (::PathFileExists(gitdir + casepath + _T("\\.git")))
640 { /* That is git submodule */
641 *status = git_wc_status_unknown;
642 if (callback)
643 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
644 continue;
648 if (IsIgnore)
650 if (g_IgnoreList.CheckIgnoreChanged(gitdir, casepath))
651 g_IgnoreList.LoadAllIgnoreFile(gitdir, casepath);
653 if (g_IgnoreList.IsIgnore(casepath, gitdir))
654 *status = git_wc_status_ignored;
655 else if (bIsDir)
656 continue;
657 else
658 *status = git_wc_status_unversioned;
660 else if (bIsDir)
661 continue;
662 else
663 *status = git_wc_status_unversioned;
665 if(callback)
666 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
668 return 0;
671 CString onepath;
672 CString casepath;
673 for(it = filelist.begin(); it<filelist.end();it++)
675 casepath=onepath = path;
676 onepath.MakeLower();
677 onepath += it->m_FileName;
678 casepath += it->m_CaseFileName;
680 LPTSTR onepathBuffer = onepath.GetBuffer();
681 int pos = SearchInSortVector(*indexptr, onepathBuffer, onepath.GetLength());
682 int posintree = SearchInSortVector(*treeptr, onepathBuffer, onepath.GetLength());
683 onepath.ReleaseBuffer();
685 bool bIsDir =false;
686 if(onepath.GetLength()>0 && onepath[onepath.GetLength()-1] == _T('/'))
687 bIsDir =true;
689 if(pos <0 && posintree<0)
691 if(onepath.GetLength() ==0)
692 continue;
694 if(bIsDir) /*check if it is directory*/
696 if(::PathFileExists(gitdir+onepath+_T("/.git")))
697 { /* That is git submodule */
698 *status = git_wc_status_unknown;
699 if(callback)
700 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
701 continue;
705 if(!IsIgnore)
707 *status = git_wc_status_unversioned;
708 if(callback)
709 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
710 continue;
713 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,casepath))
714 g_IgnoreList.LoadAllIgnoreFile(gitdir,casepath);
716 if(g_IgnoreList.IsIgnore(casepath,gitdir))
717 *status = git_wc_status_ignored;
718 else
719 *status = git_wc_status_unversioned;
721 if(callback)
722 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
725 else if(pos <0 && posintree>=0) /* check if file delete in index */
727 *status = git_wc_status_deleted;
728 if(callback)
729 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
732 else if(pos >=0 && posintree <0) /* Check if file added */
734 *status = git_wc_status_added;
735 if(callback)
736 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
738 else
740 if(onepath.GetLength() ==0)
741 continue;
743 if(onepath[onepath.GetLength()-1] == _T('/'))
745 *status = git_wc_status_normal;
746 if(callback)
747 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
749 else
751 bool assumeValid = false;
752 bool skipWorktree = false;
753 git_wc_status_kind filestatus;
754 GetFileStatus(gitdir, casepath, &filestatus, IsFul, IsRecursive, IsIgnore, callback, pData, &assumeValid, &skipWorktree);
758 }/*End of For*/
760 /* Check deleted file in system */
761 LPTSTR lowcasepathBuffer = lowcasepath.GetBuffer();
762 int start=0, end=0;
763 int pos=SearchInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength());
765 if (GetRangeInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos))
767 CGitIndexList::iterator it;
768 CString oldstring;
770 for(it = indexptr->begin()+start; it <= indexptr->begin()+end; it++)
772 int start = lowcasepath.GetLength();
773 int index = (*it).m_FileName.Find(_T('/'), start);
774 if(index<0)
775 index = (*it).m_FileName.GetLength();
777 CString filename = (*it).m_FileName.Mid(start, index-start);
778 if(oldstring != filename)
780 oldstring = filename;
781 if(SearchInSortVector(filelist, filename.GetBuffer(), filename.GetLength())<0)
783 *status = git_wc_status_deleted;
784 if(callback)
785 callback(gitdir + _T("/") + filename, *status, false, pData, false, false);
791 start = end =0;
792 pos=SearchInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength());
793 if (GetRangeInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos) == 0)
795 CGitHeadFileList::iterator it;
796 CString oldstring;
798 for(it = treeptr->begin()+start; it <= treeptr->begin()+end; it++)
800 int start = lowcasepath.GetLength();
801 int index = (*it).m_FileName.Find(_T('/'), start);
802 if(index<0)
803 index = (*it).m_FileName.GetLength();
805 CString filename = (*it).m_FileName.Mid(start, index-start);
806 if(oldstring != filename)
808 oldstring = filename;
809 if(SearchInSortVector(filelist, filename.GetBuffer(), filename.GetLength())<0)
811 *status = git_wc_status_deleted;
812 if(callback)
813 callback(gitdir + _T("/") + (*it).m_FileName, *status, false, pData, false, false);
818 lowcasepath.ReleaseBuffer();
820 }/*End of if status*/
821 }catch(...)
823 return -1;
825 return 0;
828 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)
832 TCHAR oldpath[MAX_PATH+1];
833 memset(oldpath,0,MAX_PATH+1);
835 CString path =subpath;
837 path.Replace(_T('\\'),_T('/'));
838 if(!path.IsEmpty())
839 if(path[path.GetLength()-1] != _T('/'))
840 path += _T('/'); //Add trail / to show it is directory, not file name.
842 CString lowcasepath = path;
843 lowcasepath.MakeLower();
845 if(status)
847 g_IndexFileMap.CheckAndUpdate(gitdir, true);
849 SHARED_INDEX_PTR indexptr = g_IndexFileMap.SafeGet(gitdir);
851 if (indexptr == NULL)
853 *status = git_wc_status_unversioned;
854 return 0;
857 int pos=SearchInSortVector(*indexptr,lowcasepath.GetBuffer(),lowcasepath.GetLength());
858 lowcasepath.ReleaseBuffer();
860 //Not In Version Contorl
861 if(pos<0)
863 if(!IsIgnore)
865 *status = git_wc_status_unversioned;
866 if(callback)
867 callback(gitdir + _T("/") + path, *status, false, pData, false, false);
868 return 0;
870 //Check ignore always.
872 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,path))
873 g_IgnoreList.LoadAllIgnoreFile(gitdir,path);
875 if(g_IgnoreList.IsIgnore(path,gitdir))
876 *status = git_wc_status_ignored;
877 else
878 *status = git_wc_status_unversioned;
880 g_HeadFileMap.CheckHeadUpdate(gitdir);
882 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
883 //Check init repository
884 if (treeptr->HeadIsEmpty() && path.IsEmpty())
885 *status = git_wc_status_normal;
889 else // In version control
891 *status = git_wc_status_normal;
893 int start=0;
894 int end=0;
895 if(path.IsEmpty())
897 start=0;
898 end = (int)indexptr->size() - 1;
900 LPTSTR lowcasepathBuffer = lowcasepath.GetBuffer();
901 GetRangeInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos);
902 lowcasepath.ReleaseBuffer();
903 CGitIndexList::iterator it;
905 it = indexptr->begin()+start;
907 // Check Conflict;
908 for(int i=start;i<=end;i++)
910 if (((*it).m_Flags & GIT_IDXENTRY_STAGEMASK) !=0)
912 *status = git_wc_status_conflicted;
913 if(callback)
915 int dirpos = (*it).m_FileName.Find(_T('/'), path.GetLength());
916 if(dirpos<0 || IsRecursive)
917 callback(gitdir + _T("\\") + it->m_FileName, git_wc_status_conflicted, false, pData, false, false);
919 else
920 break;
922 it++;
925 if( IsFul && (*status != git_wc_status_conflicted))
927 *status = git_wc_status_normal;
929 if(g_HeadFileMap.CheckHeadUpdate(gitdir))
931 SHARED_TREE_PTR treeptr(new CGitHeadFileList());
933 treeptr->ReadHeadHash(gitdir);
935 if(treeptr->ReadTree())
937 g_HeadFileMap.SafeSet(gitdir, treeptr);
940 //Check Add
941 it = indexptr->begin()+start;
945 //Check if new init repository
946 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
948 if (!treeptr->empty() || treeptr->HeadIsEmpty())
950 for(int i=start;i<=end;i++)
952 pos =SearchInSortVector(*treeptr, (*it).m_FileName.GetBuffer(), -1);
953 (*it).m_FileName.ReleaseBuffer();
955 if(pos < 0)
957 *status = max(git_wc_status_added, *status); // added file found
958 if(callback)
960 int dirpos = (*it).m_FileName.Find(_T('/'), path.GetLength());
961 if(dirpos<0 || IsRecursive)
962 callback(gitdir + _T("\\") + it->m_FileName, git_wc_status_added, false, pData, false, false);
965 else
966 break;
969 if( pos>=0 && treeptr->at(pos).m_Hash != (*it).m_IndexHash)
971 *status = max(git_wc_status_modified, *status); // modified file found
972 if(callback)
974 int dirpos = (*it).m_FileName.Find(_T('/'), path.GetLength());
975 if(dirpos<0 || IsRecursive)
976 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);
979 else
980 break;
983 it++;
986 //Check Delete
987 if( *status == git_wc_status_normal )
989 pos = SearchInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength());
990 if(pos <0)
992 *status = max(git_wc_status_added, *status); // added file found
995 else
997 int hstart,hend;
998 GetRangeInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength(), &hstart, &hend, pos);
999 CGitHeadFileList::iterator hit;
1000 hit = treeptr->begin() + hstart;
1001 CGitHeadFileList::iterator lastElement = treeptr->end();
1002 for(int i=hstart; i <= hend && hit != lastElement; i++)
1004 if( SearchInSortVector(*indexptr,(*hit).m_FileName.GetBuffer(),-1) < 0)
1006 (*hit).m_FileName.ReleaseBuffer();
1007 *status = max(git_wc_status_deleted, *status); // deleted file found
1008 break;
1010 (*hit).m_FileName.ReleaseBuffer();
1011 hit++;
1016 }/* End lock*/
1018 lowcasepath.ReleaseBuffer();
1019 // If define callback, it need update each file status.
1020 // If not define callback, status == git_wc_status_conflicted, needn't check each file status
1021 // because git_wc_status_conflicted is highest.s
1022 if(callback || (*status != git_wc_status_conflicted))
1024 //Check File Time;
1025 //if(IsRecursive)
1027 CString sub, currentPath;
1028 it = indexptr->begin()+start;
1029 for(int i=start;i<=end;i++,it++)
1031 if( !IsRecursive )
1033 //skip child directory
1034 int pos = (*it).m_FileName.Find(_T('/'), path.GetLength());
1036 if( pos > 0)
1038 currentPath = (*it).m_FileName.Left(pos);
1039 if( callback && (sub != currentPath) )
1041 sub = currentPath;
1042 ATLTRACE(_T("index subdir %s\n"),sub);
1043 if(callback) callback(gitdir + _T("\\") + sub,
1044 git_wc_status_normal, true, pData, false, false);
1046 continue;
1050 git_wc_status_kind filestatus = git_wc_status_none;
1051 bool assumeValid = false;
1052 bool skipWorktree = false;
1053 GetFileStatus(gitdir, (*it).m_FileName, &filestatus, IsFul, IsRecursive, IsIgnore, callback, pData, &assumeValid, &skipWorktree);
1059 if(callback) callback(gitdir + _T("/") + subpath, *status, true, pData, false, false);
1062 }catch(...)
1064 if(status)
1065 *status = git_wc_status_none;
1066 return -1;
1069 return 0;
1072 bool GitStatus::IsExistIndexLockFile(const CString &gitdir)
1074 CString sDirName= gitdir;
1076 for (;;)
1078 if(PathFileExists(sDirName + _T("\\.git")))
1080 if(PathFileExists(g_AdminDirMap.GetAdminDir(sDirName) + _T("index.lock")))
1081 return true;
1082 else
1083 return false;
1086 int x = sDirName.ReverseFind(_T('\\'));
1087 if (x < 2)
1088 return false;
1090 sDirName = sDirName.Left(x);