Drop unused msysgit dialog of installer
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob7800de4b155142759e6d6ba6bae8829b3151f97e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
4 // Copyright (C) 2008-2013 - 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 isIgnoreFileChanged = pGitStatus->HasIgnoreFilesChanged(sProjectRoot, subpaths);
277 if( isIgnoreFileChanged)
279 pGitStatus->LoadIgnoreFile(sProjectRoot, subpaths);
282 if(path.IsDirectory())
285 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path,
286 false); /* we needn't watch untracked directory*/
288 if(dirEntry)
290 AutoLocker lock(dirEntry->m_critSec);
292 git_wc_status_kind dirstatus = dirEntry->GetCurrentFullStatus() ;
293 if( dirstatus == git_wc_status_none || dirstatus >= git_wc_status_normal || isIgnoreFileChanged )
294 {/* status have not initialized*/
295 git_wc_status2_t status2;
296 bool isignore = false;
297 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
298 status2.text_status = status2.prop_status =
299 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
301 dirEntry->m_ownStatus.SetKind(git_node_dir);
302 dirEntry->m_ownStatus.SetStatus(&status2);
305 return dirEntry->m_ownStatus;
309 else /* path is file */
311 AutoLocker lock(m_critSec);
312 CString strCacheKey = GetCacheKey(path);
314 if (strCacheKey.IsEmpty())
315 return CStatusCacheEntry();
317 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
318 if(itMap == m_entryCache.end() || isIgnoreFileChanged)
320 git_wc_status2_t status2;
321 bool isignore = false;
322 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
323 status2.text_status = status2.prop_status =
324 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
325 AddEntry(path, &status2);
326 return m_entryCache[strCacheKey];
328 else
330 return itMap->second;
333 return CStatusCacheEntry();
336 else
338 EnumFiles(path, TRUE);
339 UpdateCurrentStatus();
340 return CStatusCacheEntry(m_ownStatus);
345 /// bFetch is true, fetch all status, call by crawl.
346 /// bFetch is false, get cache status, return quickly.
348 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive, bool bFetch /* = true */)
350 CString sProjectRoot;
351 bool bIsVersionedPath;
353 bool bRequestForSelf = false;
354 if(path.IsEquivalentToWithoutCase(m_directoryPath))
356 bRequestForSelf = true;
357 AutoLocker lock(m_critSec);
358 // HasAdminDir might modify m_directoryPath, so we need to do it synchronized
359 bIsVersionedPath = m_directoryPath.HasAdminDir(&sProjectRoot);
361 else
362 bIsVersionedPath = path.HasAdminDir(&sProjectRoot);
364 // In all most circumstances, we ask for the status of a member of this directory.
365 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
367 //If is not version control path
368 if( !bIsVersionedPath)
370 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": %s is not underversion control\n"), path.GetWinPath());
371 return CStatusCacheEntry();
374 // We've not got this item in the cache - let's add it
375 // We never bother asking SVN for the status of just one file, always for its containing directory
377 if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))
379 // We're being asked for the status of an .git directory
380 // It's not worth asking for this
381 return CStatusCacheEntry();
385 if(bFetch)
387 return GetStatusFromGit(path, sProjectRoot);
389 else
391 return GetStatusFromCache(path, bRecursive);
395 CStatusCacheEntry CCachedDirectory::GetCacheStatusForMember(const CTGitPath& path)
397 // no disk access!
398 AutoLocker lock(m_critSec);
399 CacheEntryMap::iterator itMap = m_entryCache.find(GetCacheKey(path));
400 if(itMap != m_entryCache.end())
401 return itMap->second;
403 return CStatusCacheEntry();
406 int CCachedDirectory::EnumFiles(const CTGitPath &path , bool IsFull)
408 CString sProjectRoot;
409 path.HasAdminDir(&sProjectRoot);
411 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": EnumFiles %s\n"), path.GetWinPath());
413 ATLASSERT( !m_directoryPath.IsEmpty() );
415 CString sSubPath;
417 CString s = path.GetWinPath();
419 if (s.GetLength() > sProjectRoot.GetLength())
421 // skip initial slash if necessary
422 if(s[sProjectRoot.GetLength()] == _T('\\'))
423 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
424 else
425 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
428 // strip "\" at the end, otherwise cache lookups for drives do not work correctly
429 sProjectRoot.TrimRight(_T("\\"));
431 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
432 UNREFERENCED_PARAMETER(pStatus);
433 git_wc_status_kind status = git_wc_status_none;
435 if (!path.IsDirectory())
437 bool assumeValid = false;
438 bool skipWorktree = false;
439 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback, this, &assumeValid, &skipWorktree);
441 else
444 AutoLocker lock(m_critSec);
445 // clear subdirectory status cache
446 m_childDirectories.clear();
449 m_mostImportantFileStatus = git_wc_status_none;
450 pStatus->EnumDirStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback,this);
452 // folders must not be displayed as added or deleted only as modified
453 if (status == git_wc_status_deleted || status == git_wc_status_added)
454 status = git_wc_status_modified;
455 // this is necessary, even if the m_mostImportantFileStatus is set in GetStatusCallback,
456 // however, if it's missing, deleted subdirectories won't mark parents as modified
457 m_mostImportantFileStatus = GitStatus::GetMoreImportant(m_mostImportantFileStatus, status);
459 if (sSubPath.IsEmpty())
461 // request a shell notification for working trees which have not been examined before
462 if (m_currentFullStatus == git_wc_status_none)
464 m_currentFullStatus = git_wc_status_normal;
465 CGitStatusCache::Instance().UpdateShell(sProjectRoot);
467 m_ownStatus = git_wc_status_normal;
469 // otherwise folders which contain at least one ignored item gets displayed as ignored
470 // or: folders which were displayed as conflicted never recover from that state
471 else if (m_ownStatus.GetEffectiveStatus() == git_wc_status_ignored || m_ownStatus.GetEffectiveStatus() == git_wc_status_conflicted)
472 m_ownStatus = git_wc_status_normal;
475 return 0;
477 void
478 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
480 AutoLocker lock(m_critSec);
481 if(path.IsDirectory())
483 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
484 if (childDir)
486 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
488 if(pGitStatus)
490 if(childDir->GetCurrentFullStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
492 CGitStatusCache::Instance().UpdateShell(path);
493 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), path.GetWinPath());
494 childDir->m_ownStatus.SetKind(git_node_dir);
495 childDir->m_ownStatus.SetStatus(pGitStatus);
499 childDir->m_ownStatus.SetKind(git_node_dir);
504 else
506 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
507 bool bNotified = false;
509 if(!childDir)
510 return ;
512 AutoLocker lock(childDir->m_critSec);
513 CString cachekey = GetCacheKey(path);
514 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
515 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
517 if (pGitStatus)
519 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
520 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
523 bNotified =true;
528 else
530 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
531 bNotified = true;
534 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
535 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
536 entry_it->second.SetKind(git_node_file);
538 if(bNotified)
540 CGitStatusCache::Instance().UpdateShell(path);
541 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), path.GetWinPath());
544 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
547 CCachedDirectory * parent = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
549 if(parent)
551 if ((parent->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
553 if(pGitStatus)
555 if(parent->GetCurrentFullStatus() < GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
557 CGitStatusCache::Instance().UpdateShell(parent->m_directoryPath);
558 //CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": shell update for %s\n"), parent->m_directoryPath.GetWinPathString());
559 parent->m_ownStatus.SetKind(git_node_dir);
560 parent->m_ownStatus.SetStatus(pGitStatus);
568 CString
569 CCachedDirectory::GetCacheKey(const CTGitPath& path)
571 // All we put into the cache as a key is just the end portion of the pathname
572 // There's no point storing the path of the containing directory for every item
573 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower().TrimLeft(_T("\\"));
576 CString
577 CCachedDirectory::GetFullPathString(const CString& cacheKey)
579 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
582 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *, bool assumeValid, bool skipWorktree)
584 git_wc_status2_t _status;
585 git_wc_status2_t *status2 = &_status;
587 status2->prop_status = status2->text_status = status;
588 status2->assumeValid = assumeValid;
589 status2->skipWorktree = skipWorktree;
591 CTGitPath gitPath(path);
593 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
595 if(pThis == NULL)
596 return FALSE;
598 // if(status->entry)
600 if (isDir)
601 { /*gitpath is directory*/
602 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
604 if (!gitPath.Exists())
606 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Miss dir %s \n"), gitPath.GetWinPath());
607 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
610 if ( status < git_wc_status_normal)
612 if( ::PathFileExists(path+_T("\\.git")))
613 { // this is submodule
614 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": skip submodule %s\n"), path);
615 return FALSE;
618 if (pThis->m_bRecursive)
620 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
621 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
622 if(status >= git_wc_status_normal)
623 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
626 // Make sure we know about this child directory
627 // This initial status value is likely to be overwritten from below at some point
628 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
630 // folders must not be displayed as added or deleted only as modified
631 if (s == git_wc_status_deleted || s == git_wc_status_added)
632 s = git_wc_status_modified;
634 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
635 if (cdir)
637 // This child directory is already in our cache!
638 // So ask this dir about its recursive status
639 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
640 AutoLocker lock(pThis->m_critSec);
641 pThis->m_childDirectories[gitPath] = st;
642 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
644 else
646 AutoLocker lock(pThis->m_critSec);
647 // the child directory is not in the cache. Create a new entry for it in the cache which is
648 // initially 'unversioned'. But we added that directory to the crawling list above, which
649 // means the cache will be updated soon.
650 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
652 pThis->m_childDirectories[gitPath] = s;
653 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
657 else /* gitpath is file*/
659 // Keep track of the most important status of all the files in this directory
660 // Don't include subdirectories in this figure, because they need to provide their
661 // own 'most important' value
662 if (status2->text_status == git_wc_status_deleted || status2->text_status == git_wc_status_added)
664 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
665 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
667 else
669 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
670 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
671 if ((status2->text_status == git_wc_status_unversioned)
672 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
674 // treat unversioned files as modified
675 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
676 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
682 pThis->AddEntry(gitPath, status2);
684 return FALSE;
687 bool
688 CCachedDirectory::IsOwnStatusValid() const
690 return m_ownStatus.HasBeenSet() &&
691 !m_ownStatus.HasExpired(GetTickCount());
694 void CCachedDirectory::Invalidate()
696 m_ownStatus.Invalidate();
699 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
701 // Combine our OWN folder status with the most important of our *FILES'* status.
702 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
704 // Now combine all our child-directorie's status
705 AutoLocker lock(m_critSec);
706 ChildDirStatus::const_iterator it;
707 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
709 retVal = GitStatus::GetMoreImportant(retVal, it->second);
712 return retVal;
715 // Update our composite status and deal with things if it's changed
716 void CCachedDirectory::UpdateCurrentStatus()
718 git_wc_status_kind newStatus = CalculateRecursiveStatus();
719 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": UpdateCurrentStatus %s new:%d old: %d\n"),
720 m_directoryPath.GetWinPath(),
721 newStatus, m_currentFullStatus);
723 if ( this->m_ownStatus.GetEffectiveStatus() < git_wc_status_normal )
725 if (::PathFileExists(this->m_directoryPath.GetWinPathString()+_T("\\.git")))
727 //project root must be normal status at least.
728 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": force update project root directory as normal status\n"));
729 this->m_ownStatus.ForceStatus(git_wc_status_normal);
731 else if (m_mostImportantFileStatus > git_wc_status_unversioned)
732 m_ownStatus.ForceStatus(m_mostImportantFileStatus);
735 if ((newStatus != m_currentFullStatus) && m_ownStatus.IsVersioned())
737 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
739 // Our status has changed - tell the shell
740 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Dir %s, status change from %d to %d\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
741 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
743 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
744 m_currentFullStatus = newStatus;
745 else
746 m_currentFullStatus = git_wc_status_missing;
748 // And tell our parent, if we've got one...
749 // we tell our parent *always* about our status, even if it hasn't
750 // changed. This is to make sure that the parent has really our current
751 // status - the parent can decide itself if our status has changed
752 // or not.
753 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
754 if(!parentPath.IsEmpty())
756 // We have a parent
757 // just version controled directory need to cache.
758 CString root1, root2;
759 if(parentPath.HasAdminDir(&root1) && m_directoryPath.HasAdminDir(&root2) && root1 == root2)
761 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
762 if (cachedDir)
763 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
769 // Receive a notification from a child that its status has changed
770 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
772 git_wc_status_kind currentStatus = git_wc_status_none;
774 AutoLocker lock(m_critSec);
775 currentStatus = m_childDirectories[childDir];
777 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
780 AutoLocker lock(m_critSec);
781 m_childDirectories[childDir] = childStatus;
783 UpdateCurrentStatus();
787 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
789 // Don't return recursive status if we're unversioned ourselves.
790 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned && m_ownStatus.GetEffectiveStatus() != git_wc_status_ignored)
792 CStatusCacheEntry recursiveStatus(m_ownStatus);
793 UpdateCurrentStatus();
794 if (m_currentFullStatus == git_wc_status_deleted || m_currentFullStatus == git_wc_status_added)
795 m_currentFullStatus = git_wc_status_modified;
796 recursiveStatus.ForceStatus(m_currentFullStatus);
797 return recursiveStatus;
799 else
801 return m_ownStatus;
805 void CCachedDirectory::RefreshStatus(bool bRecursive)
807 // Make sure that our own status is up-to-date
808 GetStatusForMember(m_directoryPath,bRecursive);
810 AutoLocker lock(m_critSec);
811 // We also need to check if all our file members have the right date on them
812 CacheEntryMap::iterator itMembers;
813 std::set<CTGitPath> refreshedpaths;
814 DWORD now = GetTickCount();
815 if (m_entryCache.empty())
816 return;
817 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
819 if (itMembers->first)
821 CTGitPath filePath(m_directoryPath);
822 filePath.AppendPathString(itMembers->first);
823 std::set<CTGitPath>::iterator refr_it;
824 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
825 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
827 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
829 lock.Unlock();
830 // We need to request this item as well
831 GetStatusForMember(filePath,bRecursive);
832 // GetStatusForMember now has recreated the m_entryCache map.
833 // So start the loop again, but add this path to the refreshed paths set
834 // to make sure we don't refresh this path again. This is to make sure
835 // that we don't end up in an endless loop.
836 lock.Lock();
837 refreshedpaths.insert(refr_it, filePath);
838 itMembers = m_entryCache.begin();
839 if (m_entryCache.empty())
840 return;
841 continue;
843 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
845 // crawl all sub folders too! Otherwise a change deep inside the
846 // tree which has changed won't get propagated up the tree.
847 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
854 void CCachedDirectory::RefreshMostImportant()
856 AutoLocker lock(m_critSec);
857 CacheEntryMap::iterator itMembers;
858 git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();
859 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
861 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
862 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
863 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
865 // treat unversioned files as modified
866 if (newStatus != git_wc_status_added)
867 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
870 if (newStatus != m_mostImportantFileStatus)
872 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": status change of path %s\n"), m_directoryPath.GetWinPath());
873 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
875 m_mostImportantFileStatus = newStatus;