1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008,2011, 2014 - TortoiseSVN
4 // Copyright (C) 2008-2016 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "ShellCache.h"
22 #include "GitFolderStatus.h"
23 #include "UnicodeUtils.h"
24 #include "..\TGitCache\CacheInterface.h"
28 extern ShellCache g_ShellCache
;
30 GitFolderStatus::GitFolderStatus(void)
33 invalidstatus
.askedcounter
= -1;
34 invalidstatus
.status
= git_wc_status_none
;
35 invalidstatus
.assumeValid
= FALSE
;
36 invalidstatus
.skipWorktree
= FALSE
;
37 dirstat
.askedcounter
= -1;
38 dirstat
.assumeValid
= dirstat
.skipWorktree
= false;
39 dirstat
.status
= git_wc_status_none
;
42 m_mostRecentStatus
= nullptr;
43 sCacheKey
.reserve(MAX_PATH
);
45 g_Git
.SetCurrentDir(_T(""));
46 m_hInvalidationEvent
= CreateEvent(nullptr, FALSE
, FALSE
, _T("TortoiseGitCacheInvalidationEvent")); // no need to explicitely close m_hInvalidationEvent in ~GitFolderStatus as it is CAutoGeneralHandle
49 GitFolderStatus::~GitFolderStatus(void)
53 const FileStatusCacheEntry
* GitFolderStatus::BuildCache(const CTGitPath
& filepath
, const CString
& /*sProjectRoot*/, BOOL bIsFolder
, BOOL bDirectFolder
)
55 //dont' build the cache if an instance of TortoiseGitProc is running
56 //since this could interfere with svn commands running (concurrent
57 //access of the .git directory).
58 if (g_ShellCache
.BlockStatus())
60 CAutoGeneralHandle TGitMutex
= ::CreateMutex(nullptr, FALSE
, _T("TortoiseGitProc.exe"));
61 if (TGitMutex
!= nullptr)
63 if (::GetLastError() == ERROR_ALREADY_EXISTS
)
64 return &invalidstatus
;
74 // NOTE: see not in GetFullStatus about project inside another project, we should only get here when
75 // that occurs, and this is not correctly handled yet
77 // initialize record members
78 dirstat
.status
= git_wc_status_none
;
79 dirstat
.askedcounter
= GITFOLDERSTATUS_CACHETIMES
;
80 dirstat
.assumeValid
= FALSE
;
81 dirstat
.skipWorktree
= FALSE
;
84 // rev.kind = git_opt_revision_unspecified;
89 /* if (dirstatus->entry)
91 dirstat.author = authors.GetString (dirstatus->entry->cmt_author);
92 dirstat.url = authors.GetString (dirstatus->entry->url);
93 dirstat.rev = dirstatus->entry->cmt_rev;
95 dirstat
.status
= GitStatus::GetMoreImportant(dirstatus
->text_status
, dirstatus
->prop_status
);
97 m_cache
[filepath
.GetWinPath()] = dirstat
;
98 m_TimeStamp
= GetTickCount64();
105 git_wc_status_kind status
;
106 bool assumeValid
= false;
107 bool skipWorktree
= false;
112 git_depth_t depth
= git_depth_infinity
;
114 if (g_ShellCache
.GetCacheType() == ShellCache::dll
)
115 depth
= git_depth_empty
;
117 t1
= ::GetCurrentTime();
118 status
= m_GitStatus
.GetAllStatus(filepath
, depth
, &assumeValid
, &skipWorktree
);
119 t2
= ::GetCurrentTime();
123 status
= git_wc_status_unknown
;
126 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": building cache for %s - time %d\n"), filepath
.GetWinPath(), t2
- t1
);
128 m_TimeStamp
= GetTickCount64();
129 FileStatusCacheEntry
* ret
= nullptr;
131 if (_tcslen(filepath
.GetWinPath())==3)
132 ret
= &m_cache
[(LPCTSTR
)filepath
.GetWinPathString().Left(2)];
134 ret
= &m_cache
[filepath
.GetWinPath()];
138 ret
->status
= status
;
139 ret
->assumeValid
= assumeValid
;
140 ret
->skipWorktree
= skipWorktree
;
143 m_mostRecentPath
= filepath
;
144 m_mostRecentStatus
= ret
;
148 return &invalidstatus
;
151 ULONGLONG
GitFolderStatus::GetTimeoutValue()
153 ULONGLONG timeout
= GITFOLDERSTATUS_CACHETIMEOUT
;
154 ULONGLONG factor
= (ULONGLONG
)m_cache
.size() / 200UL;
157 return factor
*timeout
;
160 const FileStatusCacheEntry
* GitFolderStatus::GetFullStatus(const CTGitPath
& filepath
, BOOL bIsFolder
)
162 CString sProjectRoot
;
163 BOOL bHasAdminDir
= g_ShellCache
.HasGITAdminDir(filepath
.GetWinPath(), bIsFolder
, &sProjectRoot
);
165 //no overlay for unversioned folders
167 return &invalidstatus
;
168 //for the SVNStatus column, we have to check the cache to see
169 //if it's not just unversioned but ignored
170 const FileStatusCacheEntry
* ret
= GetCachedItem(filepath
);
171 if ((ret
)&&(ret
->status
== git_wc_status_unversioned
)&&(bIsFolder
)&&(bHasAdminDir
))
173 // an 'unversioned' folder, but with an ADMIN dir --> nested layout!
174 // NOTE: this could be a sub-project in git, or just some standalone project inside of another, either way a TODO
175 ret
= BuildCache(filepath
, sProjectRoot
, bIsFolder
, TRUE
);
179 return &invalidstatus
;
184 //if it's not in the cache and has no admin dir, then we assume
185 //it's not ignored too
186 ret
= BuildCache(filepath
, sProjectRoot
, bIsFolder
);
190 return &invalidstatus
;
193 const FileStatusCacheEntry
* GitFolderStatus::GetCachedItem(const CTGitPath
& filepath
)
195 sCacheKey
.assign(filepath
.GetWinPath());
196 FileStatusMap::const_iterator iter
;
197 const FileStatusCacheEntry
* retVal
= nullptr;
199 if(m_mostRecentPath
.IsEquivalentTo(CTGitPath(sCacheKey
.c_str())))
201 // We've hit the same result as we were asked for last time
202 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": fast cache hit for %s\n"), filepath
.GetWinPath());
203 retVal
= m_mostRecentStatus
;
205 else if ((iter
= m_cache
.find(sCacheKey
)) != m_cache
.end())
207 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": cache found for %s\n"), filepath
.GetWinPath());
208 retVal
= &iter
->second
;
209 m_mostRecentStatus
= retVal
;
210 m_mostRecentPath
= CTGitPath(sCacheKey
.c_str());
216 // We found something in a cache - check that the cache is not timed-out or force-invalidated
217 ULONGLONG now
= GetTickCount64();
219 if ((now
>= m_TimeStamp
) && ((now
- m_TimeStamp
) > GetTimeoutValue()))
221 // Cache is timed-out
222 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Cache timed-out\n");
226 else if (WaitForSingleObject(m_hInvalidationEvent
, 0) == WAIT_OBJECT_0
)
228 // TortoiseGitProc has just done something which has invalidated the cache
229 CTraceToOutputDebugString::Instance()(__FUNCTION__
": Cache invalidated\n");
236 void GitFolderStatus::ClearCache()
239 m_mostRecentStatus
= nullptr;
240 m_mostRecentPath
.Reset();
241 // If we're about to rebuild the cache, there's no point hanging on to
242 // an event which tells us that it's invalid
243 ResetEvent(m_hInvalidationEvent
);