1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2017 - 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"
25 #include "GitAdminDir.h"
26 #include "StringUtils.h"
27 #include "PathUtils.h"
33 __time64_t m_ModifyTime
;
35 uint16_t m_FlagsExtended
;
42 class CGitIndexList
:public std::vector
<CGitIndex
>
45 __time64_t m_LastModifyTime
;
51 int ReadIndex(CString dotgitdir
);
52 int GetFileStatus(const CString
& gitdir
, CString path
, git_wc_status2_t
& status
, CGitHash
* pHash
= nullptr);
53 int GetFileStatus(CAutoRepository
& repository
, const CString
& gitdir
, CGitIndex
& entry
, git_wc_status2_t
& status
, __int64 time
, __int64 filesize
);
54 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
55 FRIEND_TEST(GitIndexCBasicGitWithTestRepoFixture
, GetFileStatus
);
58 __int64 m_iMaxCheckSize
;
60 int GetFileStatus(const CString
& gitdir
, const CString
& path
, git_wc_status2_t
& status
, __int64 time
, __int64 filesize
, CGitHash
* pHash
= nullptr);
63 typedef std::shared_ptr
<CGitIndexList
> SHARED_INDEX_PTR
;
64 typedef CComCritSecLock
<CComCriticalSection
> CAutoLocker
;
66 class CGitIndexFileMap
:public std::map
<CString
, SHARED_INDEX_PTR
>
69 CComCriticalSection m_critIndexSec
;
71 CGitIndexFileMap() { m_critIndexSec
.Init(); }
72 ~CGitIndexFileMap() { m_critIndexSec
.Term(); }
74 SHARED_INDEX_PTR
SafeGet(CString thePath
)
77 CAutoLocker
lock(m_critIndexSec
);
78 auto lookup
= find(thePath
);
80 return SHARED_INDEX_PTR();
81 return lookup
->second
;
84 void SafeSet(CString thePath
, SHARED_INDEX_PTR ptr
)
87 CAutoLocker
lock(m_critIndexSec
);
88 (*this)[thePath
] = ptr
;
91 bool SafeClear(CString thePath
)
94 CAutoLocker
lock(m_critIndexSec
);
95 auto lookup
= find(thePath
);
102 bool SafeClearRecursively(CString thePath
)
105 CAutoLocker
lock(m_critIndexSec
);
106 std::vector
<CString
> toRemove
;
107 for (auto it
= this->cbegin(); it
!= this->cend(); ++it
)
109 if (CStringUtils::StartsWith((*it
).first
, thePath
))
110 toRemove
.push_back((*it
).first
);
112 for (auto it
= toRemove
.cbegin(); it
!= toRemove
.cend(); ++it
)
114 return !toRemove
.empty();
117 bool HasIndexChangedOnDisk(const CString
& gitdir
);
118 int LoadIndex(const CString
&gitdir
);
120 void CheckAndUpdate(const CString
& gitdir
)
122 if (HasIndexChangedOnDisk(gitdir
))
135 /* After object create, never change field agains
136 * that needn't lock to get field
138 class CGitHeadFileList
:public std::vector
<CGitTreeItem
>
141 int GetPackRef(const CString
&gitdir
);
143 __time64_t m_LastModifyTimeHead
;
144 __time64_t m_LastModifyTimeRef
;
145 __time64_t m_LastModifyTimePackRef
;
147 CString m_HeadRefFile
;
151 CString m_PackRefFile
;
153 CGitHash m_TreeHash
; /* buffered tree hash value */
155 std::map
<CString
,CGitHash
> m_PackRefMap
;
159 : m_LastModifyTimeHead(0)
160 , m_LastModifyTimeRef(0)
161 , m_LastModifyTimePackRef(0)
166 int ReadHeadHash(const CString
& gitdir
);
167 bool CheckHeadUpdate();
168 bool HeadHashEqualsTreeHash();
169 static int CallBack(const unsigned char *, const char *, int, const char *, unsigned int, int, void *);
172 typedef std::shared_ptr
<CGitHeadFileList
> SHARED_TREE_PTR
;
173 class CGitHeadFileMap
:public std::map
<CString
,SHARED_TREE_PTR
>
177 CComCriticalSection m_critTreeSec
;
179 CGitHeadFileMap() { m_critTreeSec
.Init(); }
180 ~CGitHeadFileMap() { m_critTreeSec
.Term(); }
182 SHARED_TREE_PTR
SafeGet(CString thePath
)
185 CAutoLocker
lock(m_critTreeSec
);
186 auto lookup
= find(thePath
);
187 if (lookup
== cend())
188 return SHARED_TREE_PTR();
189 return lookup
->second
;
192 void SafeSet(CString thePath
, SHARED_TREE_PTR ptr
)
195 CAutoLocker
lock(m_critTreeSec
);
196 (*this)[thePath
] = ptr
;
199 bool SafeClear(CString thePath
)
202 CAutoLocker
lock(m_critTreeSec
);
203 auto lookup
= find(thePath
);
204 if (lookup
== cend())
210 bool SafeClearRecursively(CString thePath
)
213 CAutoLocker
lock(m_critTreeSec
);
214 std::vector
<CString
> toRemove
;
215 for (auto it
= this->cbegin(); it
!= this->cend(); ++it
)
217 if (CStringUtils::StartsWith((*it
).first
, thePath
))
218 toRemove
.push_back((*it
).first
);
220 for (auto it
= toRemove
.cbegin(); it
!= toRemove
.cend(); ++it
)
222 return !toRemove
.empty();
224 void CheckHeadAndUpdate(const CString
& gitdir
);
231 CGitFileName(LPCTSTR filename
, __int64 size
, __int64 lastmodified
)
232 : m_FileName(filename
)
234 , m_LastModified(lastmodified
)
239 __int64 m_LastModified
;
242 static bool SortCGitFileName(const CGitFileName
& item1
, const CGitFileName
& item2
)
244 return item1
.m_FileName
.Compare(item2
.m_FileName
) < 0;
251 : m_LastModifyTime(0)
252 , m_pExcludeList(nullptr)
254 , m_iIgnoreCase(nullptr)
261 git_free_exclude_list(m_pExcludeList
);
265 __time64_t m_LastModifyTime
;
268 EXCLUDE_LIST m_pExcludeList
;
271 int FetchIgnoreList(const CString
& projectroot
, const CString
& file
, bool isGlobal
, int* ignoreCase
);
274 * patha: the filename to be checked whether is is ignored or not
275 * base: must be a pointer to the beginning of the base filename WITHIN patha
276 * type: DT_DIR or DT_REG
278 int IsPathIgnored(const CStringA
& patha
, const char* base
, int& type
);
279 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
280 int IsPathIgnored(const CStringA
& patha
, int& type
);
287 bool CheckFileChanged(const CString
&path
);
288 int FetchIgnoreFile(const CString
&gitdir
, const CString
&gitignore
, bool isGlobal
);
290 int CheckIgnore(const CString
&path
, const CString
&root
, bool isDir
);
291 int CheckFileAgainstIgnoreList(const CString
&ignorefile
, const CStringA
&patha
, const char * base
, int &type
);
293 // core.excludesfile stuff
294 std::map
<CString
, CString
> m_CoreExcludesfiles
;
295 std::map
<CString
, int> m_IgnoreCase
;
296 CString m_sGitSystemConfigPath
;
297 CString m_sGitProgramDataConfigPath
;
298 ULONGLONG m_dGitSystemConfigPathLastChecked
;
299 CReaderWriterLock m_coreExcludefilesSharedMutex
;
300 // checks if the msysgit path has changed and return true/false
301 // if the path changed, the cache is update
302 // force is only ised in constructor
303 bool CheckAndUpdateGitSystemConfigPath(bool force
= true);
304 bool CheckAndUpdateCoreExcludefile(const CString
&adminDir
);
305 const CString
GetWindowsHome();
308 CReaderWriterLock m_SharedMutex
;
310 CGitIgnoreList(){ CheckAndUpdateGitSystemConfigPath(true); }
312 std::map
<CString
, CGitIgnoreItem
> m_Map
;
314 bool CheckAndUpdateIgnoreFiles(const CString
& gitdir
, const CString
& path
, bool isDir
);
315 bool IsIgnore(CString path
, const CString
& root
, bool isDir
);
318 static const size_t NPOS
= (size_t)-1; // bad/missing length/position
319 static_assert(MAXSIZE_T
== NPOS
, "NPOS must equal MAXSIZE_T");
320 #pragma warning(push)
321 #pragma warning(disable: 4310)
322 static_assert(-1 == (int)NPOS
, "NPOS must equal -1");
326 int GetRangeInSortVector(const T
& vector
, LPCTSTR pstr
, size_t len
, size_t* start
, size_t* end
, size_t pos
)
333 *start
= *end
= NPOS
;
338 if (pos
>= vector
.size())
341 if (wcsncmp(vector
[pos
].m_FileName
, pstr
, len
) != 0)
345 *end
= vector
.size() - 1;
347 // shortcut, if all entries are going match
351 for (size_t i
= pos
; i
< vector
.size(); ++i
)
353 if (wcsncmp(vector
[i
].m_FileName
, pstr
, len
) != 0)
358 for (size_t i
= pos
+ 1; i
-- > 0;)
360 if (wcsncmp(vector
[i
].m_FileName
, pstr
, len
) != 0)
370 size_t SearchInSortVector(const T
& vector
, LPCTSTR pstr
, int len
)
372 size_t end
= vector
.size() - 1;
374 size_t mid
= (start
+ end
) / 2;
379 while(!( start
== end
&& start
==mid
))
383 cmp
= wcscmp(vector
[mid
].m_FileName
, pstr
);
385 cmp
= wcsncmp(vector
[mid
].m_FileName
, pstr
, len
);
394 mid
=(start
+end
) /2;
399 if (wcscmp(vector
[mid
].m_FileName
, pstr
) == 0)
404 if (wcsncmp(vector
[mid
].m_FileName
, pstr
, len
) == 0)
410 class CGitAdminDirMap
:public std::map
<CString
, CString
>
413 CComCriticalSection m_critIndexSec
;
414 std::map
<CString
, CString
> m_reverseLookup
;
415 std::map
<CString
, CString
> m_WorktreeAdminDirLookup
;
417 CGitAdminDirMap() { m_critIndexSec
.Init(); }
418 ~CGitAdminDirMap() { m_critIndexSec
.Term(); }
420 CString
GetAdminDir(const CString
&path
)
422 CString
thePath(CPathUtils::NormalizePath(path
));
423 CAutoLocker
lock(m_critIndexSec
);
424 auto lookup
= find(thePath
);
425 if (lookup
== cend())
428 GitAdminDir::GetAdminDirPath(thePath
, adminDir
);
429 if (PathIsDirectory(adminDir
))
431 adminDir
= CPathUtils::BuildPathWithPathDelimiter(CPathUtils::NormalizePath(adminDir
));
432 (*this)[thePath
] = adminDir
;
433 m_reverseLookup
[adminDir
] = thePath
;
434 return (*this)[thePath
];
436 return thePath
+ L
".git\\"; // in case of an error stick to old behavior
439 return lookup
->second
;
442 void ResetAdminDirCache(const CString
& path
)
444 CString
thePath(path
);
446 CAutoLocker
lock(m_critIndexSec
);
447 auto lookup
= find(thePath
);
448 if (lookup
== cend())
450 m_reverseLookup
.erase(lookup
->second
.MakeLower().TrimRight(L
'\\'));
454 CString
GetAdminDirConcat(const CString
& path
, const CString
& subpath
)
456 CString
result(GetAdminDir(path
));
461 CString
GetWorktreeAdminDir(const CString
& path
)
463 CString
thePath(CPathUtils::NormalizePath(path
));
464 CAutoLocker
lock(m_critIndexSec
);
465 auto lookup
= m_WorktreeAdminDirLookup
.find(thePath
);
466 if (lookup
== m_WorktreeAdminDirLookup
.cend())
469 GitAdminDir::GetWorktreeAdminDirPath(thePath
, wtadmindir
);
470 if (PathIsDirectory(wtadmindir
))
472 wtadmindir
= CPathUtils::BuildPathWithPathDelimiter(CPathUtils::NormalizePath(wtadmindir
));
473 m_WorktreeAdminDirLookup
[thePath
] = wtadmindir
;
474 m_reverseLookup
[wtadmindir
] = thePath
;
475 return m_WorktreeAdminDirLookup
[thePath
];
478 return thePath
+ L
".git\\"; // we should never get here
480 return lookup
->second
;
483 CString
GetWorktreeAdminDirConcat(const CString
& path
, const CString
& subpath
)
485 CString
result(GetWorktreeAdminDir(path
));
490 CString
GetWorkingCopy(const CString
&gitDir
)
492 CString
path(CPathUtils::BuildPathWithPathDelimiter(CPathUtils::NormalizePath(gitDir
)));
493 CAutoLocker
lock(m_critIndexSec
);
494 auto lookup
= m_reverseLookup
.find(path
);
495 if (lookup
== m_reverseLookup
.cend())
497 return lookup
->second
;