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
);
90 CString projectConfig
= g_AdminDirMap
.GetAdminDir(dgitdir
) + L
"config";
91 CString globalConfig
= g_Git
.GetGitGlobalConfig();
92 CString globalXDGConfig
= g_Git
.GetGitGlobalXDGConfig();
93 CString
systemConfig(CRegString(REG_SYSTEM_GITCONFIGPATH
, L
"", FALSE
));
94 CString
programDataConfig(GetProgramDataGitConfig());
96 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(projectConfig
), GIT_CONFIG_LEVEL_LOCAL
, FALSE
);
97 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalConfig
), GIT_CONFIG_LEVEL_GLOBAL
, FALSE
);
98 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalXDGConfig
), GIT_CONFIG_LEVEL_XDG
, FALSE
);
99 if (!systemConfig
.IsEmpty())
100 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(systemConfig
), GIT_CONFIG_LEVEL_SYSTEM
, FALSE
);
101 if (!programDataConfig
.IsEmpty())
102 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(programDataConfig
), GIT_CONFIG_LEVEL_PROGRAMDATA
, FALSE
);
104 git_repository_set_config(repository
, config
);
107 // load index in order to enumerate files
108 if (git_repository_index(index
.GetPointer(), repository
))
114 m_bHasConflicts
= FALSE
;
116 size_t ecount
= git_index_entrycount(index
);
121 catch (const std::bad_alloc
& ex
)
124 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Could not resize index-vector: %s\n", ex
.what());
127 for (size_t i
= 0; i
< ecount
; ++i
)
129 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
131 auto& item
= (*this)[i
];
132 item
.m_FileName
= CUnicodeUtils::GetUnicode(e
->path
);
133 if (e
->mode
& S_IFDIR
)
134 item
.m_FileName
+= L
'/';
135 item
.m_ModifyTime
= e
->mtime
.seconds
;
136 item
.m_Flags
= e
->flags
;
137 item
.m_FlagsExtended
= e
->flags_extended
;
138 item
.m_IndexHash
= e
->id
.id
;
139 item
.m_Size
= e
->file_size
;
140 m_bHasConflicts
|= GIT_IDXENTRY_STAGE(e
);
143 CGit::GetFileModifyTime(g_AdminDirMap
.GetWorktreeAdminDir(dgitdir
) + L
"index", &m_LastModifyTime
);
144 std::sort(this->begin(), this->end(), SortIndex
);
149 int CGitIndexList::GetFileStatus(const CString
&gitdir
, const CString
&pathorg
, git_wc_status_kind
*status
, __int64 time
, __int64 filesize
, FILL_STATUS_CALLBACK callback
, void *pData
, CGitHash
*pHash
, bool * assumeValid
, bool * skipWorktree
)
154 CString path
= pathorg
;
156 size_t index
= SearchInSortVector(*this, path
, -1);
160 *status
= git_wc_status_unversioned
;
164 if (callback
&& assumeValid
&& skipWorktree
)
165 callback(CombinePath(gitdir
, pathorg
), *status
, false, pData
, *assumeValid
, *skipWorktree
);
170 auto& entry
= (*this)[index
];
171 // skip-worktree has higher priority than assume-valid
172 if (entry
.m_FlagsExtended
& GIT_IDXENTRY_SKIP_WORKTREE
)
174 *status
= git_wc_status_normal
;
176 *skipWorktree
= true;
178 else if (entry
.m_Flags
& GIT_IDXENTRY_VALID
)
180 *status
= git_wc_status_normal
;
184 else if (filesize
!= entry
.m_Size
)
185 *status
= git_wc_status_modified
;
186 else if (time
== entry
.m_ModifyTime
)
187 *status
= git_wc_status_normal
;
188 else if (config
&& filesize
< m_iMaxCheckSize
)
191 * Opening a new repository each time is not yet optimal, however, there is no API to clear the pack-cache
192 * When a shared repository is used, we might need a mutex to prevent concurrent access to repository instance and especially filter-lists
194 CAutoRepository
repository(gitdir
);
199 CStringA fileA
= CUnicodeUtils::GetMulti(pathorg
, CP_UTF8
);
200 git_repository_set_config(repository
, config
);
201 if (!git_repository_hashfile(&actual
, repository
, fileA
, GIT_OBJ_BLOB
, nullptr) && !git_oid_cmp(&actual
, (const git_oid
*)entry
.m_IndexHash
.m_hash
))
203 entry
.m_ModifyTime
= time
;
204 *status
= git_wc_status_normal
;
207 *status
= git_wc_status_modified
;
210 *status
= git_wc_status_modified
;
212 if (entry
.m_Flags
& GIT_IDXENTRY_STAGEMASK
)
213 *status
= git_wc_status_conflicted
;
214 else if (entry
.m_FlagsExtended
& GIT_IDXENTRY_INTENT_TO_ADD
)
215 *status
= git_wc_status_added
;
218 *pHash
= entry
.m_IndexHash
;
220 if (callback
&& assumeValid
&& skipWorktree
)
221 callback(CombinePath(gitdir
, pathorg
), *status
, false, pData
, *assumeValid
, *skipWorktree
);
226 int CGitIndexList::GetStatus(const CString
& gitdir
, CString path
, git_wc_status_kind
* status
,
227 BOOL IsFull
, BOOL
/*IsRecursive*/,
228 FILL_STATUS_CALLBACK callback
, void *pData
,
229 CGitHash
*pHash
, bool * assumeValid
, bool * skipWorktree
)
231 __int64 time
, filesize
= 0;
237 git_wc_status_kind dirstatus
= git_wc_status_none
;
240 result
= CGit::GetFileModifyTime(gitdir
, &time
, &isDir
);
242 result
= CGit::GetFileModifyTime(CombinePath(gitdir
, path
), &time
, &isDir
, &filesize
);
246 *status
= git_wc_status_deleted
;
247 if (callback
&& assumeValid
&& skipWorktree
)
248 callback(CombinePath(gitdir
, path
), git_wc_status_deleted
, false, pData
, *assumeValid
, *skipWorktree
);
255 GetFileStatus(gitdir
, path
, status
, time
, filesize
, callback
, pData
, pHash
, assumeValid
, skipWorktree
);
259 if (!path
.IsEmpty() && !CStringUtils::EndsWith(path
, L
'\\'))
262 int len
= path
.GetLength();
264 for (auto it
= cbegin(), itend
= cend(); it
!= itend
; ++it
)
267 if (!(entry
.m_FileName
.GetLength() > len
&& wcsncmp(entry
.m_FileName
, path
, len
) == 0))
272 *status
= git_wc_status_normal
;
274 callback(CombinePath(gitdir
, path
), *status
, false, pData
, (entry
.m_Flags
& GIT_IDXENTRY_VALID
) && !(entry
.m_FlagsExtended
& GIT_IDXENTRY_SKIP_WORKTREE
), (entry
.m_FlagsExtended
& GIT_IDXENTRY_SKIP_WORKTREE
) != 0);
279 result
= CGit::GetFileModifyTime(CombinePath(gitdir
, entry
.m_FileName
), &time
, nullptr, &filesize
);
283 *status
= git_wc_status_none
;
285 *assumeValid
= false;
287 *skipWorktree
= false;
289 GetFileStatus(gitdir
, entry
.m_FileName
, status
, time
, filesize
, callback
, pData
, nullptr, assumeValid
, skipWorktree
);
291 // if a file is assumed valid, we need to inform the caller, otherwise the assumevalid flag might not get to the explorer on first open of a repository
292 if (callback
&& assumeValid
&& skipWorktree
&& (*assumeValid
|| *skipWorktree
))
293 callback(CombinePath(gitdir
, path
), *status
, false, pData
, *assumeValid
, *skipWorktree
);
294 if (*status
!= git_wc_status_none
)
296 if (dirstatus
== git_wc_status_none
)
297 dirstatus
= git_wc_status_normal
;
298 if (*status
!= git_wc_status_normal
)
299 dirstatus
= git_wc_status_modified
;
303 if (dirstatus
!= git_wc_status_none
)
306 *status
= git_wc_status_unversioned
;
309 callback(CombinePath(gitdir
, path
), *status
, false, pData
, false, false);
314 int CGitIndexFileMap::Check(const CString
&gitdir
, bool *isChanged
)
318 CString IndexFile
= g_AdminDirMap
.GetWorktreeAdminDirConcat(gitdir
, L
"index");
320 if (CGit::GetFileModifyTime(IndexFile
, &time
))
322 g_AdminDirMap
.ResetAdminDirCache(gitdir
);
326 SHARED_INDEX_PTR pIndex
;
327 pIndex
= this->SafeGet(gitdir
);
336 if (pIndex
->m_LastModifyTime
== time
)
349 int CGitIndexFileMap::LoadIndex(const CString
&gitdir
)
351 SHARED_INDEX_PTR pIndex
= std::make_shared
<CGitIndexList
>();
353 if (pIndex
->ReadIndex(gitdir
))
356 this->SafeSet(gitdir
, pIndex
);
361 int CGitIndexFileMap::GetFileStatus(const CString
&gitdir
, const CString
&path
, git_wc_status_kind
*status
,BOOL IsFull
, BOOL IsRecursive
,
362 FILL_STATUS_CALLBACK callback
, void *pData
,
364 bool* assumeValid
, bool* skipWorktree
)
366 CheckAndUpdate(gitdir
);
368 SHARED_INDEX_PTR pIndex
= this->SafeGet(gitdir
);
370 pIndex
->GetStatus(gitdir
, path
, status
, IsFull
, IsRecursive
, callback
, pData
, pHash
, assumeValid
, skipWorktree
);
373 // git working tree has not index
374 *status
= git_wc_status_unversioned
;
380 int CGitIndexFileMap::IsUnderVersionControl(const CString
& gitdir
, CString subpath
, bool isDir
, bool* isVersion
)
382 if (subpath
.IsEmpty())
388 subpath
.Replace(L
'\\', L
'/');
392 CheckAndUpdate(gitdir
);
394 SHARED_INDEX_PTR pIndex
= this->SafeGet(gitdir
);
399 *isVersion
= (SearchInSortVector(*pIndex
, subpath
, subpath
.GetLength()) != NPOS
);
401 *isVersion
= (SearchInSortVector(*pIndex
, subpath
, -1) != NPOS
);
407 // This method is assumed to be called with m_SharedMutex locked.
408 int CGitHeadFileList::GetPackRef(const CString
&gitdir
)
410 CString PackRef
= g_AdminDirMap
.GetAdminDirConcat(gitdir
, L
"packed-refs");
413 if (CGit::GetFileModifyTime(PackRef
, &mtime
))
415 //packed refs is not existed
416 this->m_PackRefFile
.Empty();
417 this->m_PackRefMap
.clear();
420 else if(mtime
== m_LastModifyTimePackRef
)
424 this->m_PackRefFile
= PackRef
;
425 this->m_LastModifyTimePackRef
= mtime
;
428 m_PackRefMap
.clear();
430 CAutoFile hfile
= CreateFile(PackRef
,
432 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
435 FILE_ATTRIBUTE_NORMAL
,
441 DWORD filesize
= GetFileSize(hfile
, nullptr);
442 if (filesize
== 0 || filesize
== INVALID_FILE_SIZE
)
446 auto buff
= std::make_unique
<char[]>(filesize
);
447 ReadFile(hfile
, buff
.get(), filesize
, &size
, nullptr);
449 if (size
!= filesize
)
452 for (DWORD i
= 0; i
< filesize
;)
456 if (buff
[i
] == '#' || buff
[i
] == '^')
458 while (buff
[i
] != '\n')
470 while (buff
[i
] != ' ')
472 hash
.AppendChar(buff
[i
]);
482 while (buff
[i
] != '\n')
484 ref
.AppendChar(buff
[i
]);
491 m_PackRefMap
[ref
] = hash
;
493 while (buff
[i
] == '\n')
502 int CGitHeadFileList::ReadHeadHash(const CString
& gitdir
)
504 ATLASSERT(m_Gitdir
.IsEmpty() && m_HeadFile
.IsEmpty());
506 m_Gitdir
= g_AdminDirMap
.GetWorktreeAdminDir(gitdir
);
508 m_HeadFile
= m_Gitdir
;
509 m_HeadFile
+= L
"HEAD";
511 if( CGit::GetFileModifyTime(m_HeadFile
, &m_LastModifyTimeHead
))
514 CAutoFile hfile
= CreateFile(m_HeadFile
,
516 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
519 FILE_ATTRIBUTE_NORMAL
,
526 unsigned char buffer
[2 * GIT_HASH_SIZE
];
527 ReadFile(hfile
, buffer
, (DWORD
)strlen("ref:"), &size
, nullptr);
528 if (size
!= strlen("ref:"))
531 if (strcmp((const char*)buffer
, "ref:") == 0)
533 m_HeadRefFile
.Empty();
534 DWORD filesize
= GetFileSize(hfile
, nullptr);
535 if (filesize
< 5 || filesize
== INVALID_FILE_SIZE
)
538 unsigned char *p
= (unsigned char*)malloc(filesize
- strlen("ref:"));
542 ReadFile(hfile
, p
, filesize
- (DWORD
)strlen("ref:"), &size
, nullptr);
543 CGit::StringAppend(&m_HeadRefFile
, p
, CP_UTF8
, filesize
- (int)strlen("ref:"));
546 CString ref
= m_HeadRefFile
.Trim();
548 ref
= ref
.Tokenize(L
"\n", start
);
549 m_HeadRefFile
= g_AdminDirMap
.GetAdminDir(gitdir
) + m_HeadRefFile
;
550 m_HeadRefFile
.Replace(L
'/', L
'\\');
553 if (CGit::GetFileModifyTime(m_HeadRefFile
, &time
, nullptr))
555 m_HeadRefFile
.Empty();
556 if (GetPackRef(gitdir
))
558 if (m_PackRefMap
.find(ref
) == m_PackRefMap
.end())
561 m_Head
= m_PackRefMap
[ref
];
565 CAutoFile href
= CreateFile(m_HeadRefFile
,
567 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
570 FILE_ATTRIBUTE_NORMAL
,
575 m_HeadRefFile
.Empty();
577 if (GetPackRef(gitdir
))
580 if (m_PackRefMap
.find(ref
) == m_PackRefMap
.end())
583 m_Head
= m_PackRefMap
[ref
];
587 ReadFile(href
, buffer
, 2 * GIT_HASH_SIZE
, &size
, nullptr);
588 if (size
!= 2 * GIT_HASH_SIZE
)
591 m_Head
.ConvertFromStrA((char*)buffer
);
593 m_LastModifyTimeRef
= time
;
598 ReadFile(hfile
, buffer
+ (DWORD
)strlen("ref:"), 2 * GIT_HASH_SIZE
- (DWORD
)strlen("ref:"), &size
, nullptr);
599 if (size
!= 2 * GIT_HASH_SIZE
- (DWORD
)strlen("ref:"))
602 m_HeadRefFile
.Empty();
604 m_Head
.ConvertFromStrA((char*)buffer
);
609 bool CGitHeadFileList::CheckHeadUpdate()
611 if (this->m_HeadFile
.IsEmpty())
616 if (CGit::GetFileModifyTime(m_HeadFile
, &mtime
))
619 if (mtime
!= this->m_LastModifyTimeHead
)
622 if (!this->m_HeadRefFile
.IsEmpty())
624 if (CGit::GetFileModifyTime(m_HeadRefFile
, &mtime
))
627 if (mtime
!= this->m_LastModifyTimeRef
)
631 if(!this->m_PackRefFile
.IsEmpty())
633 if (CGit::GetFileModifyTime(m_PackRefFile
, &mtime
))
636 if (mtime
!= this->m_LastModifyTimePackRef
)
640 // in an empty repo HEAD points to refs/heads/master, but this ref doesn't exist.
641 // So we need to retry again and again until the ref exists - otherwise we will never notice
642 if (this->m_Head
.IsEmpty() && this->m_HeadRefFile
.IsEmpty() && this->m_PackRefFile
.IsEmpty())
648 bool CGitHeadFileList::HeadHashEqualsTreeHash()
650 return (m_Head
== m_TreeHash
);
653 bool CGitHeadFileList::HeadFileIsEmpty()
655 return m_HeadFile
.IsEmpty();
658 bool CGitHeadFileList::HeadIsEmpty()
660 return m_Head
.IsEmpty();
663 int CGitHeadFileList::CallBack(const unsigned char *sha1
, const char *base
, int baselen
,
664 const char *pathname
, unsigned mode
, int /*stage*/, void *context
)
666 #define S_IFGITLINK 0160000
668 CGitHeadFileList
* p
= reinterpret_cast<CGitHeadFileList
*>(context
);
670 if ((mode
& S_IFDIR
) && (mode
& S_IFMT
) != S_IFGITLINK
)
671 return READ_TREE_RECURSIVE
;
675 CGit::StringAppend(&item
.m_FileName
, (BYTE
*)base
, CP_UTF8
, baselen
);
676 CGit::StringAppend(&item
.m_FileName
, (BYTE
*)pathname
, CP_UTF8
);
677 if ((mode
& S_IFMT
) == S_IFGITLINK
)
678 item
.m_FileName
+= L
'/';
682 if( (mode
&S_IFMT
) == S_IFGITLINK
)
685 return READ_TREE_RECURSIVE
;
688 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
)
690 size_t count
= git_tree_entrycount(tree
);
691 for (size_t i
= 0; i
< count
; ++i
)
693 const git_tree_entry
*entry
= git_tree_entry_byindex(tree
, i
);
696 int mode
= git_tree_entry_filemode(entry
);
697 if( CallBack(git_tree_entry_id(entry
)->id
,
700 git_tree_entry_name(entry
),
703 data
) == READ_TREE_RECURSIVE
708 git_object
* object
= nullptr;
709 git_tree_entry_to_object(&object
, &repo
, entry
);
712 CStringA parent
= base
;
713 parent
+= git_tree_entry_name(entry
);
715 ReadTreeRecursive(repo
, (git_tree
*)object
, parent
, CallBack
, data
);
716 git_object_free(object
);
725 // ReadTree is/must only be executed on an empty list
726 int CGitHeadFileList::ReadTree()
730 CAutoRepository
repository(m_Gitdir
);
733 bool ret
= repository
;
734 ret
= ret
&& !git_commit_lookup(commit
.GetPointer(), repository
, (const git_oid
*)m_Head
.m_hash
);
735 ret
= ret
&& !git_commit_tree(tree
.GetPointer(), commit
);
738 ret
= ret
&& !ReadTreeRecursive(*repository
, tree
, "", CGitHeadFileList::CallBack
, this);
740 catch (const std::bad_alloc
& ex
)
742 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Catched exception inside ReadTreeRecursive: %s\n", ex
.what());
748 m_LastModifyTimeHead
= 0;
752 std::sort(this->begin(), this->end(), SortTree
);
753 m_TreeHash
= git_commit_id(commit
)->id
;
757 int CGitIgnoreItem::FetchIgnoreList(const CString
& projectroot
, const CString
& file
, bool isGlobal
, int* ignoreCase
)
759 if (this->m_pExcludeList
)
761 git_free_exclude_list(m_pExcludeList
);
762 m_pExcludeList
= nullptr;
767 this->m_BaseDir
.Empty();
770 CString base
= file
.Mid(projectroot
.GetLength() + 1);
771 base
.Replace(L
'\\', L
'/');
773 int start
= base
.ReverseFind(L
'/');
776 base
.Truncate(start
);
777 this->m_BaseDir
= CUnicodeUtils::GetMulti(base
, CP_UTF8
) + "/";
781 if (CGit::GetFileModifyTime(file
, &m_LastModifyTime
))
784 CAutoFile hfile
= CreateFile(file
,
786 FILE_SHARE_READ
| FILE_SHARE_DELETE
| FILE_SHARE_WRITE
,
789 FILE_ATTRIBUTE_NORMAL
,
795 DWORD filesize
= GetFileSize(hfile
, nullptr);
796 if (filesize
== INVALID_FILE_SIZE
)
799 m_buffer
= new BYTE
[filesize
+ 1];
804 if (!ReadFile(hfile
, m_buffer
, filesize
, &size
, nullptr))
810 m_buffer
[size
] = '\0';
812 if (git_create_exclude_list(&m_pExcludeList
))
819 m_iIgnoreCase
= ignoreCase
;
823 for (DWORD i
= 0; i
< size
; ++i
)
825 if (m_buffer
[i
] == '\n' || m_buffer
[i
] == '\r' || i
== (size
- 1))
827 if (m_buffer
[i
] == '\n' || m_buffer
[i
] == '\r')
830 if (p
[0] != '#' && p
[0])
831 git_add_exclude((const char*)p
, this->m_BaseDir
, m_BaseDir
.GetLength(), this->m_pExcludeList
, ++line
);
833 p
= m_buffer
+ i
+ 1;
839 git_free_exclude_list(m_pExcludeList
);
840 m_pExcludeList
= nullptr;
848 #ifdef GTEST_INCLUDE_GTEST_GTEST_H_
849 int CGitIgnoreItem::IsPathIgnored(const CStringA
& patha
, int& type
)
851 int pos
= patha
.ReverseFind('/');
852 const char* base
= (pos
>= 0) ? ((const char*)patha
+ pos
+ 1) : patha
;
854 return IsPathIgnored(patha
, base
, type
);
858 int CGitIgnoreItem::IsPathIgnored(const CStringA
& patha
, const char* base
, int& type
)
861 return -1; // error or undecided
863 return git_check_excluded_1(patha
, patha
.GetLength(), base
, &type
, m_pExcludeList
, m_iIgnoreCase
? *m_iIgnoreCase
: 1);
866 bool CGitIgnoreList::CheckFileChanged(const CString
&path
)
870 int ret
= CGit::GetFileModifyTime(path
, &time
);
874 CAutoReadLock
lock(m_SharedMutex
);
875 cacheExist
= (m_Map
.find(path
) != m_Map
.end());
878 if (!cacheExist
&& ret
== 0)
880 CAutoWriteLock
lock(m_SharedMutex
);
881 m_Map
[path
].m_LastModifyTime
= 0;
883 // both cache and file is not exist
884 if ((ret
!= 0) && (!cacheExist
))
887 // file exist but cache miss
888 if ((ret
== 0) && (!cacheExist
))
891 // file not exist but cache exist
892 if ((ret
!= 0) && (cacheExist
))
894 // file exist and cache exist
897 CAutoReadLock
lock(m_SharedMutex
);
898 if (m_Map
[path
].m_LastModifyTime
== time
)
904 int CGitIgnoreList::FetchIgnoreFile(const CString
&gitdir
, const CString
&gitignore
, bool isGlobal
)
906 if (CGit::GitPathFileExists(gitignore
)) //if .gitignore remove, we need remote cache
908 CAutoWriteLock
lock(m_SharedMutex
);
909 m_Map
[gitignore
].FetchIgnoreList(gitdir
, gitignore
, isGlobal
, &m_IgnoreCase
[g_AdminDirMap
.GetAdminDir(gitdir
)]);
913 CAutoWriteLock
lock(m_SharedMutex
);
914 m_Map
.erase(gitignore
);
919 bool CGitIgnoreList::CheckAndUpdateIgnoreFiles(const CString
& gitdir
, const CString
& path
, bool isDir
)
921 CString
temp(gitdir
);
925 temp
.Replace(L
'/', L
'\\');
929 int x
= temp
.ReverseFind(L
'\\');
934 bool updated
= false;
935 while (!temp
.IsEmpty())
937 temp
+= L
"\\.gitignore";
939 if (CheckFileChanged(temp
))
941 FetchIgnoreFile(gitdir
, temp
, false);
945 temp
.Truncate(temp
.GetLength() - (int)wcslen(L
"\\.gitignore"));
946 if (CPathUtils::ArePathStringsEqual(temp
, gitdir
))
948 CString adminDir
= g_AdminDirMap
.GetAdminDir(temp
);
949 CString wcglobalgitignore
= adminDir
+ L
"info\\exclude";
950 if (CheckFileChanged(wcglobalgitignore
))
952 FetchIgnoreFile(gitdir
, wcglobalgitignore
, true);
956 if (CheckAndUpdateCoreExcludefile(adminDir
))
958 CString excludesFile
;
960 CAutoReadLock
lock(m_SharedMutex
);
961 excludesFile
= m_CoreExcludesfiles
[adminDir
];
963 if (!excludesFile
.IsEmpty())
965 FetchIgnoreFile(gitdir
, excludesFile
, true);
973 int i
= temp
.ReverseFind(L
'\\');
974 temp
.Truncate(max(0, i
));
979 bool CGitIgnoreList::CheckAndUpdateGitSystemConfigPath(bool force
)
982 m_sGitProgramDataConfigPath
= GetProgramDataGitConfig();
983 // recheck every 30 seconds
984 if (GetTickCount64() - m_dGitSystemConfigPathLastChecked
> 30000UL || force
)
986 m_dGitSystemConfigPathLastChecked
= GetTickCount64();
987 CString
gitSystemConfigPath(CRegString(REG_SYSTEM_GITCONFIGPATH
, L
"", FALSE
));
988 if (gitSystemConfigPath
!= m_sGitSystemConfigPath
)
990 m_sGitSystemConfigPath
= gitSystemConfigPath
;
996 bool CGitIgnoreList::CheckAndUpdateCoreExcludefile(const CString
&adminDir
)
998 CString
projectConfig(adminDir
);
999 projectConfig
+= L
"config";
1000 CString globalConfig
= g_Git
.GetGitGlobalConfig();
1001 CString globalXDGConfig
= g_Git
.GetGitGlobalXDGConfig();
1003 CAutoWriteLock
lock(m_coreExcludefilesSharedMutex
);
1004 bool hasChanged
= CheckAndUpdateGitSystemConfigPath();
1005 hasChanged
= hasChanged
|| CheckFileChanged(projectConfig
);
1006 hasChanged
= hasChanged
|| CheckFileChanged(globalConfig
);
1007 hasChanged
= hasChanged
|| CheckFileChanged(globalXDGConfig
);
1008 if (!m_sGitProgramDataConfigPath
.IsEmpty())
1009 hasChanged
= hasChanged
|| CheckFileChanged(m_sGitProgramDataConfigPath
);
1010 if (!m_sGitSystemConfigPath
.IsEmpty())
1011 hasChanged
= hasChanged
|| CheckFileChanged(m_sGitSystemConfigPath
);
1013 CString excludesFile
;
1015 CAutoReadLock
lock2(m_SharedMutex
);
1016 excludesFile
= m_CoreExcludesfiles
[adminDir
];
1018 if (!excludesFile
.IsEmpty())
1019 hasChanged
= hasChanged
|| CheckFileChanged(excludesFile
);
1024 CAutoConfig
config(true);
1025 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(projectConfig
), GIT_CONFIG_LEVEL_LOCAL
, FALSE
);
1026 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalConfig
), GIT_CONFIG_LEVEL_GLOBAL
, FALSE
);
1027 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(globalXDGConfig
), GIT_CONFIG_LEVEL_XDG
, FALSE
);
1028 if (!m_sGitSystemConfigPath
.IsEmpty())
1029 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(m_sGitSystemConfigPath
), GIT_CONFIG_LEVEL_SYSTEM
, FALSE
);
1030 if (!m_sGitProgramDataConfigPath
.IsEmpty())
1031 git_config_add_file_ondisk(config
, CGit::GetGitPathStringA(m_sGitProgramDataConfigPath
), GIT_CONFIG_LEVEL_PROGRAMDATA
, FALSE
);
1033 config
.GetString(L
"core.excludesfile", excludesFile
);
1034 if (excludesFile
.IsEmpty())
1035 excludesFile
= GetWindowsHome() + L
"\\.config\\git\\ignore";
1036 else if (CStringUtils::StartsWith(excludesFile
, L
"~/"))
1037 excludesFile
= GetWindowsHome() + excludesFile
.Mid(1);
1039 CAutoWriteLock
lockMap(m_SharedMutex
);
1040 m_IgnoreCase
[adminDir
] = 1;
1041 config
.GetBOOL(L
"core.ignorecase", m_IgnoreCase
[adminDir
]);
1042 CGit::GetFileModifyTime(projectConfig
, &m_Map
[projectConfig
].m_LastModifyTime
);
1043 CGit::GetFileModifyTime(globalXDGConfig
, &m_Map
[globalXDGConfig
].m_LastModifyTime
);
1044 if (m_Map
[globalXDGConfig
].m_LastModifyTime
== 0)
1045 m_Map
.erase(globalXDGConfig
);
1046 CGit::GetFileModifyTime(globalConfig
, &m_Map
[globalConfig
].m_LastModifyTime
);
1047 if (m_Map
[globalConfig
].m_LastModifyTime
== 0)
1048 m_Map
.erase(globalConfig
);
1049 if (!m_sGitSystemConfigPath
.IsEmpty())
1050 CGit::GetFileModifyTime(m_sGitSystemConfigPath
, &m_Map
[m_sGitSystemConfigPath
].m_LastModifyTime
);
1051 if (m_Map
[m_sGitSystemConfigPath
].m_LastModifyTime
== 0 || m_sGitSystemConfigPath
.IsEmpty())
1052 m_Map
.erase(m_sGitSystemConfigPath
);
1053 if (!m_sGitProgramDataConfigPath
.IsEmpty())
1054 CGit::GetFileModifyTime(m_sGitProgramDataConfigPath
, &m_Map
[m_sGitProgramDataConfigPath
].m_LastModifyTime
);
1055 if (m_Map
[m_sGitProgramDataConfigPath
].m_LastModifyTime
== 0 || m_sGitProgramDataConfigPath
.IsEmpty())
1056 m_Map
.erase(m_sGitProgramDataConfigPath
);
1057 m_CoreExcludesfiles
[adminDir
] = excludesFile
;
1061 const CString
CGitIgnoreList::GetWindowsHome()
1063 static CString
sWindowsHome(g_Git
.GetHomeDirectory());
1064 return sWindowsHome
;
1066 bool CGitIgnoreList::IsIgnore(CString str
, const CString
& projectroot
, bool isDir
)
1068 str
.Replace(L
'\\', L
'/');
1070 if (!str
.IsEmpty() && str
[str
.GetLength() - 1] == L
'/')
1071 str
.Truncate(str
.GetLength() - 1);
1074 ret
= CheckIgnore(str
, projectroot
, isDir
);
1077 int start
= str
.ReverseFind(L
'/');
1081 str
.Truncate(start
);
1082 ret
= CheckIgnore(str
, projectroot
, isDir
);
1087 int CGitIgnoreList::CheckFileAgainstIgnoreList(const CString
&ignorefile
, const CStringA
&patha
, const char * base
, int &type
)
1089 if (m_Map
.find(ignorefile
) == m_Map
.end())
1090 return -1; // error or undecided
1092 return (m_Map
[ignorefile
].IsPathIgnored(patha
, base
, type
));
1094 int CGitIgnoreList::CheckIgnore(const CString
&path
, const CString
&projectroot
, bool isDir
)
1096 CString temp
= CombinePath(projectroot
, path
);
1097 temp
.Replace(L
'/', L
'\\');
1099 CStringA patha
= CUnicodeUtils::GetMulti(path
, CP_UTF8
);
1100 patha
.Replace('\\', '/');
1107 // strip directory name
1108 // we do not need to check for a .ignore file inside a directory we might ignore
1109 int i
= temp
.ReverseFind(L
'\\');
1117 int x
= temp
.ReverseFind(L
'\\');
1122 int pos
= patha
.ReverseFind('/');
1123 const char * base
= (pos
>= 0) ? ((const char*)patha
+ pos
+ 1) : patha
;
1127 CAutoReadLock
lock(m_SharedMutex
);
1128 while (!temp
.IsEmpty())
1130 temp
+= L
"\\.gitignore";
1132 if ((ret
= CheckFileAgainstIgnoreList(temp
, patha
, base
, type
)) != -1)
1135 temp
.Truncate(temp
.GetLength() - (int)wcslen(L
"\\.gitignore"));
1137 if (CPathUtils::ArePathStringsEqual(temp
, projectroot
))
1139 CString adminDir
= g_AdminDirMap
.GetAdminDir(temp
);
1140 CString wcglobalgitignore
= adminDir
;
1141 wcglobalgitignore
+= L
"info\\exclude";
1142 if ((ret
= CheckFileAgainstIgnoreList(wcglobalgitignore
, patha
, base
, type
)) != -1)
1145 CString excludesFile
= m_CoreExcludesfiles
[adminDir
];
1146 if (!excludesFile
.IsEmpty())
1147 return CheckFileAgainstIgnoreList(excludesFile
, patha
, base
, type
);
1152 int i
= temp
.ReverseFind(L
'\\');
1153 temp
.Truncate(max(0, i
));
1159 bool CGitHeadFileMap::CheckHeadAndUpdate(const CString
&gitdir
)
1161 SHARED_TREE_PTR ptr
= this->SafeGet(gitdir
, true);
1163 if (ptr
.get() && !ptr
->CheckHeadUpdate() && ptr
->HeadHashEqualsTreeHash())
1166 ptr
= std::make_shared
<CGitHeadFileList
>();
1167 ptr
->ReadHeadHash(gitdir
);
1170 this->SafeSet(gitdir
, ptr
);
1175 int CGitHeadFileMap::IsUnderVersionControl(const CString
& gitdir
, CString subpath
, bool isDir
, bool* isVersion
)
1177 if (subpath
.IsEmpty())
1183 subpath
.Replace(L
'\\', L
'/');
1187 CheckHeadAndUpdate(gitdir
);
1189 SHARED_TREE_PTR treeptr
= SafeGet(gitdir
);
1192 if (treeptr
->HeadFileIsEmpty())
1197 if (treeptr
->empty())
1204 *isVersion
= (SearchInSortVector(*treeptr
, subpath
, subpath
.GetLength()) != NPOS
);
1206 *isVersion
= (SearchInSortVector(*treeptr
, subpath
, -1) != NPOS
);