1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - 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.
22 #include "gitstatus.h"
23 #include "SharedMutex.h"
25 /* Copy from Git cache.h*/
31 #define CACHE_SIGNATURE 0x44495243 /* "DIRC" */
33 unsigned int hdr_signature
;
34 unsigned int hdr_version
;
35 unsigned int hdr_entries
;
39 * The "cache_time" is just the low 32 bits of the
40 * time. It doesn't matter if it overflows - we only
41 * check it for equality in the 32 bits we save.
49 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
50 * Again - this is just a (very strong in practice) heuristic that
51 * the inode hasn't changed.
53 * We save the fields in big-endian order to allow using the
54 * index file over NFS transparently.
56 struct ondisk_cache_entry
{
57 struct cache_time ctime
;
58 struct cache_time mtime
;
67 char name
[FLEX_ARRAY
]; /* more */
71 * This struct is used when CE_EXTENDED bit is 1
72 * The struct must match ondisk_cache_entry exactly from
75 struct ondisk_cache_entry_extended
{
76 struct cache_time ctime
;
77 struct cache_time mtime
;
87 char name
[FLEX_ARRAY
]; /* more */
92 #define CE_NAMEMASK (0x0fff)
93 #define CE_STAGEMASK (0x3000)
94 #define CE_EXTENDED (0x4000)
95 #define CE_VALID (0x8000)
96 #define CE_STAGESHIFT 12
98 * Range 0xFFFF0000 in ce_flags is divided into
99 * two parts: in-memory flags and on-disk ones.
100 * Flags in CE_EXTENDED_FLAGS will get saved on-disk
101 * if you want to save a new flag, add it in
104 * In-memory only flags
106 #define CE_UPDATE (0x10000)
107 #define CE_REMOVE (0x20000)
108 #define CE_UPTODATE (0x40000)
109 #define CE_ADDED (0x80000)
111 #define CE_HASHED (0x100000)
112 #define CE_UNHASHED (0x200000)
115 * Extended on-disk flags
117 #define CE_INTENT_TO_ADD 0x20000000
118 /* CE_EXTENDED2 is for future extension */
119 #define CE_EXTENDED2 0x80000000
121 #define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD)
124 * Safeguard to avoid saving wrong flags:
125 * - CE_EXTENDED2 won't get saved until its semantic is known
126 * - Bits in 0x0000FFFF have been saved in ce_flags already
127 * - Bits in 0x003F0000 are currently in-memory flags
129 #if CE_EXTENDED_FLAGS & 0x803FFFFF
130 #error "CE_EXTENDED_FLAGS out of range"
134 * Copy the sha1 and stat state of a cache entry from one to
135 * another. But we never change the name, or the hash state!
137 #define CE_STATE_MASK (CE_HASHED | CE_UNHASHED)
143 BYTE
*p1
=(BYTE
*)&data
;
144 BYTE
*p2
=(BYTE
*)&ret
;
145 for(int i
=0;i
<sizeof(T
);i
++)
147 p2
[sizeof(T
)-i
-1] = p1
[i
];
153 static inline size_t ce_namelen(T
*ce
)
155 size_t len
= Big2lit(ce
->flags
) & CE_NAMEMASK
;
156 if (len
< CE_NAMEMASK
)
158 return strlen(ce
->name
+ CE_NAMEMASK
) + CE_NAMEMASK
;
161 #define flexible_size(STRUCT,len) ((offsetof(STRUCT,name) + (len) + 8) & ~7)
163 //#define ondisk_cache_entry_size(len) flexible_size(ondisk_cache_entry,len)
164 //#define ondisk_cache_entry_extended_size(len) flexible_size(ondisk_cache_entry_extended,len)
166 //#define ondisk_ce_size(ce) (((ce)->flags & CE_EXTENDED) ? \
167 // ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
168 // ondisk_cache_entry_size(ce_namelen(ce)))
171 static inline size_t ondisk_ce_size(T
*ce
)
173 return flexible_size(T
,ce_namelen(ce
));
180 __time64_t m_ModifyTime
;
183 CGitHash m_IndexHash
;
185 int FillData(ondisk_cache_entry
* entry
);
186 int FillData(ondisk_cache_entry_extended
* entry
);
195 CAutoReadLock(SharedMutex
* lock
)
198 lock
->AcquireShared();
202 m_Lock
->ReleaseShared();
210 CAutoWriteLock(SharedMutex
* lock
)
213 lock
->AcquireExclusive();
217 m_Lock
->ReleaseExclusive();
221 class CGitIndexList
:public std::vector
<CGitIndex
>
226 std::map
<CString
,int> m_Map
;
227 __time64_t m_LastModifyTime
;
229 SharedMutex m_SharedMutex
;
232 int ReadIndex(CString file
);
233 int GetStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,BOOL IsFull
=false, BOOL IsRecursive
=false,FIll_STATUS_CALLBACK callback
=NULL
,void *pData
=NULL
,CGitHash
*pHash
=NULL
);
235 int GetFileStatus(const CString
&gitdir
,const CString
&path
, git_wc_status_kind
* status
,__int64 time
,FIll_STATUS_CALLBACK callback
=NULL
,void *pData
=NULL
,CGitHash
*pHash
=NULL
);
236 int GetDirStatus(const CString
&gitdir
,const CString
&path
, git_wc_status_kind
* status
,__int64 time
,FIll_STATUS_CALLBACK callback
=NULL
,void *pData
=NULL
,CGitHash
*pHash
=NULL
);
247 class CGitHeadFileList
:public std::vector
<CGitTreeItem
>
251 int GetPackRef(const CString
&gitdir
);
254 std::map
<CString
,int> m_Map
;
255 __time64_t m_LastModifyTimeHead
;
256 __time64_t m_LastModifyTimeRef
;
257 __time64_t m_LastModifyTimePackRef
;
259 CString m_HeadRefFile
;
263 CString m_PackRefFile
;
265 SharedMutex m_SharedMutex
;
267 std::map
<CString
,CGitHash
> m_PackRefMap
;
269 CGitHash m_TreeHash
; /* buffered tree hash value */
273 m_LastModifyTimeHead
=0;
274 m_LastModifyTimeRef
=0;
275 m_LastModifyTimePackRef
= 0;
278 int ReadHeadHash(CString gitdir
);
279 bool CheckHeadUpdate();
281 static int CallBack(const unsigned char *, const char *, int, const char *, unsigned int, int, void *);
285 class CGitHeadFileMap
:public std::map
<CString
,CGitHeadFileList
>
289 SharedMutex m_SharedMutex
;
291 CGitHeadFileMap(){ m_SharedMutex
.Init(); }
292 ~CGitHeadFileMap() { m_SharedMutex
.Release(); }
294 int GetFileStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,BOOL IsFull
=false, BOOL IsRecursive
=false,FIll_STATUS_CALLBACK callback
=NULL
,void *pData
=NULL
);
295 int CheckHeadUpdate(const CString
&gitdir
);
296 int GetHeadHash(const CString
&gitdir
, CGitHash
&hash
);
298 bool IsHashChanged(const CString
&gitdir
)
300 CAutoReadLock
lock(&m_SharedMutex
);
301 if( find(gitdir
) == end())
304 CAutoReadLock
lock1(&(*this).m_SharedMutex
);
305 return (*this)[gitdir
].m_Head
!= (*this)[gitdir
].m_TreeHash
;
310 class CGitIndexFileMap
:public std::map
<CString
,CGitIndexList
>
313 SharedMutex m_SharedMutex
;
315 CGitIndexFileMap(){ m_SharedMutex
.Init(); }
316 ~CGitIndexFileMap() { m_SharedMutex
.Release(); }
318 int CheckAndUpdateIndex(const CString
&gitdir
,bool *loaded
=NULL
);
320 int GetFileStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,BOOL IsFull
=false, BOOL IsRecursive
=false,FIll_STATUS_CALLBACK callback
=NULL
,void *pData
=NULL
,CGitHash
*pHash
=NULL
);
322 int IsUnderVersionControl(const CString
&gitdir
, const CString
&path
, bool isDir
,bool *isVersion
);
328 SharedMutex m_SharedMutex
;
333 m_pExcludeList
=NULL
;
338 git_free_exclude_list(m_pExcludeList
);
341 __time64_t m_LastModifyTime
;
343 EXCLUDE_LIST m_pExcludeList
;
344 int FetchIgnoreList(const CString
&projectroot
, const CString
&file
);
350 bool CheckFileChanged(const CString
&path
);
351 int FetchIgnoreFile(const CString
&gitdir
, const CString
&gitignore
);
353 int CheckIgnore(const CString
&path
,const CString
&root
);
356 SharedMutex m_SharedMutex
;
358 CGitIgnoreList(){ m_SharedMutex
.Init(); }
359 ~CGitIgnoreList() { m_SharedMutex
.Release(); }
361 std::map
<CString
, CGitIgnoreItem
> m_Map
;
363 int GetIgnoreFileChangeTimeList(const CString
&dir
, std::vector
<__int64
> &timelist
);
364 bool CheckIgnoreChanged(const CString
&gitdir
,const CString
&path
);
365 int LoadAllIgnoreFile(const CString
&gitdir
,const CString
&path
);
366 bool IsIgnore(const CString
&path
,const CString
&root
);
370 int GetRangeInSortVector(T
&vector
,LPTSTR pstr
,int len
, int *start
, int *end
, int pos
)
376 if(start
== 0 || end
== NULL
)
380 if( _tcsnccmp(vector
[pos
].m_FileName
, pstr
,len
) != 0)
382 for(int i
=0;i
< vector
.size();i
++)
384 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
395 *end
= vector
.size();
397 for(int i
=pos
;i
<vector
.size();i
++)
399 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
407 for(int i
=pos
;i
>=0;i
--)
409 if( _tcsnccmp(vector
[i
].m_FileName
, pstr
,len
) == 0 )
422 int SearchInSortVector(T
&vector
, LPTSTR pstr
, int len
)
424 int end
=vector
.size()-1;
426 int mid
= (start
+end
)/2;
428 if(vector
.size() == 0)
431 while(!( start
== end
&& start
==mid
))
434 cmp
= _tcsnccmp( vector
[mid
].m_FileName
,pstr
,len
);
448 mid
=(start
+end
) /2;
452 if(_tcsnccmp( vector
[mid
].m_FileName
,pstr
,len
) == 0)
462 int GetFileStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,BOOL IsFull
=false, BOOL IsRecursive
=false,FIll_STATUS_CALLBACK callback
=NULL
,void *pData
=NULL
);
464 CGitIgnoreList m_IgnoreList
;
465 CGitHeadFileMap m_HeadFilesMap
;
466 CGitIndexFileMap m_IndexFilesMap
;
468 int GetStatus(const CString
&gitdir
,const CString
&path
,git_wc_status_kind
* status
,BOOL IsFull
=false, BOOL IsRecursive
=false,FIll_STATUS_CALLBACK callback
=NULL
,void *pData
=NULL
);