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_iIndexCaps(GIT_INDEXCAP_IGNORE_CASE
| GIT_INDEXCAP_NO_SYMLINKS
)
61 m_iMaxCheckSize
= (__int64
)CRegDWORD(L
"Software\\TortoiseGit\\TGitCacheCheckContentMaxSize", 10 * 1024) * 1024; // stored in KiB
64 CGitIndexList::~CGitIndexList()
68 int CGitIndexList::ReadIndex(CString dgitdir
)
70 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
71 clear(); // HACK to make tests work, until we use CGitIndexList
75 CAutoRepository
repository(dgitdir
);
78 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not open git repository in %s: %s\n", (LPCTSTR
)dgitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
85 CString projectConfig
= g_AdminDirMap
.GetAdminDir(dgitdir
) + L
"config";
86 CString globalConfig
= g_Git
.GetGitGlobalConfig();
87 CString globalXDGConfig
= g_Git
.GetGitGlobalXDGConfig();
88 CString
systemConfig(CRegString(REG_SYSTEM_GITCONFIGPATH
, L
"", FALSE
));
89 CString
programDataConfig(GetProgramDataGitConfig());
91 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(projectConfig
), GIT_CONFIG_LEVEL_LOCAL
, repository
, FALSE
);
92 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalConfig
), GIT_CONFIG_LEVEL_GLOBAL
, repository
, FALSE
);
93 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalXDGConfig
), GIT_CONFIG_LEVEL_XDG
, repository
, FALSE
);
94 if (!systemConfig
.IsEmpty())
95 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(systemConfig
), GIT_CONFIG_LEVEL_SYSTEM
, repository
, FALSE
);
96 if (!programDataConfig
.IsEmpty())
97 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(programDataConfig
), GIT_CONFIG_LEVEL_PROGRAMDATA
, repository
, FALSE
);
99 git_repository_set_config(repository
, config
);
101 CGit::GetFileModifyTime(g_AdminDirMap
.GetWorktreeAdminDir(dgitdir
) + L
"index", &m_LastModifyTime
, nullptr, &m_LastFileSize
);
104 // load index in order to enumerate files
105 if (git_repository_index(index
.GetPointer(), repository
))
108 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not get index of git repository in %s: %s\n", (LPCTSTR
)dgitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
112 m_bHasConflicts
= FALSE
;
113 m_iIndexCaps
= git_index_caps(index
);
114 if (CRegDWORD(L
"Software\\TortoiseGit\\OverlaysCaseSensitive", TRUE
) != FALSE
)
115 m_iIndexCaps
&= ~GIT_INDEXCAP_IGNORE_CASE
;
117 size_t ecount
= git_index_entrycount(index
);
122 catch (const std::bad_alloc
& ex
)
125 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Could not resize index-vector: %s\n", ex
.what());
128 for (size_t i
= 0; i
< ecount
; ++i
)
130 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
132 auto& item
= (*this)[i
];
133 item
.m_FileName
= CUnicodeUtils::GetUnicode(e
->path
);
134 if (e
->mode
& S_IFDIR
)
135 item
.m_FileName
+= L
'/';
136 item
.m_ModifyTime
= e
->mtime
.seconds
;
137 item
.m_Flags
= e
->flags
;
138 item
.m_FlagsExtended
= e
->flags_extended
;
139 item
.m_IndexHash
= e
->id
.id
;
140 item
.m_Size
= e
->file_size
;
141 item
.m_Mode
= e
->mode
;
142 m_bHasConflicts
|= GIT_IDXENTRY_STAGE(e
);
145 DoSortFilenametSortVector(*this, IsIgnoreCase());
147 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Reloaded index for repo: %s\n", (LPCTSTR
)dgitdir
);
152 int CGitIndexList::GetFileStatus(const CString
& gitdir
, const CString
& pathorg
, git_wc_status2_t
& status
, __int64 time
, __int64 filesize
, bool isSymlink
, CGitHash
* pHash
)
154 size_t index
= SearchInSortVector(*this, pathorg
, -1, IsIgnoreCase());
158 status
.status
= git_wc_status_unversioned
;
165 auto& entry
= (*this)[index
];
167 *pHash
= entry
.m_IndexHash
;
168 ATLASSERT(IsIgnoreCase() ? pathorg
.CompareNoCase(entry
.m_FileName
) == 0 : pathorg
.Compare(entry
.m_FileName
) == 0);
169 CAutoRepository repository
;
170 return GetFileStatus(repository
, gitdir
, entry
, status
, time
, filesize
, isSymlink
);
173 int CGitIndexList::GetFileStatus(CAutoRepository
& repository
, const CString
& gitdir
, CGitIndex
& entry
, git_wc_status2_t
& status
, __int64 time
, __int64 filesize
, bool isSymlink
)
175 ATLASSERT(!status
.assumeValid
&& !status
.skipWorktree
);
177 // skip-worktree has higher priority than assume-valid
178 if (entry
.m_FlagsExtended
& GIT_IDXENTRY_SKIP_WORKTREE
)
180 status
.status
= git_wc_status_normal
;
181 status
.skipWorktree
= true;
183 else if (entry
.m_Flags
& GIT_IDXENTRY_VALID
)
185 status
.status
= git_wc_status_normal
;
186 status
.assumeValid
= true;
188 else if (filesize
== -1)
189 status
.status
= git_wc_status_deleted
;
190 else if ((isSymlink
&& !S_ISLNK(entry
.m_Mode
)) || ((m_iIndexCaps
& GIT_INDEXCAP_NO_SYMLINKS
) != GIT_INDEXCAP_NO_SYMLINKS
&& isSymlink
!= S_ISLNK(entry
.m_Mode
)))
191 status
.status
= git_wc_status_modified
;
192 else if (!isSymlink
&& filesize
!= entry
.m_Size
)
193 status
.status
= git_wc_status_modified
;
194 else if (CGit::filetime_to_time_t(time
) == entry
.m_ModifyTime
)
195 status
.status
= git_wc_status_normal
;
196 else if (config
&& filesize
< m_iMaxCheckSize
)
199 * Opening a new repository each time is not yet optimal, however, there is no API to clear the pack-cache
200 * When a shared repository is used, we might need a mutex to prevent concurrent access to repository instance and especially filter-lists
204 if (repository
.Open(gitdir
))
206 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Could not open git repository in %s for checking file: %s\n", (LPCTSTR
)gitdir
, (LPCTSTR
)CGit::GetLibGit2LastErr());
209 git_repository_set_config(repository
, config
);
213 CStringA fileA
= CUnicodeUtils::GetMulti(entry
.m_FileName
, CP_UTF8
);
214 if (isSymlink
&& S_ISLNK(entry
.m_Mode
))
216 CStringA linkDestination
;
217 if (!CPathUtils::ReadLink(CombinePath(gitdir
, entry
.m_FileName
), &linkDestination
) && !git_odb_hash(&actual
, (void*)(LPCSTR
)linkDestination
, linkDestination
.GetLength(), GIT_OBJ_BLOB
) && !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
;
225 else if (!git_repository_hashfile(&actual
, repository
, fileA
, GIT_OBJ_BLOB
, nullptr) && !git_oid_cmp(&actual
, (const git_oid
*)entry
.m_IndexHash
.m_hash
))
227 entry
.m_ModifyTime
= time
;
228 status
.status
= git_wc_status_normal
;
231 status
.status
= git_wc_status_modified
;
234 status
.status
= git_wc_status_modified
;
236 if (entry
.m_Flags
& GIT_IDXENTRY_STAGEMASK
)
237 status
.status
= git_wc_status_conflicted
;
238 else if (entry
.m_FlagsExtended
& GIT_IDXENTRY_INTENT_TO_ADD
)
239 status
.status
= git_wc_status_added
;
244 int CGitIndexList::GetFileStatus(const CString
& gitdir
, const CString
& path
, git_wc_status2_t
& status
, CGitHash
* pHash
)
246 ATLASSERT(!status
.assumeValid
&& !status
.skipWorktree
);
248 __int64 time
, filesize
= 0;
250 bool isSymlink
= false;
254 result
= CGit::GetFileModifyTime(gitdir
, &time
, &isDir
);
256 result
= CGit::GetFileModifyTime(CombinePath(gitdir
, path
), &time
, &isDir
, &filesize
, &isSymlink
);
261 if (!isDir
|| (isSymlink
&& (m_iIndexCaps
& GIT_INDEXCAP_NO_SYMLINKS
) != GIT_INDEXCAP_NO_SYMLINKS
))
262 return GetFileStatus(gitdir
, path
, status
, time
, filesize
, isSymlink
, pHash
);
264 if (CStringUtils::EndsWith(path
, L
'/'))
266 size_t index
= SearchInSortVector(*this, path
, -1, IsIgnoreCase());
269 status
.status
= git_wc_status_unversioned
;
277 *pHash
= (*this)[index
].m_IndexHash
;
280 status
.status
= git_wc_status_normal
;
282 status
.status
= git_wc_status_deleted
;
286 // we get here for symlinks which are handled as files inside the git index
287 if ((m_iIndexCaps
& GIT_INDEXCAP_NO_SYMLINKS
) != GIT_INDEXCAP_NO_SYMLINKS
)
288 return GetFileStatus(gitdir
, path
, status
, time
, filesize
, isSymlink
, pHash
);
290 // we should never get here
291 status
.status
= git_wc_status_unversioned
;
296 bool CGitIndexFileMap::HasIndexChangedOnDisk(const CString
& gitdir
)
298 __int64 time
= -1, size
= -1;
300 auto pIndex
= SafeGet(gitdir
);
305 CString IndexFile
= g_AdminDirMap
.GetWorktreeAdminDirConcat(gitdir
, L
"index");
306 // no need to refresh if there is no index right now and the current index is empty, but otherwise or lastmodified time differs
307 return (CGit::GetFileModifyTime(IndexFile
, &time
, nullptr, &size
) && !pIndex
->empty()) || pIndex
->m_LastModifyTime
!= time
|| pIndex
->m_LastFileSize
!= size
;
310 int CGitIndexFileMap::LoadIndex(const CString
&gitdir
)
312 SHARED_INDEX_PTR pIndex
= std::make_shared
<CGitIndexList
>();
314 if (pIndex
->ReadIndex(gitdir
))
320 this->SafeSet(gitdir
, pIndex
);
325 // This method is assumed to be called with m_SharedMutex locked.
326 int CGitHeadFileList::GetPackRef(const CString
&gitdir
)
328 CString PackRef
= g_AdminDirMap
.GetAdminDirConcat(gitdir
, L
"packed-refs");
330 __int64 mtime
= 0, packsize
= -1;
331 if (CGit::GetFileModifyTime(PackRef
, &mtime
, nullptr, &packsize
))
333 //packed refs is not existed
334 this->m_PackRefFile
.Empty();
335 this->m_PackRefMap
.clear();
338 else if (mtime
== m_LastModifyTimePackRef
&& packsize
== m_LastFileSizePackRef
)
342 this->m_PackRefFile
= PackRef
;
343 this->m_LastModifyTimePackRef
= mtime
;
344 this->m_LastFileSizePackRef
= packsize
;
347 m_PackRefMap
.clear();
349 CAutoFile hfile
= CreateFile(PackRef
,
351 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
354 FILE_ATTRIBUTE_NORMAL
,
360 DWORD filesize
= GetFileSize(hfile
, nullptr);
361 if (filesize
== 0 || filesize
== INVALID_FILE_SIZE
)
365 auto buff
= std::make_unique
<char[]>(filesize
);
366 ReadFile(hfile
, buff
.get(), filesize
, &size
, nullptr);
368 if (size
!= filesize
)
371 for (DWORD i
= 0; i
< filesize
;)
375 if (buff
[i
] == '#' || buff
[i
] == '^')
377 while (buff
[i
] != '\n')
389 while (buff
[i
] != ' ')
391 hash
.AppendChar(buff
[i
]);
401 while (buff
[i
] != '\n')
403 ref
.AppendChar(buff
[i
]);
410 m_PackRefMap
[ref
] = hash
;
412 while (buff
[i
] == '\n')
421 int CGitHeadFileList::ReadHeadHash(const CString
& gitdir
)
423 ATLASSERT(m_Gitdir
.IsEmpty() && m_HeadFile
.IsEmpty() && m_Head
.IsEmpty());
425 m_Gitdir
= g_AdminDirMap
.GetWorktreeAdminDir(gitdir
);
427 m_HeadFile
= m_Gitdir
;
428 m_HeadFile
+= L
"HEAD";
430 if (CGit::GetFileModifyTime(m_HeadFile
, &m_LastModifyTimeHead
, nullptr, &m_LastFileSizeHead
))
433 CAutoFile hfile
= CreateFile(m_HeadFile
,
435 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
438 FILE_ATTRIBUTE_NORMAL
,
445 unsigned char buffer
[2 * GIT_HASH_SIZE
];
446 ReadFile(hfile
, buffer
, (DWORD
)strlen("ref:"), &size
, nullptr);
447 if (size
!= strlen("ref:"))
450 if (strcmp((const char*)buffer
, "ref:") == 0)
452 m_HeadRefFile
.Empty();
453 DWORD filesize
= GetFileSize(hfile
, nullptr);
454 if (filesize
< 5 || filesize
== INVALID_FILE_SIZE
)
457 unsigned char *p
= (unsigned char*)malloc(filesize
- strlen("ref:"));
461 ReadFile(hfile
, p
, filesize
- (DWORD
)strlen("ref:"), &size
, nullptr);
462 CGit::StringAppend(&m_HeadRefFile
, p
, CP_UTF8
, filesize
- (int)strlen("ref:"));
465 CString ref
= m_HeadRefFile
.Trim();
467 ref
= ref
.Tokenize(L
"\n", start
);
468 m_HeadRefFile
= g_AdminDirMap
.GetAdminDir(gitdir
) + m_HeadRefFile
;
469 m_HeadRefFile
.Replace(L
'/', L
'\\');
472 if (CGit::GetFileModifyTime(m_HeadRefFile
, &time
, nullptr))
474 m_HeadRefFile
.Empty();
475 if (GetPackRef(gitdir
))
477 if (m_PackRefMap
.find(ref
) != m_PackRefMap
.end())
479 m_Head
= m_PackRefMap
[ref
];
489 CAutoFile href
= CreateFile(m_HeadRefFile
,
491 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
494 FILE_ATTRIBUTE_NORMAL
,
499 m_HeadRefFile
.Empty();
501 if (GetPackRef(gitdir
))
504 if (m_PackRefMap
.find(ref
) == m_PackRefMap
.end())
507 m_Head
= m_PackRefMap
[ref
];
511 ReadFile(href
, buffer
, 2 * GIT_HASH_SIZE
, &size
, nullptr);
512 if (size
!= 2 * GIT_HASH_SIZE
)
515 m_Head
.ConvertFromStrA((char*)buffer
);
517 m_LastModifyTimeRef
= time
;
522 ReadFile(hfile
, buffer
+ (DWORD
)strlen("ref:"), 2 * GIT_HASH_SIZE
- (DWORD
)strlen("ref:"), &size
, nullptr);
523 if (size
!= 2 * GIT_HASH_SIZE
- (DWORD
)strlen("ref:"))
526 m_HeadRefFile
.Empty();
528 m_Head
.ConvertFromStrA((char*)buffer
);
533 bool CGitHeadFileList::CheckHeadUpdate()
535 if (this->m_HeadFile
.IsEmpty())
538 __int64 mtime
= 0, size
= -1;
540 if (CGit::GetFileModifyTime(m_HeadFile
, &mtime
, nullptr, &size
))
543 if (mtime
!= m_LastModifyTimeHead
|| size
!= m_LastFileSizeHead
)
546 if (!this->m_HeadRefFile
.IsEmpty())
548 if (CGit::GetFileModifyTime(m_HeadRefFile
, &mtime
))
551 if (mtime
!= this->m_LastModifyTimeRef
)
555 if(!this->m_PackRefFile
.IsEmpty())
558 if (CGit::GetFileModifyTime(m_PackRefFile
, &mtime
, nullptr, &size
))
561 if (mtime
!= m_LastModifyTimePackRef
|| size
!= m_LastFileSizePackRef
)
565 // in an empty repo HEAD points to refs/heads/master, but this ref doesn't exist.
566 // So we need to retry again and again until the ref exists - otherwise we will never notice
567 if (this->m_Head
.IsEmpty() && this->m_HeadRefFile
.IsEmpty() && this->m_PackRefFile
.IsEmpty())
573 int CGitHeadFileList::ReadTreeRecursive(git_repository
& repo
, const git_tree
* tree
, const CStringA
& base
)
575 #define S_IFGITLINK 0160000
576 size_t count
= git_tree_entrycount(tree
);
577 for (size_t i
= 0; i
< count
; ++i
)
579 const git_tree_entry
*entry
= git_tree_entry_byindex(tree
, i
);
582 int mode
= git_tree_entry_filemode(entry
);
583 bool isDir
= (mode
& S_IFDIR
) == S_IFDIR
;
584 bool isSubmodule
= (mode
& S_IFMT
) == S_IFGITLINK
;
585 if (!isDir
|| isSubmodule
)
588 item
.m_Hash
= git_tree_entry_id(entry
)->id
;
589 CGit::StringAppend(&item
.m_FileName
, (BYTE
*)(LPCSTR
)base
, CP_UTF8
, base
.GetLength());
590 CGit::StringAppend(&item
.m_FileName
, (BYTE
*)git_tree_entry_name(entry
), CP_UTF8
);
592 item
.m_FileName
+= L
'/';
598 git_tree_entry_to_object(object
.GetPointer(), &repo
, entry
);
601 CStringA parent
= base
;
602 parent
+= git_tree_entry_name(entry
);
604 ReadTreeRecursive(repo
, (git_tree
*)(git_object
*)object
, parent
);
610 // ReadTree is/must only be executed on an empty list
611 int CGitHeadFileList::ReadTree(bool ignoreCase
)
616 if (m_Head
.IsEmpty())
619 CAutoRepository
repository(m_Gitdir
);
622 bool ret
= repository
;
623 ret
= ret
&& !git_commit_lookup(commit
.GetPointer(), repository
, (const git_oid
*)m_Head
.m_hash
);
624 ret
= ret
&& !git_commit_tree(tree
.GetPointer(), commit
);
627 ret
= ret
&& !ReadTreeRecursive(*repository
, tree
, "");
629 catch (const std::bad_alloc
& ex
)
631 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Catched exception inside ReadTreeRecursive: %s\n", ex
.what());
637 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());
638 m_LastModifyTimeHead
= 0;
639 m_LastFileSizeHead
= -1;
643 DoSortFilenametSortVector(*this, ignoreCase
);
645 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Reloaded HEAD tree (commit is %s) for repo: %s\n", (LPCTSTR
)m_Head
.ToString(), (LPCTSTR
)m_Gitdir
);
649 int CGitIgnoreItem::FetchIgnoreList(const CString
& projectroot
, const CString
& file
, bool isGlobal
, int* ignoreCase
)
651 if (this->m_pExcludeList
)
653 git_free_exclude_list(m_pExcludeList
);
654 m_pExcludeList
= nullptr;
659 this->m_BaseDir
.Empty();
662 CString base
= file
.Mid(projectroot
.GetLength() + 1);
663 base
.Replace(L
'\\', L
'/');
665 int start
= base
.ReverseFind(L
'/');
668 base
.Truncate(start
);
669 this->m_BaseDir
= CUnicodeUtils::GetMulti(base
, CP_UTF8
) + "/";
673 if (CGit::GetFileModifyTime(file
, &m_LastModifyTime
, nullptr, &m_LastFileSize
))
676 CAutoFile hfile
= CreateFile(file
,
678 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
681 FILE_ATTRIBUTE_NORMAL
,
687 DWORD filesize
= GetFileSize(hfile
, nullptr);
688 if (filesize
== INVALID_FILE_SIZE
)
691 m_buffer
= new BYTE
[filesize
+ 1];
696 if (!ReadFile(hfile
, m_buffer
, filesize
, &size
, nullptr))
702 m_buffer
[size
] = '\0';
704 if (git_create_exclude_list(&m_pExcludeList
))
711 m_iIgnoreCase
= ignoreCase
;
715 for (DWORD i
= 0; i
< size
; ++i
)
717 if (m_buffer
[i
] == '\n' || m_buffer
[i
] == '\r' || i
== (size
- 1))
719 if (m_buffer
[i
] == '\n' || m_buffer
[i
] == '\r')
722 if (p
[0] != '#' && p
[0])
723 git_add_exclude((const char*)p
, this->m_BaseDir
, m_BaseDir
.GetLength(), this->m_pExcludeList
, ++line
);
725 p
= m_buffer
+ i
+ 1;
731 git_free_exclude_list(m_pExcludeList
);
732 m_pExcludeList
= nullptr;
740 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
741 int CGitIgnoreItem::IsPathIgnored(const CStringA
& patha
, int& type
)
743 int pos
= patha
.ReverseFind('/');
744 const char* base
= (pos
>= 0) ? ((const char*)patha
+ pos
+ 1) : patha
;
746 return IsPathIgnored(patha
, base
, type
);
750 int CGitIgnoreItem::IsPathIgnored(const CStringA
& patha
, const char* base
, int& type
)
753 return -1; // error or undecided
755 return git_check_excluded_1(patha
, patha
.GetLength(), base
, &type
, m_pExcludeList
, m_iIgnoreCase
? *m_iIgnoreCase
: 1);
758 bool CGitIgnoreList::CheckFileChanged(const CString
&path
)
760 __int64 time
= 0, size
= -1;
762 int ret
= CGit::GetFileModifyTime(path
, &time
, nullptr, &size
);
766 CAutoReadLock
lock(m_SharedMutex
);
767 cacheExist
= (m_Map
.find(path
) != m_Map
.end());
770 if (!cacheExist
&& ret
== 0)
772 CAutoWriteLock
lock(m_SharedMutex
);
773 m_Map
[path
].m_LastModifyTime
= 0;
774 m_Map
[path
].m_LastFileSize
= -1;
776 // both cache and file is not exist
777 if ((ret
!= 0) && (!cacheExist
))
780 // file exist but cache miss
781 if ((ret
== 0) && (!cacheExist
))
784 // file not exist but cache exist
785 if ((ret
!= 0) && (cacheExist
))
787 // file exist and cache exist
790 CAutoReadLock
lock(m_SharedMutex
);
791 if (m_Map
[path
].m_LastModifyTime
== time
&& m_Map
[path
].m_LastFileSize
== size
)
797 int CGitIgnoreList::FetchIgnoreFile(const CString
&gitdir
, const CString
&gitignore
, bool isGlobal
)
799 if (CGit::GitPathFileExists(gitignore
)) //if .gitignore remove, we need remote cache
801 CAutoWriteLock
lock(m_SharedMutex
);
802 m_Map
[gitignore
].FetchIgnoreList(gitdir
, gitignore
, isGlobal
, &m_IgnoreCase
[g_AdminDirMap
.GetAdminDir(gitdir
)]);
806 CAutoWriteLock
lock(m_SharedMutex
);
807 m_Map
.erase(gitignore
);
812 bool CGitIgnoreList::CheckAndUpdateIgnoreFiles(const CString
& gitdir
, const CString
& path
, bool isDir
)
814 CString
temp(gitdir
);
818 temp
.Replace(L
'/', L
'\\');
822 int x
= temp
.ReverseFind(L
'\\');
827 bool updated
= false;
828 while (!temp
.IsEmpty())
830 temp
+= L
"\\.gitignore";
832 if (CheckFileChanged(temp
))
834 FetchIgnoreFile(gitdir
, temp
, false);
838 temp
.Truncate(temp
.GetLength() - (int)wcslen(L
"\\.gitignore"));
839 if (CPathUtils::ArePathStringsEqual(temp
, gitdir
))
841 CString adminDir
= g_AdminDirMap
.GetAdminDir(temp
);
842 CString wcglobalgitignore
= adminDir
+ L
"info\\exclude";
843 if (CheckFileChanged(wcglobalgitignore
))
845 FetchIgnoreFile(gitdir
, wcglobalgitignore
, true);
849 if (CheckAndUpdateCoreExcludefile(adminDir
))
851 CString excludesFile
;
853 CAutoReadLock
lock(m_SharedMutex
);
854 excludesFile
= m_CoreExcludesfiles
[adminDir
];
856 if (!excludesFile
.IsEmpty())
858 FetchIgnoreFile(gitdir
, excludesFile
, true);
866 int i
= temp
.ReverseFind(L
'\\');
867 temp
.Truncate(max(0, i
));
872 bool CGitIgnoreList::CheckAndUpdateGitSystemConfigPath(bool force
)
875 m_sGitProgramDataConfigPath
= GetProgramDataGitConfig();
876 // recheck every 30 seconds
877 if (GetTickCount64() - m_dGitSystemConfigPathLastChecked
> 30000UL || force
)
879 m_dGitSystemConfigPathLastChecked
= GetTickCount64();
880 CString
gitSystemConfigPath(CRegString(REG_SYSTEM_GITCONFIGPATH
, L
"", FALSE
));
881 if (gitSystemConfigPath
!= m_sGitSystemConfigPath
)
883 m_sGitSystemConfigPath
= gitSystemConfigPath
;
889 bool CGitIgnoreList::CheckAndUpdateCoreExcludefile(const CString
&adminDir
)
891 CString
projectConfig(adminDir
);
892 projectConfig
+= L
"config";
893 CString globalConfig
= g_Git
.GetGitGlobalConfig();
894 CString globalXDGConfig
= g_Git
.GetGitGlobalXDGConfig();
896 CAutoWriteLock
lock(m_coreExcludefilesSharedMutex
);
897 bool hasChanged
= CheckAndUpdateGitSystemConfigPath();
898 hasChanged
= hasChanged
|| CheckFileChanged(projectConfig
);
899 hasChanged
= hasChanged
|| CheckFileChanged(globalConfig
);
900 hasChanged
= hasChanged
|| CheckFileChanged(globalXDGConfig
);
901 if (!m_sGitProgramDataConfigPath
.IsEmpty())
902 hasChanged
= hasChanged
|| CheckFileChanged(m_sGitProgramDataConfigPath
);
903 if (!m_sGitSystemConfigPath
.IsEmpty())
904 hasChanged
= hasChanged
|| CheckFileChanged(m_sGitSystemConfigPath
);
906 CString excludesFile
;
908 CAutoReadLock
lock2(m_SharedMutex
);
909 excludesFile
= m_CoreExcludesfiles
[adminDir
];
911 if (!excludesFile
.IsEmpty())
912 hasChanged
= hasChanged
|| CheckFileChanged(excludesFile
);
917 CAutoConfig
config(true);
918 CAutoRepository
repo(adminDir
);
919 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(projectConfig
), GIT_CONFIG_LEVEL_LOCAL
, repo
, FALSE
);
920 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalConfig
), GIT_CONFIG_LEVEL_GLOBAL
, repo
, FALSE
);
921 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalXDGConfig
), GIT_CONFIG_LEVEL_XDG
, repo
, FALSE
);
922 if (!m_sGitSystemConfigPath
.IsEmpty())
923 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(m_sGitSystemConfigPath
), GIT_CONFIG_LEVEL_SYSTEM
, repo
, FALSE
);
924 if (!m_sGitProgramDataConfigPath
.IsEmpty())
925 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(m_sGitProgramDataConfigPath
), GIT_CONFIG_LEVEL_PROGRAMDATA
, repo
, FALSE
);
927 config
.GetString(L
"core.excludesfile", excludesFile
);
928 if (excludesFile
.IsEmpty())
929 excludesFile
= GetWindowsHome() + L
"\\.config\\git\\ignore";
930 else if (CStringUtils::StartsWith(excludesFile
, L
"~/"))
931 excludesFile
= GetWindowsHome() + excludesFile
.Mid(1);
933 CAutoWriteLock
lockMap(m_SharedMutex
);
934 m_IgnoreCase
[adminDir
] = 1;
935 config
.GetBOOL(L
"core.ignorecase", m_IgnoreCase
[adminDir
]);
936 CGit::GetFileModifyTime(projectConfig
, &m_Map
[projectConfig
].m_LastModifyTime
);
937 CGit::GetFileModifyTime(globalXDGConfig
, &m_Map
[globalXDGConfig
].m_LastModifyTime
);
938 if (m_Map
[globalXDGConfig
].m_LastModifyTime
== 0)
939 m_Map
.erase(globalXDGConfig
);
940 CGit::GetFileModifyTime(globalConfig
, &m_Map
[globalConfig
].m_LastModifyTime
);
941 if (m_Map
[globalConfig
].m_LastModifyTime
== 0)
942 m_Map
.erase(globalConfig
);
943 if (!m_sGitSystemConfigPath
.IsEmpty())
944 CGit::GetFileModifyTime(m_sGitSystemConfigPath
, &m_Map
[m_sGitSystemConfigPath
].m_LastModifyTime
);
945 if (m_Map
[m_sGitSystemConfigPath
].m_LastModifyTime
== 0 || m_sGitSystemConfigPath
.IsEmpty())
946 m_Map
.erase(m_sGitSystemConfigPath
);
947 if (!m_sGitProgramDataConfigPath
.IsEmpty())
948 CGit::GetFileModifyTime(m_sGitProgramDataConfigPath
, &m_Map
[m_sGitProgramDataConfigPath
].m_LastModifyTime
);
949 if (m_Map
[m_sGitProgramDataConfigPath
].m_LastModifyTime
== 0 || m_sGitProgramDataConfigPath
.IsEmpty())
950 m_Map
.erase(m_sGitProgramDataConfigPath
);
951 m_CoreExcludesfiles
[adminDir
] = excludesFile
;
955 const CString
CGitIgnoreList::GetWindowsHome()
957 static CString
sWindowsHome(g_Git
.GetHomeDirectory());
960 bool CGitIgnoreList::IsIgnore(CString str
, const CString
& projectroot
, bool isDir
)
962 str
.Replace(L
'\\', L
'/');
964 if (!str
.IsEmpty() && str
[str
.GetLength() - 1] == L
'/')
965 str
.Truncate(str
.GetLength() - 1);
968 ret
= CheckIgnore(str
, projectroot
, isDir
);
971 int start
= str
.ReverseFind(L
'/');
976 ret
= CheckIgnore(str
, projectroot
, TRUE
);
981 int CGitIgnoreList::CheckFileAgainstIgnoreList(const CString
&ignorefile
, const CStringA
&patha
, const char * base
, int &type
)
983 if (m_Map
.find(ignorefile
) == m_Map
.end())
984 return -1; // error or undecided
986 return (m_Map
[ignorefile
].IsPathIgnored(patha
, base
, type
));
988 int CGitIgnoreList::CheckIgnore(const CString
&path
, const CString
&projectroot
, bool isDir
)
990 CString temp
= CombinePath(projectroot
, path
);
991 temp
.Replace(L
'/', L
'\\');
993 CStringA patha
= CUnicodeUtils::GetMulti(path
, CP_UTF8
);
994 patha
.Replace('\\', '/');
1001 // strip directory name
1002 // we do not need to check for a .ignore file inside a directory we might ignore
1003 int i
= temp
.ReverseFind(L
'\\');
1011 int x
= temp
.ReverseFind(L
'\\');
1016 int pos
= patha
.ReverseFind('/');
1017 const char * base
= (pos
>= 0) ? ((const char*)patha
+ pos
+ 1) : patha
;
1021 CAutoReadLock
lock(m_SharedMutex
);
1022 while (!temp
.IsEmpty())
1024 temp
+= L
"\\.gitignore";
1026 if ((ret
= CheckFileAgainstIgnoreList(temp
, patha
, base
, type
)) != -1)
1029 temp
.Truncate(temp
.GetLength() - (int)wcslen(L
"\\.gitignore"));
1031 if (CPathUtils::ArePathStringsEqual(temp
, projectroot
))
1033 CString adminDir
= g_AdminDirMap
.GetAdminDir(temp
);
1034 CString wcglobalgitignore
= adminDir
;
1035 wcglobalgitignore
+= L
"info\\exclude";
1036 if ((ret
= CheckFileAgainstIgnoreList(wcglobalgitignore
, patha
, base
, type
)) != -1)
1039 CString excludesFile
= m_CoreExcludesfiles
[adminDir
];
1040 if (!excludesFile
.IsEmpty())
1041 return CheckFileAgainstIgnoreList(excludesFile
, patha
, base
, type
);
1046 int i
= temp
.ReverseFind(L
'\\');
1047 temp
.Truncate(max(0, i
));
1053 void CGitHeadFileMap::CheckHeadAndUpdate(const CString
& gitdir
, bool ignoreCase
)
1055 SHARED_TREE_PTR ptr
= this->SafeGet(gitdir
);
1057 if (ptr
.get() && !ptr
->CheckHeadUpdate())
1060 ptr
= std::make_shared
<CGitHeadFileList
>();
1061 if (ptr
->ReadHeadHash(gitdir
) || ptr
->ReadTree(ignoreCase
))
1067 this->SafeSet(gitdir
, ptr
);