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
)
60 m_iMaxCheckSize
= (__int64
)CRegDWORD(L
"Software\\TortoiseGit\\TGitCacheCheckContentMaxSize", 10 * 1024) * 1024; // stored in KiB
63 CGitIndexList::~CGitIndexList()
67 static bool SortIndex(const CGitIndex
&Item1
, const CGitIndex
&Item2
)
69 return Item1
.m_FileName
.Compare(Item2
.m_FileName
) < 0;
72 static bool SortTree(const CGitTreeItem
&Item1
, const CGitTreeItem
&Item2
)
74 return Item1
.m_FileName
.Compare(Item2
.m_FileName
) < 0;
77 int CGitIndexList::ReadIndex(CString dgitdir
)
79 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
80 clear(); // HACK to make tests work, until we use CGitIndexList
84 CAutoRepository
repository(dgitdir
);
87 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not open git repository in %s: %s\n", (LPCTSTR
)dgitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
94 CString projectConfig
= g_AdminDirMap
.GetAdminDir(dgitdir
) + L
"config";
95 CString globalConfig
= g_Git
.GetGitGlobalConfig();
96 CString globalXDGConfig
= g_Git
.GetGitGlobalXDGConfig();
97 CString
systemConfig(CRegString(REG_SYSTEM_GITCONFIGPATH
, L
"", FALSE
));
98 CString
programDataConfig(GetProgramDataGitConfig());
100 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(projectConfig
), GIT_CONFIG_LEVEL_LOCAL
, FALSE
);
101 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalConfig
), GIT_CONFIG_LEVEL_GLOBAL
, FALSE
);
102 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalXDGConfig
), GIT_CONFIG_LEVEL_XDG
, FALSE
);
103 if (!systemConfig
.IsEmpty())
104 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(systemConfig
), GIT_CONFIG_LEVEL_SYSTEM
, FALSE
);
105 if (!programDataConfig
.IsEmpty())
106 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(programDataConfig
), GIT_CONFIG_LEVEL_PROGRAMDATA
, FALSE
);
108 git_repository_set_config(repository
, config
);
110 CGit::GetFileModifyTime(g_AdminDirMap
.GetWorktreeAdminDir(dgitdir
) + L
"index", &m_LastModifyTime
, nullptr, &m_LastFileSize
);
113 // load index in order to enumerate files
114 if (git_repository_index(index
.GetPointer(), repository
))
117 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not get index of git repository in %s: %s\n", (LPCTSTR
)dgitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
121 m_bHasConflicts
= FALSE
;
123 size_t ecount
= git_index_entrycount(index
);
128 catch (const std::bad_alloc
& ex
)
131 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Could not resize index-vector: %s\n", ex
.what());
134 for (size_t i
= 0; i
< ecount
; ++i
)
136 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
138 auto& item
= (*this)[i
];
139 item
.m_FileName
= CUnicodeUtils::GetUnicode(e
->path
);
140 if (e
->mode
& S_IFDIR
)
141 item
.m_FileName
+= L
'/';
142 item
.m_ModifyTime
= e
->mtime
.seconds
;
143 item
.m_Flags
= e
->flags
;
144 item
.m_FlagsExtended
= e
->flags_extended
;
145 item
.m_IndexHash
= e
->id
.id
;
146 item
.m_Size
= e
->file_size
;
147 m_bHasConflicts
|= GIT_IDXENTRY_STAGE(e
);
150 std::sort(this->begin(), this->end(), SortIndex
);
152 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Reloaded index for repo: %s\n", (LPCTSTR
)dgitdir
);
157 int CGitIndexList::GetFileStatus(const CString
& gitdir
, const CString
& pathorg
, git_wc_status2_t
& status
, __int64 time
, __int64 filesize
, CGitHash
* pHash
)
159 size_t index
= SearchInSortVector(*this, pathorg
, -1);
163 status
.status
= git_wc_status_unversioned
;
170 auto& entry
= (*this)[index
];
172 *pHash
= entry
.m_IndexHash
;
173 ATLASSERT(pathorg
== entry
.m_FileName
);
174 CAutoRepository repository
;
175 return GetFileStatus(repository
, gitdir
, entry
, status
, time
, filesize
);
178 int CGitIndexList::GetFileStatus(CAutoRepository
& repository
, const CString
& gitdir
, CGitIndex
& entry
, git_wc_status2_t
& status
, __int64 time
, __int64 filesize
)
180 ATLASSERT(!status
.assumeValid
&& !status
.skipWorktree
);
182 // skip-worktree has higher priority than assume-valid
183 if (entry
.m_FlagsExtended
& GIT_IDXENTRY_SKIP_WORKTREE
)
185 status
.status
= git_wc_status_normal
;
186 status
.skipWorktree
= true;
188 else if (entry
.m_Flags
& GIT_IDXENTRY_VALID
)
190 status
.status
= git_wc_status_normal
;
191 status
.assumeValid
= true;
193 else if (filesize
== -1)
194 status
.status
= git_wc_status_deleted
;
195 else if (filesize
!= entry
.m_Size
)
196 status
.status
= git_wc_status_modified
;
197 else if (CGit::filetime_to_time_t(time
) == entry
.m_ModifyTime
)
198 status
.status
= git_wc_status_normal
;
199 else if (config
&& filesize
< m_iMaxCheckSize
)
202 * Opening a new repository each time is not yet optimal, however, there is no API to clear the pack-cache
203 * When a shared repository is used, we might need a mutex to prevent concurrent access to repository instance and especially filter-lists
207 if (repository
.Open(gitdir
))
209 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not open git repository in %s for checking file: %s\n", (LPCTSTR
)gitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
212 git_repository_set_config(repository
, config
);
216 CStringA fileA
= CUnicodeUtils::GetMulti(entry
.m_FileName
, CP_UTF8
);
217 if (!git_repository_hashfile(&actual
, repository
, fileA
, GIT_OBJ_BLOB
, nullptr) && !git_oid_cmp(&actual
, (const git_oid
*)entry
.m_IndexHash
.m_hash
))
219 entry
.m_ModifyTime
= time
;
220 status
.status
= git_wc_status_normal
;
223 status
.status
= git_wc_status_modified
;
226 status
.status
= git_wc_status_modified
;
228 if (entry
.m_Flags
& GIT_IDXENTRY_STAGEMASK
)
229 status
.status
= git_wc_status_conflicted
;
230 else if (entry
.m_FlagsExtended
& GIT_IDXENTRY_INTENT_TO_ADD
)
231 status
.status
= git_wc_status_added
;
236 int CGitIndexList::GetFileStatus(const CString
& gitdir
, const CString
& path
, git_wc_status2_t
& status
, CGitHash
* pHash
)
238 ATLASSERT(!status
.assumeValid
&& !status
.skipWorktree
);
240 __int64 time
, filesize
= 0;
245 result
= CGit::GetFileModifyTime(gitdir
, &time
, &isDir
);
247 result
= CGit::GetFileModifyTime(CombinePath(gitdir
, path
), &time
, &isDir
, &filesize
);
253 return GetFileStatus(gitdir
, path
, status
, time
, filesize
, pHash
);
255 if (CStringUtils::EndsWith(path
, L
'/'))
257 size_t index
= SearchInSortVector(*this, path
, -1);
260 status
.status
= git_wc_status_unversioned
;
268 *pHash
= (*this)[index
].m_IndexHash
;
271 status
.status
= git_wc_status_normal
;
273 status
.status
= git_wc_status_deleted
;
277 // we should never get here
278 status
.status
= git_wc_status_unversioned
;
283 bool CGitIndexFileMap::HasIndexChangedOnDisk(const CString
& gitdir
)
285 __int64 time
= -1, size
= -1;
287 auto pIndex
= SafeGet(gitdir
);
292 CString IndexFile
= g_AdminDirMap
.GetWorktreeAdminDirConcat(gitdir
, L
"index");
293 // no need to refresh if there is no index right now and the current index is empty, but otherwise or lastmodified time differs
294 return (CGit::GetFileModifyTime(IndexFile
, &time
, nullptr, &size
) && !pIndex
->empty()) || pIndex
->m_LastModifyTime
!= time
|| pIndex
->m_LastFileSize
!= size
;
297 int CGitIndexFileMap::LoadIndex(const CString
&gitdir
)
299 SHARED_INDEX_PTR pIndex
= std::make_shared
<CGitIndexList
>();
301 if (pIndex
->ReadIndex(gitdir
))
307 this->SafeSet(gitdir
, pIndex
);
312 // This method is assumed to be called with m_SharedMutex locked.
313 int CGitHeadFileList::GetPackRef(const CString
&gitdir
)
315 CString PackRef
= g_AdminDirMap
.GetAdminDirConcat(gitdir
, L
"packed-refs");
317 __int64 mtime
= 0, packsize
= -1;
318 if (CGit::GetFileModifyTime(PackRef
, &mtime
, nullptr, &packsize
))
320 //packed refs is not existed
321 this->m_PackRefFile
.Empty();
322 this->m_PackRefMap
.clear();
325 else if (mtime
== m_LastModifyTimePackRef
&& packsize
== m_LastFileSizePackRef
)
329 this->m_PackRefFile
= PackRef
;
330 this->m_LastModifyTimePackRef
= mtime
;
331 this->m_LastFileSizePackRef
= packsize
;
334 m_PackRefMap
.clear();
336 CAutoFile hfile
= CreateFile(PackRef
,
338 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
341 FILE_ATTRIBUTE_NORMAL
,
347 DWORD filesize
= GetFileSize(hfile
, nullptr);
348 if (filesize
== 0 || filesize
== INVALID_FILE_SIZE
)
352 auto buff
= std::make_unique
<char[]>(filesize
);
353 ReadFile(hfile
, buff
.get(), filesize
, &size
, nullptr);
355 if (size
!= filesize
)
358 for (DWORD i
= 0; i
< filesize
;)
362 if (buff
[i
] == '#' || buff
[i
] == '^')
364 while (buff
[i
] != '\n')
376 while (buff
[i
] != ' ')
378 hash
.AppendChar(buff
[i
]);
388 while (buff
[i
] != '\n')
390 ref
.AppendChar(buff
[i
]);
397 m_PackRefMap
[ref
] = hash
;
399 while (buff
[i
] == '\n')
408 int CGitHeadFileList::ReadHeadHash(const CString
& gitdir
)
410 ATLASSERT(m_Gitdir
.IsEmpty() && m_HeadFile
.IsEmpty() && m_Head
.IsEmpty());
412 m_Gitdir
= g_AdminDirMap
.GetWorktreeAdminDir(gitdir
);
414 m_HeadFile
= m_Gitdir
;
415 m_HeadFile
+= L
"HEAD";
417 if (CGit::GetFileModifyTime(m_HeadFile
, &m_LastModifyTimeHead
, nullptr, &m_LastFileSizeHead
))
420 CAutoFile hfile
= CreateFile(m_HeadFile
,
422 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
425 FILE_ATTRIBUTE_NORMAL
,
432 unsigned char buffer
[2 * GIT_HASH_SIZE
];
433 ReadFile(hfile
, buffer
, (DWORD
)strlen("ref:"), &size
, nullptr);
434 if (size
!= strlen("ref:"))
437 if (strcmp((const char*)buffer
, "ref:") == 0)
439 m_HeadRefFile
.Empty();
440 DWORD filesize
= GetFileSize(hfile
, nullptr);
441 if (filesize
< 5 || filesize
== INVALID_FILE_SIZE
)
444 unsigned char *p
= (unsigned char*)malloc(filesize
- strlen("ref:"));
448 ReadFile(hfile
, p
, filesize
- (DWORD
)strlen("ref:"), &size
, nullptr);
449 CGit::StringAppend(&m_HeadRefFile
, p
, CP_UTF8
, filesize
- (int)strlen("ref:"));
452 CString ref
= m_HeadRefFile
.Trim();
454 ref
= ref
.Tokenize(L
"\n", start
);
455 m_HeadRefFile
= g_AdminDirMap
.GetAdminDir(gitdir
) + m_HeadRefFile
;
456 m_HeadRefFile
.Replace(L
'/', L
'\\');
459 if (CGit::GetFileModifyTime(m_HeadRefFile
, &time
, nullptr))
461 m_HeadRefFile
.Empty();
462 if (GetPackRef(gitdir
))
464 if (m_PackRefMap
.find(ref
) != m_PackRefMap
.end())
466 m_Head
= m_PackRefMap
[ref
];
476 CAutoFile href
= CreateFile(m_HeadRefFile
,
478 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
481 FILE_ATTRIBUTE_NORMAL
,
486 m_HeadRefFile
.Empty();
488 if (GetPackRef(gitdir
))
491 if (m_PackRefMap
.find(ref
) == m_PackRefMap
.end())
494 m_Head
= m_PackRefMap
[ref
];
498 ReadFile(href
, buffer
, 2 * GIT_HASH_SIZE
, &size
, nullptr);
499 if (size
!= 2 * GIT_HASH_SIZE
)
502 m_Head
.ConvertFromStrA((char*)buffer
);
504 m_LastModifyTimeRef
= time
;
509 ReadFile(hfile
, buffer
+ (DWORD
)strlen("ref:"), 2 * GIT_HASH_SIZE
- (DWORD
)strlen("ref:"), &size
, nullptr);
510 if (size
!= 2 * GIT_HASH_SIZE
- (DWORD
)strlen("ref:"))
513 m_HeadRefFile
.Empty();
515 m_Head
.ConvertFromStrA((char*)buffer
);
520 bool CGitHeadFileList::CheckHeadUpdate()
522 if (this->m_HeadFile
.IsEmpty())
525 __int64 mtime
= 0, size
= -1;
527 if (CGit::GetFileModifyTime(m_HeadFile
, &mtime
, nullptr, &size
))
530 if (mtime
!= m_LastModifyTimeHead
|| size
!= m_LastFileSizeHead
)
533 if (!this->m_HeadRefFile
.IsEmpty())
535 if (CGit::GetFileModifyTime(m_HeadRefFile
, &mtime
))
538 if (mtime
!= this->m_LastModifyTimeRef
)
542 if(!this->m_PackRefFile
.IsEmpty())
545 if (CGit::GetFileModifyTime(m_PackRefFile
, &mtime
, nullptr, &size
))
548 if (mtime
!= m_LastModifyTimePackRef
|| size
!= m_LastFileSizePackRef
)
552 // in an empty repo HEAD points to refs/heads/master, but this ref doesn't exist.
553 // So we need to retry again and again until the ref exists - otherwise we will never notice
554 if (this->m_Head
.IsEmpty() && this->m_HeadRefFile
.IsEmpty() && this->m_PackRefFile
.IsEmpty())
560 int CGitHeadFileList::CallBack(const unsigned char *sha1
, const char *base
, int baselen
,
561 const char *pathname
, unsigned mode
, int /*stage*/, void *context
)
563 #define S_IFGITLINK 0160000
565 CGitHeadFileList
* p
= reinterpret_cast<CGitHeadFileList
*>(context
);
567 if ((mode
& S_IFDIR
) && (mode
& S_IFMT
) != S_IFGITLINK
)
568 return READ_TREE_RECURSIVE
;
572 CGit::StringAppend(&item
.m_FileName
, (BYTE
*)base
, CP_UTF8
, baselen
);
573 CGit::StringAppend(&item
.m_FileName
, (BYTE
*)pathname
, CP_UTF8
);
574 if ((mode
& S_IFMT
) == S_IFGITLINK
)
575 item
.m_FileName
+= L
'/';
579 if( (mode
&S_IFMT
) == S_IFGITLINK
)
582 return READ_TREE_RECURSIVE
;
585 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
)
587 size_t count
= git_tree_entrycount(tree
);
588 for (size_t i
= 0; i
< count
; ++i
)
590 const git_tree_entry
*entry
= git_tree_entry_byindex(tree
, i
);
593 int mode
= git_tree_entry_filemode(entry
);
594 if( CallBack(git_tree_entry_id(entry
)->id
,
597 git_tree_entry_name(entry
),
600 data
) == READ_TREE_RECURSIVE
605 git_object
* object
= nullptr;
606 git_tree_entry_to_object(&object
, &repo
, entry
);
609 CStringA parent
= base
;
610 parent
+= git_tree_entry_name(entry
);
612 ReadTreeRecursive(repo
, (git_tree
*)object
, parent
, CallBack
, data
);
613 git_object_free(object
);
622 // ReadTree is/must only be executed on an empty list
623 int CGitHeadFileList::ReadTree()
628 if (m_Head
.IsEmpty())
631 CAutoRepository
repository(m_Gitdir
);
634 bool ret
= repository
;
635 ret
= ret
&& !git_commit_lookup(commit
.GetPointer(), repository
, (const git_oid
*)m_Head
.m_hash
);
636 ret
= ret
&& !git_commit_tree(tree
.GetPointer(), commit
);
639 ret
= ret
&& !ReadTreeRecursive(*repository
, tree
, "", CGitHeadFileList::CallBack
, this);
641 catch (const std::bad_alloc
& ex
)
643 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Catched exception inside ReadTreeRecursive: %s\n", ex
.what());
649 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());
650 m_LastModifyTimeHead
= 0;
651 m_LastFileSizeHead
= -1;
655 std::sort(this->begin(), this->end(), SortTree
);
657 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Reloaded HEAD tree (commit is %s) for repo: %s\n", (LPCTSTR
)m_Head
.ToString(), (LPCTSTR
)m_Gitdir
);
661 int CGitIgnoreItem::FetchIgnoreList(const CString
& projectroot
, const CString
& file
, bool isGlobal
, int* ignoreCase
)
663 if (this->m_pExcludeList
)
665 git_free_exclude_list(m_pExcludeList
);
666 m_pExcludeList
= nullptr;
671 this->m_BaseDir
.Empty();
674 CString base
= file
.Mid(projectroot
.GetLength() + 1);
675 base
.Replace(L
'\\', L
'/');
677 int start
= base
.ReverseFind(L
'/');
680 base
.Truncate(start
);
681 this->m_BaseDir
= CUnicodeUtils::GetMulti(base
, CP_UTF8
) + "/";
685 if (CGit::GetFileModifyTime(file
, &m_LastModifyTime
, nullptr, &m_LastFileSize
))
688 CAutoFile hfile
= CreateFile(file
,
690 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
693 FILE_ATTRIBUTE_NORMAL
,
699 DWORD filesize
= GetFileSize(hfile
, nullptr);
700 if (filesize
== INVALID_FILE_SIZE
)
703 m_buffer
= new BYTE
[filesize
+ 1];
708 if (!ReadFile(hfile
, m_buffer
, filesize
, &size
, nullptr))
714 m_buffer
[size
] = '\0';
716 if (git_create_exclude_list(&m_pExcludeList
))
723 m_iIgnoreCase
= ignoreCase
;
727 for (DWORD i
= 0; i
< size
; ++i
)
729 if (m_buffer
[i
] == '\n' || m_buffer
[i
] == '\r' || i
== (size
- 1))
731 if (m_buffer
[i
] == '\n' || m_buffer
[i
] == '\r')
734 if (p
[0] != '#' && p
[0])
735 git_add_exclude((const char*)p
, this->m_BaseDir
, m_BaseDir
.GetLength(), this->m_pExcludeList
, ++line
);
737 p
= m_buffer
+ i
+ 1;
743 git_free_exclude_list(m_pExcludeList
);
744 m_pExcludeList
= nullptr;
752 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
753 int CGitIgnoreItem::IsPathIgnored(const CStringA
& patha
, int& type
)
755 int pos
= patha
.ReverseFind('/');
756 const char* base
= (pos
>= 0) ? ((const char*)patha
+ pos
+ 1) : patha
;
758 return IsPathIgnored(patha
, base
, type
);
762 int CGitIgnoreItem::IsPathIgnored(const CStringA
& patha
, const char* base
, int& type
)
765 return -1; // error or undecided
767 return git_check_excluded_1(patha
, patha
.GetLength(), base
, &type
, m_pExcludeList
, m_iIgnoreCase
? *m_iIgnoreCase
: 1);
770 bool CGitIgnoreList::CheckFileChanged(const CString
&path
)
772 __int64 time
= 0, size
= -1;
774 int ret
= CGit::GetFileModifyTime(path
, &time
, nullptr, &size
);
778 CAutoReadLock
lock(m_SharedMutex
);
779 cacheExist
= (m_Map
.find(path
) != m_Map
.end());
782 if (!cacheExist
&& ret
== 0)
784 CAutoWriteLock
lock(m_SharedMutex
);
785 m_Map
[path
].m_LastModifyTime
= 0;
786 m_Map
[path
].m_LastFileSize
= -1;
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
&& m_Map
[path
].m_LastFileSize
== size
)
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())
1071 ptr
= std::make_shared
<CGitHeadFileList
>();
1072 if (ptr
->ReadHeadHash(gitdir
) || ptr
->ReadTree())
1078 this->SafeSet(gitdir
, ptr
);