Fixed issue #1620: Apply Patch serial can't open file dialog on WinXP
[TortoiseGit.git] / src / Git / GitFolderStatus.cpp
blob93e6e49db3de63c86f4211d43fdd2e86007874e0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008,2011 - TortoiseSVN
4 // Copyright (C) 2008-2012 - 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 "ShellExt.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 m_nCounter = 0;
38 dirstatus = NULL;
39 sCacheKey.reserve(MAX_PATH);
41 g_Git.SetCurrentDir(_T(""));
42 m_hInvalidationEvent = CreateEvent(NULL, FALSE, FALSE, _T("TortoiseGitCacheInvalidationEvent"));
45 GitFolderStatus::~GitFolderStatus(void)
49 const FileStatusCacheEntry * GitFolderStatus::BuildCache(const CTGitPath& filepath, const CString& /*sProjectRoot*/, BOOL bIsFolder, BOOL bDirectFolder)
51 //dont' build the cache if an instance of TortoiseGitProc is running
52 //since this could interfere with svn commands running (concurrent
53 //access of the .git directory).
54 if (g_ShellCache.BlockStatus())
56 CAutoGeneralHandle TGitMutex = ::CreateMutex(NULL, FALSE, _T("TortoiseGitProc.exe"));
57 if (TGitMutex != NULL)
59 if (::GetLastError() == ERROR_ALREADY_EXISTS)
61 return &invalidstatus;
66 ClearCache();
68 if (bIsFolder)
70 if (bDirectFolder)
72 // NOTE: see not in GetFullStatus about project inside another project, we should only get here when
73 // that occurs, and this is not correctly handled yet
75 // initialize record members
76 dirstat.status = git_wc_status_none;
77 dirstat.askedcounter = GITFOLDERSTATUS_CACHETIMES;
78 dirstat.assumeValid = FALSE;
79 dirstat.skipWorktree = FALSE;
81 dirstatus = NULL;
82 // rev.kind = git_opt_revision_unspecified;
85 if (dirstatus)
87 /* if (dirstatus->entry)
89 dirstat.author = authors.GetString (dirstatus->entry->cmt_author);
90 dirstat.url = authors.GetString (dirstatus->entry->url);
91 dirstat.rev = dirstatus->entry->cmt_rev;
92 }*/
93 dirstat.status = GitStatus::GetMoreImportant(dirstatus->text_status, dirstatus->prop_status);
95 m_cache[filepath.GetWinPath()] = dirstat;
96 m_TimeStamp = GetTickCount();
97 return &dirstat;
99 } // if (bIsFolder)
101 m_nCounter = 0;
103 git_wc_status_kind status;
104 bool assumeValid = false;
105 bool skipWorktree = false;
106 int t1,t2;
107 t2=t1=0;
110 git_depth_t depth = git_depth_infinity;
112 if (g_ShellCache.GetCacheType() == ShellCache::dll)
114 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 ATLTRACE2(_T("building cache for %s - time %d\n"), filepath.GetWinPath(), t2 -t1);
128 m_TimeStamp = GetTickCount();
129 FileStatusCacheEntry * ret = NULL;
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 DWORD GitFolderStatus::GetTimeoutValue()
153 DWORD timeout = GITFOLDERSTATUS_CACHETIMEOUT;
154 DWORD factor = (DWORD)m_cache.size() / 200;
155 if (factor==0)
156 factor = 1;
157 return factor*timeout;
160 const FileStatusCacheEntry * GitFolderStatus::GetFullStatus(const CTGitPath& filepath, BOOL bIsFolder, BOOL bColumnProvider)
162 const FileStatusCacheEntry * ret = NULL;
164 CString sProjectRoot;
165 BOOL bHasAdminDir = g_ShellCache.HasGITAdminDir(filepath.GetWinPath(), bIsFolder, &sProjectRoot);
167 //no overlay for unversioned folders
168 if ((!bColumnProvider)&&(!bHasAdminDir))
169 return &invalidstatus;
170 //for the SVNStatus column, we have to check the cache to see
171 //if it's not just unversioned but ignored
172 ret = GetCachedItem(filepath);
173 if ((ret)&&(ret->status == git_wc_status_unversioned)&&(bIsFolder)&&(bHasAdminDir))
175 // an 'unversioned' folder, but with an ADMIN dir --> nested layout!
176 // NOTE: this could be a sub-project in git, or just some standalone project inside of another, either way a TODO
177 ret = BuildCache(filepath, sProjectRoot, bIsFolder, TRUE);
178 if (ret)
179 return ret;
180 else
181 return &invalidstatus;
183 if (ret)
184 return ret;
186 //if it's not in the cache and has no admin dir, then we assume
187 //it's not ignored too
188 if ((bColumnProvider)&&(!bHasAdminDir))
189 return &invalidstatus;
190 ret = BuildCache(filepath, sProjectRoot, bIsFolder);
191 if (ret)
192 return ret;
193 else
194 return &invalidstatus;
197 const FileStatusCacheEntry * GitFolderStatus::GetCachedItem(const CTGitPath& filepath)
199 sCacheKey.assign(filepath.GetWinPath());
200 FileStatusMap::const_iterator iter;
201 const FileStatusCacheEntry *retVal;
203 if(m_mostRecentPath.IsEquivalentTo(CTGitPath(sCacheKey.c_str())))
205 // We've hit the same result as we were asked for last time
206 ATLTRACE2(_T("fast cache hit for %s\n"), filepath);
207 retVal = m_mostRecentStatus;
209 else if ((iter = m_cache.find(sCacheKey)) != m_cache.end())
211 ATLTRACE2(_T("cache found for %s\n"), filepath);
212 retVal = &iter->second;
213 m_mostRecentStatus = retVal;
214 m_mostRecentPath = CTGitPath(sCacheKey.c_str());
216 else
218 retVal = NULL;
221 if(retVal != NULL)
223 // We found something in a cache - check that the cache is not timed-out or force-invalidated
224 DWORD now = GetTickCount();
226 if ((now >= m_TimeStamp)&&((now - m_TimeStamp) > GetTimeoutValue()))
228 // Cache is timed-out
229 ATLTRACE("Cache timed-out\n");
230 ClearCache();
231 retVal = NULL;
233 else if(WaitForSingleObject(m_hInvalidationEvent, 0) == WAIT_OBJECT_0)
235 // TortoiseGitProc has just done something which has invalidated the cache
236 ATLTRACE("Cache invalidated\n");
237 ClearCache();
238 retVal = NULL;
240 return retVal;
242 return NULL;
245 void GitFolderStatus::ClearCache()
247 m_cache.clear();
248 m_mostRecentStatus = NULL;
249 m_mostRecentPath.Reset();
250 // If we're about to rebuild the cache, there's no point hanging on to
251 // an event which tells us that it's invalid
252 ResetEvent(m_hInvalidationEvent);