1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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.
25 #include "GitConfig.h"
27 #include "UnicodeUtils.h"
30 #include <sys/types.h>
33 #include "SmartHandle.h"
35 CGitAdminDirMap g_AdminDirMap
;
37 int CGitIndex::Print()
39 _tprintf(_T("0x%08X 0x%08X %s %s\n"),
40 (int)this->m_ModifyTime
,
42 this->m_IndexHash
.ToString(),
48 CGitIndexList::CGitIndexList()
50 this->m_LastModifyTime
= 0;
53 static bool SortIndex(CGitIndex
&Item1
, CGitIndex
&Item2
)
55 return Item1
.m_FileName
.Compare(Item2
.m_FileName
) < 0;
58 static bool SortTree(CGitTreeItem
&Item1
, CGitTreeItem
&Item2
)
60 return Item1
.m_FileName
.Compare(Item2
.m_FileName
) < 0;
63 int CGitIndexList::ReadIndex(CString dgitdir
)
67 CStringA gitdir
= CUnicodeUtils::GetMulti(dgitdir
, CP_UTF8
);
68 git_repository
*repository
= NULL
;
69 git_index
*index
= NULL
;
71 int ret
= git_repository_open(&repository
, gitdir
.GetBuffer());
72 gitdir
.ReleaseBuffer();
76 if (git_repository_index(&index
, repository
))
78 git_repository_free(repository
);
82 unsigned int ecount
= git_index_entrycount(index
);
84 for (unsigned int i
= 0; i
< ecount
; ++i
)
86 git_index_entry
*e
= git_index_get(index
, i
);
88 this->at(i
).m_FileName
.Empty();
89 g_Git
.StringAppend(&this->at(i
).m_FileName
, (BYTE
*)e
->path
, CP_UTF8
);
90 this->at(i
).m_FileName
.MakeLower();
91 this->at(i
).m_ModifyTime
= e
->mtime
.seconds
;
92 this->at(i
).m_Flags
= e
->flags
;
93 this->at(i
).m_IndexHash
= (char *) e
->oid
.id
;
96 git_index_free(index
);
97 git_repository_free(repository
);
99 g_Git
.GetFileModifyTime(dgitdir
+ _T("index"), &this->m_LastModifyTime
);
100 std::sort(this->begin(), this->end(), SortIndex
);
105 int CGitIndexList::GetFileStatus(const CString
&gitdir
,const CString
&pathorg
,git_wc_status_kind
*status
,__int64 time
,FIll_STATUS_CALLBACK callback
,void *pData
, CGitHash
*pHash
)
109 CString path
= pathorg
;
112 int start
= SearchInSortVector(*this, ((CString
&)path
).GetBuffer(), -1);
113 ((CString
&)path
).ReleaseBuffer();
117 *status
= git_wc_status_unversioned
;
127 if (index
>= size() )
130 if (time
== at(index
).m_ModifyTime
)
132 *status
= git_wc_status_normal
;
136 *status
= git_wc_status_modified
;
139 if (at(index
).m_Flags
& GIT_IDXENTRY_STAGEMASK
)
140 *status
= git_wc_status_conflicted
;
141 else if (at(index
).m_Flags
& GIT_IDXENTRY_INTENT_TO_ADD
)
142 *status
= git_wc_status_added
;
145 *pHash
= at(index
).m_IndexHash
;
150 if(callback
&& status
)
151 callback(gitdir
+ _T("\\") + pathorg
, *status
, false, pData
);
155 int CGitIndexList::GetStatus(const CString
&gitdir
,const CString
&pathParam
, git_wc_status_kind
*status
,
156 BOOL IsFull
, BOOL IsRecursive
,
157 FIll_STATUS_CALLBACK callback
,void *pData
,
161 git_wc_status_kind dirstatus
= git_wc_status_none
;
164 CString path
= pathParam
;
169 result
= g_Git
.GetFileModifyTime(gitdir
, &time
, &isDir
);
171 result
= g_Git
.GetFileModifyTime(gitdir
+ _T("\\") + path
, &time
, &isDir
);
175 *status
= git_wc_status_deleted
;
177 callback(gitdir
+ _T("\\") + path
, git_wc_status_deleted
, false, pData
);
185 if (path
.Right(1) != _T("\\"))
188 int len
= path
.GetLength();
190 for (int i
= 0; i
< size(); i
++)
192 if (at(i
).m_FileName
.GetLength() > len
)
194 if (at(i
).m_FileName
.Left(len
) == path
)
198 *status
= git_wc_status_normal
;
200 callback(gitdir
+ _T("\\") + path
, *status
, false, pData
);
206 result
= g_Git
.GetFileModifyTime(gitdir
+_T("\\") + at(i
).m_FileName
, &time
);
210 *status
= git_wc_status_none
;
211 GetFileStatus(gitdir
, at(i
).m_FileName
, status
, time
, callback
, pData
);
212 if (*status
!= git_wc_status_none
)
214 if (dirstatus
== git_wc_status_none
)
216 dirstatus
= git_wc_status_normal
;
218 if (*status
!= git_wc_status_normal
)
220 dirstatus
= git_wc_status_modified
;
229 if (dirstatus
!= git_wc_status_none
)
235 *status
= git_wc_status_unversioned
;
238 callback(gitdir
+ _T("\\") + path
, *status
, false, pData
);
245 GetFileStatus(gitdir
, path
, status
, time
, callback
, pData
, pHash
);
251 int CGitIndexFileMap::Check(const CString
&gitdir
, bool *isChanged
)
256 CString IndexFile
= g_AdminDirMap
.GetAdminDir(gitdir
) + _T("index");
258 /* Get data associated with "crt_stat.c": */
259 result
= g_Git
.GetFileModifyTime(IndexFile
, &time
);
264 SHARED_INDEX_PTR pIndex
;
265 pIndex
= this->SafeGet(gitdir
);
267 if (pIndex
.get() == NULL
)
274 if (pIndex
->m_LastModifyTime
== time
)
287 int CGitIndexFileMap::LoadIndex(const CString
&gitdir
)
291 SHARED_INDEX_PTR
pIndex(new CGitIndexList
);
293 if(pIndex
->ReadIndex(g_AdminDirMap
.GetAdminDir(gitdir
)))
296 this->SafeSet(gitdir
, pIndex
);
305 int CGitIndexFileMap::GetFileStatus(const CString
&gitdir
, const CString
&path
, git_wc_status_kind
*status
,BOOL IsFull
, BOOL IsRecursive
,
306 FIll_STATUS_CALLBACK callback
,void *pData
,
308 bool isLoadUpdatedIndex
)
312 CheckAndUpdate(gitdir
, isLoadUpdatedIndex
);
314 SHARED_INDEX_PTR pIndex
= this->SafeGet(gitdir
);
315 if (pIndex
.get() != NULL
)
317 pIndex
->GetStatus(gitdir
, path
, status
, IsFull
, IsRecursive
, callback
, pData
, pHash
);
328 int CGitIndexFileMap::IsUnderVersionControl(const CString
&gitdir
, const CString
&path
, bool isDir
,bool *isVersion
, bool isLoadUpdateIndex
)
338 CString subpath
= path
;
339 subpath
.Replace(_T('\\'), _T('/'));
345 CheckAndUpdate(gitdir
, isLoadUpdateIndex
);
347 SHARED_INDEX_PTR pIndex
= this->SafeGet(gitdir
);
352 *isVersion
= (SearchInSortVector(*pIndex
, subpath
.GetBuffer(), subpath
.GetLength()) >= 0);
354 *isVersion
= (SearchInSortVector(*pIndex
, subpath
.GetBuffer(), -1) >= 0);
355 subpath
.ReleaseBuffer();
365 int CGitHeadFileList::GetPackRef(const CString
&gitdir
)
367 CString PackRef
= g_AdminDirMap
.GetAdminDir(gitdir
) + _T("packed-refs");
370 if (g_Git
.GetFileModifyTime(PackRef
, &mtime
))
372 CAutoWriteLock
lock(&this->m_SharedMutex
);
373 //packed refs is not existed
374 this->m_PackRefFile
.Empty();
375 this->m_PackRefMap
.clear();
378 else if(mtime
== m_LastModifyTimePackRef
)
384 CAutoWriteLock
lock(&this->m_SharedMutex
);
385 this->m_PackRefFile
= PackRef
;
386 this->m_LastModifyTimePackRef
= mtime
;
391 CAutoWriteLock
lock(&this->m_SharedMutex
);
392 this->m_PackRefMap
.clear();
394 CAutoFile hfile
= CreateFile(PackRef
,
396 FILE_SHARE_READ
|FILE_SHARE_DELETE
|FILE_SHARE_WRITE
,
399 FILE_ATTRIBUTE_NORMAL
,
409 DWORD filesize
= GetFileSize(hfile
, NULL
);
412 buff
= new char[filesize
];
414 ReadFile(hfile
, buff
, filesize
, &size
, NULL
);
416 if (size
!= filesize
)
425 for(DWORD i
=0;i
<filesize
;)
429 if (buff
[i
] == '#' || buff
[i
] == '^')
431 while (buff
[i
] != '\n')
443 while (buff
[i
] != ' ')
445 hash
.AppendChar(buff
[i
]);
455 while (buff
[i
] != '\n')
457 ref
.AppendChar(buff
[i
]);
465 this->m_PackRefMap
[ref
] = hash
;
468 while (buff
[i
] == '\n')
483 int CGitHeadFileList::ReadHeadHash(CString gitdir
)
486 CAutoWriteLock
lock(&this->m_SharedMutex
);
487 m_Gitdir
= g_AdminDirMap
.GetAdminDir(gitdir
);
489 m_HeadFile
= m_Gitdir
+ _T("HEAD");
491 if( g_Git
.GetFileModifyTime(m_HeadFile
, &m_LastModifyTimeHead
))
498 CAutoFile hfile
= CreateFile(m_HeadFile
,
500 FILE_SHARE_READ
|FILE_SHARE_DELETE
|FILE_SHARE_WRITE
,
503 FILE_ATTRIBUTE_NORMAL
,
512 DWORD size
= 0,filesize
= 0;
513 unsigned char buffer
[40] ;
514 ReadFile(hfile
, buffer
, 4, &size
, NULL
);
521 if (strcmp((const char*)buffer
,"ref:") == 0)
523 filesize
= GetFileSize(hfile
, NULL
);
525 unsigned char *p
= (unsigned char*)malloc(filesize
-4);
527 ReadFile(hfile
, p
, filesize
- 4, &size
, NULL
);
529 m_HeadRefFile
.Empty();
530 g_Git
.StringAppend(&this->m_HeadRefFile
, p
, CP_UTF8
, filesize
- 4);
531 CString ref
= this->m_HeadRefFile
;
534 ref
= ref
.Tokenize(_T("\n"), start
);
536 m_HeadRefFile
= m_Gitdir
+ m_HeadRefFile
.Trim();
537 m_HeadRefFile
.Replace(_T('/'),_T('\\'));
540 if (g_Git
.GetFileModifyTime(m_HeadRefFile
, &time
, NULL
))
542 m_HeadRefFile
.Empty();
543 if (GetPackRef(gitdir
))
548 if (this->m_PackRefMap
.find(ref
) == m_PackRefMap
.end())
553 this ->m_Head
= m_PackRefMap
[ref
];
558 CAutoFile href
= CreateFile(m_HeadRefFile
,
560 FILE_SHARE_READ
|FILE_SHARE_DELETE
|FILE_SHARE_WRITE
,
563 FILE_ATTRIBUTE_NORMAL
,
568 m_HeadRefFile
.Empty();
570 if (GetPackRef(gitdir
))
576 if (this->m_PackRefMap
.find(ref
) == m_PackRefMap
.end())
581 this ->m_Head
= m_PackRefMap
[ref
];
585 ReadFile(href
, buffer
, 40, &size
, NULL
);
591 this->m_Head
.ConvertFromStrA((char*)buffer
);
593 this->m_LastModifyTimeRef
= time
;
598 ReadFile(hfile
, buffer
+ 4, 40 - 4, &size
, NULL
);
604 m_HeadRefFile
.Empty();
606 this->m_Head
.ConvertFromStrA((char*)buffer
);
618 bool CGitHeadFileList::CheckHeadUpdate()
620 CAutoReadLock
lock(&m_SharedMutex
);
621 if (this->m_HeadFile
.IsEmpty())
626 if (g_Git
.GetFileModifyTime(m_HeadFile
, &mtime
))
629 if (mtime
!= this->m_LastModifyTimeHead
)
632 if (!this->m_HeadRefFile
.IsEmpty())
634 if (g_Git
.GetFileModifyTime(m_HeadRefFile
, &mtime
))
637 if (mtime
!= this->m_LastModifyTimeRef
)
641 if(!this->m_PackRefFile
.IsEmpty())
643 if (g_Git
.GetFileModifyTime(m_PackRefFile
, &mtime
))
646 if (mtime
!= this->m_LastModifyTimePackRef
)
650 // in an empty repo HEAD points to refs/heads/master, but this ref doesn't exist.
651 // So we need to retry again and again until the ref exists - otherwise we will never notice
652 if (this->m_Head
.IsEmpty() && this->m_HeadRefFile
.IsEmpty() && this->m_PackRefFile
.IsEmpty())
658 int CGitHeadFileList::ReadTree()
661 if (this->m_Head
.IsEmpty())
666 CAutoLocker
lock(g_Git
.m_critGitDllSec
);
667 CAutoWriteLock
lock1(&this->m_SharedMutex
);
669 if (m_Gitdir
!= g_Git
.m_CurrentDir
)
671 g_Git
.SetCurrentDir(m_Gitdir
);
672 SetCurrentDirectory(g_Git
.m_CurrentDir
);
679 ret
= git_read_tree(this->m_Head
.m_hash
, CGitHeadFileList::CallBack
, this);
691 int CGitHeadFileList::CallBack(const unsigned char *sha1
, const char *base
, int baselen
,
692 const char *pathname
, unsigned mode
, int /*stage*/, void *context
)
694 #define S_IFGITLINK 0160000
696 CGitHeadFileList
*p
= (CGitHeadFileList
*)context
;
699 if( (mode
&S_IFMT
) != S_IFGITLINK
)
700 return READ_TREE_RECURSIVE
;
703 unsigned int cur
= p
->size();
704 p
->resize(p
->size() + 1);
705 p
->at(cur
).m_Hash
= (char*)sha1
;
706 p
->at(cur
).m_FileName
.Empty();
709 g_Git
.StringAppend(&p
->at(cur
).m_FileName
, (BYTE
*)base
, CP_UTF8
, baselen
);
711 g_Git
.StringAppend(&p
->at(cur
).m_FileName
,(BYTE
*)pathname
, CP_UTF8
);
713 p
->at(cur
).m_FileName
.MakeLower();
715 //p->at(cur).m_FileName.Replace(_T('/'), _T('\\'));
717 //p->m_Map[p->at(cur).m_FileName] = cur;
719 if( (mode
&S_IFMT
) == S_IFGITLINK
)
722 return READ_TREE_RECURSIVE
;
725 int ReadTreeRecursive(git_repository
&repo
, git_tree
* tree
, CStringA base
, int (*CallBack
) (const unsigned char *, const char *, int, const char *, unsigned int, int, void *),void *data
)
727 size_t count
= git_tree_entrycount(tree
);
728 for (int i
= 0; i
< count
; i
++)
730 const git_tree_entry
*entry
= git_tree_entry_byindex(tree
, i
);
733 int mode
= git_tree_entry_attributes(entry
);
734 if( CallBack(git_tree_entry_id(entry
)->id
,
737 git_tree_entry_name(entry
),
740 data
) == READ_TREE_RECURSIVE
745 git_object
*object
= NULL
;
746 git_tree_entry_to_object(&object
, &repo
, entry
);
749 CStringA parent
= base
;
750 parent
+= git_tree_entry_name(entry
);
752 ReadTreeRecursive(repo
, (git_tree
*)object
, parent
, CallBack
, data
);
753 git_object_free(object
);
762 int CGitHeadFileList::ReadTree()
764 CAutoWriteLock
lock(&m_SharedMutex
);
765 CStringA gitdir
= CUnicodeUtils::GetMulti(m_Gitdir
, CP_UTF8
);
766 git_repository
*repository
= NULL
;
767 git_commit
*commit
= NULL
;
768 git_tree
* tree
= NULL
;
770 this->clear(); // hack to avoid duplicates in the head list, which are introduced in GitStatus::GetFileStatus when this method is called
773 ret
= git_repository_open(&repository
, gitdir
.GetBuffer());
774 gitdir
.ReleaseBuffer();
777 ret
= git_commit_lookup(&commit
, repository
, (const git_oid
*)m_Head
.m_hash
);
781 ret
= git_commit_tree(&tree
, commit
);
785 ret
= ReadTreeRecursive(*repository
, tree
,"", CGitHeadFileList::CallBack
,this);
789 std::sort(this->begin(), this->end(), SortTree
);
790 this->m_TreeHash
= (char*)(git_commit_id(commit
)->id
);
798 git_commit_free(commit
);
801 git_repository_free(repository
);
806 int CGitIgnoreItem::FetchIgnoreList(const CString
&projectroot
, const CString
&file
, bool isGlobal
)
808 CAutoWriteLock
lock(&this->m_SharedMutex
);
810 if (this->m_pExcludeList
)
812 free(m_pExcludeList
);
816 this->m_BaseDir
.Empty();
819 CString base
= file
.Mid(projectroot
.GetLength() + 1);
820 base
.Replace(_T('\\'), _T('/'));
822 int start
= base
.ReverseFind(_T('/'));
825 base
= base
.Left(start
);
826 this->m_BaseDir
= CUnicodeUtils::GetMulti(base
, CP_UTF8
) + "/";
831 if(g_Git
.GetFileModifyTime(file
, &m_LastModifyTime
))
834 if(git_create_exclude_list(&this->m_pExcludeList
))
838 CAutoFile hfile
= CreateFile(file
,
840 FILE_SHARE_READ
|FILE_SHARE_DELETE
|FILE_SHARE_WRITE
,
843 FILE_ATTRIBUTE_NORMAL
,
850 DWORD size
=0,filesize
=0;
852 filesize
=GetFileSize(hfile
, NULL
);
854 if(filesize
== INVALID_FILE_SIZE
)
857 BYTE
*buffer
= new BYTE
[filesize
+ 1];
862 if(! ReadFile(hfile
, buffer
,filesize
,&size
,NULL
))
863 return GetLastError();
866 for (int i
= 0; i
< size
; i
++)
868 if (buffer
[i
] == '\n' || buffer
[i
] == '\r' || i
== (size
- 1))
870 if (buffer
[i
] == '\n' || buffer
[i
] == '\r')
875 if(p
[0] != '#' && p
[0] != 0)
876 git_add_exclude((const char*)p
,
877 this->m_BaseDir
.GetBuffer(),
878 m_BaseDir
.GetLength(),
879 this->m_pExcludeList
);
884 /* Can't free buffer, exluced list will use this buffer*/
891 bool CGitIgnoreList::CheckFileChanged(const CString
&path
)
895 int ret
= g_Git
.GetFileModifyTime(path
, &time
);
897 this->m_SharedMutex
.AcquireShared();
898 bool cacheExist
= (m_Map
.find(path
) != m_Map
.end());
899 this->m_SharedMutex
.ReleaseShared();
901 if (!cacheExist
&& ret
== 0)
903 CAutoWriteLock
lock(&this->m_SharedMutex
);
904 m_Map
[path
].m_LastModifyTime
= 0;
905 m_Map
[path
].m_SharedMutex
.Init();
907 // both cache and file is not exist
908 if ((ret
!= 0) && (!cacheExist
))
911 // file exist but cache miss
912 if ((ret
== 0) && (!cacheExist
))
915 // file not exist but cache exist
916 if ((ret
!= 0) && (cacheExist
))
920 // file exist and cache exist
923 CAutoReadLock
lock(&this->m_SharedMutex
);
924 if (m_Map
[path
].m_LastModifyTime
== time
)
930 bool CGitIgnoreList::CheckIgnoreChanged(const CString
&gitdir
,const CString
&path
)
937 temp
.Replace(_T('/'), _T('\\'));
939 while(!temp
.IsEmpty())
941 CString tempOrig
= temp
;
942 temp
+= _T("\\.git");
944 if (CGit::GitPathFileExists(temp
))
946 CString gitignore
=temp
;
947 gitignore
+= _T("ignore");
948 if (CheckFileChanged(gitignore
))
951 CString adminDir
= g_AdminDirMap
.GetAdminDir(tempOrig
);
952 CString wcglobalgitignore
= adminDir
+ _T("info\\exclude");
953 if (CheckFileChanged(wcglobalgitignore
))
956 if (CheckAndUpdateCoreExcludefile(adminDir
))
963 temp
+= _T("ignore");
964 if (CheckFileChanged(temp
))
970 for (i
= temp
.GetLength() - 1; i
>= 0; i
--)
972 if(temp
[i
] == _T('\\'))
984 int CGitIgnoreList::FetchIgnoreFile(const CString
&gitdir
, const CString
&gitignore
, bool isGlobal
)
986 if (CGit::GitPathFileExists(gitignore
)) //if .gitignore remove, we need remote cache
988 CAutoWriteLock
lock(&this->m_SharedMutex
);
989 if (m_Map
.find(gitignore
) == m_Map
.end())
990 m_Map
[gitignore
].m_SharedMutex
.Init();
992 m_Map
[gitignore
].FetchIgnoreList(gitdir
, gitignore
, isGlobal
);
996 CAutoWriteLock
lock(&this->m_SharedMutex
);
997 if (m_Map
.find(gitignore
) != m_Map
.end())
998 m_Map
[gitignore
].m_SharedMutex
.Release();
1000 m_Map
.erase(gitignore
);
1005 int CGitIgnoreList::LoadAllIgnoreFile(const CString
&gitdir
,const CString
&path
)
1013 temp
.Replace(_T('/'), _T('\\'));
1015 while (!temp
.IsEmpty())
1017 CString tempOrig
= temp
;
1018 temp
+= _T("\\.git");
1020 if (CGit::GitPathFileExists(temp
))
1022 CString gitignore
= temp
;
1023 gitignore
+= _T("ignore");
1024 if (CheckFileChanged(gitignore
))
1026 FetchIgnoreFile(gitdir
, gitignore
, false);
1029 CString adminDir
= g_AdminDirMap
.GetAdminDir(tempOrig
);
1030 CString wcglobalgitignore
= adminDir
+ _T("info\\exclude");
1031 if (CheckFileChanged(wcglobalgitignore
))
1033 FetchIgnoreFile(gitdir
, wcglobalgitignore
, true);
1036 if (CheckAndUpdateCoreExcludefile(adminDir
))
1038 m_SharedMutex
.AcquireShared();
1039 CString excludesFile
= m_CoreExcludesfiles
[adminDir
];
1040 m_SharedMutex
.ReleaseShared();
1041 if (!excludesFile
.IsEmpty())
1042 FetchIgnoreFile(gitdir
, excludesFile
, true);
1049 temp
+= _T("ignore");
1050 if (CheckFileChanged(temp
))
1052 FetchIgnoreFile(gitdir
, temp
, false);
1058 for (i
= temp
.GetLength() - 1; i
>= 0; i
--)
1060 if(temp
[i
] == _T('\\'))
1067 temp
= temp
.Left(i
);
1071 bool CGitIgnoreList::CheckAndUpdateMsysGitBinpath(bool force
)
1073 // recheck every 30 seconds
1074 if (GetTickCount() - m_dMsysGitBinPathLastChecked
> 30000 || force
)
1076 m_dMsysGitBinPathLastChecked
= GetTickCount();
1077 CString
msysGitBinPath(CRegString(REG_MSYSGIT_PATH
, _T(""), FALSE
));
1078 if (msysGitBinPath
!= m_sMsysGitBinPath
)
1080 m_sMsysGitBinPath
= msysGitBinPath
;
1086 bool CGitIgnoreList::CheckAndUpdateCoreExcludefile(const CString
&adminDir
)
1088 bool hasChanged
= false;
1090 CString projectConfig
= adminDir
+ _T("config");
1091 CString globalConfig
= GetWindowsHome() + _T("\\.gitconfig");
1093 CAutoWriteLock
lock(&m_coreExcludefilesSharedMutex
);
1094 hasChanged
= CheckAndUpdateMsysGitBinpath();
1095 CString systemConfig
= m_sMsysGitBinPath
+ _T("\\..\\etc\\gitconfig");
1097 hasChanged
= hasChanged
|| CheckFileChanged(projectConfig
);
1098 hasChanged
= hasChanged
|| CheckFileChanged(globalConfig
);
1099 if (!m_sMsysGitBinPath
.IsEmpty())
1100 hasChanged
= hasChanged
|| CheckFileChanged(systemConfig
);
1102 m_SharedMutex
.AcquireShared();
1103 CString excludesFile
= m_CoreExcludesfiles
[adminDir
];
1104 m_SharedMutex
.ReleaseShared();
1105 if (!excludesFile
.IsEmpty())
1106 hasChanged
= hasChanged
|| CheckFileChanged(excludesFile
);
1111 git_config
* config
;
1112 git_config_new(&config
);
1113 CStringA projectConfigA
= CUnicodeUtils::GetMulti(projectConfig
, CP_UTF8
);
1114 git_config_add_file_ondisk(config
, projectConfigA
.GetBuffer(), 3);
1115 projectConfigA
.ReleaseBuffer();
1116 CStringA globalConfigA
= CUnicodeUtils::GetMulti(globalConfig
, CP_UTF8
);
1117 git_config_add_file_ondisk(config
, globalConfigA
.GetBuffer(), 2);
1118 globalConfigA
.ReleaseBuffer();
1119 if (!m_sMsysGitBinPath
.IsEmpty())
1121 CStringA systemConfigA
= CUnicodeUtils::GetMulti(systemConfig
, CP_UTF8
);
1122 git_config_add_file_ondisk(config
, systemConfigA
.GetBuffer(), 1);
1123 systemConfigA
.ReleaseBuffer();
1125 const char * out
= NULL
;
1126 CStringA
name(_T("core.excludesfile"));
1127 git_config_get_string(&out
, config
, name
.GetBuffer());
1128 name
.ReleaseBuffer();
1129 CStringA
excludesFileA(out
);
1130 excludesFile
= CUnicodeUtils::GetUnicode(excludesFileA
);
1131 if (excludesFile
.Find(_T("~/")) == 0)
1132 excludesFile
= GetWindowsHome() + excludesFile
.Mid(1);
1133 git_config_free(config
);
1135 CAutoWriteLock
lockMap(&m_SharedMutex
);
1136 g_Git
.GetFileModifyTime(projectConfig
, &m_Map
[projectConfig
].m_LastModifyTime
);
1137 g_Git
.GetFileModifyTime(globalConfig
, &m_Map
[globalConfig
].m_LastModifyTime
);
1138 if (m_Map
[globalConfig
].m_LastModifyTime
== 0)
1140 m_Map
[globalConfig
].m_SharedMutex
.Release();
1141 m_Map
.erase(globalConfig
);
1143 if (!m_sMsysGitBinPath
.IsEmpty())
1144 g_Git
.GetFileModifyTime(systemConfig
, &m_Map
[systemConfig
].m_LastModifyTime
);
1145 if (m_Map
[systemConfig
].m_LastModifyTime
== 0 || m_sMsysGitBinPath
.IsEmpty())
1147 m_Map
[systemConfig
].m_SharedMutex
.Release();
1148 m_Map
.erase(systemConfig
);
1150 m_CoreExcludesfiles
[adminDir
] = excludesFile
;
1154 const CString
CGitIgnoreList::GetWindowsHome()
1156 static CString
sWindowsHome(g_Git
.GetHomeDirectory());
1157 return sWindowsHome
;
1159 bool CGitIgnoreList::IsIgnore(const CString
&path
,const CString
&projectroot
)
1163 str
.Replace(_T('\\'),_T('/'));
1165 if (str
.GetLength()>0)
1166 if (str
[str
.GetLength()-1] == _T('/'))
1167 str
= str
.Left(str
.GetLength() - 1);
1170 ret
= CheckIgnore(str
, projectroot
);
1173 int start
= str
.ReverseFind(_T('/'));
1177 str
= str
.Left(start
);
1178 ret
= CheckIgnore(str
, projectroot
);
1183 int CGitIgnoreList::CheckFileAgainstIgnoreList(const CString
&ignorefile
, const CStringA
&patha
, const char * base
, int &type
)
1185 if (m_Map
.find(ignorefile
) != m_Map
.end())
1188 if(m_Map
[ignorefile
].m_pExcludeList
)
1189 ret
= git_check_excluded_1(patha
, patha
.GetLength(), base
, &type
, m_Map
[ignorefile
].m_pExcludeList
);
1190 if (ret
== 0 || ret
== 1)
1195 int CGitIgnoreList::CheckIgnore(const CString
&path
,const CString
&projectroot
)
1199 CString temp
= projectroot
+ _T("\\") + path
;
1200 temp
.Replace(_T('/'), _T('\\'));
1202 CStringA patha
= CUnicodeUtils::GetMulti(path
, CP_UTF8
);
1203 patha
.Replace('\\', '/');
1205 if(g_Git
.GetFileModifyTime(temp
, &time
, &dir
))
1213 // strip directory name
1214 // we do not need to check for a .ignore file inside a directory we might ignore
1215 int i
= temp
.ReverseFind(_T('\\'));
1217 temp
= temp
.Left(i
);
1223 int pos
= patha
.ReverseFind('/');
1224 base
= pos
>= 0 ? patha
.GetBuffer() + pos
+ 1 : patha
.GetBuffer();
1228 CAutoReadLock
lock(&this->m_SharedMutex
);
1229 while (!temp
.IsEmpty())
1231 CString tempOrig
= temp
;
1232 temp
+= _T("\\.git");
1234 if (CGit::GitPathFileExists(temp
))
1236 CString gitignore
= temp
;
1237 gitignore
+= _T("ignore");
1238 if ((ret
= CheckFileAgainstIgnoreList(gitignore
, patha
, base
, type
)) != -1)
1241 CString adminDir
= g_AdminDirMap
.GetAdminDir(tempOrig
);
1242 CString wcglobalgitignore
= adminDir
+ _T("info\\exclude");
1243 if ((ret
= CheckFileAgainstIgnoreList(wcglobalgitignore
, patha
, base
, type
)) != -1)
1246 m_SharedMutex
.AcquireShared();
1247 CString excludesFile
= m_CoreExcludesfiles
[adminDir
];
1248 m_SharedMutex
.ReleaseShared();
1249 if (!excludesFile
.IsEmpty())
1250 ret
= CheckFileAgainstIgnoreList(excludesFile
, patha
, base
, type
);
1256 temp
+= _T("ignore");
1257 if ((ret
= CheckFileAgainstIgnoreList(temp
, patha
, base
, type
)) != -1)
1263 for (i
= temp
.GetLength() - 1; i
>= 0; i
--)
1265 if (temp
[i
] == _T('\\'))
1272 temp
= temp
.Left(i
);
1275 patha
.ReleaseBuffer();
1280 bool CGitHeadFileMap::CheckHeadUpdate(const CString
&gitdir
)
1282 SHARED_TREE_PTR ptr
;
1283 ptr
= this->SafeGet(gitdir
);
1287 return ptr
->CheckHeadUpdate();
1291 SHARED_TREE_PTR
ptr1(new CGitHeadFileList
);
1292 ptr1
->ReadHeadHash(gitdir
);
1294 this->SafeSet(gitdir
, ptr1
);
1300 int CGitHeadFileMap::IsUnderVersionControl(const CString
&gitdir
, const CString
&path
, bool isDir
, bool *isVersion
)
1310 CString subpath
= path
;
1311 subpath
.Replace(_T('\\'), _T('/'));
1315 subpath
.MakeLower();
1317 CheckHeadUpdate(gitdir
);
1319 SHARED_TREE_PTR treeptr
;
1320 treeptr
= SafeGet(gitdir
);
1322 if (treeptr
->m_Head
!= treeptr
->m_TreeHash
)
1324 treeptr
->ReadHeadHash(gitdir
);
1327 if (treeptr
->m_HeadFile
.IsEmpty())
1332 else if (treeptr
->ReadTree())
1334 treeptr
->m_LastModifyTimeHead
= 0;
1338 SafeSet(gitdir
, treeptr
);
1342 *isVersion
= (SearchInSortVector(*treeptr
, subpath
.GetBuffer(), subpath
.GetLength()) >= 0);
1344 *isVersion
= (SearchInSortVector(*treeptr
, subpath
.GetBuffer(), -1) >= 0);
1345 subpath
.ReleaseBuffer();
1355 int CGitHeadFileMap::GetHeadHash(const CString
&gitdir
, CGitHash
&hash
)
1357 SHARED_TREE_PTR ptr
;
1358 ptr
= this->SafeGet(gitdir
);
1360 if(ptr
.get() == NULL
)
1362 SHARED_TREE_PTR
ptr1(new CGitHeadFileList());
1363 ptr1
->ReadHeadHash(gitdir
);
1365 hash
= ptr1
->m_Head
;
1367 this->SafeSet(gitdir
, ptr1
);
1372 if(ptr
->CheckHeadUpdate())
1374 SHARED_TREE_PTR
ptr1(new CGitHeadFileList());
1375 ptr1
->ReadHeadHash(gitdir
);
1377 hash
= ptr1
->m_Head
;
1378 this->SafeSet(gitdir
, ptr1
);