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.
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"
31 //# include "MessageBox.h"
32 //# include "registry.h"
33 //# include "TGitPath.h"
34 //# include "PathUtils.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()
51 GitStatus::~GitStatus(void)
56 git_wc_status_kind
GitStatus::GetAllStatus(const CTGitPath
& path
, git_depth_t depth
, bool * assumeValid
, bool * skipWorktree
)
58 git_wc_status_kind statuskind
;
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
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());
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
);
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
;
96 err
= GetFileStatus(sProjectRoot
, sSubPath
, &statuskind
, isfull
, false, isfull
, NULL
, NULL
, assumeValid
, skipWorktree
);
103 git_wc_status_kind
GitStatus::GetAllStatusRecursive(const CTGitPath
& path
)
105 return GetAllStatus(path
, git_depth_infinity
);
109 git_wc_status_kind
GitStatus::GetMoreImportant(git_wc_status_kind status1
, git_wc_status_kind status2
)
111 if (GetStatusRanking(status1
) >= GetStatusRanking(status2
))
115 // static private method
116 int GitStatus::GetStatusRanking(git_wc_status_kind status
)
120 case git_wc_status_none
:
122 case git_wc_status_unversioned
:
124 case git_wc_status_ignored
:
126 case git_wc_status_incomplete
:
128 case git_wc_status_normal
:
129 case git_wc_status_external
:
131 case git_wc_status_added
:
133 case git_wc_status_missing
:
135 case git_wc_status_deleted
:
137 case git_wc_status_replaced
:
139 case git_wc_status_modified
:
141 case git_wc_status_merged
:
143 case git_wc_status_conflicted
:
145 case git_wc_status_obstructed
:
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
) )
161 bool isfull
= ((DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\CacheType"),
162 GetSystemMetrics(SM_REMOTESESSION
) ? ShellCache::dll
: ShellCache::exe
) == ShellCache::dllFull
);
165 LPCTSTR lpszSubPath
= NULL
;
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('\\'))
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
);
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
202 void GitStatus::GetStatusString(git_wc_status_kind status
, size_t buflen
, TCHAR
* string
)
207 case git_wc_status_none
:
210 case git_wc_status_unversioned
:
211 buf
= _T("unversioned\0");
213 case git_wc_status_normal
:
214 buf
= _T("normal\0");
216 case git_wc_status_added
:
219 case git_wc_status_missing
:
220 buf
= _T("missing\0");
222 case git_wc_status_deleted
:
223 buf
= _T("deleted\0");
225 case git_wc_status_replaced
:
226 buf
= _T("replaced\0");
228 case git_wc_status_modified
:
229 buf
= _T("modified\0");
231 case git_wc_status_merged
:
232 buf
= _T("merged\0");
234 case git_wc_status_conflicted
:
235 buf
= _T("conflicted\0");
237 case git_wc_status_obstructed
:
238 buf
= _T("obstructed\0");
240 case git_wc_status_ignored
:
243 case git_wc_status_external
:
244 buf
= _T("external");
246 case git_wc_status_incomplete
:
247 buf
= _T("incomplete\0");
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
)
260 case git_wc_status_none
:
261 LoadStringEx(hInst
, IDS_STATUSNONE
, string
, size
, lang
);
263 case git_wc_status_unversioned
:
264 LoadStringEx(hInst
, IDS_STATUSUNVERSIONED
, string
, size
, lang
);
266 case git_wc_status_normal
:
267 LoadStringEx(hInst
, IDS_STATUSNORMAL
, string
, size
, lang
);
269 case git_wc_status_added
:
270 LoadStringEx(hInst
, IDS_STATUSADDED
, string
, size
, lang
);
272 case git_wc_status_missing
:
273 LoadStringEx(hInst
, IDS_STATUSABSENT
, string
, size
, lang
);
275 case git_wc_status_deleted
:
276 LoadStringEx(hInst
, IDS_STATUSDELETED
, string
, size
, lang
);
278 case git_wc_status_replaced
:
279 LoadStringEx(hInst
, IDS_STATUSREPLACED
, string
, size
, lang
);
281 case git_wc_status_modified
:
282 LoadStringEx(hInst
, IDS_STATUSMODIFIED
, string
, size
, lang
);
284 case git_wc_status_merged
:
285 LoadStringEx(hInst
, IDS_STATUSMERGED
, string
, size
, lang
);
287 case git_wc_status_conflicted
:
288 LoadStringEx(hInst
, IDS_STATUSCONFLICTED
, string
, size
, lang
);
290 case git_wc_status_ignored
:
291 LoadStringEx(hInst
, IDS_STATUSIGNORED
, string
, size
, lang
);
293 case git_wc_status_obstructed
:
294 LoadStringEx(hInst
, IDS_STATUSOBSTRUCTED
, string
, size
, lang
);
296 case git_wc_status_external
:
297 LoadStringEx(hInst
, IDS_STATUSEXTERNAL
, string
, size
, lang
);
299 case git_wc_status_incomplete
:
300 LoadStringEx(hInst
, IDS_STATUSINCOMPLETE
, string
, size
, lang
);
303 LoadStringEx(hInst
, IDS_STATUSNONE
, string
, size
, lang
);
308 int GitStatus::LoadStringEx(HINSTANCE hInstance
, UINT uID
, LPTSTR lpBuffer
, int nBufferMax
, WORD wLanguage
)
310 const STRINGRESOURCEIMAGE
* pImage
;
311 const STRINGRESOURCEIMAGE
* pImageEnd
;
317 HRSRC hResource
= FindResourceEx(hInstance
, RT_STRING
, MAKEINTRESOURCE(((uID
>>4)+1)), wLanguage
);
320 // try the default language before giving up!
321 hResource
= FindResource(hInstance
, MAKEINTRESOURCE(((uID
>>4)+1)), RT_STRING
);
325 hGlobal
= LoadResource(hInstance
, hResource
);
328 pImage
= (const STRINGRESOURCEIMAGE
*)::LockResource(hGlobal
);
332 nResourceSize
= ::SizeofResource(hInstance
, hResource
);
333 pImageEnd
= (const STRINGRESOURCEIMAGE
*)(LPBYTE(pImage
)+nResourceSize
);
336 while ((iIndex
> 0) && (pImage
< pImageEnd
))
338 pImage
= (const STRINGRESOURCEIMAGE
*)(LPBYTE(pImage
)+(sizeof(STRINGRESOURCEIMAGE
)+(pImage
->nLength
*sizeof(WCHAR
))));
341 if (pImage
>= pImageEnd
)
343 if (pImage
->nLength
== 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;
354 wcsncpy_s(lpBuffer
, nBufferMax
, pImage
->achString
, pImage
->nLength
);
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();
378 git_wc_status_kind st
= git_wc_status_none
;
381 g_IndexFileMap
.GetFileStatus(gitdir
, path
, &st
, IsFull
, false, callback
, pData
, &hash
, true, assumeValid
, skipWorktree
);
383 if( st
== git_wc_status_conflicted
)
387 callback(gitdir
+ _T("/") + path
, st
, false, pData
, *assumeValid
, *skipWorktree
);
391 if( st
== git_wc_status_unversioned
)
395 *status
= git_wc_status_unversioned
;
397 callback(gitdir
+ _T("/") + path
, *status
, false, pData
, *assumeValid
, *skipWorktree
);
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
;
411 callback(gitdir
+ _T("/") + path
, st
, false, pData
, *assumeValid
, *skipWorktree
);
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
);
432 if (treeptr
->HeadFileIsEmpty())
434 *status
=st
=git_wc_status_added
;
436 callback(gitdir
+ _T("/") + path
, st
, false, pData
, *assumeValid
, *skipWorktree
);
439 if(treeptr
->ReadTree())
441 //Check if init repository
442 *status
= treeptr
->HeadIsEmpty() ? git_wc_status_added
: st
;
444 callback(gitdir
+ _T("/") + path
, *status
, false, pData
, *assumeValid
, *skipWorktree
);
447 g_HeadFileMap
.SafeSet(gitdir
, treeptr
);
450 // Check Head Tree Hash;
454 int start
=SearchInSortVector(*treeptr
,lowcasepath
.GetBuffer(),-1);
455 lowcasepath
.ReleaseBuffer();
459 *status
=st
=git_wc_status_added
;
460 ATLTRACE(_T("File miss in head tree %s"), path
);
462 callback(gitdir
+ _T("/") + path
, st
, false, pData
, *assumeValid
, *skipWorktree
);
466 //staged and not commit
467 if( treeptr
->at(start
).m_Hash
!= hash
)
469 *status
=st
=git_wc_status_modified
;
471 callback(gitdir
+ _T("/") + path
, st
, false, pData
, *assumeValid
, *skipWorktree
);
479 callback(gitdir
+ _T("/") + path
, st
, false, pData
, *assumeValid
, *skipWorktree
);
486 *status
= git_wc_status_none
;
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
))
507 if( mode
& GIT_MODE_IGNORE
)
509 if(g_IgnoreList
.CheckIgnoreChanged(gitdir
,subpaths
))
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
))
524 return g_HeadFileMap
.IsUnderVersionControl(gitdir
, path
, isDir
, isVersion
);
528 __int64
GitStatus::GetIndexFileTime(const CString
&gitdir
)
530 SHARED_INDEX_PTR ptr
=g_IndexFileMap
.SafeGet(gitdir
);
531 if(ptr
.get() == NULL
)
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
);
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)
561 if(_tcscmp(data
.cFileName
, _T(".")) == 0)
564 if(_tcscmp(data
.cFileName
, _T("..")) == 0)
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
));
583 std::sort(list
.begin(), list
.end(), SortFileName
);
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('/'));
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
);
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
;
634 if (casepath
.GetLength() > 0 && casepath
[casepath
.GetLength() - 1] == _T('/'))
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
;
643 callback(gitdir
+ _T("/") + casepath
, *status
, bIsDir
, pData
, false, false);
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
;
658 *status
= git_wc_status_unversioned
;
663 *status
= git_wc_status_unversioned
;
666 callback(gitdir
+ _T("/") + casepath
, *status
, bIsDir
, pData
, false, false);
673 for(it
= filelist
.begin(); it
<filelist
.end();it
++)
675 casepath
=onepath
= path
;
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();
686 if(onepath
.GetLength()>0 && onepath
[onepath
.GetLength()-1] == _T('/'))
689 if(pos
<0 && posintree
<0)
691 if(onepath
.GetLength() ==0)
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
;
700 callback(gitdir
+ _T("/") + casepath
, *status
, bIsDir
, pData
, false, false);
707 *status
= git_wc_status_unversioned
;
709 callback(gitdir
+ _T("/") + casepath
, *status
, bIsDir
, pData
, false, false);
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
;
719 *status
= git_wc_status_unversioned
;
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
;
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
;
736 callback(gitdir
+ _T("/") + casepath
, *status
, bIsDir
, pData
, false, false);
740 if(onepath
.GetLength() ==0)
743 if(onepath
[onepath
.GetLength()-1] == _T('/'))
745 *status
= git_wc_status_normal
;
747 callback(gitdir
+ _T("/") + casepath
, *status
, bIsDir
, pData
, false, false);
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
);
760 /* Check deleted file in system */
761 LPTSTR lowcasepathBuffer
= lowcasepath
.GetBuffer();
763 int pos
=SearchInSortVector(*indexptr
, lowcasepathBuffer
, lowcasepath
.GetLength());
765 if (GetRangeInSortVector(*indexptr
, lowcasepathBuffer
, lowcasepath
.GetLength(), &start
, &end
, pos
))
767 CGitIndexList::iterator it
;
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
);
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
;
785 callback(gitdir
+ _T("/") + filename
, *status
, false, pData
, false, false);
792 pos
=SearchInSortVector(*treeptr
, lowcasepathBuffer
, lowcasepath
.GetLength());
793 if (GetRangeInSortVector(*treeptr
, lowcasepathBuffer
, lowcasepath
.GetLength(), &start
, &end
, pos
) == 0)
795 CGitHeadFileList::iterator it
;
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
);
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
;
813 callback(gitdir
+ _T("/") + (*it
).m_FileName
, *status
, false, pData
, false, false);
818 lowcasepath
.ReleaseBuffer();
820 }/*End of if status*/
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('/'));
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();
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
;
857 int pos
=SearchInSortVector(*indexptr
,lowcasepath
.GetBuffer(),lowcasepath
.GetLength());
858 lowcasepath
.ReleaseBuffer();
860 //Not In Version Contorl
865 *status
= git_wc_status_unversioned
;
867 callback(gitdir
+ _T("/") + path
, *status
, false, pData
, false, false);
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
;
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
;
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
;
908 for(int i
=start
;i
<=end
;i
++)
910 if (((*it
).m_Flags
& GIT_IDXENTRY_STAGEMASK
) !=0)
912 *status
= git_wc_status_conflicted
;
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);
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
);
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();
957 *status
= max(git_wc_status_added
, *status
); // added file found
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);
969 if( pos
>=0 && treeptr
->at(pos
).m_Hash
!= (*it
).m_IndexHash
)
971 *status
= max(git_wc_status_modified
, *status
); // modified file found
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);
987 if( *status
== git_wc_status_normal
)
989 pos
= SearchInSortVector(*treeptr
, lowcasepathBuffer
, lowcasepath
.GetLength());
992 *status
= max(git_wc_status_added
, *status
); // added file found
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
1010 (*hit
).m_FileName
.ReleaseBuffer();
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
))
1027 CString sub
, currentPath
;
1028 it
= indexptr
->begin()+start
;
1029 for(int i
=start
;i
<=end
;i
++,it
++)
1033 //skip child directory
1034 int pos
= (*it
).m_FileName
.Find(_T('/'), path
.GetLength());
1038 currentPath
= (*it
).m_FileName
.Left(pos
);
1039 if( callback
&& (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);
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);
1065 *status
= git_wc_status_none
;
1072 bool GitStatus::IsExistIndexLockFile(const CString
&gitdir
)
1074 CString sDirName
= gitdir
;
1078 if(PathFileExists(sDirName
+ _T("\\.git")))
1080 if(PathFileExists(g_AdminDirMap
.GetAdminDir(sDirName
) + _T("index.lock")))
1086 int x
= sDirName
.ReverseFind(_T('\\'));
1090 sDirName
= sDirName
.Left(x
);