Add more debugging info to TGitCache when (re)loading index and HEAD tree
[TortoiseGit.git] / src / Git / GitIndex.cpp
bloba106678ff9830b84ddf7cadc2cdd831d53319544
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.
20 #include "stdafx.h"
21 #include "Git.h"
22 #include "registry.h"
23 #include "UnicodeUtils.h"
24 #include "PathUtils.h"
25 #include "gitindex.h"
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include "SmartHandle.h"
29 #include "git2/sys/repository.h"
31 CGitAdminDirMap g_AdminDirMap;
33 static CString GetProgramDataGitConfig()
35 if (!((CRegDWORD(L"Software\\TortoiseGit\\CygwinHack", FALSE) == TRUE) || (CRegDWORD(L"Software\\TortoiseGit\\Msys2Hack", FALSE) == TRUE)))
37 CString programdataConfig;
38 if (SHGetFolderPath(nullptr, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, CStrBuf(programdataConfig, MAX_PATH)) == S_OK && programdataConfig.GetLength() < MAX_PATH - (int)wcslen(L"\\Git\\config"))
39 return programdataConfig + L"\\Git\\config";
41 return L"";
44 int CGitIndex::Print()
46 wprintf(L"0x%08X 0x%08X %s %s\n",
47 (int)this->m_ModifyTime,
48 this->m_Flags,
49 (LPCTSTR)this->m_IndexHash.ToString(),
50 (LPCTSTR)this->m_FileName);
52 return 0;
55 CGitIndexList::CGitIndexList()
56 : m_bHasConflicts(FALSE)
57 , m_LastModifyTime(0)
59 m_iMaxCheckSize = (__int64)CRegDWORD(L"Software\\TortoiseGit\\TGitCacheCheckContentMaxSize", 10 * 1024) * 1024; // stored in KiB
62 CGitIndexList::~CGitIndexList()
66 static bool SortIndex(const CGitIndex &Item1, const CGitIndex &Item2)
68 return Item1.m_FileName.Compare(Item2.m_FileName) < 0;
71 static bool SortTree(const CGitTreeItem &Item1, const CGitTreeItem &Item2)
73 return Item1.m_FileName.Compare(Item2.m_FileName) < 0;
76 int CGitIndexList::ReadIndex(CString dgitdir)
78 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
79 clear(); // HACK to make tests work, until we use CGitIndexList
80 #endif
81 ATLASSERT(empty());
83 CAutoRepository repository(dgitdir);
84 if (!repository)
86 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Could not open git repository in %s: %s\n", (LPCTSTR)dgitdir, (LPCTSTR)CGit::GetLibGit2LastErr());
87 return -1;
90 // add config files
91 config.New();
93 CString projectConfig = g_AdminDirMap.GetAdminDir(dgitdir) + L"config";
94 CString globalConfig = g_Git.GetGitGlobalConfig();
95 CString globalXDGConfig = g_Git.GetGitGlobalXDGConfig();
96 CString systemConfig(CRegString(REG_SYSTEM_GITCONFIGPATH, L"", FALSE));
97 CString programDataConfig(GetProgramDataGitConfig());
99 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(projectConfig), GIT_CONFIG_LEVEL_LOCAL, FALSE);
100 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalConfig), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
101 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalXDGConfig), GIT_CONFIG_LEVEL_XDG, FALSE);
102 if (!systemConfig.IsEmpty())
103 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(systemConfig), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
104 if (!programDataConfig.IsEmpty())
105 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(programDataConfig), GIT_CONFIG_LEVEL_PROGRAMDATA, FALSE);
107 git_repository_set_config(repository, config);
109 CAutoIndex index;
110 // load index in order to enumerate files
111 if (git_repository_index(index.GetPointer(), repository))
113 config.Free();
114 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Could not get index of git repository in %s: %s\n", (LPCTSTR)dgitdir, (LPCTSTR)CGit::GetLibGit2LastErr());
115 return -1;
118 m_bHasConflicts = FALSE;
120 size_t ecount = git_index_entrycount(index);
123 resize(ecount);
125 catch (const std::bad_alloc& ex)
127 config.Free();
128 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Could not resize index-vector: %s\n", ex.what());
129 return -1;
131 for (size_t i = 0; i < ecount; ++i)
133 const git_index_entry *e = git_index_get_byindex(index, i);
135 auto& item = (*this)[i];
136 item.m_FileName = CUnicodeUtils::GetUnicode(e->path);
137 if (e->mode & S_IFDIR)
138 item.m_FileName += L'/';
139 item.m_ModifyTime = e->mtime.seconds;
140 item.m_Flags = e->flags;
141 item.m_FlagsExtended = e->flags_extended;
142 item.m_IndexHash = e->id.id;
143 item.m_Size = e->file_size;
144 m_bHasConflicts |= GIT_IDXENTRY_STAGE(e);
147 CGit::GetFileModifyTime(g_AdminDirMap.GetWorktreeAdminDir(dgitdir) + L"index", &m_LastModifyTime);
148 std::sort(this->begin(), this->end(), SortIndex);
150 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Reloaded index for repo: %s\n", (LPCTSTR)dgitdir);
152 return 0;
155 int CGitIndexList::GetFileStatus(const CString& gitdir, const CString& pathorg, git_wc_status2_t& status, __int64 time, __int64 filesize, CGitHash* pHash)
157 size_t index = SearchInSortVector(*this, pathorg, -1);
159 if (index == NPOS)
161 status.status = git_wc_status_unversioned;
162 if (pHash)
163 pHash->Empty();
165 return 0;
168 auto& entry = (*this)[index];
169 if (pHash)
170 *pHash = entry.m_IndexHash;
171 ATLASSERT(pathorg == entry.m_FileName);
172 CAutoRepository repository;
173 return GetFileStatus(repository, gitdir, entry, status, time, filesize);
176 int CGitIndexList::GetFileStatus(CAutoRepository& repository, const CString& gitdir, CGitIndex& entry, git_wc_status2_t& status, __int64 time, __int64 filesize)
178 ATLASSERT(!status.assumeValid && !status.skipWorktree);
180 // skip-worktree has higher priority than assume-valid
181 if (entry.m_FlagsExtended & GIT_IDXENTRY_SKIP_WORKTREE)
183 status.status = git_wc_status_normal;
184 status.skipWorktree = true;
186 else if (entry.m_Flags & GIT_IDXENTRY_VALID)
188 status.status = git_wc_status_normal;
189 status.assumeValid = true;
191 else if (filesize == -1)
192 status.status = git_wc_status_deleted;
193 else if (filesize != entry.m_Size)
194 status.status = git_wc_status_modified;
195 else if (time == entry.m_ModifyTime)
196 status.status = git_wc_status_normal;
197 else if (config && filesize < m_iMaxCheckSize)
200 * Opening a new repository each time is not yet optimal, however, there is no API to clear the pack-cache
201 * When a shared repository is used, we might need a mutex to prevent concurrent access to repository instance and especially filter-lists
203 if (!repository)
205 if (repository.Open(gitdir))
207 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Could not open git repository in %s for checking file: %s\n", (LPCTSTR)gitdir, (LPCTSTR)CGit::GetLibGit2LastErr());
208 return -1;
210 git_repository_set_config(repository, config);
213 git_oid actual;
214 CStringA fileA = CUnicodeUtils::GetMulti(entry.m_FileName, CP_UTF8);
215 if (!git_repository_hashfile(&actual, repository, fileA, GIT_OBJ_BLOB, nullptr) && !git_oid_cmp(&actual, (const git_oid*)entry.m_IndexHash.m_hash))
217 entry.m_ModifyTime = time;
218 status.status = git_wc_status_normal;
220 else
221 status.status = git_wc_status_modified;
223 else
224 status.status = git_wc_status_modified;
226 if (entry.m_Flags & GIT_IDXENTRY_STAGEMASK)
227 status.status = git_wc_status_conflicted;
228 else if (entry.m_FlagsExtended & GIT_IDXENTRY_INTENT_TO_ADD)
229 status.status = git_wc_status_added;
231 return 0;
234 int CGitIndexList::GetFileStatus(const CString& gitdir, const CString& path, git_wc_status2_t& status, CGitHash* pHash)
236 ATLASSERT(!status.assumeValid && !status.skipWorktree);
238 __int64 time, filesize = 0;
239 bool isDir = false;
241 int result;
242 if (path.IsEmpty())
243 result = CGit::GetFileModifyTime(gitdir, &time, &isDir);
244 else
245 result = CGit::GetFileModifyTime(CombinePath(gitdir, path), &time, &isDir, &filesize);
247 if (result)
248 filesize = -1;
250 if (!isDir)
251 return GetFileStatus(gitdir, path, status, time, filesize, pHash);
253 if (CStringUtils::EndsWith(path, L'/'))
255 size_t index = SearchInSortVector(*this, path, -1);
256 if (index == NPOS)
258 status.status = git_wc_status_unversioned;
259 if (pHash)
260 pHash->Empty();
262 return 0;
265 if (pHash)
266 *pHash = (*this)[index].m_IndexHash;
268 if (!result)
269 status.status = git_wc_status_normal;
270 else
271 status.status = git_wc_status_deleted;
272 return 0;
275 // we should never get here
276 status.status = git_wc_status_unversioned;
278 return -1;
281 bool CGitIndexFileMap::HasIndexChangedOnDisk(const CString& gitdir)
283 __int64 time;
285 auto pIndex = SafeGet(gitdir);
287 if (!pIndex)
288 return true;
290 CString IndexFile = g_AdminDirMap.GetWorktreeAdminDirConcat(gitdir, L"index");
291 // no need to refresh if there is no index right now and the current index is empty, but otherwise or lastmodified time differs
292 return (CGit::GetFileModifyTime(IndexFile, &time) && !pIndex->empty()) || pIndex->m_LastModifyTime != time;
295 int CGitIndexFileMap::LoadIndex(const CString &gitdir)
297 SHARED_INDEX_PTR pIndex = std::make_shared<CGitIndexList>();
299 if (pIndex->ReadIndex(gitdir))
301 SafeClear(gitdir);
302 return -1;
305 this->SafeSet(gitdir, pIndex);
307 return 0;
310 // This method is assumed to be called with m_SharedMutex locked.
311 int CGitHeadFileList::GetPackRef(const CString &gitdir)
313 CString PackRef = g_AdminDirMap.GetAdminDirConcat(gitdir, L"packed-refs");
315 __int64 mtime;
316 if (CGit::GetFileModifyTime(PackRef, &mtime))
318 //packed refs is not existed
319 this->m_PackRefFile.Empty();
320 this->m_PackRefMap.clear();
321 return 0;
323 else if(mtime == m_LastModifyTimePackRef)
324 return 0;
325 else
327 this->m_PackRefFile = PackRef;
328 this->m_LastModifyTimePackRef = mtime;
331 m_PackRefMap.clear();
333 CAutoFile hfile = CreateFile(PackRef,
334 GENERIC_READ,
335 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
336 nullptr,
337 OPEN_EXISTING,
338 FILE_ATTRIBUTE_NORMAL,
339 nullptr);
341 if (!hfile)
342 return -1;
344 DWORD filesize = GetFileSize(hfile, nullptr);
345 if (filesize == 0 || filesize == INVALID_FILE_SIZE)
346 return -1;
348 DWORD size = 0;
349 auto buff = std::make_unique<char[]>(filesize);
350 ReadFile(hfile, buff.get(), filesize, &size, nullptr);
352 if (size != filesize)
353 return -1;
355 for (DWORD i = 0; i < filesize;)
357 CString hash;
358 CString ref;
359 if (buff[i] == '#' || buff[i] == '^')
361 while (buff[i] != '\n')
363 ++i;
364 if (i == filesize)
365 break;
367 ++i;
370 if (i >= filesize)
371 break;
373 while (buff[i] != ' ')
375 hash.AppendChar(buff[i]);
376 ++i;
377 if (i == filesize)
378 break;
381 ++i;
382 if (i >= filesize)
383 break;
385 while (buff[i] != '\n')
387 ref.AppendChar(buff[i]);
388 ++i;
389 if (i == filesize)
390 break;
393 if (!ref.IsEmpty())
394 m_PackRefMap[ref] = hash;
396 while (buff[i] == '\n')
398 ++i;
399 if (i == filesize)
400 break;
403 return 0;
405 int CGitHeadFileList::ReadHeadHash(const CString& gitdir)
407 ATLASSERT(m_Gitdir.IsEmpty() && m_HeadFile.IsEmpty() && m_Head.IsEmpty());
409 m_Gitdir = g_AdminDirMap.GetWorktreeAdminDir(gitdir);
411 m_HeadFile = m_Gitdir;
412 m_HeadFile += L"HEAD";
414 if( CGit::GetFileModifyTime(m_HeadFile, &m_LastModifyTimeHead))
415 return -1;
417 CAutoFile hfile = CreateFile(m_HeadFile,
418 GENERIC_READ,
419 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
420 nullptr,
421 OPEN_EXISTING,
422 FILE_ATTRIBUTE_NORMAL,
423 nullptr);
425 if (!hfile)
426 return -1;
428 DWORD size = 0;
429 unsigned char buffer[2 * GIT_HASH_SIZE];
430 ReadFile(hfile, buffer, (DWORD)strlen("ref:"), &size, nullptr);
431 if (size != strlen("ref:"))
432 return -1;
433 buffer[4] = '\0';
434 if (strcmp((const char*)buffer, "ref:") == 0)
436 m_HeadRefFile.Empty();
437 DWORD filesize = GetFileSize(hfile, nullptr);
438 if (filesize < 5 || filesize == INVALID_FILE_SIZE)
439 return -1;
441 unsigned char *p = (unsigned char*)malloc(filesize - strlen("ref:"));
442 if (!p)
443 return -1;
445 ReadFile(hfile, p, filesize - (DWORD)strlen("ref:"), &size, nullptr);
446 CGit::StringAppend(&m_HeadRefFile, p, CP_UTF8, filesize - (int)strlen("ref:"));
447 free(p);
449 CString ref = m_HeadRefFile.Trim();
450 int start = 0;
451 ref = ref.Tokenize(L"\n", start);
452 m_HeadRefFile = g_AdminDirMap.GetAdminDir(gitdir) + m_HeadRefFile;
453 m_HeadRefFile.Replace(L'/', L'\\');
455 __int64 time;
456 if (CGit::GetFileModifyTime(m_HeadRefFile, &time, nullptr))
458 m_HeadRefFile.Empty();
459 if (GetPackRef(gitdir))
460 return -1;
461 if (m_PackRefMap.find(ref) != m_PackRefMap.end())
463 m_Head = m_PackRefMap[ref];
464 return 0;
467 // unborn branch
468 m_Head.Empty();
470 return 0;
473 CAutoFile href = CreateFile(m_HeadRefFile,
474 GENERIC_READ,
475 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
476 nullptr,
477 OPEN_EXISTING,
478 FILE_ATTRIBUTE_NORMAL,
479 nullptr);
481 if (!href)
483 m_HeadRefFile.Empty();
485 if (GetPackRef(gitdir))
486 return -1;
488 if (m_PackRefMap.find(ref) == m_PackRefMap.end())
489 return -1;
491 m_Head = m_PackRefMap[ref];
492 return 0;
495 ReadFile(href, buffer, 2 * GIT_HASH_SIZE, &size, nullptr);
496 if (size != 2 * GIT_HASH_SIZE)
497 return -1;
499 m_Head.ConvertFromStrA((char*)buffer);
501 m_LastModifyTimeRef = time;
503 return 0;
506 ReadFile(hfile, buffer + (DWORD)strlen("ref:"), 2 * GIT_HASH_SIZE - (DWORD)strlen("ref:"), &size, nullptr);
507 if (size != 2 * GIT_HASH_SIZE - (DWORD)strlen("ref:"))
508 return -1;
510 m_HeadRefFile.Empty();
512 m_Head.ConvertFromStrA((char*)buffer);
514 return 0;
517 bool CGitHeadFileList::CheckHeadUpdate()
519 if (this->m_HeadFile.IsEmpty())
520 return true;
522 __int64 mtime=0;
524 if (CGit::GetFileModifyTime(m_HeadFile, &mtime))
525 return true;
527 if (mtime != this->m_LastModifyTimeHead)
528 return true;
530 if (!this->m_HeadRefFile.IsEmpty())
532 if (CGit::GetFileModifyTime(m_HeadRefFile, &mtime))
533 return true;
535 if (mtime != this->m_LastModifyTimeRef)
536 return true;
539 if(!this->m_PackRefFile.IsEmpty())
541 if (CGit::GetFileModifyTime(m_PackRefFile, &mtime))
542 return true;
544 if (mtime != this->m_LastModifyTimePackRef)
545 return true;
548 // in an empty repo HEAD points to refs/heads/master, but this ref doesn't exist.
549 // So we need to retry again and again until the ref exists - otherwise we will never notice
550 if (this->m_Head.IsEmpty() && this->m_HeadRefFile.IsEmpty() && this->m_PackRefFile.IsEmpty())
551 return true;
553 return false;
556 bool CGitHeadFileList::HeadHashEqualsTreeHash()
558 return (m_Head == m_TreeHash);
561 int CGitHeadFileList::CallBack(const unsigned char *sha1, const char *base, int baselen,
562 const char *pathname, unsigned mode, int /*stage*/, void *context)
564 #define S_IFGITLINK 0160000
566 CGitHeadFileList* p = reinterpret_cast<CGitHeadFileList*>(context);
568 if ((mode & S_IFDIR) && (mode & S_IFMT) != S_IFGITLINK)
569 return READ_TREE_RECURSIVE;
571 CGitTreeItem item;
572 item.m_Hash = sha1;
573 CGit::StringAppend(&item.m_FileName, (BYTE*)base, CP_UTF8, baselen);
574 CGit::StringAppend(&item.m_FileName, (BYTE*)pathname, CP_UTF8);
575 if ((mode & S_IFMT) == S_IFGITLINK)
576 item.m_FileName += L'/';
578 p->push_back(item);
580 if( (mode&S_IFMT) == S_IFGITLINK)
581 return 0;
583 return READ_TREE_RECURSIVE;
586 int ReadTreeRecursive(git_repository &repo, const git_tree * tree, const CStringA& base, int (*CallBack) (const unsigned char *, const char *, int, const char *, unsigned int, int, void *), void *data)
588 size_t count = git_tree_entrycount(tree);
589 for (size_t i = 0; i < count; ++i)
591 const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
592 if (!entry)
593 continue;
594 int mode = git_tree_entry_filemode(entry);
595 if( CallBack(git_tree_entry_id(entry)->id,
596 base,
597 base.GetLength(),
598 git_tree_entry_name(entry),
599 mode,
601 data) == READ_TREE_RECURSIVE
604 if(mode&S_IFDIR)
606 git_object* object = nullptr;
607 git_tree_entry_to_object(&object, &repo, entry);
608 if (!object)
609 continue;
610 CStringA parent = base;
611 parent += git_tree_entry_name(entry);
612 parent += "/";
613 ReadTreeRecursive(repo, (git_tree*)object, parent, CallBack, data);
614 git_object_free(object);
620 return 0;
623 // ReadTree is/must only be executed on an empty list
624 int CGitHeadFileList::ReadTree()
626 ATLASSERT(empty() && m_TreeHash.IsEmpty());
628 // unborn branch
629 if (m_Head.IsEmpty())
630 return 0;
632 CAutoRepository repository(m_Gitdir);
633 CAutoCommit commit;
634 CAutoTree tree;
635 bool ret = repository;
636 ret = ret && !git_commit_lookup(commit.GetPointer(), repository, (const git_oid*)m_Head.m_hash);
637 ret = ret && !git_commit_tree(tree.GetPointer(), commit);
640 ret = ret && !ReadTreeRecursive(*repository, tree, "", CGitHeadFileList::CallBack, this);
642 catch (const std::bad_alloc& ex)
644 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Catched exception inside ReadTreeRecursive: %s\n", ex.what());
645 return -1;
647 if (!ret)
649 clear();
650 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Could not open git repository in %s and read HEAD commit %s: %s\n", (LPCTSTR)m_Gitdir, (LPCTSTR)m_Head.ToString(), (LPCTSTR)CGit::GetLibGit2LastErr());
651 m_LastModifyTimeHead = 0;
652 return -1;
655 std::sort(this->begin(), this->end(), SortTree);
656 m_TreeHash = git_commit_id(commit)->id;
658 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Reloaded HEAD tree (commit is %s) for repo: %s\n", (LPCTSTR)m_Head.ToString(), (LPCTSTR)m_Gitdir);
660 return 0;
662 int CGitIgnoreItem::FetchIgnoreList(const CString& projectroot, const CString& file, bool isGlobal, int* ignoreCase)
664 if (this->m_pExcludeList)
666 git_free_exclude_list(m_pExcludeList);
667 m_pExcludeList = nullptr;
669 free(m_buffer);
670 m_buffer = nullptr;
672 this->m_BaseDir.Empty();
673 if (!isGlobal)
675 CString base = file.Mid(projectroot.GetLength() + 1);
676 base.Replace(L'\\', L'/');
678 int start = base.ReverseFind(L'/');
679 if(start > 0)
681 base.Truncate(start);
682 this->m_BaseDir = CUnicodeUtils::GetMulti(base, CP_UTF8) + "/";
686 if (CGit::GetFileModifyTime(file, &m_LastModifyTime))
687 return -1;
689 CAutoFile hfile = CreateFile(file,
690 GENERIC_READ,
691 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
692 nullptr,
693 OPEN_EXISTING,
694 FILE_ATTRIBUTE_NORMAL,
695 nullptr);
697 if (!hfile)
698 return -1 ;
700 DWORD filesize = GetFileSize(hfile, nullptr);
701 if (filesize == INVALID_FILE_SIZE)
702 return -1;
704 m_buffer = new BYTE[filesize + 1];
705 if (!m_buffer)
706 return -1;
708 DWORD size = 0;
709 if (!ReadFile(hfile, m_buffer, filesize, &size, nullptr))
711 free(m_buffer);
712 m_buffer = nullptr;
713 return -1;
715 m_buffer[size] = '\0';
717 if (git_create_exclude_list(&m_pExcludeList))
719 free(m_buffer);
720 m_buffer = nullptr;
721 return -1;
724 m_iIgnoreCase = ignoreCase;
726 BYTE *p = m_buffer;
727 int line = 0;
728 for (DWORD i = 0; i < size; ++i)
730 if (m_buffer[i] == '\n' || m_buffer[i] == '\r' || i == (size - 1))
732 if (m_buffer[i] == '\n' || m_buffer[i] == '\r')
733 m_buffer[i] = '\0';
735 if (p[0] != '#' && p[0])
736 git_add_exclude((const char*)p, this->m_BaseDir, m_BaseDir.GetLength(), this->m_pExcludeList, ++line);
738 p = m_buffer + i + 1;
742 if (!line)
744 git_free_exclude_list(m_pExcludeList);
745 m_pExcludeList = nullptr;
746 free(m_buffer);
747 m_buffer = nullptr;
750 return 0;
753 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
754 int CGitIgnoreItem::IsPathIgnored(const CStringA& patha, int& type)
756 int pos = patha.ReverseFind('/');
757 const char* base = (pos >= 0) ? ((const char*)patha + pos + 1) : patha;
759 return IsPathIgnored(patha, base, type);
761 #endif
763 int CGitIgnoreItem::IsPathIgnored(const CStringA& patha, const char* base, int& type)
765 if (!m_pExcludeList)
766 return -1; // error or undecided
768 return git_check_excluded_1(patha, patha.GetLength(), base, &type, m_pExcludeList, m_iIgnoreCase ? *m_iIgnoreCase : 1);
771 bool CGitIgnoreList::CheckFileChanged(const CString &path)
773 __int64 time = 0;
775 int ret = CGit::GetFileModifyTime(path, &time);
777 bool cacheExist;
779 CAutoReadLock lock(m_SharedMutex);
780 cacheExist = (m_Map.find(path) != m_Map.end());
783 if (!cacheExist && ret == 0)
785 CAutoWriteLock lock(m_SharedMutex);
786 m_Map[path].m_LastModifyTime = 0;
788 // both cache and file is not exist
789 if ((ret != 0) && (!cacheExist))
790 return false;
792 // file exist but cache miss
793 if ((ret == 0) && (!cacheExist))
794 return true;
796 // file not exist but cache exist
797 if ((ret != 0) && (cacheExist))
798 return true;
799 // file exist and cache exist
802 CAutoReadLock lock(m_SharedMutex);
803 if (m_Map[path].m_LastModifyTime == time)
804 return false;
806 return true;
809 int CGitIgnoreList::FetchIgnoreFile(const CString &gitdir, const CString &gitignore, bool isGlobal)
811 if (CGit::GitPathFileExists(gitignore)) //if .gitignore remove, we need remote cache
813 CAutoWriteLock lock(m_SharedMutex);
814 m_Map[gitignore].FetchIgnoreList(gitdir, gitignore, isGlobal, &m_IgnoreCase[g_AdminDirMap.GetAdminDir(gitdir)]);
816 else
818 CAutoWriteLock lock(m_SharedMutex);
819 m_Map.erase(gitignore);
821 return 0;
824 bool CGitIgnoreList::CheckAndUpdateIgnoreFiles(const CString& gitdir, const CString& path, bool isDir)
826 CString temp(gitdir);
827 temp += L'\\';
828 temp += path;
830 temp.Replace(L'/', L'\\');
832 if (!isDir)
834 int x = temp.ReverseFind(L'\\');
835 if (x >= 2)
836 temp.Truncate(x);
839 bool updated = false;
840 while (!temp.IsEmpty())
842 temp += L"\\.gitignore";
844 if (CheckFileChanged(temp))
846 FetchIgnoreFile(gitdir, temp, false);
847 updated = true;
850 temp.Truncate(temp.GetLength() - (int)wcslen(L"\\.gitignore"));
851 if (CPathUtils::ArePathStringsEqual(temp, gitdir))
853 CString adminDir = g_AdminDirMap.GetAdminDir(temp);
854 CString wcglobalgitignore = adminDir + L"info\\exclude";
855 if (CheckFileChanged(wcglobalgitignore))
857 FetchIgnoreFile(gitdir, wcglobalgitignore, true);
858 updated = true;
861 if (CheckAndUpdateCoreExcludefile(adminDir))
863 CString excludesFile;
865 CAutoReadLock lock(m_SharedMutex);
866 excludesFile = m_CoreExcludesfiles[adminDir];
868 if (!excludesFile.IsEmpty())
870 FetchIgnoreFile(gitdir, excludesFile, true);
871 updated = true;
875 return updated;
878 int i = temp.ReverseFind(L'\\');
879 temp.Truncate(max(0, i));
881 return updated;
884 bool CGitIgnoreList::CheckAndUpdateGitSystemConfigPath(bool force)
886 if (force)
887 m_sGitProgramDataConfigPath = GetProgramDataGitConfig();
888 // recheck every 30 seconds
889 if (GetTickCount64() - m_dGitSystemConfigPathLastChecked > 30000UL || force)
891 m_dGitSystemConfigPathLastChecked = GetTickCount64();
892 CString gitSystemConfigPath(CRegString(REG_SYSTEM_GITCONFIGPATH, L"", FALSE));
893 if (gitSystemConfigPath != m_sGitSystemConfigPath)
895 m_sGitSystemConfigPath = gitSystemConfigPath;
896 return true;
899 return false;
901 bool CGitIgnoreList::CheckAndUpdateCoreExcludefile(const CString &adminDir)
903 CString projectConfig(adminDir);
904 projectConfig += L"config";
905 CString globalConfig = g_Git.GetGitGlobalConfig();
906 CString globalXDGConfig = g_Git.GetGitGlobalXDGConfig();
908 CAutoWriteLock lock(m_coreExcludefilesSharedMutex);
909 bool hasChanged = CheckAndUpdateGitSystemConfigPath();
910 hasChanged = hasChanged || CheckFileChanged(projectConfig);
911 hasChanged = hasChanged || CheckFileChanged(globalConfig);
912 hasChanged = hasChanged || CheckFileChanged(globalXDGConfig);
913 if (!m_sGitProgramDataConfigPath.IsEmpty())
914 hasChanged = hasChanged || CheckFileChanged(m_sGitProgramDataConfigPath);
915 if (!m_sGitSystemConfigPath.IsEmpty())
916 hasChanged = hasChanged || CheckFileChanged(m_sGitSystemConfigPath);
918 CString excludesFile;
920 CAutoReadLock lock2(m_SharedMutex);
921 excludesFile = m_CoreExcludesfiles[adminDir];
923 if (!excludesFile.IsEmpty())
924 hasChanged = hasChanged || CheckFileChanged(excludesFile);
926 if (!hasChanged)
927 return false;
929 CAutoConfig config(true);
930 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(projectConfig), GIT_CONFIG_LEVEL_LOCAL, FALSE);
931 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalConfig), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
932 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalXDGConfig), GIT_CONFIG_LEVEL_XDG, FALSE);
933 if (!m_sGitSystemConfigPath.IsEmpty())
934 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(m_sGitSystemConfigPath), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
935 if (!m_sGitProgramDataConfigPath.IsEmpty())
936 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(m_sGitProgramDataConfigPath), GIT_CONFIG_LEVEL_PROGRAMDATA, FALSE);
938 config.GetString(L"core.excludesfile", excludesFile);
939 if (excludesFile.IsEmpty())
940 excludesFile = GetWindowsHome() + L"\\.config\\git\\ignore";
941 else if (CStringUtils::StartsWith(excludesFile, L"~/"))
942 excludesFile = GetWindowsHome() + excludesFile.Mid(1);
944 CAutoWriteLock lockMap(m_SharedMutex);
945 m_IgnoreCase[adminDir] = 1;
946 config.GetBOOL(L"core.ignorecase", m_IgnoreCase[adminDir]);
947 CGit::GetFileModifyTime(projectConfig, &m_Map[projectConfig].m_LastModifyTime);
948 CGit::GetFileModifyTime(globalXDGConfig, &m_Map[globalXDGConfig].m_LastModifyTime);
949 if (m_Map[globalXDGConfig].m_LastModifyTime == 0)
950 m_Map.erase(globalXDGConfig);
951 CGit::GetFileModifyTime(globalConfig, &m_Map[globalConfig].m_LastModifyTime);
952 if (m_Map[globalConfig].m_LastModifyTime == 0)
953 m_Map.erase(globalConfig);
954 if (!m_sGitSystemConfigPath.IsEmpty())
955 CGit::GetFileModifyTime(m_sGitSystemConfigPath, &m_Map[m_sGitSystemConfigPath].m_LastModifyTime);
956 if (m_Map[m_sGitSystemConfigPath].m_LastModifyTime == 0 || m_sGitSystemConfigPath.IsEmpty())
957 m_Map.erase(m_sGitSystemConfigPath);
958 if (!m_sGitProgramDataConfigPath.IsEmpty())
959 CGit::GetFileModifyTime(m_sGitProgramDataConfigPath, &m_Map[m_sGitProgramDataConfigPath].m_LastModifyTime);
960 if (m_Map[m_sGitProgramDataConfigPath].m_LastModifyTime == 0 || m_sGitProgramDataConfigPath.IsEmpty())
961 m_Map.erase(m_sGitProgramDataConfigPath);
962 m_CoreExcludesfiles[adminDir] = excludesFile;
964 return true;
966 const CString CGitIgnoreList::GetWindowsHome()
968 static CString sWindowsHome(g_Git.GetHomeDirectory());
969 return sWindowsHome;
971 bool CGitIgnoreList::IsIgnore(CString str, const CString& projectroot, bool isDir)
973 str.Replace(L'\\', L'/');
975 if (!str.IsEmpty() && str[str.GetLength() - 1] == L'/')
976 str.Truncate(str.GetLength() - 1);
978 int ret;
979 ret = CheckIgnore(str, projectroot, isDir);
980 while (ret < 0)
982 int start = str.ReverseFind(L'/');
983 if(start < 0)
984 return (ret == 1);
986 str.Truncate(start);
987 ret = CheckIgnore(str, projectroot, isDir);
990 return (ret == 1);
992 int CGitIgnoreList::CheckFileAgainstIgnoreList(const CString &ignorefile, const CStringA &patha, const char * base, int &type)
994 if (m_Map.find(ignorefile) == m_Map.end())
995 return -1; // error or undecided
997 return (m_Map[ignorefile].IsPathIgnored(patha, base, type));
999 int CGitIgnoreList::CheckIgnore(const CString &path, const CString &projectroot, bool isDir)
1001 CString temp = CombinePath(projectroot, path);
1002 temp.Replace(L'/', L'\\');
1004 CStringA patha = CUnicodeUtils::GetMulti(path, CP_UTF8);
1005 patha.Replace('\\', '/');
1007 int type = 0;
1008 if (isDir)
1010 type = DT_DIR;
1012 // strip directory name
1013 // we do not need to check for a .ignore file inside a directory we might ignore
1014 int i = temp.ReverseFind(L'\\');
1015 if (i >= 0)
1016 temp.Truncate(i);
1018 else
1020 type = DT_REG;
1022 int x = temp.ReverseFind(L'\\');
1023 if (x >= 2)
1024 temp.Truncate(x);
1027 int pos = patha.ReverseFind('/');
1028 const char * base = (pos >= 0) ? ((const char*)patha + pos + 1) : patha;
1030 int ret = -1;
1032 CAutoReadLock lock(m_SharedMutex);
1033 while (!temp.IsEmpty())
1035 temp += L"\\.gitignore";
1037 if ((ret = CheckFileAgainstIgnoreList(temp, patha, base, type)) != -1)
1038 return ret;
1040 temp.Truncate(temp.GetLength() - (int)wcslen(L"\\.gitignore"));
1042 if (CPathUtils::ArePathStringsEqual(temp, projectroot))
1044 CString adminDir = g_AdminDirMap.GetAdminDir(temp);
1045 CString wcglobalgitignore = adminDir;
1046 wcglobalgitignore += L"info\\exclude";
1047 if ((ret = CheckFileAgainstIgnoreList(wcglobalgitignore, patha, base, type)) != -1)
1048 return ret;
1050 CString excludesFile = m_CoreExcludesfiles[adminDir];
1051 if (!excludesFile.IsEmpty())
1052 return CheckFileAgainstIgnoreList(excludesFile, patha, base, type);
1054 return -1;
1057 int i = temp.ReverseFind(L'\\');
1058 temp.Truncate(max(0, i));
1061 return -1;
1064 void CGitHeadFileMap::CheckHeadAndUpdate(const CString &gitdir)
1066 SHARED_TREE_PTR ptr = this->SafeGet(gitdir);
1068 if (ptr.get() && !ptr->CheckHeadUpdate() && ptr->HeadHashEqualsTreeHash())
1069 return;
1071 ptr = std::make_shared<CGitHeadFileList>();
1072 if (ptr->ReadHeadHash(gitdir) || ptr->ReadTree())
1074 SafeClear(gitdir);
1075 return;
1078 this->SafeSet(gitdir, ptr);
1080 return;