Fix stash problem when svn dcommit at dirty working space
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob6460789f9dcd3b0d848c0a64623a414663e434c2
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
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.
19 #include "StdAfx.h"
20 #include ".\cacheddirectory.h"
21 //#include "SVNHelpers.h"
22 #include "GitStatusCache.h"
23 #include "GitStatus.h"
24 #include <set>
26 CCachedDirectory::CCachedDirectory(void)
28 m_indexFileTime = 0;
29 // m_propsFileTime = 0;
30 m_currentStatusFetchingPathTicks = 0;
31 m_bCurrentFullStatusValid = false;
32 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
33 m_bRecursive = true;
36 CCachedDirectory::~CCachedDirectory(void)
40 CCachedDirectory::CCachedDirectory(const CTGitPath& directoryPath)
42 ATLASSERT(directoryPath.IsDirectory() || !PathFileExists(directoryPath.GetWinPath()));
44 m_directoryPath = directoryPath;
45 m_indexFileTime = 0;
46 // m_propsFileTime = 0;
47 m_currentStatusFetchingPathTicks = 0;
48 m_bCurrentFullStatusValid = false;
49 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
50 m_bRecursive = true;
53 BOOL CCachedDirectory::SaveToDisk(FILE * pFile)
55 AutoLocker lock(m_critSec);
56 #define WRITEVALUETOFILE(x) if (fwrite(&x, sizeof(x), 1, pFile)!=1) return false;
58 unsigned int value = 1;
59 WRITEVALUETOFILE(value); // 'version' of this save-format
60 value = (int)m_entryCache.size();
61 WRITEVALUETOFILE(value); // size of the cache map
62 // now iterate through the maps and save every entry.
63 for (CacheEntryMap::iterator I = m_entryCache.begin(); I != m_entryCache.end(); ++I)
65 const CString& key = I->first;
66 value = key.GetLength();
67 WRITEVALUETOFILE(value);
68 if (value)
70 if (fwrite((LPCTSTR)key, sizeof(TCHAR), value, pFile)!=value)
71 return false;
72 if (!I->second.SaveToDisk(pFile))
73 return false;
76 value = (int)m_childDirectories.size();
77 WRITEVALUETOFILE(value);
78 for (ChildDirStatus::iterator I = m_childDirectories.begin(); I != m_childDirectories.end(); ++I)
80 const CString& path = I->first.GetWinPathString();
81 value = path.GetLength();
82 WRITEVALUETOFILE(value);
83 if (value)
85 if (fwrite((LPCTSTR)path, sizeof(TCHAR), value, pFile)!=value)
86 return false;
87 git_wc_status_kind status = I->second;
88 WRITEVALUETOFILE(status);
91 WRITEVALUETOFILE(m_indexFileTime);
92 // WRITEVALUETOFILE(m_propsFileTime);
93 value = m_directoryPath.GetWinPathString().GetLength();
94 WRITEVALUETOFILE(value);
95 if (value)
97 if (fwrite(m_directoryPath.GetWinPath(), sizeof(TCHAR), value, pFile)!=value)
98 return false;
100 if (!m_ownStatus.SaveToDisk(pFile))
101 return false;
102 WRITEVALUETOFILE(m_currentFullStatus);
103 WRITEVALUETOFILE(m_mostImportantFileStatus);
104 return true;
107 BOOL CCachedDirectory::LoadFromDisk(FILE * pFile)
109 AutoLocker lock(m_critSec);
110 #define LOADVALUEFROMFILE(x) if (fread(&x, sizeof(x), 1, pFile)!=1) return false;
113 unsigned int value = 0;
114 LOADVALUEFROMFILE(value);
115 if (value != 1)
116 return false; // not the correct version
117 int mapsize = 0;
118 LOADVALUEFROMFILE(mapsize);
119 for (int i=0; i<mapsize; ++i)
121 LOADVALUEFROMFILE(value);
122 if (value > MAX_PATH)
123 return false;
124 if (value)
126 CString sKey;
127 if (fread(sKey.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
129 sKey.ReleaseBuffer(0);
130 return false;
132 sKey.ReleaseBuffer(value);
133 CStatusCacheEntry entry;
134 if (!entry.LoadFromDisk(pFile))
135 return false;
136 m_entryCache[sKey] = entry;
139 LOADVALUEFROMFILE(mapsize);
140 for (int i=0; i<mapsize; ++i)
142 LOADVALUEFROMFILE(value);
143 if (value > MAX_PATH)
144 return false;
145 if (value)
147 CString sPath;
148 if (fread(sPath.GetBuffer(value), sizeof(TCHAR), value, pFile)!=value)
150 sPath.ReleaseBuffer(0);
151 return false;
153 sPath.ReleaseBuffer(value);
154 git_wc_status_kind status;
155 LOADVALUEFROMFILE(status);
156 m_childDirectories[CTGitPath(sPath)] = status;
159 LOADVALUEFROMFILE(m_indexFileTime);
160 // LOADVALUEFROMFILE(m_propsFileTime);
161 LOADVALUEFROMFILE(value);
162 if (value > MAX_PATH)
163 return false;
164 if (value)
166 CString sPath;
167 if (fread(sPath.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
169 sPath.ReleaseBuffer(0);
170 return false;
172 sPath.ReleaseBuffer(value);
173 m_directoryPath.SetFromWin(sPath);
175 if (!m_ownStatus.LoadFromDisk(pFile))
176 return false;
178 LOADVALUEFROMFILE(m_currentFullStatus);
179 LOADVALUEFROMFILE(m_mostImportantFileStatus);
181 catch ( CAtlException )
183 return false;
185 return true;
189 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive, bool bFetch /* = true */)
191 CString strCacheKey;
192 bool bThisDirectoryIsUnversioned = false;
193 bool bRequestForSelf = false;
194 if(path.IsEquivalentToWithoutCase(m_directoryPath))
196 bRequestForSelf = true;
198 //OutputDebugStringA("GetStatusForMember: ");OutputDebugStringW(path.GetWinPathString());OutputDebugStringA("\r\n");
199 // In all most circumstances, we ask for the status of a member of this directory.
200 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
202 CString sProjectRoot;
203 const BOOL bIsVersionedPath = m_directoryPath.HasAdminDir(&sProjectRoot);
205 // Check if the index file has been changed
206 CTGitPath indexFilePath(bIsVersionedPath ? sProjectRoot : m_directoryPath);
207 // CTGitPath propsDirPath(m_directoryPath);
208 if (g_GitAdminDir.IsVSNETHackActive())
210 indexFilePath.AppendPathString(g_GitAdminDir.GetVSNETAdminDirName() + _T("\\index"));
211 // propsDirPath.AppendPathString(g_GitAdminDir.GetVSNETAdminDirName() + _T("\\dir-props"));
213 else
215 indexFilePath.AppendPathString(g_GitAdminDir.GetAdminDirName() + _T("\\index"));
216 // propsDirPath.AppendPathString(g_GitAdminDir.GetAdminDirName() + _T("\\dir-props"));
218 if ( (m_indexFileTime == indexFilePath.GetLastWriteTime()) /*&& ((indexFilePath.GetLastWriteTime() == 0) || (m_propsFileTime == propsDirPath.GetLastWriteTime()))*/ )
220 // m_indexFileTime = indexFilePath.GetLastWriteTime();
221 // if (m_indexFileTime)
222 // m_propsFileTime = propsDirPath.GetLastWriteTime();
224 //if(m_indexFileTime == 0)
225 // a newly created project (without commits) has no index file but we still want it to count as versioned
226 if(m_indexFileTime == 0 && !bIsVersionedPath)
228 // We are a folder which is not in a working copy
229 bThisDirectoryIsUnversioned = true;
230 m_ownStatus.SetStatus(NULL);
232 // If a user removes the .git directory, we get here with m_entryCache
233 // not being empty, but still us being unversioned
234 if (!m_entryCache.empty())
236 m_entryCache.clear();
238 ATLASSERT(m_entryCache.empty());
240 // However, a member *DIRECTORY* might be the top of WC
241 // so we need to ask them to get their own status
242 if(!path.IsDirectory())
244 if ((PathFileExists(path.GetWinPath()))||(bRequestForSelf))
245 return CStatusCacheEntry();
246 // the entry doesn't exist anymore!
247 // but we can't remove it from the cache here:
248 // the GetStatusForMember() method is called only with a read
249 // lock and not a write lock!
250 // So mark it for crawling, and let the crawler remove it
251 // later
252 CGitStatusCache::Instance().AddFolderForCrawling(path.GetContainingDirectory());
254 return CStatusCacheEntry();
256 else
258 // If we're in the special case of a directory being asked for its own status
259 // and this directory is unversioned, then we should just return that here
260 if(bRequestForSelf)
261 return CStatusCacheEntry();
265 if(path.IsDirectory())
267 // We don't have directory status in our cache
268 // Ask the directory if it knows its own status
269 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
270 if ((dirEntry)&&(dirEntry->IsOwnStatusValid()))
272 // To keep recursive status up to date, we'll request that children are all crawled again
273 // This will be very quick if nothings changed, because it will all be cache hits
274 if (bRecursive)
276 AutoLocker lock(dirEntry->m_critSec);
277 ChildDirStatus::const_iterator it;
278 for(it = dirEntry->m_childDirectories.begin(); it != dirEntry->m_childDirectories.end(); ++it)
280 CGitStatusCache::Instance().AddFolderForCrawling(it->first);
283 return dirEntry->GetOwnStatus(bRecursive);
286 else
289 // if we currently are fetching the status of the directory
290 // we want the status for, we just return an empty entry here
291 // and don't wait for that fetching to finish.
292 // That's because fetching the status can take a *really* long
293 // time (e.g. if a commit is also in progress on that same
294 // directory), and we don't want to make the explorer appear
295 // to hang.
296 AutoLocker pathlock(m_critSecPath);
297 if ((!bFetch)&&(!m_currentStatusFetchingPath.IsEmpty()))
299 if ((m_currentStatusFetchingPath.IsAncestorOf(path))&&((m_currentStatusFetchingPathTicks + 1000)<GetTickCount()))
301 ATLTRACE(_T("returning empty status (status fetch in progress) for %s\n"), path.GetWinPath());
302 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
303 return CStatusCacheEntry();
307 // Look up a file in our own cache
308 AutoLocker lock(m_critSec);
309 strCacheKey = GetCacheKey(path);
310 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
311 if(itMap != m_entryCache.end())
313 // We've hit the cache - check for timeout
314 if(!itMap->second.HasExpired((long)GetTickCount()))
316 if(itMap->second.DoesFileTimeMatch(path.GetLastWriteTime()))
318 if ((itMap->second.GetEffectiveStatus()!=git_wc_status_missing)||(!PathFileExists(path.GetWinPath())))
320 // Note: the filetime matches after a modified has been committed too.
321 // So in that case, we would return a wrong status (e.g. 'modified' instead
322 // of 'normal') here.
323 return itMap->second;
330 else
332 AutoLocker pathlock(m_critSecPath);
333 if ((!bFetch)&&(!m_currentStatusFetchingPath.IsEmpty()))
335 if ((m_currentStatusFetchingPath.IsAncestorOf(path))&&((m_currentStatusFetchingPathTicks + 1000)<GetTickCount()))
337 ATLTRACE(_T("returning empty status (status fetch in progress) for %s\n"), path.GetWinPath());
338 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
339 return CStatusCacheEntry();
342 // if we're fetching the status for the explorer,
343 // we don't refresh the status but use the one
344 // we already have (to save time and make the explorer
345 // more responsive in stress conditions).
346 // We leave the refreshing to the crawler.
347 if ((!bFetch)&&(m_indexFileTime))
349 CGitStatusCache::Instance().AddFolderForCrawling(path.GetDirectory());
350 return CStatusCacheEntry();
352 AutoLocker lock(m_critSec);
353 m_indexFileTime = indexFilePath.GetLastWriteTime();
354 // m_propsFileTime = propsDirPath.GetLastWriteTime();
355 m_entryCache.clear();
356 strCacheKey = GetCacheKey(path);
359 // svn_opt_revision_t revision;
360 // revision.kind = svn_opt_revision_unspecified;
362 // We've not got this item in the cache - let's add it
363 // We never bother asking SVN for the status of just one file, always for its containing directory
365 if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))
367 // We're being asked for the status of an .git directory
368 // It's not worth asking for this
369 return CStatusCacheEntry();
374 AutoLocker pathlock(m_critSecPath);
375 if ((!bFetch)&&(!m_currentStatusFetchingPath.IsEmpty()))
377 if ((m_currentStatusFetchingPath.IsAncestorOf(path))&&((m_currentStatusFetchingPathTicks + 1000)<GetTickCount()))
379 ATLTRACE(_T("returning empty status (status fetch in progress) for %s\n"), path.GetWinPath());
380 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
381 return CStatusCacheEntry();
385 // SVNPool subPool(CGitStatusCache::Instance().m_svnHelp.Pool());
387 AutoLocker lock(m_critSec);
388 m_mostImportantFileStatus = git_wc_status_none;
389 m_childDirectories.clear();
390 m_entryCache.clear();
391 m_ownStatus.SetStatus(NULL);
392 m_bRecursive = bRecursive;
394 if(!bThisDirectoryIsUnversioned)
397 AutoLocker pathlock(m_critSecPath);
398 m_currentStatusFetchingPath = m_directoryPath;
399 m_currentStatusFetchingPathTicks = GetTickCount();
401 ATLTRACE(_T("git_enum_files for '%s' (req %s)\n"), m_directoryPath.GetWinPath(), path.GetWinPath());
403 CString sProjectRoot;
404 m_directoryPath.HasAdminDir(&sProjectRoot);
405 ATLASSERT( !m_directoryPath.IsEmpty() );
407 LPCTSTR lpszSubPath = NULL;
408 CString sSubPath;
409 CString s = m_directoryPath.GetDirectory().GetWinPathString();
410 if (s.GetLength() > sProjectRoot.GetLength())
412 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() - 1/*otherwise it gets initial slash*/);
413 lpszSubPath = sSubPath;
415 //MessageBoxA(NULL, CStringA(sProjectRoot), sSubPath, MB_OK);
416 //OutputDebugStringA("###");OutputDebugStringW(sProjectRoot);OutputDebugStringA(" - ");OutputDebugStringA(sSubPath);OutputDebugStringA("\r\n");
417 BOOL pErr = !wgEnumFiles(sProjectRoot, lpszSubPath, WGEFF_NoRecurse|WGEFF_FullPath, &GetStatusCallback, this);
419 /*git_error_t* pErr = svn_client_status4 (
420 NULL,
421 m_directoryPath.GetSVNApiPath(subPool),
422 &revision,
423 GetStatusCallback,
424 this,
425 svn_depth_immediates,
426 TRUE, //getall
427 FALSE,
428 TRUE, //noignore
429 FALSE, //ignore externals
430 NULL, //changelists
431 CGitStatusCache::Instance().m_svnHelp.ClientContext(),
432 subPool
433 );*/
435 AutoLocker pathlock(m_critSecPath);
436 m_currentStatusFetchingPath.Reset();
438 ATLTRACE(_T("git_enum_files finished for '%s'\n"), m_directoryPath.GetWinPath(), path.GetWinPath());
439 if(pErr)
441 // Handle an error
442 // The most likely error on a folder is that it's not part of a WC
443 // In most circumstances, this will have been caught earlier,
444 // but in some situations, we'll get this error.
445 // If we allow ourselves to fall on through, then folders will be asked
446 // for their own status, and will set themselves as unversioned, for the
447 // benefit of future requests
448 // ATLTRACE("git_enum_files err: '%s'\n", pErr->message);
449 // svn_error_clear(pErr);
450 // No assert here! Since we _can_ get here, an assertion is not an option!
451 // Reasons to get here:
452 // - renaming a folder with many sub folders --> results in "not a working copy" if the revert
453 // happens between our checks and the svn_client_status() call.
454 // - reverting a move/copy --> results in "not a working copy" (as above)
455 if (!m_directoryPath.HasAdminDir())
457 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
458 return CStatusCacheEntry();
460 else
462 ATLTRACE("git_enum_files error, assume none status\n");
463 // Since we only assume a none status here due to svn_client_status()
464 // returning an error, make sure that this status times out soon.
465 CGitStatusCache::Instance().m_folderCrawler.BlockPath(m_directoryPath, 2000);
466 CGitStatusCache::Instance().AddFolderForCrawling(m_directoryPath);
467 return CStatusCacheEntry();
471 else
473 ATLTRACE("Skipped git status for unversioned folder\n");
476 // Now that we've refreshed our SVN status, we can see if it's
477 // changed the 'most important' status value for this directory.
478 // If it has, then we should tell our parent
479 UpdateCurrentStatus();
481 if (path.IsDirectory())
483 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
484 if ((dirEntry)&&(dirEntry->IsOwnStatusValid()))
486 CGitStatusCache::Instance().AddFolderForCrawling(path);
487 return dirEntry->GetOwnStatus(bRecursive);
490 // If the status *still* isn't valid here, it means that
491 // the current directory is unversioned, and we shall need to ask its children for info about themselves
492 if (dirEntry)
493 return dirEntry->GetStatusForMember(path,bRecursive);
494 CGitStatusCache::Instance().AddFolderForCrawling(path);
495 return CStatusCacheEntry();
497 else
499 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
500 if(itMap != m_entryCache.end())
502 return itMap->second;
506 AddEntry(path, NULL);
507 return CStatusCacheEntry();
510 void
511 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
513 AutoLocker lock(m_critSec);
514 if(path.IsDirectory())
516 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
517 if (childDir)
519 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
520 childDir->m_ownStatus.SetStatus(pGitStatus);
521 childDir->m_ownStatus.SetKind(git_node_dir);
524 else
526 CString cachekey = GetCacheKey(path);
527 CacheEntryMap::iterator entry_it = m_entryCache.lower_bound(cachekey);
528 if (entry_it != m_entryCache.end() && entry_it->first == cachekey)
530 if (pGitStatus)
532 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
533 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
535 CGitStatusCache::Instance().UpdateShell(path);
536 ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
540 else
542 entry_it = m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
544 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
545 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
546 entry_it->second.SetKind(git_node_file);
551 CString
552 CCachedDirectory::GetCacheKey(const CTGitPath& path)
554 // All we put into the cache as a key is just the end portion of the pathname
555 // There's no point storing the path of the containing directory for every item
556 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength());
559 CString
560 CCachedDirectory::GetFullPathString(const CString& cacheKey)
562 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
565 BOOL CCachedDirectory::GetStatusCallback(const struct wgFile_s *pFile, void *pUserData)
567 CCachedDirectory* pThis = (CCachedDirectory*)pUserData;
569 const TCHAR *path = pFile->sFileName;
571 if (path == NULL)
572 return FALSE;
574 git_wc_status2_t _status;
575 git_wc_status2_t *status = &_status;
577 if ((pFile->nFlags & WGFF_Directory) && pFile->nStatus == WGFS_Unknown)
578 status->prop_status = status->text_status = git_wc_status_incomplete;
579 else
580 status->prop_status = status->text_status = GitStatusFromWingit(pFile->nStatus);
581 //if (pFile->nStatus > WGFS_Normal) {CStringA s; s.Format("==>%s %d\r\n",pFile->sFileName,pFile->nStatus); OutputDebugStringA(s);}
582 CTGitPath svnPath;
584 // if(status->entry)
586 //if ((status->text_status != git_wc_status_none)&&(status->text_status != git_wc_status_missing))
587 svnPath.SetFromGit(path, pFile->nFlags & WGFF_Directory);
588 /*else
589 svnPath.SetFromGit(path);*/
591 if (pFile->nFlags & WGFF_Directory)
593 if ( !svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
595 if (pThis->m_bRecursive)
597 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
598 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
599 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
602 // Make sure we know about this child directory
603 // This initial status value is likely to be overwritten from below at some point
604 git_wc_status_kind s = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
605 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(svnPath);
606 if (cdir)
608 // This child directory is already in our cache!
609 // So ask this dir about its recursive status
610 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
611 AutoLocker lock(pThis->m_critSec);
612 pThis->m_childDirectories[svnPath] = st;
614 else
616 // the child directory is not in the cache. Create a new entry for it in the cache which is
617 // initially 'unversioned'. But we added that directory to the crawling list above, which
618 // means the cache will be updated soon.
619 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
620 AutoLocker lock(pThis->m_critSec);
621 pThis->m_childDirectories[svnPath] = s;
625 else
627 // Keep track of the most important status of all the files in this directory
628 // Don't include subdirectories in this figure, because they need to provide their
629 // own 'most important' value
630 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->text_status);
631 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->prop_status);
632 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_none))
633 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
635 // treat unversioned files as modified
636 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
637 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
641 #if 0
642 else
644 svnPath.SetFromGit(path);
645 // Subversion returns no 'entry' field for versioned folders if they're
646 // part of another working copy (nested layouts).
647 // So we have to make sure that such an 'unversioned' folder really
648 // is unversioned.
649 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_missing))&&(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))&&(svnPath.IsDirectory()))
651 if (svnPath.HasAdminDir())
653 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
654 // Mark the directory as 'versioned' (status 'normal' for now).
655 // This initial value will be overwritten from below some time later
657 AutoLocker lock(pThis->m_critSec);
658 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
660 // Make sure the entry is also in the cache
661 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
662 // also mark the status in the status object as normal
663 status->text_status = git_wc_status_normal;
666 else if (status->text_status == git_wc_status_external)
668 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
669 // Mark the directory as 'versioned' (status 'normal' for now).
670 // This initial value will be overwritten from below some time later
672 AutoLocker lock(pThis->m_critSec);
673 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
675 // we have added a directory to the child-directory list of this
676 // directory. We now must make sure that this directory also has
677 // an entry in the cache.
678 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
679 // also mark the status in the status object as normal
680 status->text_status = git_wc_status_normal;
682 else
684 if (svnPath.IsDirectory())
686 AutoLocker lock(pThis->m_critSec);
687 pThis->m_childDirectories[svnPath] = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
689 else if ((CGitStatusCache::Instance().IsUnversionedAsModified())&&(status->text_status != git_wc_status_missing))
691 // make this unversioned item change the most important status of this
692 // folder to modified if it doesn't already have another status
693 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
694 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
698 #endif
700 pThis->AddEntry(svnPath, status);
702 return FALSE;
705 #if 0
706 git_error_t * CCachedDirectory::GetStatusCallback(void *baton, const char *path, git_wc_status2_t *status)
708 CCachedDirectory* pThis = (CCachedDirectory*)baton;
710 if (path == NULL)
711 return 0;
713 CTGitPath svnPath;
715 if(status->entry)
717 if ((status->text_status != git_wc_status_none)&&(status->text_status != git_wc_status_missing))
718 svnPath.SetFromSVN(path, (status->entry->kind == svn_node_dir));
719 else
720 svnPath.SetFromSVN(path);
722 if(svnPath.IsDirectory())
724 if(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))
726 if (pThis->m_bRecursive)
728 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
729 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
732 // Make sure we know about this child directory
733 // This initial status value is likely to be overwritten from below at some point
734 git_wc_status_kind s = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
735 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(svnPath);
736 if (cdir)
738 // This child directory is already in our cache!
739 // So ask this dir about its recursive status
740 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
741 AutoLocker lock(pThis->m_critSec);
742 pThis->m_childDirectories[svnPath] = st;
744 else
746 // the child directory is not in the cache. Create a new entry for it in the cache which is
747 // initially 'unversioned'. But we added that directory to the crawling list above, which
748 // means the cache will be updated soon.
749 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
750 AutoLocker lock(pThis->m_critSec);
751 pThis->m_childDirectories[svnPath] = s;
755 else
757 // Keep track of the most important status of all the files in this directory
758 // Don't include subdirectories in this figure, because they need to provide their
759 // own 'most important' value
760 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->text_status);
761 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->prop_status);
762 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_none))
763 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
765 // treat unversioned files as modified
766 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
767 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
771 else
773 svnPath.SetFromSVN(path);
774 // Subversion returns no 'entry' field for versioned folders if they're
775 // part of another working copy (nested layouts).
776 // So we have to make sure that such an 'unversioned' folder really
777 // is unversioned.
778 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_missing))&&(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))&&(svnPath.IsDirectory()))
780 if (svnPath.HasAdminDir())
782 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
783 // Mark the directory as 'versioned' (status 'normal' for now).
784 // This initial value will be overwritten from below some time later
786 AutoLocker lock(pThis->m_critSec);
787 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
789 // Make sure the entry is also in the cache
790 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
791 // also mark the status in the status object as normal
792 status->text_status = git_wc_status_normal;
795 else if (status->text_status == git_wc_status_external)
797 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
798 // Mark the directory as 'versioned' (status 'normal' for now).
799 // This initial value will be overwritten from below some time later
801 AutoLocker lock(pThis->m_critSec);
802 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
804 // we have added a directory to the child-directory list of this
805 // directory. We now must make sure that this directory also has
806 // an entry in the cache.
807 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
808 // also mark the status in the status object as normal
809 status->text_status = git_wc_status_normal;
811 else
813 if (svnPath.IsDirectory())
815 AutoLocker lock(pThis->m_critSec);
816 pThis->m_childDirectories[svnPath] = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
818 else if ((CGitStatusCache::Instance().IsUnversionedAsModified())&&(status->text_status != git_wc_status_missing))
820 // make this unversioned item change the most important status of this
821 // folder to modified if it doesn't already have another status
822 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
823 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
828 pThis->AddEntry(svnPath, status);
830 return 0;
832 #endif
834 bool
835 CCachedDirectory::IsOwnStatusValid() const
837 return m_ownStatus.HasBeenSet() &&
838 !m_ownStatus.HasExpired(GetTickCount()) &&
839 // 'external' isn't a valid status. That just
840 // means the folder is not part of the current working
841 // copy but it still has its own 'real' status
842 m_ownStatus.GetEffectiveStatus()!=git_wc_status_external &&
843 m_ownStatus.IsKindKnown();
846 void CCachedDirectory::Invalidate()
848 m_ownStatus.Invalidate();
851 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
853 // Combine our OWN folder status with the most important of our *FILES'* status.
854 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
856 // NOTE: TSVN marks dir as modified if it contains added/deleted/missing files, but we prefer the most important
857 // status to propagate upward in its original state
858 /*if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))
860 if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))
861 retVal = git_wc_status_modified;
864 // Now combine all our child-directorie's status
866 AutoLocker lock(m_critSec);
867 ChildDirStatus::const_iterator it;
868 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
870 retVal = GitStatus::GetMoreImportant(retVal, it->second);
871 /*if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))
873 if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))
874 retVal = git_wc_status_modified;
878 return retVal;
881 // Update our composite status and deal with things if it's changed
882 void CCachedDirectory::UpdateCurrentStatus()
884 git_wc_status_kind newStatus = CalculateRecursiveStatus();
886 if ((newStatus != m_currentFullStatus)&&(m_ownStatus.IsVersioned()))
888 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
890 // Our status has changed - tell the shell
891 ATLTRACE(_T("Dir %s, status change from %d to %d, send shell notification\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
892 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
894 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
895 m_currentFullStatus = newStatus;
896 else
897 m_currentFullStatus = git_wc_status_missing;
899 // And tell our parent, if we've got one...
900 // we tell our parent *always* about our status, even if it hasn't
901 // changed. This is to make sure that the parent has really our current
902 // status - the parent can decide itself if our status has changed
903 // or not.
904 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
905 if(!parentPath.IsEmpty())
907 // We have a parent
908 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
909 if (cachedDir)
910 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
915 // Receive a notification from a child that its status has changed
916 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
918 git_wc_status_kind currentStatus = git_wc_status_none;
920 AutoLocker lock(m_critSec);
921 currentStatus = m_childDirectories[childDir];
923 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
926 AutoLocker lock(m_critSec);
927 m_childDirectories[childDir] = childStatus;
929 UpdateCurrentStatus();
933 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
935 // Don't return recursive status if we're unversioned ourselves.
936 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned)
938 CStatusCacheEntry recursiveStatus(m_ownStatus);
939 UpdateCurrentStatus();
940 recursiveStatus.ForceStatus(m_currentFullStatus);
941 return recursiveStatus;
943 else
945 return m_ownStatus;
949 void CCachedDirectory::RefreshStatus(bool bRecursive)
951 // Make sure that our own status is up-to-date
952 GetStatusForMember(m_directoryPath,bRecursive);
954 AutoLocker lock(m_critSec);
955 // We also need to check if all our file members have the right date on them
956 CacheEntryMap::iterator itMembers;
957 std::set<CTGitPath> refreshedpaths;
958 DWORD now = GetTickCount();
959 if (m_entryCache.size() == 0)
960 return;
961 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
963 if (itMembers->first)
965 CTGitPath filePath(m_directoryPath);
966 filePath.AppendPathString(itMembers->first);
967 std::set<CTGitPath>::iterator refr_it;
968 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
969 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
971 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
973 lock.Unlock();
974 // We need to request this item as well
975 GetStatusForMember(filePath,bRecursive);
976 // GetStatusForMember now has recreated the m_entryCache map.
977 // So start the loop again, but add this path to the refreshed paths set
978 // to make sure we don't refresh this path again. This is to make sure
979 // that we don't end up in an endless loop.
980 lock.Lock();
981 refreshedpaths.insert(refr_it, filePath);
982 itMembers = m_entryCache.begin();
983 if (m_entryCache.size()==0)
984 return;
985 continue;
987 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
989 // crawl all sub folders too! Otherwise a change deep inside the
990 // tree which has changed won't get propagated up the tree.
991 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
998 void CCachedDirectory::RefreshMostImportant()
1000 CacheEntryMap::iterator itMembers;
1001 git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();
1002 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
1004 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
1005 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
1006 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
1008 // treat unversioned files as modified
1009 if (newStatus != git_wc_status_added)
1010 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
1013 if (newStatus != m_mostImportantFileStatus)
1015 ATLTRACE(_T("status change of path %s\n"), m_directoryPath.GetWinPath());
1016 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
1018 m_mostImportantFileStatus = newStatus;