494772f189e1d704fff9da347ca68a13bbd0c3b8
[TortoiseGit.git] / src / Git / GitStatus.cpp
blob494772f189e1d704fff9da347ca68a13bbd0c3b8
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 "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.CheckHeadAndUpdate(gitdir);
421 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
423 // Check Head Tree Hash;
425 //add item
427 int start =SearchInSortVector(*treeptr,lowcasepath.GetBuffer(),-1);
428 lowcasepath.ReleaseBuffer();
430 if(start<0)
432 *status =st=git_wc_status_added;
433 ATLTRACE(_T("File miss in head tree %s"), path);
434 if(callback)
435 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
436 return 0;
439 //staged and not commit
440 if( treeptr->at(start).m_Hash != hash )
442 *status =st=git_wc_status_modified;
443 if(callback)
444 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
445 return 0;
450 *status =st;
451 if(callback)
452 callback(gitdir + _T("/") + path, st, false, pData, *assumeValid, *skipWorktree);
453 return 0;
456 catch(...)
458 if(status)
459 *status = git_wc_status_none;
460 return -1;
463 return 0;
467 bool GitStatus::HasIgnoreFilesChanged(const CString &gitdir, const CString &subpaths)
469 return g_IgnoreList.CheckIgnoreChanged(gitdir, subpaths);
472 int GitStatus::LoadIgnoreFile(const CString &gitdir,const CString &subpaths)
474 return g_IgnoreList.LoadAllIgnoreFile(gitdir,subpaths);
476 int GitStatus::IsUnderVersionControl(const CString &gitdir, const CString &path, bool isDir,bool *isVersion)
478 if (g_IndexFileMap.IsUnderVersionControl(gitdir, path, isDir, isVersion))
479 return 1;
480 if (!*isVersion)
481 return g_HeadFileMap.IsUnderVersionControl(gitdir, path, isDir, isVersion);
482 return 0;
485 __int64 GitStatus::GetIndexFileTime(const CString &gitdir)
487 SHARED_INDEX_PTR ptr=g_IndexFileMap.SafeGet(gitdir);
488 if(ptr.get() == NULL)
489 return 0;
491 return ptr->m_LastModifyTime;
494 int GitStatus::IsIgnore(const CString &gitdir, const CString &path, bool *isIgnore)
496 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,path))
497 g_IgnoreList.LoadAllIgnoreFile(gitdir,path);
499 *isIgnore = g_IgnoreList.IsIgnore(path,gitdir);
501 return 0;
504 static bool SortFileName(CGitFileName &Item1, CGitFileName &Item2)
506 return Item1.m_FileName.Compare(Item2.m_FileName)<0;
509 int GitStatus::GetFileList(const CString &gitdir, const CString &subpath, std::vector<CGitFileName> &list)
511 WIN32_FIND_DATA data;
512 HANDLE handle=::FindFirstFile(gitdir+_T("\\")+subpath+_T("\\*.*"), &data);
515 if(_tcscmp(data.cFileName, _T(".git")) == 0)
516 continue;
518 if(_tcscmp(data.cFileName, _T(".")) == 0)
519 continue;
521 if(_tcscmp(data.cFileName, _T("..")) == 0)
522 continue;
524 CGitFileName filename;
526 filename.m_CaseFileName = filename.m_FileName = data.cFileName;
527 filename.m_FileName.MakeLower();
529 if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
531 filename.m_FileName += _T('/');
534 list.push_back(filename);
536 }while(::FindNextFile(handle, &data));
538 FindClose(handle);
540 std::sort(list.begin(), list.end(), SortFileName);
541 return 0;
544 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)
548 TCHAR oldpath[MAX_PATH+1];
549 memset(oldpath,0,MAX_PATH+1);
551 CString path =subpath;
553 path.Replace(_T('\\'),_T('/'));
554 if(!path.IsEmpty())
555 if(path[path.GetLength()-1] != _T('/'))
556 path += _T('/'); //Add trail / to show it is directory, not file name.
558 CString lowcasepath = path;
559 lowcasepath.MakeLower();
561 std::vector<CGitFileName> filelist;
562 GetFileList(gitdir, subpath, filelist);
564 if(status)
566 g_IndexFileMap.CheckAndUpdate(gitdir,true);
568 g_HeadFileMap.CheckHeadAndUpdate(gitdir);
570 SHARED_INDEX_PTR indexptr = g_IndexFileMap.SafeGet(gitdir);
571 SHARED_TREE_PTR treeptr = g_HeadFileMap.SafeGet(gitdir);
573 std::vector<CGitFileName>::iterator it;
575 // new git working tree has no index file
576 if (indexptr.get() == NULL)
578 for (it = filelist.begin(); it < filelist.end(); ++it)
580 CString casepath = path + it->m_CaseFileName;
582 bool bIsDir = false;
583 if (casepath.GetLength() > 0 && casepath[casepath.GetLength() - 1] == _T('/'))
584 bIsDir = true;
586 if (bIsDir) /*check if it is directory*/
588 if (::PathFileExists(gitdir + casepath + _T("\\.git")))
589 { /* That is git submodule */
590 *status = git_wc_status_unknown;
591 if (callback)
592 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
593 continue;
597 if (IsIgnore)
599 if (g_IgnoreList.CheckIgnoreChanged(gitdir, casepath))
600 g_IgnoreList.LoadAllIgnoreFile(gitdir, casepath);
602 if (g_IgnoreList.IsIgnore(casepath, gitdir))
603 *status = git_wc_status_ignored;
604 else if (bIsDir)
605 continue;
606 else
607 *status = git_wc_status_unversioned;
609 else if (bIsDir)
610 continue;
611 else
612 *status = git_wc_status_unversioned;
614 if(callback)
615 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
617 return 0;
620 CString onepath;
621 CString casepath;
622 for (it = filelist.begin(); it < filelist.end(); ++it)
624 casepath=onepath = path;
625 onepath.MakeLower();
626 onepath += it->m_FileName;
627 casepath += it->m_CaseFileName;
629 LPTSTR onepathBuffer = onepath.GetBuffer();
630 int pos = SearchInSortVector(*indexptr, onepathBuffer, onepath.GetLength());
631 int posintree = SearchInSortVector(*treeptr, onepathBuffer, onepath.GetLength());
632 onepath.ReleaseBuffer();
634 bool bIsDir =false;
635 if(onepath.GetLength()>0 && onepath[onepath.GetLength()-1] == _T('/'))
636 bIsDir =true;
638 if(pos <0 && posintree<0)
640 if(onepath.GetLength() ==0)
641 continue;
643 if(bIsDir) /*check if it is directory*/
645 if(::PathFileExists(gitdir+onepath+_T("/.git")))
646 { /* That is git submodule */
647 *status = git_wc_status_unknown;
648 if(callback)
649 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
650 continue;
654 if(!IsIgnore)
656 *status = git_wc_status_unversioned;
657 if(callback)
658 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
659 continue;
662 if(::g_IgnoreList.CheckIgnoreChanged(gitdir,casepath))
663 g_IgnoreList.LoadAllIgnoreFile(gitdir,casepath);
665 if(g_IgnoreList.IsIgnore(casepath,gitdir))
666 *status = git_wc_status_ignored;
667 else
668 *status = git_wc_status_unversioned;
670 if(callback)
671 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
674 else if(pos <0 && posintree>=0) /* check if file delete in index */
676 *status = git_wc_status_deleted;
677 if(callback)
678 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
681 else if(pos >=0 && posintree <0) /* Check if file added */
683 *status = git_wc_status_added;
684 if(callback)
685 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
687 else
689 if(onepath.GetLength() ==0)
690 continue;
692 if(onepath[onepath.GetLength()-1] == _T('/'))
694 *status = git_wc_status_normal;
695 if(callback)
696 callback(gitdir + _T("/") + casepath, *status, bIsDir, pData, false, false);
698 else
700 bool assumeValid = false;
701 bool skipWorktree = false;
702 git_wc_status_kind filestatus;
703 GetFileStatus(gitdir, casepath, &filestatus, IsFul, IsRecursive, IsIgnore, callback, pData, &assumeValid, &skipWorktree);
707 }/*End of For*/
709 /* Check deleted file in system */
710 LPTSTR lowcasepathBuffer = lowcasepath.GetBuffer();
711 int start=0, end=0;
712 int pos=SearchInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength());
714 if (GetRangeInSortVector(*indexptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos) == 0)
716 CGitIndexList::iterator it;
717 CString oldstring;
719 for (it = indexptr->begin() + start; it <= indexptr->begin() + end; ++it)
721 int start = lowcasepath.GetLength();
722 int index = (*it).m_FileName.Find(_T('/'), start);
723 if(index<0)
724 index = (*it).m_FileName.GetLength();
726 CString filename = (*it).m_FileName.Mid(start, index-start);
727 if(oldstring != filename)
729 oldstring = filename;
730 if(SearchInSortVector(filelist, filename.GetBuffer(), filename.GetLength())<0)
732 *status = git_wc_status_deleted;
733 if(callback)
734 callback(gitdir + _T("/") + filename, *status, false, pData, false, false);
740 start = end =0;
741 pos=SearchInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength());
742 if (GetRangeInSortVector(*treeptr, lowcasepathBuffer, lowcasepath.GetLength(), &start, &end, pos) == 0)
744 CGitHeadFileList::iterator it;
745 CString oldstring;
747 for (it = treeptr->begin() + start; it <= treeptr->begin() + end; ++it)
749 int start = lowcasepath.GetLength();
750 int index = (*it).m_FileName.Find(_T('/'), start);
751 if(index<0)
752 index = (*it).m_FileName.GetLength();
754 CString filename = (*it).m_FileName.Mid(start, index-start);
755 if(oldstring != filename)
757 oldstring = filename;
758 if(SearchInSortVector(filelist, filename.GetBuffer(), filename.GetLength())<0)
760 *status = git_wc_status_deleted;
761 if(callback)
762 callback(gitdir + _T("/") + (*it).m_FileName, *status, false, pData, false, false);
767 lowcasepath.ReleaseBuffer();
769 }/*End of if status*/
770 }catch(...)
772 return -1;
774 return 0;
777 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)
781 TCHAR oldpath[MAX_PATH+1];
782 memset(oldpath,0,MAX_PATH+1);
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 ATLTRACE(_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);