1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - 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 "GitStatus.h"
23 #include "UnicodeUtils.h"
24 #include "ReaderWriterLock.h"
30 __time64_t m_ModifyTime
;
31 unsigned short m_Flags
;
38 class CGitIndexList
:public std::vector
<CGitIndex
>
43 __time64_t m_LastModifyTime
;
48 int ReadIndex(CString file
);
49 int GetStatus(const CString
&gitdir
, const CString
&path
, git_wc_status_kind
* status
, BOOL IsFull
= false, BOOL IsRecursive
= false, FILL_STATUS_CALLBACK callback
= nullptr, void *pData
= nullptr, CGitHash
*pHash
= nullptr, bool * assumeValid
= nullptr, bool * skipWorktree
= nullptr);
52 __int64 m_iMaxCheckSize
;
53 CComCriticalSection m_critRepoSec
;
54 CAutoRepository repository
;
55 int GetFileStatus(const CString
&gitdir
, const CString
&path
, git_wc_status_kind
* status
, __int64 time
, __int64 filesize
, FILL_STATUS_CALLBACK callback
= nullptr, void *pData
= nullptr, CGitHash
*pHash
= nullptr, bool * assumeValid
= nullptr, bool * skipWorktree
= nullptr);
56 int GetDirStatus(const CString
&gitdir
, const CString
&path
, git_wc_status_kind
* status
,__int64 time
, FILL_STATUS_CALLBACK callback
= nullptr, void *pData
= nullptr,CGitHash
*pHash
= nullptr);
59 typedef std::tr1::shared_ptr
<CGitIndexList
> SHARED_INDEX_PTR
;
60 typedef CComCritSecLock
<CComCriticalSection
> CAutoLocker
;
62 class CGitIndexFileMap
:public std::map
<CString
, SHARED_INDEX_PTR
>
65 CComCriticalSection m_critIndexSec
;
67 CGitIndexFileMap() { m_critIndexSec
.Init(); }
68 ~CGitIndexFileMap() { m_critIndexSec
.Term(); }
70 SHARED_INDEX_PTR
SafeGet(const CString
&path
)
72 CString thePath
= path
;
74 CAutoLocker
lock(m_critIndexSec
);
75 if(this->find(thePath
) == end())
76 return SHARED_INDEX_PTR();
78 return (*this)[thePath
];
81 void SafeSet(const CString
&path
, SHARED_INDEX_PTR ptr
)
83 CString thePath
= path
;
85 CAutoLocker
lock(m_critIndexSec
);
86 (*this)[thePath
] = ptr
;
89 bool SafeClear(const CString
&path
)
91 CString thePath
= path
;
93 CAutoLocker
lock(m_critIndexSec
);
94 if (this->find(thePath
) == end())
96 (*this)[thePath
] = nullptr;
100 bool SafeClearRecursively(const CString
&rootpath
)
102 CString thePath
= rootpath
;
104 CAutoLocker
lock(m_critIndexSec
);
105 for (auto it
= this->begin(); it
!= this->end(); ++it
)
107 if ((*it
).first
.Find(thePath
) == 0)
108 (*this)[(*it
).first
] = nullptr;
113 int Check(const CString
&gitdir
, bool *isChanged
);
114 int LoadIndex(const CString
&gitdir
);
116 bool CheckAndUpdate(const CString
&gitdir
,bool isLoadUpdatedIndex
)
118 bool isChanged
=false;
119 if(isLoadUpdatedIndex
&& Check(gitdir
,&isChanged
))
122 if(isChanged
&& isLoadUpdatedIndex
)
130 int GetFileStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,
131 BOOL IsFull
=false, BOOL IsRecursive
=false,
132 FILL_STATUS_CALLBACK callback
= nullptr,
133 void *pData
=NULL
,CGitHash
*pHash
=NULL
,
134 bool isLoadUpdatedIndex
= true, bool * assumeValid
= NULL
, bool * skipWorktree
= NULL
);
136 int IsUnderVersionControl(const CString
&gitdir
,
140 bool isLoadUpdateIndex
=true);
152 /* After object create, never change field agains
153 * that needn't lock to get field
155 class CGitHeadFileList
:public std::vector
<CGitTreeItem
>
158 int GetPackRef(const CString
&gitdir
);
159 CReaderWriterLock m_SharedMutex
;
161 __time64_t m_LastModifyTimeHead
;
162 __time64_t m_LastModifyTimeRef
;
163 __time64_t m_LastModifyTimePackRef
;
165 CString m_HeadRefFile
;
169 CString m_PackRefFile
;
171 CGitHash m_TreeHash
; /* buffered tree hash value */
173 std::map
<CString
,CGitHash
> m_PackRefMap
;
178 m_LastModifyTimeHead
=0;
179 m_LastModifyTimeRef
=0;
180 m_LastModifyTimePackRef
= 0;
184 int ReadHeadHash(CString gitdir
);
185 bool CheckHeadUpdate();
186 bool HeadHashEqualsTreeHash();
187 bool HeadFileIsEmpty();
189 static int CallBack(const unsigned char *, const char *, int, const char *, unsigned int, int, void *);
192 typedef std::tr1::shared_ptr
<CGitHeadFileList
> SHARED_TREE_PTR
;
193 class CGitHeadFileMap
:public std::map
<CString
,SHARED_TREE_PTR
>
197 CComCriticalSection m_critTreeSec
;
199 CGitHeadFileMap() { m_critTreeSec
.Init(); }
200 ~CGitHeadFileMap() { m_critTreeSec
.Term(); }
202 SHARED_TREE_PTR
SafeGet(const CString
&path
)
204 CString thePath
= path
;
206 CAutoLocker
lock(m_critTreeSec
);
207 if(this->find(thePath
) == end())
208 return SHARED_TREE_PTR();
210 return (*this)[thePath
];
213 void SafeSet(const CString
&path
, SHARED_TREE_PTR ptr
)
215 CString thePath
= path
;
217 CAutoLocker
lock(m_critTreeSec
);
218 (*this)[thePath
] = ptr
;
221 int GetFileStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,BOOL IsFull
=false, BOOL IsRecursive
=false,
222 FILL_STATUS_CALLBACK callback
= nullptr, void *pData
= nullptr,
223 bool isLoaded
=false);
224 bool CheckHeadAndUpdate(const CString
&gitdir
, bool readTree
= true);
225 int IsUnderVersionControl(const CString
&gitdir
, const CString
&path
, bool isDir
, bool *isVersion
);
232 CString m_CaseFileName
;
241 m_pExcludeList
=NULL
;
247 git_free_exclude_list(m_pExcludeList
);
252 __time64_t m_LastModifyTime
;
255 EXCLUDE_LIST m_pExcludeList
;
257 int FetchIgnoreList(const CString
&projectroot
, const CString
&file
, bool isGlobal
);
263 bool CheckFileChanged(const CString
&path
);
264 int FetchIgnoreFile(const CString
&gitdir
, const CString
&gitignore
, bool isGlobal
);
266 int CheckIgnore(const CString
&path
, const CString
&root
, bool isDir
);
267 int CheckFileAgainstIgnoreList(const CString
&ignorefile
, const CStringA
&patha
, const char * base
, int &type
);
269 // core.excludesfile stuff
270 std::map
<CString
, CString
> m_CoreExcludesfiles
;
271 CString m_sMsysGitBinPath
;
272 DWORD m_dMsysGitBinPathLastChecked
;
273 CReaderWriterLock m_coreExcludefilesSharedMutex
;
274 // checks if the msysgit path has changed and return true/false
275 // if the path changed, the cache is update
276 // force is only ised in constructor
277 bool CheckAndUpdateMsysGitBinpath(bool force
= true);
278 bool CheckAndUpdateCoreExcludefile(const CString
&adminDir
);
279 const CString
GetWindowsHome();
282 CReaderWriterLock m_SharedMutex
;
284 CGitIgnoreList(){ CheckAndUpdateMsysGitBinpath(true); }
286 std::map
<CString
, CGitIgnoreItem
> m_Map
;
288 bool CheckIgnoreChanged(const CString
&gitdir
,const CString
&path
, bool isDir
);
289 int LoadAllIgnoreFile(const CString
&gitdir
, const CString
&path
, bool isDir
);
290 bool IsIgnore(const CString
&path
, const CString
&root
, bool isDir
);
294 int GetRangeInSortVector(const T
&vector
, LPCTSTR pstr
, int len
, int *start
, int *end
, int pos
)
300 if(start
== 0 || end
== NULL
)
308 if (pos
>= (int)vector
.size())
311 if( _tcsnccmp(vector
[pos
].m_FileName
, pstr
,len
) != 0)
313 for (int i
= 0; i
< (int)vector
.size(); ++i
)
315 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
327 *end
= (int)vector
.size();
329 for (int i
= pos
; i
< (int)vector
.size(); ++i
)
331 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
340 for(int i
=pos
;i
>=0;i
--)
342 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
356 int SearchInSortVector(const T
&vector
, LPCTSTR pstr
, int len
)
358 int end
= (int)vector
.size() - 1;
360 int mid
= (start
+end
)/2;
365 while(!( start
== end
&& start
==mid
))
369 cmp
= _tcscmp(vector
[mid
].m_FileName
,pstr
);
371 cmp
= _tcsnccmp( vector
[mid
].m_FileName
,pstr
,len
);
380 mid
=(start
+end
) /2;
385 if(_tcscmp(vector
[mid
].m_FileName
,pstr
) == 0)
390 if(_tcsnccmp( vector
[mid
].m_FileName
,pstr
,len
) == 0)
396 class CGitAdminDirMap
:public std::map
<CString
, CString
>
399 CComCriticalSection m_critIndexSec
;
400 std::map
<CString
, CString
> m_reverseLookup
;
402 CGitAdminDirMap() { m_critIndexSec
.Init(); }
403 ~CGitAdminDirMap() { m_critIndexSec
.Term(); }
405 CString
GetAdminDir(const CString
&path
)
407 CString thePath
= path
;
409 CAutoLocker
lock(m_critIndexSec
);
410 if(this->find(thePath
) == end())
412 if (PathIsDirectory(path
+ _T("\\.git")))
414 (*this)[thePath
] = path
+ _T("\\.git\\");
415 m_reverseLookup
[thePath
+ _T("\\.git")] = path
;
416 return (*this)[thePath
];
420 FILE * pFile
= _tfsopen(path
+ _T("\\.git"), _T("r"), SH_DENYWR
);
424 std::unique_ptr
<char[]> buffer(new char[size
]);
426 if ((length
= (int)fread(buffer
.get(), sizeof(char), size
, pFile
)) >= 8)
429 CStringA
strA(buffer
.get(), length
);
430 if (strA
.Left(8) == "gitdir: ")
432 CString str
= CUnicodeUtils::GetUnicode(strA
.Trim().Mid(8)); // 8 = len("gitdir: ")
433 str
.Replace(_T("/"), _T("\\"));
434 str
.TrimRight(_T("\\"));
435 if (str
.GetLength() > 0 && str
[0] == _T('.'))
437 str
= path
+ _T("\\") + str
;
439 PathCanonicalize(newPath
.GetBuffer(MAX_PATH
), str
.GetBuffer());
440 newPath
.ReleaseBuffer();
444 (*this)[thePath
] = str
+ _T("\\");
445 m_reverseLookup
[str
.MakeLower()] = path
;
446 return (*this)[thePath
];
452 return path
+ _T("\\.git\\"); // in case of an error stick to old behavior
456 return (*this)[thePath
];
459 CString
GetWorkingCopy(const CString
&gitDir
)
461 CString path
= gitDir
;
463 CAutoLocker
lock(m_critIndexSec
);
464 if (m_reverseLookup
.find(path
) == m_reverseLookup
.end())
467 return m_reverseLookup
[path
];