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.
23 #include "UnicodeUtils.h"
24 #include "PathUtils.h"
26 #include <sys/types.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";
44 int CGitIndex::Print()
46 wprintf(L
"0x%08X 0x%08X %s %s\n",
47 (int)this->m_ModifyTime
,
49 (LPCTSTR
)this->m_IndexHash
.ToString(),
50 (LPCTSTR
)this->m_FileName
);
55 CGitIndexList::CGitIndexList()
56 : m_bHasConflicts(FALSE
)
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
83 CAutoRepository
repository(dgitdir
);
86 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not open git repository in %s: %s\n", (LPCTSTR
)dgitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
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
);
110 // load index in order to enumerate files
111 if (git_repository_index(index
.GetPointer(), repository
))
114 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not get index of git repository in %s: %s\n", (LPCTSTR
)dgitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
118 m_bHasConflicts
= FALSE
;
120 size_t ecount
= git_index_entrycount(index
);
125 catch (const std::bad_alloc
& ex
)
128 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Could not resize index-vector: %s\n", ex
.what());
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
);
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);
161 status
.status
= git_wc_status_unversioned
;
168 auto& entry
= (*this)[index
];
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
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());
210 git_repository_set_config(repository
, config
);
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
;
221 status
.status
= git_wc_status_modified
;
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
;
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;
243 result
= CGit::GetFileModifyTime(gitdir
, &time
, &isDir
);
245 result
= CGit::GetFileModifyTime(CombinePath(gitdir
, path
), &time
, &isDir
, &filesize
);
251 return GetFileStatus(gitdir
, path
, status
, time
, filesize
, pHash
);
253 if (CStringUtils::EndsWith(path
, L
'/'))
255 size_t index
= SearchInSortVector(*this, path
, -1);
258 status
.status
= git_wc_status_unversioned
;
266 *pHash
= (*this)[index
].m_IndexHash
;
269 status
.status
= git_wc_status_normal
;
271 status
.status
= git_wc_status_deleted
;
275 // we should never get here
276 status
.status
= git_wc_status_unversioned
;
281 bool CGitIndexFileMap::HasIndexChangedOnDisk(const CString
& gitdir
)
285 auto pIndex
= SafeGet(gitdir
);
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
))
305 this->SafeSet(gitdir
, pIndex
);
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");
316 if (CGit::GetFileModifyTime(PackRef
, &mtime
))
318 //packed refs is not existed
319 this->m_PackRefFile
.Empty();
320 this->m_PackRefMap
.clear();
323 else if(mtime
== m_LastModifyTimePackRef
)
327 this->m_PackRefFile
= PackRef
;
328 this->m_LastModifyTimePackRef
= mtime
;
331 m_PackRefMap
.clear();
333 CAutoFile hfile
= CreateFile(PackRef
,
335 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
338 FILE_ATTRIBUTE_NORMAL
,
344 DWORD filesize
= GetFileSize(hfile
, nullptr);
345 if (filesize
== 0 || filesize
== INVALID_FILE_SIZE
)
349 auto buff
= std::make_unique
<char[]>(filesize
);
350 ReadFile(hfile
, buff
.get(), filesize
, &size
, nullptr);
352 if (size
!= filesize
)
355 for (DWORD i
= 0; i
< filesize
;)
359 if (buff
[i
] == '#' || buff
[i
] == '^')
361 while (buff
[i
] != '\n')
373 while (buff
[i
] != ' ')
375 hash
.AppendChar(buff
[i
]);
385 while (buff
[i
] != '\n')
387 ref
.AppendChar(buff
[i
]);
394 m_PackRefMap
[ref
] = hash
;
396 while (buff
[i
] == '\n')
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
))
417 CAutoFile hfile
= CreateFile(m_HeadFile
,
419 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
422 FILE_ATTRIBUTE_NORMAL
,
429 unsigned char buffer
[2 * GIT_HASH_SIZE
];
430 ReadFile(hfile
, buffer
, (DWORD
)strlen("ref:"), &size
, nullptr);
431 if (size
!= strlen("ref:"))
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
)
441 unsigned char *p
= (unsigned char*)malloc(filesize
- strlen("ref:"));
445 ReadFile(hfile
, p
, filesize
- (DWORD
)strlen("ref:"), &size
, nullptr);
446 CGit::StringAppend(&m_HeadRefFile
, p
, CP_UTF8
, filesize
- (int)strlen("ref:"));
449 CString ref
= m_HeadRefFile
.Trim();
451 ref
= ref
.Tokenize(L
"\n", start
);
452 m_HeadRefFile
= g_AdminDirMap
.GetAdminDir(gitdir
) + m_HeadRefFile
;
453 m_HeadRefFile
.Replace(L
'/', L
'\\');
456 if (CGit::GetFileModifyTime(m_HeadRefFile
, &time
, nullptr))
458 m_HeadRefFile
.Empty();
459 if (GetPackRef(gitdir
))
461 if (m_PackRefMap
.find(ref
) != m_PackRefMap
.end())
463 m_Head
= m_PackRefMap
[ref
];
473 CAutoFile href
= CreateFile(m_HeadRefFile
,
475 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
478 FILE_ATTRIBUTE_NORMAL
,
483 m_HeadRefFile
.Empty();
485 if (GetPackRef(gitdir
))
488 if (m_PackRefMap
.find(ref
) == m_PackRefMap
.end())
491 m_Head
= m_PackRefMap
[ref
];
495 ReadFile(href
, buffer
, 2 * GIT_HASH_SIZE
, &size
, nullptr);
496 if (size
!= 2 * GIT_HASH_SIZE
)
499 m_Head
.ConvertFromStrA((char*)buffer
);
501 m_LastModifyTimeRef
= time
;
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:"))
510 m_HeadRefFile
.Empty();
512 m_Head
.ConvertFromStrA((char*)buffer
);
517 bool CGitHeadFileList::CheckHeadUpdate()
519 if (this->m_HeadFile
.IsEmpty())
524 if (CGit::GetFileModifyTime(m_HeadFile
, &mtime
))
527 if (mtime
!= this->m_LastModifyTimeHead
)
530 if (!this->m_HeadRefFile
.IsEmpty())
532 if (CGit::GetFileModifyTime(m_HeadRefFile
, &mtime
))
535 if (mtime
!= this->m_LastModifyTimeRef
)
539 if(!this->m_PackRefFile
.IsEmpty())
541 if (CGit::GetFileModifyTime(m_PackRefFile
, &mtime
))
544 if (mtime
!= this->m_LastModifyTimePackRef
)
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())
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
;
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
'/';
580 if( (mode
&S_IFMT
) == S_IFGITLINK
)
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
);
594 int mode
= git_tree_entry_filemode(entry
);
595 if( CallBack(git_tree_entry_id(entry
)->id
,
598 git_tree_entry_name(entry
),
601 data
) == READ_TREE_RECURSIVE
606 git_object
* object
= nullptr;
607 git_tree_entry_to_object(&object
, &repo
, entry
);
610 CStringA parent
= base
;
611 parent
+= git_tree_entry_name(entry
);
613 ReadTreeRecursive(repo
, (git_tree
*)object
, parent
, CallBack
, data
);
614 git_object_free(object
);
623 // ReadTree is/must only be executed on an empty list
624 int CGitHeadFileList::ReadTree()
626 ATLASSERT(empty() && m_TreeHash
.IsEmpty());
629 if (m_Head
.IsEmpty())
632 CAutoRepository
repository(m_Gitdir
);
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());
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;
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
);
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;
672 this->m_BaseDir
.Empty();
675 CString base
= file
.Mid(projectroot
.GetLength() + 1);
676 base
.Replace(L
'\\', L
'/');
678 int start
= base
.ReverseFind(L
'/');
681 base
.Truncate(start
);
682 this->m_BaseDir
= CUnicodeUtils::GetMulti(base
, CP_UTF8
) + "/";
686 if (CGit::GetFileModifyTime(file
, &m_LastModifyTime
))
689 CAutoFile hfile
= CreateFile(file
,
691 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
694 FILE_ATTRIBUTE_NORMAL
,
700 DWORD filesize
= GetFileSize(hfile
, nullptr);
701 if (filesize
== INVALID_FILE_SIZE
)
704 m_buffer
= new BYTE
[filesize
+ 1];
709 if (!ReadFile(hfile
, m_buffer
, filesize
, &size
, nullptr))
715 m_buffer
[size
] = '\0';
717 if (git_create_exclude_list(&m_pExcludeList
))
724 m_iIgnoreCase
= ignoreCase
;
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')
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;
744 git_free_exclude_list(m_pExcludeList
);
745 m_pExcludeList
= nullptr;
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
);
763 int CGitIgnoreItem::IsPathIgnored(const CStringA
& patha
, const char* base
, int& type
)
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
)
775 int ret
= CGit::GetFileModifyTime(path
, &time
);
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
))
792 // file exist but cache miss
793 if ((ret
== 0) && (!cacheExist
))
796 // file not exist but cache exist
797 if ((ret
!= 0) && (cacheExist
))
799 // file exist and cache exist
802 CAutoReadLock
lock(m_SharedMutex
);
803 if (m_Map
[path
].m_LastModifyTime
== time
)
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
)]);
818 CAutoWriteLock
lock(m_SharedMutex
);
819 m_Map
.erase(gitignore
);
824 bool CGitIgnoreList::CheckAndUpdateIgnoreFiles(const CString
& gitdir
, const CString
& path
, bool isDir
)
826 CString
temp(gitdir
);
830 temp
.Replace(L
'/', L
'\\');
834 int x
= temp
.ReverseFind(L
'\\');
839 bool updated
= false;
840 while (!temp
.IsEmpty())
842 temp
+= L
"\\.gitignore";
844 if (CheckFileChanged(temp
))
846 FetchIgnoreFile(gitdir
, temp
, false);
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);
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);
878 int i
= temp
.ReverseFind(L
'\\');
879 temp
.Truncate(max(0, i
));
884 bool CGitIgnoreList::CheckAndUpdateGitSystemConfigPath(bool 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
;
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
);
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
;
966 const CString
CGitIgnoreList::GetWindowsHome()
968 static CString
sWindowsHome(g_Git
.GetHomeDirectory());
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);
979 ret
= CheckIgnore(str
, projectroot
, isDir
);
982 int start
= str
.ReverseFind(L
'/');
987 ret
= CheckIgnore(str
, projectroot
, isDir
);
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('\\', '/');
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
'\\');
1022 int x
= temp
.ReverseFind(L
'\\');
1027 int pos
= patha
.ReverseFind('/');
1028 const char * base
= (pos
>= 0) ? ((const char*)patha
+ pos
+ 1) : patha
;
1032 CAutoReadLock
lock(m_SharedMutex
);
1033 while (!temp
.IsEmpty())
1035 temp
+= L
"\\.gitignore";
1037 if ((ret
= CheckFileAgainstIgnoreList(temp
, patha
, base
, type
)) != -1)
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)
1050 CString excludesFile
= m_CoreExcludesfiles
[adminDir
];
1051 if (!excludesFile
.IsEmpty())
1052 return CheckFileAgainstIgnoreList(excludesFile
, patha
, base
, type
);
1057 int i
= temp
.ReverseFind(L
'\\');
1058 temp
.Truncate(max(0, i
));
1064 void CGitHeadFileMap::CheckHeadAndUpdate(const CString
&gitdir
)
1066 SHARED_TREE_PTR ptr
= this->SafeGet(gitdir
);
1068 if (ptr
.get() && !ptr
->CheckHeadUpdate() && ptr
->HeadHashEqualsTreeHash())
1071 ptr
= std::make_shared
<CGitHeadFileList
>();
1072 if (ptr
->ReadHeadHash(gitdir
) || ptr
->ReadTree())
1078 this->SafeSet(gitdir
, ptr
);