Do not use GitAdminDir objects
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob3b6402e5e7b94ad3030193928dbd5b8bbdc49831
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
4 // Copyright (C) 2008-2015 - 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 "CachedDirectory.h"
22 #include "GitStatusCache.h"
23 #include "GitStatus.h"
24 #include <set>
26 CCachedDirectory::CCachedDirectory(void)
28 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
29 m_bRecursive = true;
32 CCachedDirectory::~CCachedDirectory(void)
36 CCachedDirectory::CCachedDirectory(const CTGitPath& directoryPath)
38 ATLASSERT(directoryPath.IsDirectory() || !PathFileExists(directoryPath.GetWinPath()));
40 m_directoryPath = directoryPath;
41 m_directoryPath.GetGitPathString(); // make sure git path string is set
43 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
44 m_bRecursive = true;
47 BOOL CCachedDirectory::SaveToDisk(FILE * pFile)
49 AutoLocker lock(m_critSec);
50 #define WRITEVALUETOFILE(x) if (fwrite(&x, sizeof(x), 1, pFile)!=1) return false;
52 unsigned int value = GIT_CACHE_VERSION;
53 WRITEVALUETOFILE(value); // 'version' of this save-format
54 value = (int)m_entryCache.size();
55 WRITEVALUETOFILE(value); // size of the cache map
56 // now iterate through the maps and save every entry.
57 for (CacheEntryMap::iterator I = m_entryCache.begin(); I != m_entryCache.end(); ++I)
59 const CString& key = I->first;
60 value = key.GetLength();
61 WRITEVALUETOFILE(value);
62 if (value)
64 if (fwrite((LPCTSTR)key, sizeof(TCHAR), value, pFile)!=value)
65 return false;
66 if (!I->second.SaveToDisk(pFile))
67 return false;
70 value = (int)m_childDirectories.size();
71 WRITEVALUETOFILE(value);
72 for (ChildDirStatus::iterator I = m_childDirectories.begin(); I != m_childDirectories.end(); ++I)
74 const CString& path = I->first.GetWinPathString();
75 value = path.GetLength();
76 WRITEVALUETOFILE(value);
77 if (value)
79 if (fwrite((LPCTSTR)path, sizeof(TCHAR), value, pFile)!=value)
80 return false;
81 git_wc_status_kind status = I->second;
82 WRITEVALUETOFILE(status);
85 // WRITEVALUETOFILE(m_propsFileTime);
86 value = m_directoryPath.GetWinPathString().GetLength();
87 WRITEVALUETOFILE(value);
88 if (value)
90 if (fwrite(m_directoryPath.GetWinPath(), sizeof(TCHAR), value, pFile)!=value)
91 return false;
93 if (!m_ownStatus.SaveToDisk(pFile))
94 return false;
95 WRITEVALUETOFILE(m_currentFullStatus);
96 WRITEVALUETOFILE(m_mostImportantFileStatus);
97 return true;
100 BOOL CCachedDirectory::LoadFromDisk(FILE * pFile)
102 AutoLocker lock(m_critSec);
103 #define LOADVALUEFROMFILE(x) if (fread(&x, sizeof(x), 1, pFile)!=1) return false;
106 unsigned int value = 0;
107 LOADVALUEFROMFILE(value);
108 if (value != GIT_CACHE_VERSION)
109 return false; // not the correct version
110 int mapsize = 0;
111 LOADVALUEFROMFILE(mapsize);
112 for (int i=0; i<mapsize; ++i)
114 LOADVALUEFROMFILE(value);
115 if (value > MAX_PATH)
116 return false;
117 if (value)
119 CString sKey;
120 if (fread(sKey.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
122 sKey.ReleaseBuffer(0);
123 return false;
125 sKey.ReleaseBuffer(value);
126 CStatusCacheEntry entry;
127 if (!entry.LoadFromDisk(pFile))
128 return false;
129 // only read non empty keys (just needed for transition from old TGit clients)
130 if (!sKey.IsEmpty())
131 m_entryCache[sKey] = entry;
134 LOADVALUEFROMFILE(mapsize);
135 for (int i=0; i<mapsize; ++i)
137 LOADVALUEFROMFILE(value);
138 if (value > MAX_PATH)
139 return false;
140 if (value)
142 CString sPath;
143 if (fread(sPath.GetBuffer(value), sizeof(TCHAR), value, pFile)!=value)
145 sPath.ReleaseBuffer(0);
146 return false;
148 sPath.ReleaseBuffer(value);
149 git_wc_status_kind status;
150 LOADVALUEFROMFILE(status);
151 m_childDirectories[CTGitPath(sPath)] = status;
154 LOADVALUEFROMFILE(value);
155 if (value > MAX_PATH)
156 return false;
157 if (value)
159 CString sPath;
160 if (fread(sPath.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
162 sPath.ReleaseBuffer(0);
163 return false;
165 sPath.ReleaseBuffer(value);
166 // make sure paths do not end with backslash (just needed for transition from old TGit clients)
167 if (sPath.GetLength() > 3 && sPath[sPath.GetLength() - 1] == _T('\\'))
168 sPath.TrimRight(_T("\\"));
169 m_directoryPath.SetFromWin(sPath);
170 m_directoryPath.GetGitPathString(); // make sure git path string is set
172 if (!m_ownStatus.LoadFromDisk(pFile))
173 return false;
175 LOADVALUEFROMFILE(m_currentFullStatus);
176 LOADVALUEFROMFILE(m_mostImportantFileStatus);
178 catch ( CAtlException )
180 return false;
182 return true;
187 CStatusCacheEntry CCachedDirectory::GetStatusFromCache(const CTGitPath& path, bool bRecursive)
189 if(path.IsDirectory())
191 // We don't have directory status in our cache
192 // Ask the directory if it knows its own status
193 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
194 if( dirEntry)
196 if (dirEntry->IsOwnStatusValid())
197 return dirEntry->GetOwnStatus(bRecursive);
198 else
200 /* cache have outof date, need crawl again*/
202 /*AutoLocker lock(dirEntry->m_critSec);
203 ChildDirStatus::const_iterator it;
204 for(it = dirEntry->m_childDirectories.begin(); it != dirEntry->m_childDirectories.end(); ++it)
206 CGitStatusCache::Instance().AddFolderForCrawling(it->first);
209 CGitStatusCache::Instance().AddFolderForCrawling(path);
211 /*Return old status during crawling*/
212 return dirEntry->GetOwnStatus(bRecursive);
215 else
217 CGitStatusCache::Instance().AddFolderForCrawling(path);
219 return CStatusCacheEntry();
221 else
223 //All file ignored if under ignore directory
224 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_ignored)
225 return CStatusCacheEntry(git_wc_status_ignored);
226 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_unversioned)
227 return CStatusCacheEntry(git_wc_status_unversioned);
229 // Look up a file in our own cache
230 AutoLocker lock(m_critSec);
231 CString strCacheKey = GetCacheKey(path);
232 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
233 if(itMap != m_entryCache.end())
235 // We've hit the cache - check for timeout
236 if(!itMap->second.HasExpired((long)GetTickCount()))
238 if(itMap->second.DoesFileTimeMatch(path.GetLastWriteTime()))
240 if ((itMap->second.GetEffectiveStatus()!=git_wc_status_missing)||(!PathFileExists(path.GetWinPath())))
242 // Note: the filetime matches after a modified has been committed too.
243 // So in that case, we would return a wrong status (e.g. 'modified' instead
244 // of 'normal') here.
245 return itMap->second;
251 CGitStatusCache::Instance().AddFolderForCrawling(path.GetContainingDirectory());
252 return CStatusCacheEntry();
257 CStatusCacheEntry CCachedDirectory::GetStatusFromGit(const CTGitPath &path, CString sProjectRoot)
259 CString subpaths = path.GetGitPathString();
260 if(subpaths.GetLength() >= sProjectRoot.GetLength())
262 if(subpaths[sProjectRoot.GetLength()] == _T('/'))
263 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength()-1);
264 else
265 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength());
268 GitStatus *pGitStatus = &CGitStatusCache::Instance().m_GitStatus;
269 UNREFERENCED_PARAMETER(pGitStatus);
271 bool isVersion =true;
272 pGitStatus->IsUnderVersionControl(sProjectRoot, subpaths, path.IsDirectory(), &isVersion);
273 if(!isVersion)
274 { //untracked file
275 bool isDir = path.IsDirectory();
276 bool isIgnoreFileChanged = pGitStatus->HasIgnoreFilesChanged(sProjectRoot, subpaths, isDir);
278 if( isIgnoreFileChanged)
280 pGitStatus->LoadIgnoreFile(sProjectRoot, subpaths, isDir);
283 if (isDir)
286 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path,
287 false); /* we needn't watch untracked directory*/
289 if(dirEntry)
291 AutoLocker lock(dirEntry->m_critSec);
293 git_wc_status_kind dirstatus = dirEntry->GetCurrentFullStatus() ;
294 if (CGitStatusCache::Instance().IsUnversionedAsModified() || dirstatus == git_wc_status_none || dirstatus >= git_wc_status_normal || isIgnoreFileChanged)
295 {/* status have not initialized*/
296 bool isignore = false;
297 pGitStatus->IsIgnore(sProjectRoot, subpaths, &isignore, isDir);
299 if (!isignore && CGitStatusCache::Instance().IsUnversionedAsModified())
301 dirEntry->EnumFiles(path, TRUE);
302 dirEntry->UpdateCurrentStatus();
303 return CStatusCacheEntry(dirEntry->GetCurrentFullStatus());
306 git_wc_status2_t status2;
307 status2.text_status = status2.prop_status =
308 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
310 // we do not know anything about files here, all we know is that there are not versioned files in this dir
311 dirEntry->m_mostImportantFileStatus = git_wc_status_none;
312 dirEntry->m_ownStatus.SetKind(git_node_dir);
313 dirEntry->m_ownStatus.SetStatus(&status2);
314 dirEntry->m_currentFullStatus = status2.text_status;
316 return dirEntry->m_ownStatus;
320 else /* path is file */
322 AutoLocker lock(m_critSec);
323 CString strCacheKey = GetCacheKey(path);
325 if (strCacheKey.IsEmpty())
326 return CStatusCacheEntry();
328 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
329 if(itMap == m_entryCache.end() || isIgnoreFileChanged)
331 git_wc_status2_t status2;
332 bool isignore = false;
333 pGitStatus->IsIgnore(sProjectRoot, subpaths, &isignore, isDir);
334 status2.text_status = status2.prop_status =
335 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
336 AddEntry(path, &status2);
337 return m_entryCache[strCacheKey];
339 else
341 return itMap->second;
344 return CStatusCacheEntry();
347 else
349 EnumFiles(path, TRUE);
350 UpdateCurrentStatus();
351 if (!path.IsDirectory())
352 return GetCacheStatusForMember(path);
353 return CStatusCacheEntry(m_ownStatus);
358 /// bFetch is true, fetch all status, call by crawl.
359 /// bFetch is false, get cache status, return quickly.
361 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive, bool bFetch /* = true */)
363 CString sProjectRoot;
364 bool bIsVersionedPath;
366 bool bRequestForSelf = false;
367 if(path.IsEquivalentToWithoutCase(m_directoryPath))
369 bRequestForSelf = true;
370 AutoLocker lock(m_critSec);
371 // HasAdminDir might modify m_directoryPath, so we need to do it synchronized
372 bIsVersionedPath = m_directoryPath.HasAdminDir(&sProjectRoot);
374 else
375 bIsVersionedPath = path.HasAdminDir(&sProjectRoot);
377 // In all most circumstances, we ask for the status of a member of this directory.
378 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
380 //If is not version control path
381 if( !bIsVersionedPath)
383 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": %s is not underversion control\n"), path.GetWinPath());
384 return CStatusCacheEntry();
387 // We've not got this item in the cache - let's add it
388 // We never bother asking SVN for the status of just one file, always for its containing directory
390 if (GitAdminDir::IsAdminDirPath(path.GetWinPathString()))
392 // We're being asked for the status of an .git directory
393 // It's not worth asking for this
394 return CStatusCacheEntry();
398 if(bFetch)
400 return GetStatusFromGit(path, sProjectRoot);
402 else
404 return GetStatusFromCache(path, bRecursive);
408 CStatusCacheEntry CCachedDirectory::GetCacheStatusForMember(const CTGitPath& path)
410 // no disk access!
411 AutoLocker lock(m_critSec);
412 CacheEntryMap::iterator itMap = m_entryCache.find(GetCacheKey(path));
413 if(itMap != m_entryCache.end())
414 return itMap->second;
416 return CStatusCacheEntry();
419 int CCachedDirectory::EnumFiles(const CTGitPath &path , bool IsFull)
421 CString sProjectRoot;
422 path.HasAdminDir(&sProjectRoot);
424 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": EnumFiles %s\n"), path.GetWinPath());
426 ATLASSERT( !m_directoryPath.IsEmpty() );
428 CString sSubPath;
430 CString s = path.GetWinPath();
432 if (s.GetLength() > sProjectRoot.GetLength())
434 // skip initial slash if necessary
435 if(s[sProjectRoot.GetLength()] == _T('\\'))
436 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
437 else
438 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
441 // strip "\" at the end, otherwise cache lookups for drives do not work correctly
442 sProjectRoot.TrimRight(_T("\\"));
444 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
445 UNREFERENCED_PARAMETER(pStatus);
446 git_wc_status_kind status = git_wc_status_none;
448 if (!path.IsDirectory())
450 bool assumeValid = false;
451 bool skipWorktree = false;
452 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback, this, &assumeValid, &skipWorktree);
453 if (status < m_mostImportantFileStatus)
454 RefreshMostImportant();
456 else
459 AutoLocker lock(m_critSec);
460 // clear subdirectory status cache
461 m_childDirectories.clear();
462 // build new files status cache
463 m_entryCache_tmp.clear();
466 m_mostImportantFileStatus = git_wc_status_none;
467 pStatus->EnumDirStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback,this);
468 m_mostImportantFileStatus = GitStatus::GetMoreImportant(m_mostImportantFileStatus, status);
471 AutoLocker lock(m_critSec);
472 // use a tmp files status cache so that we can still use the old cached values
473 // for deciding whether we have to issue a shell notify
474 m_entryCache = m_entryCache_tmp;
475 m_entryCache_tmp.clear();
478 // need to set/construct m_ownStatus (only unversioned and normal are valid values)
479 m_ownStatus = git_wc_status_unversioned;
480 m_ownStatus.SetKind(git_node_dir);
481 if (m_mostImportantFileStatus > git_wc_status_unversioned)
483 git_wc_status2_t status2;
484 status2.text_status = status2.prop_status = git_wc_status_normal;
485 m_ownStatus.SetStatus(&status2);
487 else
489 if (::PathFileExists(m_directoryPath.GetWinPathString() + _T("\\.git"))) {
490 git_wc_status2_t status2;
491 status2.text_status = status2.prop_status = git_wc_status_normal;
492 m_ownStatus.SetStatus(&status2);
494 else
496 git_wc_status2_t status2;
497 status2.text_status = status2.prop_status = CalculateRecursiveStatus();
498 m_ownStatus.SetStatus(&status2);
503 return 0;
505 void
506 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
508 AutoLocker lock(m_critSec);
509 if(path.IsDirectory())
511 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
512 if (childDir)
514 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
516 if(pGitStatus)
518 if(childDir->GetCurrentFullStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
520 CGitStatusCache::Instance().UpdateShell(path);
521 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), path.GetWinPath());
522 childDir->m_ownStatus.SetKind(git_node_dir);
523 childDir->m_ownStatus.SetStatus(pGitStatus);
527 childDir->m_ownStatus.SetKind(git_node_dir);
532 else
534 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
535 bool bNotified = false;
537 if(!childDir)
538 return ;
540 AutoLocker lock(childDir->m_critSec);
541 CString cachekey = GetCacheKey(path);
542 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
543 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
545 if (pGitStatus)
547 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
548 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
551 bNotified =true;
556 else
558 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
559 bNotified = true;
562 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
563 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
564 entry_it->second.SetKind(git_node_file);
566 childDir->m_entryCache_tmp[cachekey] = entry_it->second;
568 if(bNotified)
570 CGitStatusCache::Instance().UpdateShell(path);
571 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), path.GetWinPath());
574 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
579 CString
580 CCachedDirectory::GetCacheKey(const CTGitPath& path)
582 // All we put into the cache as a key is just the end portion of the pathname
583 // There's no point storing the path of the containing directory for every item
584 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower().TrimLeft(_T("\\"));
587 CString
588 CCachedDirectory::GetFullPathString(const CString& cacheKey)
590 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
593 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *, bool assumeValid, bool skipWorktree)
595 git_wc_status2_t _status;
596 git_wc_status2_t *status2 = &_status;
598 status2->prop_status = status2->text_status = status;
599 status2->assumeValid = assumeValid;
600 status2->skipWorktree = skipWorktree;
602 CTGitPath gitPath(path);
604 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
606 if(pThis == NULL)
607 return FALSE;
609 // if(status->entry)
611 if (isDir)
612 { /*gitpath is directory*/
613 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
615 if (!gitPath.Exists())
617 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Miss dir %s \n"), gitPath.GetWinPath());
618 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
621 if ( status < git_wc_status_normal)
623 if( ::PathFileExists(path+_T("\\.git")))
624 { // this is submodule
625 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": skip submodule %s\n"), path);
626 return FALSE;
629 if (pThis->m_bRecursive)
631 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
632 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
633 if (status >= git_wc_status_normal || (CGitStatusCache::Instance().IsUnversionedAsModified() && status == git_wc_status_unversioned))
634 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
637 // Make sure we know about this child directory
638 // This initial status value is likely to be overwritten from below at some point
639 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
641 // folders must not be displayed as added or deleted only as modified
642 if (s == git_wc_status_deleted || s == git_wc_status_added)
643 s = git_wc_status_modified;
645 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
646 if (cdir)
648 // This child directory is already in our cache!
649 // So ask this dir about its recursive status
650 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
651 AutoLocker lock(pThis->m_critSec);
652 pThis->m_childDirectories[gitPath] = st;
653 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
655 else
657 AutoLocker lock(pThis->m_critSec);
658 // the child directory is not in the cache. Create a new entry for it in the cache which is
659 // initially 'unversioned'. But we added that directory to the crawling list above, which
660 // means the cache will be updated soon.
661 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
663 pThis->m_childDirectories[gitPath] = s;
664 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
668 else /* gitpath is file*/
670 // Keep track of the most important status of all the files in this directory
671 // Don't include subdirectories in this figure, because they need to provide their
672 // own 'most important' value
673 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
674 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
675 if ((status2->text_status == git_wc_status_unversioned) && (CGitStatusCache::Instance().IsUnversionedAsModified()))
677 // treat unversioned files as modified
678 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
679 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
684 pThis->AddEntry(gitPath, status2);
686 return FALSE;
689 bool
690 CCachedDirectory::IsOwnStatusValid() const
692 return m_ownStatus.HasBeenSet() &&
693 !m_ownStatus.HasExpired(GetTickCount());
696 void CCachedDirectory::Invalidate()
698 m_ownStatus.Invalidate();
701 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
703 // Combine our OWN folder status with the most important of our *FILES'* status.
704 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
706 // folders can only be none, unversioned, normal, modified, and conflicted
707 if (retVal == git_wc_status_deleted || retVal == git_wc_status_added)
708 retVal = git_wc_status_modified;
710 // Now combine all our child-directorie's status
711 AutoLocker lock(m_critSec);
712 ChildDirStatus::const_iterator it;
713 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
715 retVal = GitStatus::GetMoreImportant(retVal, it->second);
718 return retVal;
721 // Update our composite status and deal with things if it's changed
722 void CCachedDirectory::UpdateCurrentStatus()
724 git_wc_status_kind newStatus = CalculateRecursiveStatus();
725 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": UpdateCurrentStatus %s new:%d old: %d\n"),
726 m_directoryPath.GetWinPath(),
727 newStatus, m_currentFullStatus);
729 if (newStatus != m_currentFullStatus && m_ownStatus.IsDirectory())
731 m_currentFullStatus = newStatus;
733 // Our status has changed - tell the shell
734 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Dir %s, status change from %d to %d\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
735 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
737 // And tell our parent, if we've got one...
738 // we tell our parent *always* about our status, even if it hasn't
739 // changed. This is to make sure that the parent has really our current
740 // status - the parent can decide itself if our status has changed
741 // or not.
742 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
743 if(!parentPath.IsEmpty())
745 // We have a parent
746 // just version controled directory need to cache.
747 CString root1, root2;
748 if (parentPath.HasAdminDir(&root1) && (CGitStatusCache::Instance().IsRecurseSubmodules() || m_directoryPath.HasAdminDir(&root2) && root1 == root2))
750 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
751 if (cachedDir)
752 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
758 // Receive a notification from a child that its status has changed
759 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
761 git_wc_status_kind currentStatus = git_wc_status_none;
763 AutoLocker lock(m_critSec);
764 currentStatus = m_childDirectories[childDir];
766 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
769 AutoLocker lock(m_critSec);
770 m_childDirectories[childDir] = childStatus;
772 UpdateCurrentStatus();
776 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
778 // Don't return recursive status if we're unversioned ourselves.
779 if(bRecursive && m_ownStatus.IsDirectory() && m_ownStatus.GetEffectiveStatus() != git_wc_status_ignored)
781 CStatusCacheEntry recursiveStatus(m_ownStatus);
782 UpdateCurrentStatus();
783 recursiveStatus.ForceStatus(m_currentFullStatus);
784 return recursiveStatus;
786 else
788 return m_ownStatus;
792 void CCachedDirectory::RefreshStatus(bool bRecursive)
794 // Make sure that our own status is up-to-date
795 GetStatusForMember(m_directoryPath,bRecursive);
797 AutoLocker lock(m_critSec);
798 // We also need to check if all our file members have the right date on them
799 CacheEntryMap::iterator itMembers;
800 std::set<CTGitPath> refreshedpaths;
801 DWORD now = GetTickCount();
802 if (m_entryCache.empty())
803 return;
804 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
806 if (itMembers->first)
808 CTGitPath filePath(m_directoryPath);
809 filePath.AppendPathString(itMembers->first);
810 std::set<CTGitPath>::iterator refr_it;
811 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
812 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
814 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
816 lock.Unlock();
817 // We need to request this item as well
818 GetStatusForMember(filePath,bRecursive);
819 // GetStatusForMember now has recreated the m_entryCache map.
820 // So start the loop again, but add this path to the refreshed paths set
821 // to make sure we don't refresh this path again. This is to make sure
822 // that we don't end up in an endless loop.
823 lock.Lock();
824 refreshedpaths.insert(refr_it, filePath);
825 itMembers = m_entryCache.begin();
826 if (m_entryCache.empty())
827 return;
828 continue;
830 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
832 // crawl all sub folders too! Otherwise a change deep inside the
833 // tree which has changed won't get propagated up the tree.
834 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
841 void CCachedDirectory::RefreshMostImportant()
843 AutoLocker lock(m_critSec);
844 CacheEntryMap::iterator itMembers;
845 git_wc_status_kind newStatus = git_wc_status_unversioned;
846 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
848 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
849 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
850 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
852 // treat unversioned files as modified
853 if (newStatus != git_wc_status_added)
854 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
857 if (newStatus != m_mostImportantFileStatus)
859 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": status change of path %s\n"), m_directoryPath.GetWinPath());
860 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
862 m_mostImportantFileStatus = newStatus;