Make sure we also kill AsyncReadStdErrThread when we kill a thread
[TortoiseGit.git] / src / Git / GitFolderStatus.cpp
blob8178ecb4cd7275ad6a2cbae08a9646c3c65e350d
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.
20 #include "stdafx.h"
21 #include "ShellCache.h"
22 #include "GitFolderStatus.h"
23 #include "UnicodeUtils.h"
24 #include "..\TGitCache\CacheInterface.h"
25 #include "Git.h"
26 #include "gitindex.h"
28 extern ShellCache g_ShellCache;
30 GitFolderStatus::GitFolderStatus(void)
32 m_TimeStamp = 0;
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;
40 m_nCounter = 0;
41 dirstatus = nullptr;
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;
68 ClearCache();
70 if (bIsFolder)
72 if (bDirectFolder)
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;
83 dirstatus = nullptr;
84 // rev.kind = git_opt_revision_unspecified;
87 if (dirstatus)
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;
94 }*/
95 dirstat.status = GitStatus::GetMoreImportant(dirstatus->text_status, dirstatus->prop_status);
97 m_cache[filepath.GetWinPath()] = dirstat;
98 m_TimeStamp = GetTickCount64();
99 return &dirstat;
101 } // if (bIsFolder)
103 m_nCounter = 0;
105 git_wc_status_kind status;
106 bool assumeValid = false;
107 bool skipWorktree = false;
108 int t1,t2;
109 t2=t1=0;
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();
121 catch ( ... )
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)];
133 else
134 ret = &m_cache[filepath.GetWinPath()];
136 if (ret)
138 ret->status = status;
139 ret->assumeValid = assumeValid;
140 ret->skipWorktree = skipWorktree;
143 m_mostRecentPath = filepath;
144 m_mostRecentStatus = ret;
146 if (ret)
147 return ret;
148 return &invalidstatus;
151 ULONGLONG GitFolderStatus::GetTimeoutValue()
153 ULONGLONG timeout = GITFOLDERSTATUS_CACHETIMEOUT;
154 ULONGLONG factor = (ULONGLONG)m_cache.size() / 200UL;
155 if (factor==0)
156 factor = 1;
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
166 if (!bHasAdminDir)
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);
176 if (ret)
177 return ret;
178 else
179 return &invalidstatus;
181 if (ret)
182 return ret;
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);
187 if (ret)
188 return ret;
189 else
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());
213 if (!retVal)
214 return nullptr;
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");
223 ClearCache();
224 return nullptr;
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");
230 ClearCache();
231 return nullptr;
233 return retVal;
236 void GitFolderStatus::ClearCache()
238 m_cache.clear();
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);