1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2003-2008 - TortoiseSVN
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
20 #include "ShellExt.h"
\r
21 #include "GitFolderStatus.h"
\r
22 #include "UnicodeUtils.h"
\r
23 #include "..\TGitCache\CacheInterface.h"
\r
25 //#include "GitGlobal.h"
\r
26 #include "gitindex.h"
\r
28 extern ShellCache g_ShellCache;
\r
30 extern CGitIndexFileMap g_IndexFileMap;
\r
32 // get / auto-alloc a string "copy"
\r
34 const char* StringPool::GetString (const char* value)
\r
36 // special case: NULL pointer
\r
43 // do we already have a string with the desired value?
\r
45 pool_type::const_iterator iter = pool.find (value);
\r
46 if (iter != pool.end())
\r
54 const char* newString = _strdup (value);
\r
57 pool.insert (newString);
\r
67 // clear internal pool
\r
69 void StringPool::clear()
\r
71 // delete all strings
\r
73 for (pool_type::iterator iter = pool.begin(), end = pool.end(); iter != end; ++iter)
\r
78 // remove pointers from pool
\r
83 CTGitPath GitFolderStatus::folderpath;
\r
86 GitFolderStatus::GitFolderStatus(void)
\r
90 invalidstatus.author = emptyString;
\r
91 invalidstatus.askedcounter = -1;
\r
92 invalidstatus.status = git_wc_status_none;
\r
93 invalidstatus.url = emptyString;
\r
94 // invalidstatus.rev = -1;
\r
95 invalidstatus.owner = emptyString;
\r
96 invalidstatus.needslock = false;
\r
97 invalidstatus.tree_conflict = false;
\r
100 sCacheKey.reserve(MAX_PATH);
\r
102 //rootpool = svn_pool_create (NULL);
\r
104 m_hInvalidationEvent = CreateEvent(NULL, FALSE, FALSE, _T("TortoiseGitCacheInvalidationEvent"));
\r
107 GitFolderStatus::~GitFolderStatus(void)
\r
109 //svn_pool_destroy(rootpool);
\r
110 CloseHandle(m_hInvalidationEvent);
\r
113 const FileStatusCacheEntry * GitFolderStatus::BuildCache(const CTGitPath& filepath, const CString& sProjectRoot, BOOL bIsFolder, BOOL bDirectFolder)
\r
115 // svn_client_ctx_t * localctx;
\r
116 // apr_hash_t * statushash;
\r
117 // apr_pool_t * pool;
\r
118 //git_error_t * err = NULL; // If svn_client_status comes out through catch(...), err would else be unassigned
\r
121 //dont' build the cache if an instance of TortoiseProc is running
\r
122 //since this could interfere with svn commands running (concurrent
\r
123 //access of the .git directory).
\r
124 if (g_ShellCache.BlockStatus())
\r
126 HANDLE TGitMutex = ::CreateMutex(NULL, FALSE, _T("TortoiseGitProc.exe"));
\r
127 if (TGitMutex != NULL)
\r
129 if (::GetLastError() == ERROR_ALREADY_EXISTS)
\r
131 ::CloseHandle(TGitMutex);
\r
132 return &invalidstatus;
\r
135 ::CloseHandle(TGitMutex);
\r
138 // pool = svn_pool_create (rootpool); // create the memory pool
\r
141 // svn_error_clear(svn_client_create_context(&localctx, pool));
\r
142 // set up the configuration
\r
143 // Note: I know this is an 'expensive' call, but without this, ignores
\r
144 // done in the global ignore pattern won't show up.
\r
145 if (g_ShellCache.ShowIgnoredOverlay())
\r
146 ;// svn_error_clear(svn_config_get_config (&(localctx->config), g_pConfigDir, pool));
\r
148 // strings pools are unused, now -> we may clear them
\r
154 ATLTRACE2(_T("building cache for %s\n"), filepath);
\r
159 // NOTE: see not in GetFullStatus about project inside another project, we should only get here when
\r
160 // that occurs, and this is not correctly handled yet
\r
162 // initialize record members
\r
163 // dirstat.rev = -1;
\r
164 dirstat.status = git_wc_status_none;
\r
165 dirstat.author = authors.GetString(NULL);
\r
166 dirstat.url = urls.GetString(NULL);
\r
167 dirstat.owner = owners.GetString(NULL);
\r
168 dirstat.askedcounter = GITFOLDERSTATUS_CACHETIMES;
\r
169 dirstat.needslock = false;
\r
170 dirstat.tree_conflict = false;
\r
173 // statushash = apr_hash_make(pool);
\r
174 // git_revnum_t youngest = GIT_INVALID_REVNUM;
\r
175 // git_opt_revision_t rev;
\r
176 // rev.kind = git_opt_revision_unspecified;
\r
179 folderpath = filepath;
\r
181 /*err = svn_client_status4 (&youngest,
\r
182 filepath.GetDirectory().GetSVNApiPath(pool),
\r
186 svn_depth_empty,//depth
\r
190 FALSE, //ignore externals
\r
203 /* if (dirstatus->entry)
\r
205 dirstat.author = authors.GetString (dirstatus->entry->cmt_author);
\r
206 dirstat.url = authors.GetString (dirstatus->entry->url);
\r
207 dirstat.rev = dirstatus->entry->cmt_rev;
\r
208 dirstat.owner = owners.GetString(dirstatus->entry->lock_owner);
\r
210 dirstat.status = GitStatus::GetMoreImportant(dirstatus->text_status, dirstatus->prop_status);
\r
211 // dirstat.tree_conflict = dirstatus->tree_conflict != NULL;
\r
213 m_cache[filepath.GetWinPath()] = dirstat;
\r
214 m_TimeStamp = GetTickCount();
\r
215 // svn_error_clear(err);
\r
216 // svn_pool_destroy (pool); //free allocated memory
\r
219 } // if (bIsFolder)
\r
223 //Fill in the cache with
\r
224 //all files inside the same folder as the asked file/folder is
\r
225 //since subversion can do this in one step
\r
226 // localctx->auth_baton = NULL;
\r
228 // statushash = apr_hash_make(pool);
\r
229 // git_revnum_t youngest = GIT_INVALID_REVNUM;
\r
230 // git_opt_revision_t rev;
\r
231 // rev.kind = git_opt_revision_unspecified;
\r
234 // extract the sub-path (relative to project root)
\r
235 //MessageBox(NULL, filepath.GetDirectory().GetWinPathString(), sProjectRoot, MB_OK);
\r
236 // LPCSTR lpszSubPath = NULL;
\r
238 CString s = filepath.GetWinPathString();
\r
239 if (s.GetLength() > sProjectRoot.GetLength())
\r
241 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() - 1/*otherwise it gets initial slash*/);
\r
242 // lpszSubPath = sSubPath;
\r
245 //if (lpszSubPath) MessageBoxA(NULL, lpszSubPath, "BuildCache", MB_OK);
\r
246 //MessageBoxA(NULL, CStringA(sProjectRoot), sSubPath, MB_OK);
\r
247 //err = !wgEnumFiles_safe(CStringA(sProjectRoot), lpszSubPath, WGEFF_NoRecurse|WGEFF_FullPath|WGEFF_DirStatusAll, &fillstatusmap, this);
\r
249 //path.SetFromWin(sSubPath);
\r
250 git_wc_status_kind status;
\r
252 err = g_IndexFileMap.GetFileStatus((CString&)sProjectRoot,sSubPath,&status,true,true,fillstatusmap,this);
\r
254 //err = g_IndexFileMap.GetFileStatus(sProjectRoot,&path,
\r
255 /*err = svn_client_status4 (&youngest,
\r
256 filepath.GetDirectory().GetSVNApiPath(pool),
\r
260 svn_depth_immediates, //depth
\r
264 FALSE, //ignore externals
\r
273 // Error present if function is not under version control
\r
276 // svn_error_clear(err);
\r
277 // svn_pool_destroy (pool); //free allocated memory
\r
278 return &invalidstatus;
\r
281 // svn_error_clear(err);
\r
282 // svn_pool_destroy (pool); //free allocated memory
\r
283 m_TimeStamp = GetTickCount();
\r
284 const FileStatusCacheEntry * ret = NULL;
\r
285 FileStatusMap::const_iterator iter;
\r
286 if ((iter = m_cache.find(filepath.GetWinPath())) != m_cache.end())
\r
288 ret = &iter->second;
\r
289 m_mostRecentPath = filepath;
\r
290 m_mostRecentStatus = ret;
\r
294 // for SUBST'ed drives, Subversion doesn't return a path with a backslash
\r
295 // e.g. G:\ but only G: when fetching the status. So search for that
\r
296 // path too before giving up.
\r
297 // This is especially true when right-clicking directly on a SUBST'ed
\r
298 // drive to get the context menu
\r
299 if (_tcslen(filepath.GetWinPath())==3)
\r
301 if ((iter = m_cache.find((LPCTSTR)filepath.GetWinPathString().Left(2))) != m_cache.end())
\r
303 ret = &iter->second;
\r
304 m_mostRecentPath = filepath;
\r
305 m_mostRecentStatus = ret;
\r
311 return &invalidstatus;
\r
314 DWORD GitFolderStatus::GetTimeoutValue()
\r
316 DWORD timeout = GITFOLDERSTATUS_CACHETIMEOUT;
\r
317 DWORD factor = m_cache.size()/200;
\r
320 return factor*timeout;
\r
323 const FileStatusCacheEntry * GitFolderStatus::GetFullStatus(const CTGitPath& filepath, BOOL bIsFolder, BOOL bColumnProvider)
\r
325 const FileStatusCacheEntry * ret = NULL;
\r
327 CString sProjectRoot;
\r
328 BOOL bHasAdminDir = g_ShellCache.HasSVNAdminDir(filepath.GetWinPath(), bIsFolder, &sProjectRoot);
\r
330 //no overlay for unversioned folders
\r
331 if ((!bColumnProvider)&&(!bHasAdminDir))
\r
332 return &invalidstatus;
\r
333 //for the SVNStatus column, we have to check the cache to see
\r
334 //if it's not just unversioned but ignored
\r
335 ret = GetCachedItem(filepath);
\r
336 if ((ret)&&(ret->status == git_wc_status_unversioned)&&(bIsFolder)&&(bHasAdminDir))
\r
338 // an 'unversioned' folder, but with an ADMIN dir --> nested layout!
\r
339 // NOTE: this could be a sub-project in git, or just some standalone project inside of another, either way a TODO
\r
340 ret = BuildCache(filepath, sProjectRoot, bIsFolder, TRUE);
\r
344 return &invalidstatus;
\r
349 //if it's not in the cache and has no admin dir, then we assume
\r
350 //it's not ignored too
\r
351 if ((bColumnProvider)&&(!bHasAdminDir))
\r
352 return &invalidstatus;
\r
353 ret = BuildCache(filepath, sProjectRoot, bIsFolder);
\r
357 return &invalidstatus;
\r
360 const FileStatusCacheEntry * GitFolderStatus::GetCachedItem(const CTGitPath& filepath)
\r
362 sCacheKey.assign(filepath.GetWinPath());
\r
363 FileStatusMap::const_iterator iter;
\r
364 const FileStatusCacheEntry *retVal;
\r
366 if(m_mostRecentPath.IsEquivalentTo(CTGitPath(sCacheKey.c_str())))
\r
368 // We've hit the same result as we were asked for last time
\r
369 ATLTRACE2(_T("fast cache hit for %s\n"), filepath);
\r
370 retVal = m_mostRecentStatus;
\r
372 else if ((iter = m_cache.find(sCacheKey)) != m_cache.end())
\r
374 ATLTRACE2(_T("cache found for %s\n"), filepath);
\r
375 retVal = &iter->second;
\r
376 m_mostRecentStatus = retVal;
\r
377 m_mostRecentPath = CTGitPath(sCacheKey.c_str());
\r
386 // We found something in a cache - check that the cache is not timed-out or force-invalidated
\r
387 DWORD now = GetTickCount();
\r
389 if ((now >= m_TimeStamp)&&((now - m_TimeStamp) > GetTimeoutValue()))
\r
391 // Cache is timed-out
\r
392 ATLTRACE("Cache timed-out\n");
\r
396 else if(WaitForSingleObject(m_hInvalidationEvent, 0) == WAIT_OBJECT_0)
\r
398 // TortoiseProc has just done something which has invalidated the cache
\r
399 ATLTRACE("Cache invalidated\n");
\r
408 void GitFolderStatus::fillstatusmap(CString &path,git_wc_status_kind status,void *pUserData)
\r
410 GitFolderStatus *Stat = (GitFolderStatus*)pUserData;
\r
412 FileStatusMap &cache = Stat->m_cache;
\r
413 FileStatusCacheEntry s;
\r
414 s.needslock = false;
\r
415 s.tree_conflict = false;
\r
417 s.author = Stat->authors.GetString(NULL);
\r
418 s.url = Stat->urls.GetString(NULL);
\r
420 s.owner = Stat->owners.GetString(NULL);
\r
424 //s.status = GitStatus::GetMoreImportant(s.status, status->text_status);
\r
425 //s.status = GitStatus::GetMoreImportant(s.status, status->prop_status);
\r
426 //s.status = GitStatusFromWingit(pFile->nStatus);
\r
428 // TODO ?: s.blaha = pFile->nStage
\r
430 //s.lock = status->repos_lock;
\r
431 //s.tree_conflict = (status->tree_conflict != NULL);
\r
433 s.askedcounter = GITFOLDERSTATUS_CACHETIMES;
\r
435 //if (pFile->sFileName)
\r
437 // str = CUnicodeUtils::StdGetUnicode(pFile->sFileName);
\r
438 // std::replace(str.begin(), str.end(), '/', '\\');
\r
439 //MessageBox(NULL, str.c_str(), _T(""), MB_OK);
\r
443 if( path.Right(1) == _T("\\"))
\r
445 path=path.Left(path.GetLength()-1);
\r
455 git_error_t* GitFolderStatus::fillstatusmap(void * baton, const char * path, git_wc_status2_t * status, apr_pool_t * /*pool*/)
\r
457 GitFolderStatus * Stat = (GitFolderStatus *)baton;
\r
458 FileStatusMap * cache = &Stat->m_cache;
\r
459 FileStatusCacheEntry s;
\r
460 s.needslock = false;
\r
461 s.tree_conflict = false;
\r
462 if ((status)&&(status->entry))
\r
464 s.author = Stat->authors.GetString(status->entry->cmt_author);
\r
465 s.url = Stat->urls.GetString(status->entry->url);
\r
466 s.rev = status->entry->cmt_rev;
\r
467 s.owner = Stat->owners.GetString(status->entry->lock_owner);
\r
468 if (status->entry->present_props)
\r
469 s.needslock = strstr(status->entry->present_props, "svn:needs-lock") ? true : false;
\r
473 s.author = Stat->authors.GetString(NULL);
\r
474 s.url = Stat->urls.GetString(NULL);
\r
476 s.owner = Stat->owners.GetString(NULL);
\r
478 s.status = git_wc_status_none;
\r
481 s.status = GitStatus::GetMoreImportant(s.status, status->text_status);
\r
482 s.status = GitStatus::GetMoreImportant(s.status, status->prop_status);
\r
483 s.lock = status->repos_lock;
\r
484 s.tree_conflict = (status->tree_conflict != NULL);
\r
486 s.askedcounter = GITFOLDERSTATUS_CACHETIMES;
\r
490 str = CUnicodeUtils::StdGetUnicode(path);
\r
491 std::replace(str.begin(), str.end(), '/', '\\');
\r
497 return GIT_NO_ERROR;
\r
500 git_error_t* GitFolderStatus::findfolderstatus(void * baton, const char * path, git_wc_status2_t * status, apr_pool_t * /*pool*/)
\r
502 GitFolderStatus * Stat = (GitFolderStatus *)baton;
\r
503 if ((Stat)&&(Stat->folderpath.IsEquivalentTo(CTGitPath(CString(path)))))
\r
505 Stat->dirstatus = status;
\r
508 return GIT_NO_ERROR;
\r
512 void GitFolderStatus::ClearCache()
\r
515 m_mostRecentStatus = NULL;
\r
516 m_mostRecentPath.Reset();
\r
517 // If we're about to rebuild the cache, there's no point hanging on to
\r
518 // an event which tells us that it's invalid
\r
519 ResetEvent(m_hInvalidationEvent);
\r