Add more options to version.txt
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob9588b09a8486fa9de859869c06563324ab48462a
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
4 // Copyright (C) 2008-2014 - 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( dirstatus == git_wc_status_none || dirstatus >= git_wc_status_normal || isIgnoreFileChanged )
295 {/* status have not initialized*/
296 git_wc_status2_t status2;
297 bool isignore = false;
298 pGitStatus->IsIgnore(sProjectRoot, subpaths, &isignore, isDir);
299 status2.text_status = status2.prop_status =
300 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
302 dirEntry->m_ownStatus.SetKind(git_node_dir);
303 dirEntry->m_ownStatus.SetStatus(&status2);
306 return dirEntry->m_ownStatus;
310 else /* path is file */
312 AutoLocker lock(m_critSec);
313 CString strCacheKey = GetCacheKey(path);
315 if (strCacheKey.IsEmpty())
316 return CStatusCacheEntry();
318 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
319 if(itMap == m_entryCache.end() || isIgnoreFileChanged)
321 git_wc_status2_t status2;
322 bool isignore = false;
323 pGitStatus->IsIgnore(sProjectRoot, subpaths, &isignore, isDir);
324 status2.text_status = status2.prop_status =
325 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
326 AddEntry(path, &status2);
327 return m_entryCache[strCacheKey];
329 else
331 return itMap->second;
334 return CStatusCacheEntry();
337 else
339 EnumFiles(path, TRUE);
340 UpdateCurrentStatus();
341 if (!path.IsDirectory())
342 return GetCacheStatusForMember(path);
343 return CStatusCacheEntry(m_ownStatus);
348 /// bFetch is true, fetch all status, call by crawl.
349 /// bFetch is false, get cache status, return quickly.
351 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive, bool bFetch /* = true */)
353 CString sProjectRoot;
354 bool bIsVersionedPath;
356 bool bRequestForSelf = false;
357 if(path.IsEquivalentToWithoutCase(m_directoryPath))
359 bRequestForSelf = true;
360 AutoLocker lock(m_critSec);
361 // HasAdminDir might modify m_directoryPath, so we need to do it synchronized
362 bIsVersionedPath = m_directoryPath.HasAdminDir(&sProjectRoot);
364 else
365 bIsVersionedPath = path.HasAdminDir(&sProjectRoot);
367 // In all most circumstances, we ask for the status of a member of this directory.
368 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
370 //If is not version control path
371 if( !bIsVersionedPath)
373 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": %s is not underversion control\n"), path.GetWinPath());
374 return CStatusCacheEntry();
377 // We've not got this item in the cache - let's add it
378 // We never bother asking SVN for the status of just one file, always for its containing directory
380 if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))
382 // We're being asked for the status of an .git directory
383 // It's not worth asking for this
384 return CStatusCacheEntry();
388 if(bFetch)
390 return GetStatusFromGit(path, sProjectRoot);
392 else
394 return GetStatusFromCache(path, bRecursive);
398 CStatusCacheEntry CCachedDirectory::GetCacheStatusForMember(const CTGitPath& path)
400 // no disk access!
401 AutoLocker lock(m_critSec);
402 CacheEntryMap::iterator itMap = m_entryCache.find(GetCacheKey(path));
403 if(itMap != m_entryCache.end())
404 return itMap->second;
406 return CStatusCacheEntry();
409 int CCachedDirectory::EnumFiles(const CTGitPath &path , bool IsFull)
411 CString sProjectRoot;
412 path.HasAdminDir(&sProjectRoot);
414 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": EnumFiles %s\n"), path.GetWinPath());
416 ATLASSERT( !m_directoryPath.IsEmpty() );
418 CString sSubPath;
420 CString s = path.GetWinPath();
422 if (s.GetLength() > sProjectRoot.GetLength())
424 // skip initial slash if necessary
425 if(s[sProjectRoot.GetLength()] == _T('\\'))
426 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
427 else
428 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
431 // strip "\" at the end, otherwise cache lookups for drives do not work correctly
432 sProjectRoot.TrimRight(_T("\\"));
434 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
435 UNREFERENCED_PARAMETER(pStatus);
436 git_wc_status_kind status = git_wc_status_none;
438 if (!path.IsDirectory())
440 bool assumeValid = false;
441 bool skipWorktree = false;
442 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback, this, &assumeValid, &skipWorktree);
443 if (status < m_mostImportantFileStatus)
445 m_mostImportantFileStatus = git_wc_status_none;
446 m_ownStatus = git_wc_status_none;
447 RefreshMostImportant();
448 UpdateCurrentStatus();
451 else
454 AutoLocker lock(m_critSec);
455 // clear subdirectory status cache
456 m_childDirectories.clear();
459 m_mostImportantFileStatus = git_wc_status_none;
460 pStatus->EnumDirStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback,this);
462 // folders must not be displayed as added or deleted only as modified
463 if (status == git_wc_status_deleted || status == git_wc_status_added)
464 status = git_wc_status_modified;
465 // this is necessary, even if the m_mostImportantFileStatus is set in GetStatusCallback,
466 // however, if it's missing, deleted subdirectories won't mark parents as modified
467 m_mostImportantFileStatus = GitStatus::GetMoreImportant(m_mostImportantFileStatus, status);
469 if (sSubPath.IsEmpty())
471 // request a shell notification for working trees which have not been examined before
472 if (m_currentFullStatus == git_wc_status_none)
474 m_currentFullStatus = git_wc_status_normal;
475 CGitStatusCache::Instance().UpdateShell(sProjectRoot);
477 m_ownStatus = git_wc_status_normal;
479 // otherwise folders which contain at least one ignored item gets displayed as ignored
480 // or: folders which were displayed as conflicted never recover from that state
481 else if (m_ownStatus.GetEffectiveStatus() == git_wc_status_ignored || m_ownStatus.GetEffectiveStatus() == git_wc_status_conflicted)
482 m_ownStatus = git_wc_status_normal;
485 return 0;
487 void
488 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
490 AutoLocker lock(m_critSec);
491 if(path.IsDirectory())
493 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
494 if (childDir)
496 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
498 if(pGitStatus)
500 if(childDir->GetCurrentFullStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
502 CGitStatusCache::Instance().UpdateShell(path);
503 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), path.GetWinPath());
504 childDir->m_ownStatus.SetKind(git_node_dir);
505 childDir->m_ownStatus.SetStatus(pGitStatus);
509 childDir->m_ownStatus.SetKind(git_node_dir);
514 else
516 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
517 bool bNotified = false;
519 if(!childDir)
520 return ;
522 AutoLocker lock(childDir->m_critSec);
523 CString cachekey = GetCacheKey(path);
524 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
525 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
527 if (pGitStatus)
529 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
530 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
533 bNotified =true;
538 else
540 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
541 bNotified = true;
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);
548 if(bNotified)
550 CGitStatusCache::Instance().UpdateShell(path);
551 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), path.GetWinPath());
554 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
557 CCachedDirectory * parent = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
559 if(parent)
561 if ((parent->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
563 if(pGitStatus)
565 if(parent->GetCurrentFullStatus() < GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
567 CGitStatusCache::Instance().UpdateShell(parent->m_directoryPath);
568 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), parent->m_directoryPath.GetWinPathString());
569 parent->m_ownStatus.SetKind(git_node_dir);
570 parent->m_ownStatus.SetStatus(pGitStatus);
578 CString
579 CCachedDirectory::GetCacheKey(const CTGitPath& path)
581 // All we put into the cache as a key is just the end portion of the pathname
582 // There's no point storing the path of the containing directory for every item
583 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower().TrimLeft(_T("\\"));
586 CString
587 CCachedDirectory::GetFullPathString(const CString& cacheKey)
589 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
592 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *, bool assumeValid, bool skipWorktree)
594 git_wc_status2_t _status;
595 git_wc_status2_t *status2 = &_status;
597 status2->prop_status = status2->text_status = status;
598 status2->assumeValid = assumeValid;
599 status2->skipWorktree = skipWorktree;
601 CTGitPath gitPath(path);
603 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
605 if(pThis == NULL)
606 return FALSE;
608 // if(status->entry)
610 if (isDir)
611 { /*gitpath is directory*/
612 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
614 if (!gitPath.Exists())
616 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Miss dir %s \n"), gitPath.GetWinPath());
617 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
620 if ( status < git_wc_status_normal)
622 if( ::PathFileExists(path+_T("\\.git")))
623 { // this is submodule
624 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": skip submodule %s\n"), path);
625 return FALSE;
628 if (pThis->m_bRecursive)
630 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
631 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
632 if(status >= git_wc_status_normal)
633 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
636 // Make sure we know about this child directory
637 // This initial status value is likely to be overwritten from below at some point
638 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
640 // folders must not be displayed as added or deleted only as modified
641 if (s == git_wc_status_deleted || s == git_wc_status_added)
642 s = git_wc_status_modified;
644 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
645 if (cdir)
647 // This child directory is already in our cache!
648 // So ask this dir about its recursive status
649 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
650 AutoLocker lock(pThis->m_critSec);
651 pThis->m_childDirectories[gitPath] = st;
652 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
654 else
656 AutoLocker lock(pThis->m_critSec);
657 // the child directory is not in the cache. Create a new entry for it in the cache which is
658 // initially 'unversioned'. But we added that directory to the crawling list above, which
659 // means the cache will be updated soon.
660 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
662 pThis->m_childDirectories[gitPath] = s;
663 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
667 else /* gitpath is file*/
669 // Keep track of the most important status of all the files in this directory
670 // Don't include subdirectories in this figure, because they need to provide their
671 // own 'most important' value
672 if (status2->text_status == git_wc_status_deleted || status2->text_status == git_wc_status_added)
674 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
675 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
677 else
679 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
680 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
681 if ((status2->text_status == git_wc_status_unversioned)
682 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
684 // treat unversioned files as modified
685 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
686 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
692 pThis->AddEntry(gitPath, status2);
694 return FALSE;
697 bool
698 CCachedDirectory::IsOwnStatusValid() const
700 return m_ownStatus.HasBeenSet() &&
701 !m_ownStatus.HasExpired(GetTickCount());
704 void CCachedDirectory::Invalidate()
706 m_ownStatus.Invalidate();
709 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
711 // Combine our OWN folder status with the most important of our *FILES'* status.
712 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
714 // Now combine all our child-directorie's status
715 AutoLocker lock(m_critSec);
716 ChildDirStatus::const_iterator it;
717 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
719 retVal = GitStatus::GetMoreImportant(retVal, it->second);
722 return retVal;
725 // Update our composite status and deal with things if it's changed
726 void CCachedDirectory::UpdateCurrentStatus()
728 git_wc_status_kind newStatus = CalculateRecursiveStatus();
729 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": UpdateCurrentStatus %s new:%d old: %d\n"),
730 m_directoryPath.GetWinPath(),
731 newStatus, m_currentFullStatus);
733 if ( this->m_ownStatus.GetEffectiveStatus() < git_wc_status_normal )
735 if (::PathFileExists(this->m_directoryPath.GetWinPathString()+_T("\\.git")))
737 //project root must be normal status at least.
738 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": force update project root directory as normal status\n"));
739 this->m_ownStatus.ForceStatus(git_wc_status_normal);
741 else if (m_mostImportantFileStatus > git_wc_status_unversioned)
742 m_ownStatus.ForceStatus(m_mostImportantFileStatus);
745 if ((newStatus != m_currentFullStatus) && m_ownStatus.IsVersioned())
747 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
749 // Our status has changed - tell the shell
750 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Dir %s, status change from %d to %d\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
751 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
753 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
754 m_currentFullStatus = newStatus;
755 else
756 m_currentFullStatus = git_wc_status_missing;
758 // And tell our parent, if we've got one...
759 // we tell our parent *always* about our status, even if it hasn't
760 // changed. This is to make sure that the parent has really our current
761 // status - the parent can decide itself if our status has changed
762 // or not.
763 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
764 if(!parentPath.IsEmpty())
766 // We have a parent
767 // just version controled directory need to cache.
768 CString root1, root2;
769 if(parentPath.HasAdminDir(&root1) && m_directoryPath.HasAdminDir(&root2) && root1 == root2)
771 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
772 if (cachedDir)
773 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
779 // Receive a notification from a child that its status has changed
780 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
782 git_wc_status_kind currentStatus = git_wc_status_none;
784 AutoLocker lock(m_critSec);
785 currentStatus = m_childDirectories[childDir];
787 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
790 AutoLocker lock(m_critSec);
791 m_childDirectories[childDir] = childStatus;
793 UpdateCurrentStatus();
797 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
799 // Don't return recursive status if we're unversioned ourselves.
800 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned && m_ownStatus.GetEffectiveStatus() != git_wc_status_ignored)
802 CStatusCacheEntry recursiveStatus(m_ownStatus);
803 UpdateCurrentStatus();
804 if (m_currentFullStatus == git_wc_status_deleted || m_currentFullStatus == git_wc_status_added)
805 m_currentFullStatus = git_wc_status_modified;
806 recursiveStatus.ForceStatus(m_currentFullStatus);
807 return recursiveStatus;
809 else
811 return m_ownStatus;
815 void CCachedDirectory::RefreshStatus(bool bRecursive)
817 // Make sure that our own status is up-to-date
818 GetStatusForMember(m_directoryPath,bRecursive);
820 AutoLocker lock(m_critSec);
821 // We also need to check if all our file members have the right date on them
822 CacheEntryMap::iterator itMembers;
823 std::set<CTGitPath> refreshedpaths;
824 DWORD now = GetTickCount();
825 if (m_entryCache.empty())
826 return;
827 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
829 if (itMembers->first)
831 CTGitPath filePath(m_directoryPath);
832 filePath.AppendPathString(itMembers->first);
833 std::set<CTGitPath>::iterator refr_it;
834 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
835 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
837 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
839 lock.Unlock();
840 // We need to request this item as well
841 GetStatusForMember(filePath,bRecursive);
842 // GetStatusForMember now has recreated the m_entryCache map.
843 // So start the loop again, but add this path to the refreshed paths set
844 // to make sure we don't refresh this path again. This is to make sure
845 // that we don't end up in an endless loop.
846 lock.Lock();
847 refreshedpaths.insert(refr_it, filePath);
848 itMembers = m_entryCache.begin();
849 if (m_entryCache.empty())
850 return;
851 continue;
853 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
855 // crawl all sub folders too! Otherwise a change deep inside the
856 // tree which has changed won't get propagated up the tree.
857 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
864 void CCachedDirectory::RefreshMostImportant()
866 AutoLocker lock(m_critSec);
867 CacheEntryMap::iterator itMembers;
868 git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();
869 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
871 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
872 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
873 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
875 // treat unversioned files as modified
876 if (newStatus != git_wc_status_added)
877 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
880 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
881 if (newStatus == git_wc_status_deleted || newStatus == git_wc_status_added)
882 newStatus = git_wc_status_modified;
883 if (newStatus != m_mostImportantFileStatus)
885 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": status change of path %s\n"), m_directoryPath.GetWinPath());
886 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
888 m_mostImportantFileStatus = newStatus;