Moved bisect start logic to AppUtils
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blobba9055097d866d69f00353675bfd95fe5fcd460d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
4 // Copyright (C) 2008-2012 - 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 "SVNHelpers.h"
23 #include "GitStatusCache.h"
24 #include "GitStatus.h"
25 #include <set>
27 CCachedDirectory::CCachedDirectory(void)
29 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
30 m_bRecursive = true;
33 CCachedDirectory::~CCachedDirectory(void)
37 CCachedDirectory::CCachedDirectory(const CTGitPath& directoryPath)
39 ATLASSERT(directoryPath.IsDirectory() || !PathFileExists(directoryPath.GetWinPath()));
41 m_directoryPath = directoryPath;
42 m_directoryPath.GetGitPathString(); // make sure git path string is set
44 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;
45 m_bRecursive = true;
48 BOOL CCachedDirectory::SaveToDisk(FILE * pFile)
50 AutoLocker lock(m_critSec);
51 #define WRITEVALUETOFILE(x) if (fwrite(&x, sizeof(x), 1, pFile)!=1) return false;
53 unsigned int value = GIT_CACHE_VERSION;
54 WRITEVALUETOFILE(value); // 'version' of this save-format
55 value = (int)m_entryCache.size();
56 WRITEVALUETOFILE(value); // size of the cache map
57 // now iterate through the maps and save every entry.
58 for (CacheEntryMap::iterator I = m_entryCache.begin(); I != m_entryCache.end(); ++I)
60 const CString& key = I->first;
61 value = key.GetLength();
62 WRITEVALUETOFILE(value);
63 if (value)
65 if (fwrite((LPCTSTR)key, sizeof(TCHAR), value, pFile)!=value)
66 return false;
67 if (!I->second.SaveToDisk(pFile))
68 return false;
71 value = (int)m_childDirectories.size();
72 WRITEVALUETOFILE(value);
73 for (ChildDirStatus::iterator I = m_childDirectories.begin(); I != m_childDirectories.end(); ++I)
75 const CString& path = I->first.GetWinPathString();
76 value = path.GetLength();
77 WRITEVALUETOFILE(value);
78 if (value)
80 if (fwrite((LPCTSTR)path, sizeof(TCHAR), value, pFile)!=value)
81 return false;
82 git_wc_status_kind status = I->second;
83 WRITEVALUETOFILE(status);
86 // WRITEVALUETOFILE(m_propsFileTime);
87 value = m_directoryPath.GetWinPathString().GetLength();
88 WRITEVALUETOFILE(value);
89 if (value)
91 if (fwrite(m_directoryPath.GetWinPath(), sizeof(TCHAR), value, pFile)!=value)
92 return false;
94 if (!m_ownStatus.SaveToDisk(pFile))
95 return false;
96 WRITEVALUETOFILE(m_currentFullStatus);
97 WRITEVALUETOFILE(m_mostImportantFileStatus);
98 return true;
101 BOOL CCachedDirectory::LoadFromDisk(FILE * pFile)
103 AutoLocker lock(m_critSec);
104 #define LOADVALUEFROMFILE(x) if (fread(&x, sizeof(x), 1, pFile)!=1) return false;
107 unsigned int value = 0;
108 LOADVALUEFROMFILE(value);
109 if (value != GIT_CACHE_VERSION)
110 return false; // not the correct version
111 int mapsize = 0;
112 LOADVALUEFROMFILE(mapsize);
113 for (int i=0; i<mapsize; ++i)
115 LOADVALUEFROMFILE(value);
116 if (value > MAX_PATH)
117 return false;
118 if (value)
120 CString sKey;
121 if (fread(sKey.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
123 sKey.ReleaseBuffer(0);
124 return false;
126 sKey.ReleaseBuffer(value);
127 CStatusCacheEntry entry;
128 if (!entry.LoadFromDisk(pFile))
129 return false;
130 m_entryCache[sKey] = entry;
133 LOADVALUEFROMFILE(mapsize);
134 for (int i=0; i<mapsize; ++i)
136 LOADVALUEFROMFILE(value);
137 if (value > MAX_PATH)
138 return false;
139 if (value)
141 CString sPath;
142 if (fread(sPath.GetBuffer(value), sizeof(TCHAR), value, pFile)!=value)
144 sPath.ReleaseBuffer(0);
145 return false;
147 sPath.ReleaseBuffer(value);
148 git_wc_status_kind status;
149 LOADVALUEFROMFILE(status);
150 m_childDirectories[CTGitPath(sPath)] = status;
153 LOADVALUEFROMFILE(value);
154 if (value > MAX_PATH)
155 return false;
156 if (value)
158 CString sPath;
159 if (fread(sPath.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
161 sPath.ReleaseBuffer(0);
162 return false;
164 sPath.ReleaseBuffer(value);
165 m_directoryPath.SetFromWin(sPath);
166 m_directoryPath.GetGitPathString(); // make sure git path string is set
168 if (!m_ownStatus.LoadFromDisk(pFile))
169 return false;
171 LOADVALUEFROMFILE(m_currentFullStatus);
172 LOADVALUEFROMFILE(m_mostImportantFileStatus);
174 catch ( CAtlException )
176 return false;
178 return true;
183 CStatusCacheEntry CCachedDirectory::GetStatusFromCache(const CTGitPath& path, bool bRecursive)
185 if(path.IsDirectory())
187 // We don't have directory status in our cache
188 // Ask the directory if it knows its own status
189 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
190 if( dirEntry)
192 if (dirEntry->IsOwnStatusValid())
193 return dirEntry->GetOwnStatus(bRecursive);
194 else
196 /* cache have outof date, need crawl again*/
198 /*AutoLocker lock(dirEntry->m_critSec);
199 ChildDirStatus::const_iterator it;
200 for(it = dirEntry->m_childDirectories.begin(); it != dirEntry->m_childDirectories.end(); ++it)
202 CGitStatusCache::Instance().AddFolderForCrawling(it->first);
205 CGitStatusCache::Instance().AddFolderForCrawling(path);
207 /*Return old status during crawling*/
208 return dirEntry->GetOwnStatus(bRecursive);
211 else
213 CGitStatusCache::Instance().AddFolderForCrawling(path);
215 return CStatusCacheEntry();
217 else
219 //All file ignored if under ignore directory
220 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_ignored)
221 return CStatusCacheEntry(git_wc_status_ignored);
222 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_unversioned)
223 return CStatusCacheEntry(git_wc_status_unversioned);
225 // Look up a file in our own cache
226 AutoLocker lock(m_critSec);
227 CString strCacheKey = GetCacheKey(path);
228 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
229 if(itMap != m_entryCache.end())
231 // We've hit the cache - check for timeout
232 if(!itMap->second.HasExpired((long)GetTickCount()))
234 if(itMap->second.DoesFileTimeMatch(path.GetLastWriteTime()))
236 if ((itMap->second.GetEffectiveStatus()!=git_wc_status_missing)||(!PathFileExists(path.GetWinPath())))
238 // Note: the filetime matches after a modified has been committed too.
239 // So in that case, we would return a wrong status (e.g. 'modified' instead
240 // of 'normal') here.
241 return itMap->second;
247 CGitStatusCache::Instance().AddFolderForCrawling(path.GetContainingDirectory());
248 return CStatusCacheEntry();
253 CStatusCacheEntry CCachedDirectory::GetStatusFromGit(const CTGitPath &path, CString sProjectRoot)
255 CString subpaths = path.GetGitPathString();
256 if(subpaths.GetLength() >= sProjectRoot.GetLength())
258 if(subpaths[sProjectRoot.GetLength()] == _T('/'))
259 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength()-1);
260 else
261 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength());
264 GitStatus *pGitStatus = &CGitStatusCache::Instance().m_GitStatus;
265 UNREFERENCED_PARAMETER(pGitStatus);
267 CGitHash head;
269 pGitStatus->GetHeadHash(sProjectRoot,head);
271 bool isVersion =true;
272 pGitStatus->IsUnderVersionControl(sProjectRoot, subpaths, path.IsDirectory(), &isVersion);
273 if(!isVersion)
274 { //untracked file
275 bool isIgnoreFileChanged=false;
277 isIgnoreFileChanged = pGitStatus->IsGitReposChanged(sProjectRoot, subpaths, GIT_MODE_IGNORE);
279 if( isIgnoreFileChanged)
281 pGitStatus->LoadIgnoreFile(sProjectRoot, subpaths);
284 if(path.IsDirectory())
287 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path,
288 false); /* we needn't watch untracked directory*/
290 if(dirEntry)
292 AutoLocker lock(dirEntry->m_critSec);
294 git_wc_status_kind dirstatus = dirEntry->GetCurrentFullStatus() ;
295 if( dirstatus == git_wc_status_none || dirstatus >= git_wc_status_normal || isIgnoreFileChanged )
296 {/* status have not initialized*/
297 git_wc_status2_t status2;
298 bool isignore = false;
299 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
300 status2.text_status = status2.prop_status =
301 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
303 dirEntry->m_ownStatus.SetKind(git_node_dir);
304 dirEntry->m_ownStatus.SetStatus(&status2);
307 return dirEntry->m_ownStatus;
311 else /* path is file */
313 AutoLocker lock(m_critSec);
314 CString strCacheKey = GetCacheKey(path);
316 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
317 if(itMap == m_entryCache.end() || isIgnoreFileChanged)
319 git_wc_status2_t status2;
320 bool isignore = false;
321 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
322 status2.text_status = status2.prop_status =
323 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
324 AddEntry(path, &status2);
325 return m_entryCache[strCacheKey];
327 else
329 return itMap->second;
332 return CStatusCacheEntry();
335 else
337 EnumFiles((CTGitPath*)&path, TRUE);
338 UpdateCurrentStatus();
339 return CStatusCacheEntry(m_ownStatus);
344 /// bFetch is true, fetch all status, call by crawl.
345 /// bFetch is false, get cache status, return quickly.
347 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive, bool bFetch /* = true */)
349 CString sProjectRoot;
350 bool bIsVersionedPath;
352 bool bRequestForSelf = false;
353 if(path.IsEquivalentToWithoutCase(m_directoryPath))
355 bRequestForSelf = true;
356 AutoLocker lock(m_critSec);
357 // HasAdminDir might modify m_directoryPath, so we need to do it synchronized
358 bIsVersionedPath = m_directoryPath.HasAdminDir(&sProjectRoot);
360 else
361 bIsVersionedPath = path.HasAdminDir(&sProjectRoot);
363 // In all most circumstances, we ask for the status of a member of this directory.
364 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
366 //If is not version control path
367 if( !bIsVersionedPath)
369 ATLTRACE(_T("%s is not underversion control\n"), path.GetWinPath());
370 return CStatusCacheEntry();
373 // We've not got this item in the cache - let's add it
374 // We never bother asking SVN for the status of just one file, always for its containing directory
376 if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))
378 // We're being asked for the status of an .git directory
379 // It's not worth asking for this
380 return CStatusCacheEntry();
384 if(bFetch)
386 return GetStatusFromGit(path, sProjectRoot);
388 else
390 return GetStatusFromCache(path, bRecursive);
394 CStatusCacheEntry CCachedDirectory::GetCacheStatusForMember(const CTGitPath& path)
396 // no disk access!
397 AutoLocker lock(m_critSec);
398 CacheEntryMap::iterator itMap = m_entryCache.find(GetCacheKey(path));
399 if(itMap != m_entryCache.end())
400 return itMap->second;
402 return CStatusCacheEntry();
405 int CCachedDirectory::EnumFiles(CTGitPath *path , bool IsFull)
407 CString sProjectRoot;
408 if(path)
409 path->HasAdminDir(&sProjectRoot);
410 else
411 m_directoryPath.HasAdminDir(&sProjectRoot);
413 ATLTRACE(_T("EnumFiles %s\n"), path->GetWinPath());
415 ATLASSERT( !m_directoryPath.IsEmpty() );
417 CString sSubPath;
419 CString s;
420 if(path)
421 s=path->GetWinPath();
422 else
423 s=m_directoryPath.GetDirectory().GetWinPathString();
425 if (s.GetLength() > sProjectRoot.GetLength())
427 // skip initial slash if necessary
428 if(s[sProjectRoot.GetLength()] == _T('\\'))
429 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
430 else
431 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
434 // strip "\" at the end, otherwise cache lookups for drives do not work correctly
435 sProjectRoot.TrimRight(_T("\\"));
437 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
438 UNREFERENCED_PARAMETER(pStatus);
439 git_wc_status_kind status = git_wc_status_none;
441 if(!path->IsDirectory())
443 bool assumeValid = false;
444 bool skipWorktree = false;
445 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback, this, &assumeValid, &skipWorktree);
447 else
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 //ATLTRACE(_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 CString cachekey = GetCacheKey(path);
513 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
514 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
516 if (pGitStatus)
518 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
519 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
522 bNotified =true;
527 else
529 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
530 bNotified = true;
533 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
534 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
535 entry_it->second.SetKind(git_node_file);
537 if(bNotified)
539 CGitStatusCache::Instance().UpdateShell(path);
540 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
543 //ATLTRACE(_T("Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
546 CCachedDirectory * parent = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
548 if(parent)
550 if ((parent->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
552 if(pGitStatus)
554 if(parent->GetCurrentFullStatus() < GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
556 CGitStatusCache::Instance().UpdateShell(parent->m_directoryPath);
557 //ATLTRACE(_T("shell update for %s\n"), parent->m_directoryPath.GetWinPathString());
558 parent->m_ownStatus.SetKind(git_node_dir);
559 parent->m_ownStatus.SetStatus(pGitStatus);
567 CString
568 CCachedDirectory::GetCacheKey(const CTGitPath& path)
570 // All we put into the cache as a key is just the end portion of the pathname
571 // There's no point storing the path of the containing directory for every item
572 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower().TrimLeft(_T("\\"));
575 CString
576 CCachedDirectory::GetFullPathString(const CString& cacheKey)
578 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
581 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *, bool assumeValid, bool skipWorktree)
583 git_wc_status2_t _status;
584 git_wc_status2_t *status2 = &_status;
586 status2->prop_status = status2->text_status = status;
587 status2->assumeValid = assumeValid;
588 status2->skipWorktree = skipWorktree;
590 CTGitPath gitPath;
592 CString lowcasepath = path;
593 lowcasepath.MakeLower();
594 gitPath.SetFromUnknown(lowcasepath);
596 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
598 if(pThis == NULL)
599 return FALSE;
601 // if(status->entry)
603 if (isDir)
604 { /*gitpath is directory*/
605 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
607 if (!gitPath.Exists())
609 ATLTRACE(_T("Miss dir %s \n"), gitPath.GetWinPath());
610 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
613 if ( status < git_wc_status_normal)
615 if( ::PathFileExists(path+_T("\\.git")))
616 { // this is submodule
617 ATLTRACE(_T("skip submodule %s\n"), path);
618 return FALSE;
621 if (pThis->m_bRecursive)
623 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
624 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
625 if(status >= git_wc_status_normal)
626 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
629 // Make sure we know about this child directory
630 // This initial status value is likely to be overwritten from below at some point
631 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
633 // folders must not be displayed as added or deleted only as modified
634 if (s == git_wc_status_deleted || s == git_wc_status_added)
635 s = git_wc_status_modified;
637 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
638 if (cdir)
640 // This child directory is already in our cache!
641 // So ask this dir about its recursive status
642 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
643 AutoLocker lock(pThis->m_critSec);
644 pThis->m_childDirectories[gitPath] = st;
645 ATLTRACE(_T("call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
647 else
649 AutoLocker lock(pThis->m_critSec);
650 // the child directory is not in the cache. Create a new entry for it in the cache which is
651 // initially 'unversioned'. But we added that directory to the crawling list above, which
652 // means the cache will be updated soon.
653 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
655 pThis->m_childDirectories[gitPath] = s;
656 ATLTRACE(_T("call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
660 else /* gitpath is file*/
662 // Keep track of the most important status of all the files in this directory
663 // Don't include subdirectories in this figure, because they need to provide their
664 // own 'most important' value
665 if (status2->text_status == git_wc_status_deleted || status2->text_status == git_wc_status_added)
667 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
668 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
670 else
672 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
673 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
674 if ((status2->text_status == git_wc_status_unversioned)
675 &&(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);
685 pThis->AddEntry(gitPath, status2);
687 return FALSE;
690 bool
691 CCachedDirectory::IsOwnStatusValid() const
693 return m_ownStatus.HasBeenSet() &&
694 !m_ownStatus.HasExpired(GetTickCount());
697 void CCachedDirectory::Invalidate()
699 m_ownStatus.Invalidate();
702 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
704 // Combine our OWN folder status with the most important of our *FILES'* status.
705 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
707 // Now combine all our child-directorie's status
708 AutoLocker lock(m_critSec);
709 ChildDirStatus::const_iterator it;
710 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
712 retVal = GitStatus::GetMoreImportant(retVal, it->second);
715 return retVal;
718 // Update our composite status and deal with things if it's changed
719 void CCachedDirectory::UpdateCurrentStatus()
721 git_wc_status_kind newStatus = CalculateRecursiveStatus();
722 ATLTRACE(_T("UpdateCurrentStatus %s new:%d old: %d\n"),
723 m_directoryPath.GetWinPath(),
724 newStatus, m_currentFullStatus);
726 if ( this->m_ownStatus.GetEffectiveStatus() < git_wc_status_normal )
728 if (::PathFileExists(this->m_directoryPath.GetWinPathString()+_T("\\.git")))
730 //project root must be normal status at least.
731 ATLTRACE(_T("force update project root directory as normal status\n"));
732 this->m_ownStatus.ForceStatus(git_wc_status_normal);
736 if ((newStatus != m_currentFullStatus) && m_ownStatus.IsVersioned())
738 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
740 // Our status has changed - tell the shell
741 ATLTRACE(_T("Dir %s, status change from %d to %d, send shell notification\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
742 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
744 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
745 m_currentFullStatus = newStatus;
746 else
747 m_currentFullStatus = git_wc_status_missing;
749 // And tell our parent, if we've got one...
750 // we tell our parent *always* about our status, even if it hasn't
751 // changed. This is to make sure that the parent has really our current
752 // status - the parent can decide itself if our status has changed
753 // or not.
754 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
755 if(!parentPath.IsEmpty())
757 // We have a parent
758 // just version controled directory need to cache.
759 CString root1, root2;
760 if(parentPath.HasAdminDir(&root1) && m_directoryPath.HasAdminDir(&root2) && root1 == root2)
762 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
763 if (cachedDir)
764 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
770 // Receive a notification from a child that its status has changed
771 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
773 git_wc_status_kind currentStatus = git_wc_status_none;
775 AutoLocker lock(m_critSec);
776 currentStatus = m_childDirectories[childDir];
778 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
781 AutoLocker lock(m_critSec);
782 m_childDirectories[childDir] = childStatus;
784 UpdateCurrentStatus();
788 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
790 // Don't return recursive status if we're unversioned ourselves.
791 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned && m_ownStatus.GetEffectiveStatus() != git_wc_status_ignored)
793 CStatusCacheEntry recursiveStatus(m_ownStatus);
794 UpdateCurrentStatus();
795 if (m_currentFullStatus == git_wc_status_deleted || m_currentFullStatus == git_wc_status_added)
796 m_currentFullStatus = git_wc_status_modified;
797 recursiveStatus.ForceStatus(m_currentFullStatus);
798 return recursiveStatus;
800 else
802 return m_ownStatus;
806 void CCachedDirectory::RefreshStatus(bool bRecursive)
808 // Make sure that our own status is up-to-date
809 GetStatusForMember(m_directoryPath,bRecursive);
811 AutoLocker lock(m_critSec);
812 // We also need to check if all our file members have the right date on them
813 CacheEntryMap::iterator itMembers;
814 std::set<CTGitPath> refreshedpaths;
815 DWORD now = GetTickCount();
816 if (m_entryCache.empty())
817 return;
818 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
820 if (itMembers->first)
822 CTGitPath filePath(m_directoryPath);
823 filePath.AppendPathString(itMembers->first);
824 std::set<CTGitPath>::iterator refr_it;
825 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
826 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
828 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
830 lock.Unlock();
831 // We need to request this item as well
832 GetStatusForMember(filePath,bRecursive);
833 // GetStatusForMember now has recreated the m_entryCache map.
834 // So start the loop again, but add this path to the refreshed paths set
835 // to make sure we don't refresh this path again. This is to make sure
836 // that we don't end up in an endless loop.
837 lock.Lock();
838 refreshedpaths.insert(refr_it, filePath);
839 itMembers = m_entryCache.begin();
840 if (m_entryCache.empty())
841 return;
842 continue;
844 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
846 // crawl all sub folders too! Otherwise a change deep inside the
847 // tree which has changed won't get propagated up the tree.
848 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
855 void CCachedDirectory::RefreshMostImportant()
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 ATLTRACE(_T("status change of path %s\n"), m_directoryPath.GetWinPath());
873 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
875 m_mostImportantFileStatus = newStatus;