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
;
37 class CGitIndexList
:public std::vector
<CGitIndex
>
42 __time64_t m_LastModifyTime
;
47 int ReadIndex(CString file
);
48 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);
51 __int64 m_iMaxCheckSize
;
52 CComCriticalSection m_critRepoSec
;
53 CAutoRepository repository
;
54 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);
55 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);
58 typedef std::tr1::shared_ptr
<CGitIndexList
> SHARED_INDEX_PTR
;
59 typedef CComCritSecLock
<CComCriticalSection
> CAutoLocker
;
61 class CGitIndexFileMap
:public std::map
<CString
, SHARED_INDEX_PTR
>
64 CComCriticalSection m_critIndexSec
;
66 CGitIndexFileMap() { m_critIndexSec
.Init(); }
67 ~CGitIndexFileMap() { m_critIndexSec
.Term(); }
69 SHARED_INDEX_PTR
SafeGet(const CString
&path
)
71 CString thePath
= path
;
73 CAutoLocker
lock(m_critIndexSec
);
74 if(this->find(thePath
) == end())
75 return SHARED_INDEX_PTR();
77 return (*this)[thePath
];
80 void SafeSet(const CString
&path
, SHARED_INDEX_PTR ptr
)
82 CString thePath
= path
;
84 CAutoLocker
lock(m_critIndexSec
);
85 (*this)[thePath
] = ptr
;
88 bool SafeClear(const CString
&path
)
90 CString thePath
= path
;
92 CAutoLocker
lock(m_critIndexSec
);
93 if (this->find(thePath
) == end())
95 (*this)[thePath
] = nullptr;
99 bool SafeClearRecursively(const CString
&rootpath
)
101 CString thePath
= rootpath
;
103 CAutoLocker
lock(m_critIndexSec
);
104 for (auto it
= this->begin(); it
!= this->end(); ++it
)
106 if ((*it
).first
.Find(thePath
) == 0)
107 (*this)[(*it
).first
] = nullptr;
112 int Check(const CString
&gitdir
, bool *isChanged
);
113 int LoadIndex(const CString
&gitdir
);
115 bool CheckAndUpdate(const CString
&gitdir
,bool isLoadUpdatedIndex
)
117 bool isChanged
=false;
118 if(isLoadUpdatedIndex
&& Check(gitdir
,&isChanged
))
121 if(isChanged
&& isLoadUpdatedIndex
)
129 int GetFileStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,
130 BOOL IsFull
=false, BOOL IsRecursive
=false,
131 FILL_STATUS_CALLBACK callback
= nullptr,
132 void *pData
=NULL
,CGitHash
*pHash
=NULL
,
133 bool isLoadUpdatedIndex
= true, bool * assumeValid
= NULL
, bool * skipWorktree
= NULL
);
135 int IsUnderVersionControl(const CString
&gitdir
,
139 bool isLoadUpdateIndex
=true);
151 /* After object create, never change field agains
152 * that needn't lock to get field
154 class CGitHeadFileList
:public std::vector
<CGitTreeItem
>
157 int GetPackRef(const CString
&gitdir
);
158 CReaderWriterLock m_SharedMutex
;
160 __time64_t m_LastModifyTimeHead
;
161 __time64_t m_LastModifyTimeRef
;
162 __time64_t m_LastModifyTimePackRef
;
164 CString m_HeadRefFile
;
168 CString m_PackRefFile
;
170 CGitHash m_TreeHash
; /* buffered tree hash value */
172 std::map
<CString
,CGitHash
> m_PackRefMap
;
177 m_LastModifyTimeHead
=0;
178 m_LastModifyTimeRef
=0;
179 m_LastModifyTimePackRef
= 0;
183 int ReadHeadHash(CString gitdir
);
184 bool CheckHeadUpdate();
185 bool HeadHashEqualsTreeHash();
186 bool HeadFileIsEmpty();
188 static int CallBack(const unsigned char *, const char *, int, const char *, unsigned int, int, void *);
191 typedef std::tr1::shared_ptr
<CGitHeadFileList
> SHARED_TREE_PTR
;
192 class CGitHeadFileMap
:public std::map
<CString
,SHARED_TREE_PTR
>
196 CComCriticalSection m_critTreeSec
;
198 CGitHeadFileMap() { m_critTreeSec
.Init(); }
199 ~CGitHeadFileMap() { m_critTreeSec
.Term(); }
201 SHARED_TREE_PTR
SafeGet(const CString
&path
)
203 CString thePath
= path
;
205 CAutoLocker
lock(m_critTreeSec
);
206 if(this->find(thePath
) == end())
207 return SHARED_TREE_PTR();
209 return (*this)[thePath
];
212 void SafeSet(const CString
&path
, SHARED_TREE_PTR ptr
)
214 CString thePath
= path
;
216 CAutoLocker
lock(m_critTreeSec
);
217 (*this)[thePath
] = ptr
;
220 int GetFileStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,BOOL IsFull
=false, BOOL IsRecursive
=false,
221 FILL_STATUS_CALLBACK callback
= nullptr, void *pData
= nullptr,
222 bool isLoaded
=false);
223 bool CheckHeadAndUpdate(const CString
&gitdir
, bool readTree
= true);
224 int IsUnderVersionControl(const CString
&gitdir
, const CString
&path
, bool isDir
, bool *isVersion
);
231 CString m_CaseFileName
;
240 m_pExcludeList
=NULL
;
246 git_free_exclude_list(m_pExcludeList
);
251 __time64_t m_LastModifyTime
;
254 EXCLUDE_LIST m_pExcludeList
;
256 int FetchIgnoreList(const CString
&projectroot
, const CString
&file
, bool isGlobal
);
262 bool CheckFileChanged(const CString
&path
);
263 int FetchIgnoreFile(const CString
&gitdir
, const CString
&gitignore
, bool isGlobal
);
265 int CheckIgnore(const CString
&path
, const CString
&root
, bool isDir
);
266 int CheckFileAgainstIgnoreList(const CString
&ignorefile
, const CStringA
&patha
, const char * base
, int &type
);
268 // core.excludesfile stuff
269 std::map
<CString
, CString
> m_CoreExcludesfiles
;
270 CString m_sMsysGitBinPath
;
271 DWORD m_dMsysGitBinPathLastChecked
;
272 CReaderWriterLock m_coreExcludefilesSharedMutex
;
273 // checks if the msysgit path has changed and return true/false
274 // if the path changed, the cache is update
275 // force is only ised in constructor
276 bool CheckAndUpdateMsysGitBinpath(bool force
= true);
277 bool CheckAndUpdateCoreExcludefile(const CString
&adminDir
);
278 const CString
GetWindowsHome();
281 CReaderWriterLock m_SharedMutex
;
283 CGitIgnoreList(){ CheckAndUpdateMsysGitBinpath(true); }
285 std::map
<CString
, CGitIgnoreItem
> m_Map
;
287 bool CheckIgnoreChanged(const CString
&gitdir
,const CString
&path
, bool isDir
);
288 int LoadAllIgnoreFile(const CString
&gitdir
, const CString
&path
, bool isDir
);
289 bool IsIgnore(const CString
&path
, const CString
&root
, bool isDir
);
293 int GetRangeInSortVector(const T
&vector
, LPCTSTR pstr
, int len
, int *start
, int *end
, int pos
)
299 if(start
== 0 || end
== NULL
)
307 if (pos
>= (int)vector
.size())
310 if( _tcsnccmp(vector
[pos
].m_FileName
, pstr
,len
) != 0)
312 for (int i
= 0; i
< (int)vector
.size(); ++i
)
314 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
326 *end
= (int)vector
.size();
328 for (int i
= pos
; i
< (int)vector
.size(); ++i
)
330 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
339 for(int i
=pos
;i
>=0;i
--)
341 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
355 int SearchInSortVector(const T
&vector
, LPCTSTR pstr
, int len
)
357 int end
= (int)vector
.size() - 1;
359 int mid
= (start
+end
)/2;
364 while(!( start
== end
&& start
==mid
))
368 cmp
= _tcscmp(vector
[mid
].m_FileName
,pstr
);
370 cmp
= _tcsnccmp( vector
[mid
].m_FileName
,pstr
,len
);
379 mid
=(start
+end
) /2;
384 if(_tcscmp(vector
[mid
].m_FileName
,pstr
) == 0)
389 if(_tcsnccmp( vector
[mid
].m_FileName
,pstr
,len
) == 0)
395 class CGitAdminDirMap
:public std::map
<CString
, CString
>
398 CComCriticalSection m_critIndexSec
;
399 std::map
<CString
, CString
> m_reverseLookup
;
401 CGitAdminDirMap() { m_critIndexSec
.Init(); }
402 ~CGitAdminDirMap() { m_critIndexSec
.Term(); }
404 CString
GetAdminDir(const CString
&path
)
406 CString thePath
= path
;
408 CAutoLocker
lock(m_critIndexSec
);
409 if(this->find(thePath
) == end())
411 if (PathIsDirectory(path
+ _T("\\.git")))
413 (*this)[thePath
] = path
+ _T("\\.git\\");
414 m_reverseLookup
[thePath
+ _T("\\.git")] = path
;
415 return (*this)[thePath
];
419 FILE * pFile
= _tfsopen(path
+ _T("\\.git"), _T("r"), SH_DENYWR
);
423 std::unique_ptr
<char[]> buffer(new char[size
]);
425 if ((length
= (int)fread(buffer
.get(), sizeof(char), size
, pFile
)) >= 8)
428 CStringA
strA(buffer
.get(), length
);
429 if (strA
.Left(8) == "gitdir: ")
431 CString str
= CUnicodeUtils::GetUnicode(strA
.Trim().Mid(8)); // 8 = len("gitdir: ")
432 str
.Replace(_T("/"), _T("\\"));
433 str
.TrimRight(_T("\\"));
434 if (str
.GetLength() > 0 && str
[0] == _T('.'))
436 str
= path
+ _T("\\") + str
;
438 PathCanonicalize(newPath
.GetBuffer(MAX_PATH
), str
.GetBuffer());
439 newPath
.ReleaseBuffer();
443 (*this)[thePath
] = str
+ _T("\\");
444 m_reverseLookup
[str
.MakeLower()] = path
;
445 return (*this)[thePath
];
451 return path
+ _T("\\.git\\"); // in case of an error stick to old behavior
455 return (*this)[thePath
];
458 CString
GetWorkingCopy(const CString
&gitDir
)
460 CString path
= gitDir
;
462 CAutoLocker
lock(m_critIndexSec
);
463 if (m_reverseLookup
.find(path
) == m_reverseLookup
.end())
466 return m_reverseLookup
[path
];