Refactored: Replace 'count' parameter of CGit::GetLogCmd() with CFilterData::m_Number...
[TortoiseGit.git] / src / Git / GitIndex.cpp
blob78704a6946ee39630acbbb61ed80ec1a134141cf
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "Git.h"
22 #include "registry.h"
23 #include "UnicodeUtils.h"
24 #include "TGitPath.h"
25 #include "gitindex.h"
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include "SmartHandle.h"
29 #include "git2/sys/repository.h"
31 CGitAdminDirMap g_AdminDirMap;
33 int CGitIndex::Print()
35 _tprintf(_T("0x%08X 0x%08X %s %s\n"),
36 (int)this->m_ModifyTime,
37 this->m_Flags,
38 (LPCTSTR)this->m_IndexHash.ToString(),
39 (LPCTSTR)this->m_FileName);
41 return 0;
44 CGitIndexList::CGitIndexList()
46 this->m_LastModifyTime = 0;
47 m_critRepoSec.Init();
48 m_bCheckContent = !!(CRegDWORD(_T("Software\\TortoiseGit\\TGitCacheCheckContent"), TRUE) == TRUE);
49 m_iMaxCheckSize = (__int64)CRegDWORD(_T("Software\\TortoiseGit\\TGitCacheCheckContentMaxSize"), 10 * 1024) * 1024; // stored in KiB
52 CGitIndexList::~CGitIndexList()
54 m_critRepoSec.Term();
57 static bool SortIndex(const CGitIndex &Item1, const CGitIndex &Item2)
59 return Item1.m_FileName.Compare(Item2.m_FileName) < 0;
62 static bool SortTree(const CGitTreeItem &Item1, const CGitTreeItem &Item2)
64 return Item1.m_FileName.Compare(Item2.m_FileName) < 0;
67 int CGitIndexList::ReadIndex(CString dgitdir)
69 this->clear();
71 m_critRepoSec.Lock();
72 if (repository.Open(dgitdir))
74 m_critRepoSec.Unlock();
75 return -1;
78 // add config files
79 CAutoConfig config(true);
81 CString projectConfig = dgitdir + _T("config");
82 CString globalConfig = g_Git.GetGitGlobalConfig();
83 CString globalXDGConfig = g_Git.GetGitGlobalXDGConfig();
84 CString systemConfig(CRegString(REG_SYSTEM_GITCONFIGPATH, _T(""), FALSE));
86 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(projectConfig), GIT_CONFIG_LEVEL_LOCAL, FALSE);
87 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalConfig), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
88 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalXDGConfig), GIT_CONFIG_LEVEL_XDG, FALSE);
89 if (!systemConfig.IsEmpty())
90 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(systemConfig), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
92 git_repository_set_config(repository, config);
94 CAutoIndex index;
95 // load index in order to enumerate files
96 if (git_repository_index(index.GetPointer(), repository))
98 repository.Free();
99 m_critRepoSec.Unlock();
100 return -1;
103 size_t ecount = git_index_entrycount(index);
104 resize(ecount);
105 for (size_t i = 0; i < ecount; ++i)
107 const git_index_entry *e = git_index_get_byindex(index, i);
109 this->at(i).m_FileName.Empty();
110 this->at(i).m_FileName = CUnicodeUtils::GetUnicode(e->path);
111 this->at(i).m_FileName.MakeLower();
112 this->at(i).m_ModifyTime = e->mtime.seconds;
113 this->at(i).m_Flags = e->flags | e->flags_extended;
114 this->at(i).m_IndexHash = e->id.id;
115 this->at(i).m_Size = e->file_size;
118 g_Git.GetFileModifyTime(dgitdir + _T("index"), &this->m_LastModifyTime);
119 std::sort(this->begin(), this->end(), SortIndex);
121 m_critRepoSec.Unlock();
123 return 0;
126 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)
128 if(status)
130 CString path = pathorg;
131 path.MakeLower();
133 int start = SearchInSortVector(*this, path, -1);
135 if (start < 0)
137 *status = git_wc_status_unversioned;
138 if (pHash)
139 pHash->Empty();
142 else
144 int index = start;
145 if (index >= (int)size())
146 return -1;
148 // skip-worktree has higher priority than assume-valid
149 if (at(index).m_Flags & GIT_IDXENTRY_SKIP_WORKTREE)
151 *status = git_wc_status_normal;
152 if (skipWorktree)
153 *skipWorktree = true;
155 else if (at(index).m_Flags & GIT_IDXENTRY_VALID)
157 *status = git_wc_status_normal;
158 if (assumeValid)
159 *assumeValid = true;
161 else if (filesize != at(index).m_Size)
162 *status = git_wc_status_modified;
163 else if (time == at(index).m_ModifyTime)
165 *status = git_wc_status_normal;
167 else if (m_bCheckContent && repository && filesize < m_iMaxCheckSize)
169 git_oid actual;
170 CStringA fileA = CUnicodeUtils::GetMulti(pathorg, CP_UTF8);
171 m_critRepoSec.Lock(); // prevent concurrent access to repository instance and especially filter-lists
172 if (!git_repository_hashfile(&actual, repository, fileA, GIT_OBJ_BLOB, NULL) && !git_oid_cmp(&actual, (const git_oid*)at(index).m_IndexHash.m_hash))
174 at(index).m_ModifyTime = time;
175 *status = git_wc_status_normal;
177 else
178 *status = git_wc_status_modified;
179 m_critRepoSec.Unlock();
181 else
182 *status = git_wc_status_modified;
184 if (at(index).m_Flags & GIT_IDXENTRY_STAGEMASK)
185 *status = git_wc_status_conflicted;
186 else if (at(index).m_Flags & GIT_IDXENTRY_INTENT_TO_ADD)
187 *status = git_wc_status_added;
189 if(pHash)
190 *pHash = at(index).m_IndexHash;
195 if (callback && status && assumeValid && skipWorktree)
196 callback(gitdir + _T("\\") + pathorg, *status, false, pData, *assumeValid, *skipWorktree);
197 return 0;
200 int CGitIndexList::GetStatus(const CString &gitdir,const CString &pathParam, git_wc_status_kind *status,
201 BOOL IsFull, BOOL /*IsRecursive*/,
202 FILL_STATUS_CALLBACK callback, void *pData,
203 CGitHash *pHash, bool * assumeValid, bool * skipWorktree)
205 __int64 time, filesize = 0;
206 bool isDir = false;
207 CString path = pathParam;
209 if (status)
211 git_wc_status_kind dirstatus = git_wc_status_none;
212 int result;
213 if (path.IsEmpty())
214 result = g_Git.GetFileModifyTime(gitdir, &time, &isDir);
215 else
216 result = g_Git.GetFileModifyTime(gitdir + _T("\\") + path, &time, &isDir, &filesize);
218 if (result)
220 *status = git_wc_status_deleted;
221 if (callback && assumeValid && skipWorktree)
222 callback(gitdir + _T("\\") + path, git_wc_status_deleted, false, pData, *assumeValid, *skipWorktree);
224 return 0;
226 if (isDir)
228 if (!path.IsEmpty())
230 if (path.Right(1) != _T("\\"))
231 path += _T("\\");
233 int len = path.GetLength();
235 for (size_t i = 0; i < size(); ++i)
237 if (at(i).m_FileName.GetLength() > len)
239 if (at(i).m_FileName.Left(len) == path)
241 if (!IsFull)
243 *status = git_wc_status_normal;
244 if (callback)
245 callback(gitdir + _T("\\") + path, *status, false, pData, (at(i).m_Flags & GIT_IDXENTRY_VALID) && !(at(i).m_Flags & GIT_IDXENTRY_SKIP_WORKTREE), (at(i).m_Flags & GIT_IDXENTRY_SKIP_WORKTREE) != 0);
246 return 0;
249 else
251 result = g_Git.GetFileModifyTime(gitdir + _T("\\") + at(i).m_FileName, &time, nullptr, &filesize);
252 if (result)
253 continue;
255 *status = git_wc_status_none;
256 if (assumeValid)
257 *assumeValid = false;
258 if (skipWorktree)
259 *skipWorktree = false;
260 GetFileStatus(gitdir, at(i).m_FileName, status, time, filesize, callback, pData, NULL, assumeValid, skipWorktree);
261 // 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
262 if (callback && assumeValid && skipWorktree && (*assumeValid || *skipWorktree))
263 callback(gitdir + _T("\\") + path, *status, false, pData, *assumeValid, *skipWorktree);
264 if (*status != git_wc_status_none)
266 if (dirstatus == git_wc_status_none)
268 dirstatus = git_wc_status_normal;
270 if (*status != git_wc_status_normal)
272 dirstatus = git_wc_status_modified;
279 } /* End For */
281 if (dirstatus != git_wc_status_none)
283 *status = dirstatus;
285 else
287 *status = git_wc_status_unversioned;
289 if(callback)
290 callback(gitdir + _T("\\") + path, *status, false, pData, false, false);
292 return 0;
295 else
297 GetFileStatus(gitdir, path, status, time, filesize, callback, pData, pHash, assumeValid, skipWorktree);
300 return 0;
303 int CGitIndexFileMap::Check(const CString &gitdir, bool *isChanged)
305 __int64 time;
306 int result;
308 CString IndexFile = g_AdminDirMap.GetAdminDir(gitdir) + _T("index");
310 /* Get data associated with "crt_stat.c": */
311 result = g_Git.GetFileModifyTime(IndexFile, &time);
313 if (result)
314 return result;
316 SHARED_INDEX_PTR pIndex;
317 pIndex = this->SafeGet(gitdir);
319 if (pIndex.get() == NULL)
321 if(isChanged)
322 *isChanged = true;
323 return 0;
326 if (pIndex->m_LastModifyTime == time)
328 if (isChanged)
329 *isChanged = false;
331 else
333 if (isChanged)
334 *isChanged = true;
336 return 0;
339 int CGitIndexFileMap::LoadIndex(const CString &gitdir)
343 SHARED_INDEX_PTR pIndex(new CGitIndexList);
345 if(pIndex->ReadIndex(g_AdminDirMap.GetAdminDir(gitdir)))
346 return -1;
348 this->SafeSet(gitdir, pIndex);
350 }catch(...)
352 return -1;
354 return 0;
357 int CGitIndexFileMap::GetFileStatus(const CString &gitdir, const CString &path, git_wc_status_kind *status,BOOL IsFull, BOOL IsRecursive,
358 FILL_STATUS_CALLBACK callback, void *pData,
359 CGitHash *pHash,
360 bool isLoadUpdatedIndex, bool * assumeValid, bool * skipWorktree)
364 CheckAndUpdate(gitdir, isLoadUpdatedIndex);
366 SHARED_INDEX_PTR pIndex = this->SafeGet(gitdir);
367 if (pIndex.get() != NULL)
369 pIndex->GetStatus(gitdir, path, status, IsFull, IsRecursive, callback, pData, pHash, assumeValid, skipWorktree);
371 else
373 // git working tree has not index
374 *status = git_wc_status_unversioned;
377 catch(...)
379 return -1;
381 return 0;
384 int CGitIndexFileMap::IsUnderVersionControl(const CString &gitdir, const CString &path, bool isDir,bool *isVersion, bool isLoadUpdateIndex)
388 if (path.IsEmpty())
390 *isVersion = true;
391 return 0;
394 CString subpath = path;
395 subpath.Replace(_T('\\'), _T('/'));
396 if(isDir)
397 subpath += _T('/');
399 subpath.MakeLower();
401 CheckAndUpdate(gitdir, isLoadUpdateIndex);
403 SHARED_INDEX_PTR pIndex = this->SafeGet(gitdir);
405 if(pIndex.get())
407 if(isDir)
408 *isVersion = (SearchInSortVector(*pIndex, subpath, subpath.GetLength()) >= 0);
409 else
410 *isVersion = (SearchInSortVector(*pIndex, subpath, -1) >= 0);
413 }catch(...)
415 return -1;
417 return 0;
420 // This method is assumed to be called with m_SharedMutex locked.
421 int CGitHeadFileList::GetPackRef(const CString &gitdir)
423 CString PackRef = g_AdminDirMap.GetAdminDir(gitdir) + _T("packed-refs");
425 __int64 mtime;
426 if (g_Git.GetFileModifyTime(PackRef, &mtime))
428 //packed refs is not existed
429 this->m_PackRefFile.Empty();
430 this->m_PackRefMap.clear();
431 return 0;
433 else if(mtime == m_LastModifyTimePackRef)
435 return 0;
437 else
439 this->m_PackRefFile = PackRef;
440 this->m_LastModifyTimePackRef = mtime;
443 m_PackRefMap.clear();
445 CAutoFile hfile = CreateFile(PackRef,
446 GENERIC_READ,
447 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
448 nullptr,
449 OPEN_EXISTING,
450 FILE_ATTRIBUTE_NORMAL,
451 nullptr);
453 if (!hfile)
454 return -1;
456 DWORD filesize = GetFileSize(hfile, nullptr);
457 if (filesize == 0)
458 return -1;
460 DWORD size = 0;
461 std::unique_ptr<char[]> buff(new char[filesize]);
462 ReadFile(hfile, buff.get(), filesize, &size, nullptr);
464 if (size != filesize)
465 return -1;
467 CString hash;
468 CString ref;
469 for (DWORD i = 0; i < filesize;)
471 hash.Empty();
472 ref.Empty();
473 if (buff[i] == '#' || buff[i] == '^')
475 while (buff[i] != '\n')
477 ++i;
478 if (i == filesize)
479 break;
481 ++i;
484 if (i >= filesize)
485 break;
487 while (buff[i] != ' ')
489 hash.AppendChar(buff[i]);
490 ++i;
491 if (i == filesize)
492 break;
495 ++i;
496 if (i >= filesize)
497 break;
499 while (buff[i] != '\n')
501 ref.AppendChar(buff[i]);
502 ++i;
503 if (i == filesize)
504 break;
507 if (!ref.IsEmpty())
508 m_PackRefMap[ref] = hash;
510 while (buff[i] == '\n')
512 ++i;
513 if (i == filesize)
514 break;
517 return 0;
519 int CGitHeadFileList::ReadHeadHash(CString gitdir)
521 CAutoWriteLock lock(m_SharedMutex);
522 m_Gitdir = g_AdminDirMap.GetAdminDir(gitdir);
524 m_HeadFile = m_Gitdir + _T("HEAD");
526 if( g_Git.GetFileModifyTime(m_HeadFile, &m_LastModifyTimeHead))
527 return -1;
529 CAutoFile hfile = CreateFile(m_HeadFile,
530 GENERIC_READ,
531 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
532 nullptr,
533 OPEN_EXISTING,
534 FILE_ATTRIBUTE_NORMAL,
535 nullptr);
537 if (!hfile)
538 return -1;
540 DWORD size = 0;
541 unsigned char buffer[40];
542 ReadFile(hfile, buffer, 4, &size, nullptr);
543 if (size != 4)
544 return -1;
545 buffer[4] = 0;
546 if (strcmp((const char*)buffer, "ref:") == 0)
548 m_HeadRefFile.Empty();
549 DWORD filesize = GetFileSize(hfile, nullptr);
550 if (filesize < 5)
551 return -1;
553 unsigned char *p = (unsigned char*)malloc(filesize - 4);
554 if (!p)
555 return -1;
557 ReadFile(hfile, p, filesize - 4, &size, nullptr);
558 CGit::StringAppend(&m_HeadRefFile, p, CP_UTF8, filesize - 4);
559 free(p);
561 CString ref = m_HeadRefFile.Trim();
562 int start = 0;
563 ref = ref.Tokenize(_T("\n"), start);
564 m_HeadRefFile = m_Gitdir + m_HeadRefFile;
565 m_HeadRefFile.Replace(_T('/'), _T('\\'));
567 __int64 time;
568 if (g_Git.GetFileModifyTime(m_HeadRefFile, &time, nullptr))
570 m_HeadRefFile.Empty();
571 if (GetPackRef(gitdir))
572 return -1;
573 if (m_PackRefMap.find(ref) == m_PackRefMap.end())
574 return -1;
576 m_Head = m_PackRefMap[ref];
577 return 0;
580 CAutoFile href = CreateFile(m_HeadRefFile,
581 GENERIC_READ,
582 FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
583 nullptr,
584 OPEN_EXISTING,
585 FILE_ATTRIBUTE_NORMAL,
586 nullptr);
588 if (!href)
590 m_HeadRefFile.Empty();
592 if (GetPackRef(gitdir))
593 return -1;
595 if (m_PackRefMap.find(ref) == m_PackRefMap.end())
596 return -1;
598 m_Head = m_PackRefMap[ref];
599 return 0;
602 ReadFile(href, buffer, 40, &size, nullptr);
603 if (size != 40)
604 return -1;
606 m_Head.ConvertFromStrA((char*)buffer);
608 m_LastModifyTimeRef = time;
610 else
612 ReadFile(hfile, buffer + 4, 40 - 4, &size, NULL);
613 if (size != 36)
614 return -1;
616 m_HeadRefFile.Empty();
618 m_Head.ConvertFromStrA((char*)buffer);
621 return 0;
624 bool CGitHeadFileList::CheckHeadUpdate()
626 CAutoReadLock lock(m_SharedMutex);
627 if (this->m_HeadFile.IsEmpty())
628 return true;
630 __int64 mtime=0;
632 if (g_Git.GetFileModifyTime(m_HeadFile, &mtime))
633 return true;
635 if (mtime != this->m_LastModifyTimeHead)
636 return true;
638 if (!this->m_HeadRefFile.IsEmpty())
640 if (g_Git.GetFileModifyTime(m_HeadRefFile, &mtime))
641 return true;
643 if (mtime != this->m_LastModifyTimeRef)
644 return true;
647 if(!this->m_PackRefFile.IsEmpty())
649 if (g_Git.GetFileModifyTime(m_PackRefFile, &mtime))
650 return true;
652 if (mtime != this->m_LastModifyTimePackRef)
653 return true;
656 // in an empty repo HEAD points to refs/heads/master, but this ref doesn't exist.
657 // So we need to retry again and again until the ref exists - otherwise we will never notice
658 if (this->m_Head.IsEmpty() && this->m_HeadRefFile.IsEmpty() && this->m_PackRefFile.IsEmpty())
659 return true;
661 return false;
664 bool CGitHeadFileList::HeadHashEqualsTreeHash()
666 CAutoReadLock lock(m_SharedMutex);
667 return (m_Head == m_TreeHash);
670 bool CGitHeadFileList::HeadFileIsEmpty()
672 CAutoReadLock lock(m_SharedMutex);
673 return m_HeadFile.IsEmpty();
676 bool CGitHeadFileList::HeadIsEmpty()
678 CAutoReadLock lock(m_SharedMutex);
679 return m_Head.IsEmpty();
682 int CGitHeadFileList::CallBack(const unsigned char *sha1, const char *base, int baselen,
683 const char *pathname, unsigned mode, int /*stage*/, void *context)
685 #define S_IFGITLINK 0160000
687 CGitHeadFileList *p = (CGitHeadFileList*)context;
688 if( mode&S_IFDIR )
690 if( (mode&S_IFMT) != S_IFGITLINK)
691 return READ_TREE_RECURSIVE;
694 size_t cur = p->size();
695 p->resize(p->size() + 1);
696 p->at(cur).m_Hash = sha1;
697 p->at(cur).m_FileName.Empty();
699 CGit::StringAppend(&p->at(cur).m_FileName, (BYTE*)base, CP_UTF8, baselen);
700 CGit::StringAppend(&p->at(cur).m_FileName, (BYTE*)pathname, CP_UTF8);
702 p->at(cur).m_FileName.MakeLower();
704 //p->at(cur).m_FileName.Replace(_T('/'), _T('\\'));
706 //p->m_Map[p->at(cur).m_FileName] = cur;
708 if( (mode&S_IFMT) == S_IFGITLINK)
709 return 0;
711 return READ_TREE_RECURSIVE;
714 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)
716 size_t count = git_tree_entrycount(tree);
717 for (size_t i = 0; i < count; ++i)
719 const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
720 if (entry == NULL)
721 continue;
722 int mode = git_tree_entry_filemode(entry);
723 if( CallBack(git_tree_entry_id(entry)->id,
724 base,
725 base.GetLength(),
726 git_tree_entry_name(entry),
727 mode,
729 data) == READ_TREE_RECURSIVE
732 if(mode&S_IFDIR)
734 git_object *object = NULL;
735 git_tree_entry_to_object(&object, &repo, entry);
736 if (object == NULL)
737 continue;
738 CStringA parent = base;
739 parent += git_tree_entry_name(entry);
740 parent += "/";
741 ReadTreeRecursive(repo, (git_tree*)object, parent, CallBack, data);
742 git_object_free(object);
748 return 0;
751 // ReadTree is/must only be executed on an empty list
752 int CGitHeadFileList::ReadTree()
754 CAutoWriteLock lock(m_SharedMutex);
755 ATLASSERT(empty());
757 CAutoRepository repository(m_Gitdir);
758 CAutoCommit commit;
759 CAutoTree tree;
760 bool ret = repository;
761 ret = ret && !git_commit_lookup(commit.GetPointer(), repository, (const git_oid*)m_Head.m_hash);
762 ret = ret && !git_commit_tree(tree.GetPointer(), commit);
763 ret = ret && !ReadTreeRecursive(*repository, tree, "", CGitHeadFileList::CallBack, this);
764 if (!ret)
766 clear();
767 m_LastModifyTimeHead = 0;
768 return -1;
771 std::sort(this->begin(), this->end(), SortTree);
772 m_TreeHash = git_commit_id(commit)->id;
774 return 0;
776 int CGitIgnoreItem::FetchIgnoreList(const CString &projectroot, const CString &file, bool isGlobal)
778 if (this->m_pExcludeList)
780 git_free_exclude_list(m_pExcludeList);
781 m_pExcludeList=NULL;
783 free(m_buffer);
784 m_buffer = nullptr;
786 this->m_BaseDir.Empty();
787 if (!isGlobal)
789 CString base = file.Mid(projectroot.GetLength() + 1);
790 base.Replace(_T('\\'), _T('/'));
792 int start = base.ReverseFind(_T('/'));
793 if(start > 0)
795 base = base.Left(start);
796 this->m_BaseDir = CUnicodeUtils::GetMulti(base, CP_UTF8) + "/";
801 if(g_Git.GetFileModifyTime(file, &m_LastModifyTime))
802 return -1;
804 if(git_create_exclude_list(&this->m_pExcludeList))
805 return -1;
808 CAutoFile hfile = CreateFile(file,
809 GENERIC_READ,
810 FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE,
811 NULL,
812 OPEN_EXISTING,
813 FILE_ATTRIBUTE_NORMAL,
814 NULL);
817 if (!hfile)
818 return -1 ;
820 DWORD size=0,filesize=0;
822 filesize=GetFileSize(hfile, NULL);
824 if(filesize == INVALID_FILE_SIZE)
825 return -1;
827 m_buffer = new BYTE[filesize + 1];
829 if (m_buffer == NULL)
830 return -1;
832 if (!ReadFile(hfile, m_buffer, filesize, &size, NULL))
833 return GetLastError();
835 BYTE *p = m_buffer;
836 int line = 0;
837 for (DWORD i = 0; i < size; ++i)
839 if (m_buffer[i] == '\n' || m_buffer[i] == '\r' || i == (size - 1))
841 if (m_buffer[i] == '\n' || m_buffer[i] == '\r')
842 m_buffer[i] = 0;
843 if (i == size - 1)
844 m_buffer[size] = 0;
846 if(p[0] != '#' && p[0] != 0)
847 git_add_exclude((const char*)p,
848 this->m_BaseDir,
849 m_BaseDir.GetLength(),
850 this->m_pExcludeList, ++line);
852 p = m_buffer + i + 1;
856 return 0;
859 bool CGitIgnoreList::CheckFileChanged(const CString &path)
861 __int64 time = 0;
863 int ret = g_Git.GetFileModifyTime(path, &time);
865 bool cacheExist;
867 CAutoReadLock lock(m_SharedMutex);
868 cacheExist = (m_Map.find(path) != m_Map.end());
871 if (!cacheExist && ret == 0)
873 CAutoWriteLock lock(m_SharedMutex);
874 m_Map[path].m_LastModifyTime = 0;
876 // both cache and file is not exist
877 if ((ret != 0) && (!cacheExist))
878 return false;
880 // file exist but cache miss
881 if ((ret == 0) && (!cacheExist))
882 return true;
884 // file not exist but cache exist
885 if ((ret != 0) && (cacheExist))
887 return true;
889 // file exist and cache exist
892 CAutoReadLock lock(m_SharedMutex);
893 if (m_Map[path].m_LastModifyTime == time)
894 return false;
896 return true;
899 bool CGitIgnoreList::CheckIgnoreChanged(const CString &gitdir, const CString &path, bool isDir)
901 CString temp;
902 temp = gitdir;
903 temp += _T("\\");
904 temp += path;
906 temp.Replace(_T('/'), _T('\\'));
908 if (!isDir)
910 int x = temp.ReverseFind(_T('\\'));
911 if (x >= 2)
912 temp = temp.Left(x);
915 while(!temp.IsEmpty())
917 CString tempOrig = temp;
918 temp += _T("\\.git");
920 if (CGit::GitPathFileExists(temp))
922 CString gitignore=temp;
923 gitignore += _T("ignore");
924 if (CheckFileChanged(gitignore))
925 return true;
927 CString adminDir = g_AdminDirMap.GetAdminDir(tempOrig);
928 CString wcglobalgitignore = adminDir + _T("info\\exclude");
929 if (CheckFileChanged(wcglobalgitignore))
930 return true;
932 if (CheckAndUpdateCoreExcludefile(adminDir))
933 return true;
935 return false;
937 else
939 temp += _T("ignore");
940 if (CheckFileChanged(temp))
941 return true;
944 int found=0;
945 int i;
946 for (i = temp.GetLength() - 1; i >= 0; i--)
948 if(temp[i] == _T('\\'))
949 ++found;
951 if(found == 2)
952 break;
955 temp = temp.Left(i);
957 return true;
960 int CGitIgnoreList::FetchIgnoreFile(const CString &gitdir, const CString &gitignore, bool isGlobal)
962 if (CGit::GitPathFileExists(gitignore)) //if .gitignore remove, we need remote cache
964 CAutoWriteLock lock(m_SharedMutex);
965 m_Map[gitignore].FetchIgnoreList(gitdir, gitignore, isGlobal);
967 else
969 CAutoWriteLock lock(m_SharedMutex);
970 m_Map.erase(gitignore);
972 return 0;
975 int CGitIgnoreList::LoadAllIgnoreFile(const CString &gitdir, const CString &path, bool isDir)
977 CString temp;
979 temp = gitdir;
980 temp += _T("\\");
981 temp += path;
983 temp.Replace(_T('/'), _T('\\'));
985 if (!isDir)
987 int x = temp.ReverseFind(_T('\\'));
988 if (x >= 2)
989 temp = temp.Left(x);
992 while (!temp.IsEmpty())
994 CString tempOrig = temp;
995 temp += _T("\\.git");
997 if (CGit::GitPathFileExists(temp))
999 CString gitignore = temp;
1000 gitignore += _T("ignore");
1001 if (CheckFileChanged(gitignore))
1003 FetchIgnoreFile(gitdir, gitignore, false);
1006 CString adminDir = g_AdminDirMap.GetAdminDir(tempOrig);
1007 CString wcglobalgitignore = adminDir + _T("info\\exclude");
1008 if (CheckFileChanged(wcglobalgitignore))
1010 FetchIgnoreFile(gitdir, wcglobalgitignore, true);
1013 if (CheckAndUpdateCoreExcludefile(adminDir))
1015 CString excludesFile;
1017 CAutoReadLock lock(m_SharedMutex);
1018 excludesFile = m_CoreExcludesfiles[adminDir];
1020 if (!excludesFile.IsEmpty())
1021 FetchIgnoreFile(gitdir, excludesFile, true);
1024 return 0;
1026 else
1028 temp += _T("ignore");
1029 if (CheckFileChanged(temp))
1031 FetchIgnoreFile(gitdir, temp, false);
1035 int found = 0;
1036 int i;
1037 for (i = temp.GetLength() - 1; i >= 0; i--)
1039 if(temp[i] == _T('\\'))
1040 ++found;
1042 if(found == 2)
1043 break;
1046 temp = temp.Left(i);
1048 return 0;
1050 bool CGitIgnoreList::CheckAndUpdateGitSystemConfigPath(bool force)
1052 // recheck every 30 seconds
1053 if (GetTickCount() - m_dGitSystemConfigPathLastChecked > 30000 || force)
1055 m_dGitSystemConfigPathLastChecked = GetTickCount();
1056 CString gitSystemConfigPath(CRegString(REG_SYSTEM_GITCONFIGPATH, _T(""), FALSE));
1057 if (gitSystemConfigPath != m_sGitSystemConfigPath)
1059 m_sGitSystemConfigPath = gitSystemConfigPath;
1060 return true;
1063 return false;
1065 bool CGitIgnoreList::CheckAndUpdateCoreExcludefile(const CString &adminDir)
1067 CString projectConfig = adminDir + _T("config");
1068 CString globalConfig = g_Git.GetGitGlobalConfig();
1069 CString globalXDGConfig = g_Git.GetGitGlobalXDGConfig();
1071 CAutoWriteLock lock(m_coreExcludefilesSharedMutex);
1072 bool hasChanged = CheckAndUpdateGitSystemConfigPath();
1073 hasChanged = hasChanged || CheckFileChanged(projectConfig);
1074 hasChanged = hasChanged || CheckFileChanged(globalConfig);
1075 hasChanged = hasChanged || CheckFileChanged(globalXDGConfig);
1076 if (!m_sGitSystemConfigPath.IsEmpty())
1077 hasChanged = hasChanged || CheckFileChanged(m_sGitSystemConfigPath);
1079 CString excludesFile;
1081 CAutoReadLock lock2(m_SharedMutex);
1082 excludesFile = m_CoreExcludesfiles[adminDir];
1084 if (!excludesFile.IsEmpty())
1085 hasChanged = hasChanged || CheckFileChanged(excludesFile);
1087 if (!hasChanged)
1088 return false;
1090 CAutoConfig config(true);
1091 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(projectConfig), GIT_CONFIG_LEVEL_LOCAL, FALSE);
1092 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalConfig), GIT_CONFIG_LEVEL_GLOBAL, FALSE);
1093 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(globalXDGConfig), GIT_CONFIG_LEVEL_XDG, FALSE);
1094 if (!m_sGitSystemConfigPath.IsEmpty())
1095 git_config_add_file_ondisk(config, CGit::GetGitPathStringA(m_sGitSystemConfigPath), GIT_CONFIG_LEVEL_SYSTEM, FALSE);
1096 config.GetString(_T("core.excludesfile"), excludesFile);
1097 if (excludesFile.IsEmpty())
1098 excludesFile = GetWindowsHome() + _T("\\.config\\git\\ignore");
1099 else if (excludesFile.Find(_T("~/")) == 0)
1100 excludesFile = GetWindowsHome() + excludesFile.Mid(1);
1102 CAutoWriteLock lockMap(m_SharedMutex);
1103 g_Git.GetFileModifyTime(projectConfig, &m_Map[projectConfig].m_LastModifyTime);
1104 g_Git.GetFileModifyTime(globalXDGConfig, &m_Map[globalXDGConfig].m_LastModifyTime);
1105 if (m_Map[globalXDGConfig].m_LastModifyTime == 0)
1106 m_Map.erase(globalXDGConfig);
1107 g_Git.GetFileModifyTime(globalConfig, &m_Map[globalConfig].m_LastModifyTime);
1108 if (m_Map[globalConfig].m_LastModifyTime == 0)
1109 m_Map.erase(globalConfig);
1110 if (!m_sGitSystemConfigPath.IsEmpty())
1111 g_Git.GetFileModifyTime(m_sGitSystemConfigPath, &m_Map[m_sGitSystemConfigPath].m_LastModifyTime);
1112 if (m_Map[m_sGitSystemConfigPath].m_LastModifyTime == 0 || m_sGitSystemConfigPath.IsEmpty())
1113 m_Map.erase(m_sGitSystemConfigPath);
1114 m_CoreExcludesfiles[adminDir] = excludesFile;
1116 return true;
1118 const CString CGitIgnoreList::GetWindowsHome()
1120 static CString sWindowsHome(g_Git.GetHomeDirectory());
1121 return sWindowsHome;
1123 bool CGitIgnoreList::IsIgnore(const CString &path, const CString &projectroot, bool isDir)
1125 CString str=path;
1127 str.Replace(_T('\\'),_T('/'));
1129 if (str.GetLength()>0)
1130 if (str[str.GetLength()-1] == _T('/'))
1131 str = str.Left(str.GetLength() - 1);
1133 int ret;
1134 ret = CheckIgnore(str, projectroot, isDir);
1135 while (ret < 0)
1137 int start = str.ReverseFind(_T('/'));
1138 if(start < 0)
1139 return (ret == 1);
1141 str = str.Left(start);
1142 ret = CheckIgnore(str, projectroot, isDir);
1145 return (ret == 1);
1147 int CGitIgnoreList::CheckFileAgainstIgnoreList(const CString &ignorefile, const CStringA &patha, const char * base, int &type)
1149 if (m_Map.find(ignorefile) != m_Map.end())
1151 int ret = -1;
1152 if(m_Map[ignorefile].m_pExcludeList)
1153 ret = git_check_excluded_1(patha, patha.GetLength(), base, &type, m_Map[ignorefile].m_pExcludeList);
1154 if (ret == 0 || ret == 1)
1155 return ret;
1157 return -1;
1159 int CGitIgnoreList::CheckIgnore(const CString &path, const CString &projectroot, bool isDir)
1161 CString temp = projectroot + _T("\\") + path;
1162 temp.Replace(_T('/'), _T('\\'));
1164 CStringA patha = CUnicodeUtils::GetMulti(path, CP_UTF8);
1165 patha.Replace('\\', '/');
1167 int type = 0;
1168 if (isDir)
1170 type = DT_DIR;
1172 // strip directory name
1173 // we do not need to check for a .ignore file inside a directory we might ignore
1174 int i = temp.ReverseFind(_T('\\'));
1175 if (i >= 0)
1176 temp = temp.Left(i);
1178 else
1180 type = DT_REG;
1182 int x = temp.ReverseFind(_T('\\'));
1183 if (x >= 2)
1184 temp = temp.Left(x);
1187 int pos = patha.ReverseFind('/');
1188 const char * base = (pos >= 0) ? ((const char*)patha + pos + 1) : patha;
1190 int ret = -1;
1192 CAutoReadLock lock(m_SharedMutex);
1193 while (!temp.IsEmpty())
1195 CString tempOrig = temp;
1196 temp += _T("\\.git");
1198 if (CGit::GitPathFileExists(temp))
1200 CString gitignore = temp;
1201 gitignore += _T("ignore");
1202 if ((ret = CheckFileAgainstIgnoreList(gitignore, patha, base, type)) != -1)
1203 break;
1205 CString adminDir = g_AdminDirMap.GetAdminDir(tempOrig);
1206 CString wcglobalgitignore = adminDir + _T("info\\exclude");
1207 if ((ret = CheckFileAgainstIgnoreList(wcglobalgitignore, patha, base, type)) != -1)
1208 break;
1210 CString excludesFile = m_CoreExcludesfiles[adminDir];
1211 if (!excludesFile.IsEmpty())
1212 ret = CheckFileAgainstIgnoreList(excludesFile, patha, base, type);
1214 break;
1216 else
1218 temp += _T("ignore");
1219 if ((ret = CheckFileAgainstIgnoreList(temp, patha, base, type)) != -1)
1220 break;
1223 int found = 0;
1224 int i;
1225 for (i = temp.GetLength() - 1; i >= 0; i--)
1227 if (temp[i] == _T('\\'))
1228 ++found;
1230 if (found == 2)
1231 break;
1234 temp = temp.Left(i);
1237 return ret;
1240 bool CGitHeadFileMap::CheckHeadAndUpdate(const CString &gitdir, bool readTree /* = true */)
1242 SHARED_TREE_PTR ptr;
1243 ptr = this->SafeGet(gitdir);
1245 if (ptr.get() && !ptr->CheckHeadUpdate() && (!readTree || ptr->HeadHashEqualsTreeHash()))
1246 return false;
1248 ptr = SHARED_TREE_PTR(new CGitHeadFileList);
1249 ptr->ReadHeadHash(gitdir);
1250 if (readTree)
1251 ptr->ReadTree();
1253 this->SafeSet(gitdir, ptr);
1255 return true;
1258 int CGitHeadFileMap::IsUnderVersionControl(const CString &gitdir, const CString &path, bool isDir, bool *isVersion)
1262 if (path.IsEmpty())
1264 *isVersion = true;
1265 return 0;
1268 CString subpath = path;
1269 subpath.Replace(_T('\\'), _T('/'));
1270 if(isDir)
1271 subpath += _T('/');
1273 subpath.MakeLower();
1275 CheckHeadAndUpdate(gitdir);
1277 SHARED_TREE_PTR treeptr = SafeGet(gitdir);
1279 // Init Repository
1280 if (treeptr->HeadFileIsEmpty())
1282 *isVersion = false;
1283 return 0;
1285 else if (treeptr->empty())
1287 *isVersion = false;
1288 return 1;
1291 if(isDir)
1292 *isVersion = (SearchInSortVector(*treeptr, subpath, subpath.GetLength()) >= 0);
1293 else
1294 *isVersion = (SearchInSortVector(*treeptr, subpath, -1) >= 0);
1296 catch(...)
1298 return -1;
1301 return 0;