TGitCache: Project root folder might not get crawled correctly
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob7998ad3ec71a17ab340b6fa44d7322ddfe5562ba
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;
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 m_entryCache[sKey] = entry;
132 LOADVALUEFROMFILE(mapsize);
133 for (int i=0; i<mapsize; ++i)
135 LOADVALUEFROMFILE(value);
136 if (value > MAX_PATH)
137 return false;
138 if (value)
140 CString sPath;
141 if (fread(sPath.GetBuffer(value), sizeof(TCHAR), value, pFile)!=value)
143 sPath.ReleaseBuffer(0);
144 return false;
146 sPath.ReleaseBuffer(value);
147 git_wc_status_kind status;
148 LOADVALUEFROMFILE(status);
149 m_childDirectories[CTGitPath(sPath)] = status;
152 LOADVALUEFROMFILE(value);
153 if (value > MAX_PATH)
154 return false;
155 if (value)
157 CString sPath;
158 if (fread(sPath.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
160 sPath.ReleaseBuffer(0);
161 return false;
163 sPath.ReleaseBuffer(value);
164 m_directoryPath.SetFromWin(sPath);
166 if (!m_ownStatus.LoadFromDisk(pFile))
167 return false;
169 LOADVALUEFROMFILE(m_currentFullStatus);
170 LOADVALUEFROMFILE(m_mostImportantFileStatus);
172 catch ( CAtlException )
174 return false;
176 return true;
181 CStatusCacheEntry CCachedDirectory::GetStatusFromCache(const CTGitPath& path, bool bRecursive)
183 if(path.IsDirectory())
185 // We don't have directory status in our cache
186 // Ask the directory if it knows its own status
187 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
188 if( dirEntry)
190 if (dirEntry->IsOwnStatusValid())
191 return dirEntry->GetOwnStatus(bRecursive);
192 else
194 /* cache have outof date, need crawl again*/
196 /*AutoLocker lock(dirEntry->m_critSec);
197 ChildDirStatus::const_iterator it;
198 for(it = dirEntry->m_childDirectories.begin(); it != dirEntry->m_childDirectories.end(); ++it)
200 CGitStatusCache::Instance().AddFolderForCrawling(it->first);
203 CGitStatusCache::Instance().AddFolderForCrawling(path);
205 /*Return old status during crawling*/
206 return dirEntry->GetOwnStatus(bRecursive);
209 else
211 CGitStatusCache::Instance().AddFolderForCrawling(path);
213 return CStatusCacheEntry();
215 else
217 //All file ignored if under ignore directory
218 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_ignored)
219 return CStatusCacheEntry(git_wc_status_ignored);
220 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_unversioned)
221 return CStatusCacheEntry(git_wc_status_unversioned);
223 // Look up a file in our own cache
224 AutoLocker lock(m_critSec);
225 CString strCacheKey = GetCacheKey(path);
226 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
227 if(itMap != m_entryCache.end())
229 // We've hit the cache - check for timeout
230 if(!itMap->second.HasExpired((long)GetTickCount()))
232 if(itMap->second.DoesFileTimeMatch(path.GetLastWriteTime()))
234 if ((itMap->second.GetEffectiveStatus()!=git_wc_status_missing)||(!PathFileExists(path.GetWinPath())))
236 // Note: the filetime matches after a modified has been committed too.
237 // So in that case, we would return a wrong status (e.g. 'modified' instead
238 // of 'normal') here.
239 return itMap->second;
245 CGitStatusCache::Instance().AddFolderForCrawling(path.GetContainingDirectory());
246 return CStatusCacheEntry();
251 CStatusCacheEntry CCachedDirectory::GetStatusFromGit(const CTGitPath &path, CString sProjectRoot)
253 CString subpaths = path.GetGitPathString();
254 if(subpaths.GetLength() >= sProjectRoot.GetLength())
256 if(subpaths[sProjectRoot.GetLength()] == _T('/'))
257 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength()-1);
258 else
259 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength());
262 GitStatus *pGitStatus = &CGitStatusCache::Instance().m_GitStatus;
264 CGitHash head;
266 pGitStatus->GetHeadHash(sProjectRoot,head);
268 bool isVersion =true;
269 pGitStatus->IsUnderVersionControl(sProjectRoot, subpaths, path.IsDirectory(), &isVersion);
270 if(!isVersion)
271 { //untracked file
272 bool isIgnoreFileChanged=false;
274 isIgnoreFileChanged = pGitStatus->IsGitReposChanged(sProjectRoot, subpaths, GIT_MODE_IGNORE);
276 if( isIgnoreFileChanged)
278 pGitStatus->LoadIgnoreFile(sProjectRoot, subpaths);
281 if(path.IsDirectory())
284 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path,
285 false); /* we needn't watch untracked directory*/
287 if(dirEntry)
289 AutoLocker lock(dirEntry->m_critSec);
291 git_wc_status_kind dirstatus = dirEntry->GetCurrentFullStatus() ;
292 if( dirstatus == git_wc_status_none || dirstatus >= git_wc_status_normal || isIgnoreFileChanged )
293 {/* status have not initialized*/
294 git_wc_status2_t status2;
295 bool isignore = false;
296 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
297 status2.text_status = status2.prop_status =
298 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
300 dirEntry->m_ownStatus.SetStatus(&status2);
301 dirEntry->m_ownStatus.SetKind(git_node_dir);
304 return dirEntry->m_ownStatus;
308 else /* path is file */
310 AutoLocker lock(m_critSec);
311 CString strCacheKey = GetCacheKey(path);
313 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
314 if(itMap == m_entryCache.end() || isIgnoreFileChanged)
316 git_wc_status2_t status2;
317 bool isignore = false;
318 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
319 status2.text_status = status2.prop_status =
320 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
321 AddEntry(path, &status2);
322 return m_entryCache[strCacheKey];
324 else
326 return itMap->second;
329 return CStatusCacheEntry();
332 else
334 EnumFiles((CTGitPath*)&path, TRUE);
335 UpdateCurrentStatus();
336 return CStatusCacheEntry(m_ownStatus);
341 /// bFetch is true, fetch all status, call by crawl.
342 /// bFetch is false, get cache status, return quickly.
344 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive, bool bFetch /* = true */)
346 bool bRequestForSelf = false;
347 if(path.IsEquivalentToWithoutCase(m_directoryPath))
349 bRequestForSelf = true;
351 //OutputDebugStringA("GetStatusForMember: ");OutputDebugStringW(path.GetWinPathString());OutputDebugStringA("\r\n");
352 // In all most circumstances, we ask for the status of a member of this directory.
353 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
355 CString sProjectRoot;
356 const BOOL bIsVersionedPath = path.HasAdminDir(&sProjectRoot);
358 //If is not version control path
359 if( !bIsVersionedPath)
361 ATLTRACE(_T("%s is not underversion control\n"), path.GetWinPath());
362 return CStatusCacheEntry();
365 // We've not got this item in the cache - let's add it
366 // We never bother asking SVN for the status of just one file, always for its containing directory
368 if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))
370 // We're being asked for the status of an .git directory
371 // It's not worth asking for this
372 return CStatusCacheEntry();
376 if(bFetch)
378 return GetStatusFromGit(path, sProjectRoot);
380 else
382 return GetStatusFromCache(path, bRecursive);
386 CStatusCacheEntry CCachedDirectory::GetCacheStatusForMember(const CTGitPath& path)
388 // no disk access!
389 AutoLocker lock(m_critSec);
390 CacheEntryMap::iterator itMap = m_entryCache.find(GetCacheKey(path));
391 if(itMap != m_entryCache.end())
392 return itMap->second;
394 return CStatusCacheEntry();
397 int CCachedDirectory::EnumFiles(CTGitPath *path , bool IsFull)
399 CString sProjectRoot;
400 if(path)
401 path->HasAdminDir(&sProjectRoot);
402 else
403 m_directoryPath.HasAdminDir(&sProjectRoot);
405 ATLTRACE(_T("EnumFiles %s\n"), path->GetWinPath());
407 ATLASSERT( !m_directoryPath.IsEmpty() );
409 CString sSubPath;
411 CString s;
412 if(path)
413 s=path->GetWinPath();
414 else
415 s=m_directoryPath.GetDirectory().GetWinPathString();
417 if (s.GetLength() > sProjectRoot.GetLength())
419 // skip initial slash if necessary
420 if(s[sProjectRoot.GetLength()] == _T('\\'))
421 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
422 else
423 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
426 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
427 UNREFERENCED_PARAMETER(pStatus);
428 git_wc_status_kind status;
429 bool assumeValid = false;
431 if(!path->IsDirectory())
432 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback, this, &assumeValid);
433 else
435 m_mostImportantFileStatus = git_wc_status_normal;
436 pStatus->EnumDirStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback,this);
438 m_ownStatus = git_wc_status_normal;
441 m_mostImportantFileStatus = GitStatus::GetMoreImportant(m_mostImportantFileStatus, status);
443 return 0;
445 void
446 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
448 AutoLocker lock(m_critSec);
449 if(path.IsDirectory())
451 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
452 if (childDir)
454 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
456 if(pGitStatus)
458 if(childDir->GetCurrentFullStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
460 CGitStatusCache::Instance().UpdateShell(path);
461 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
462 childDir->m_ownStatus.SetStatus(pGitStatus);
463 childDir->m_ownStatus.SetKind(git_node_dir);
467 childDir->m_ownStatus.SetKind(git_node_dir);
472 else
474 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
475 bool bNotified = false;
477 if(!childDir)
478 return ;
480 CString cachekey = GetCacheKey(path);
481 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
482 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
484 if (pGitStatus)
486 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
487 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
490 bNotified =true;
495 else
497 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
498 bNotified = true;
501 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
502 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
503 entry_it->second.SetKind(git_node_file);
505 if(bNotified)
507 CGitStatusCache::Instance().UpdateShell(path);
508 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
511 //ATLTRACE(_T("Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
514 CCachedDirectory * parent = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
516 if(parent)
518 if ((parent->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
520 if(pGitStatus)
522 if(parent->GetCurrentFullStatus() < GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
524 CGitStatusCache::Instance().UpdateShell(parent->m_directoryPath);
525 //ATLTRACE(_T("shell update for %s\n"), parent->m_directoryPath.GetWinPathString());
526 parent->m_ownStatus.SetStatus(pGitStatus);
527 parent->m_ownStatus.SetKind(git_node_dir);
535 CString
536 CCachedDirectory::GetCacheKey(const CTGitPath& path)
538 // All we put into the cache as a key is just the end portion of the pathname
539 // There's no point storing the path of the containing directory for every item
540 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower();
543 CString
544 CCachedDirectory::GetFullPathString(const CString& cacheKey)
546 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
549 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *pUserData, bool assumeValid)
551 git_wc_status2_t _status;
552 git_wc_status2_t *status2 = &_status;
554 status2->prop_status = status2->text_status = status;
555 status2->assumeValid = assumeValid;
557 CTGitPath gitPath;
559 CString lowcasepath = path;
560 lowcasepath.MakeLower();
561 gitPath.SetFromUnknown(lowcasepath);
563 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
565 if(pThis == NULL)
566 return FALSE;
568 // if(status->entry)
570 if (isDir)
571 { /*gitpath is directory*/
572 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
574 if (!gitPath.Exists())
576 ATLTRACE(_T("Miss dir %s \n"), gitPath.GetWinPath());
577 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
580 if ( status < git_wc_status_normal)
582 if( ::PathFileExists(path+_T("\\.git")))
583 { // this is submodule
584 ATLTRACE(_T("skip submodule %s\n"), path);
585 return FALSE;
588 if (pThis->m_bRecursive)
590 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
591 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
592 if(status >= git_wc_status_normal)
593 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
596 // Make sure we know about this child directory
597 // This initial status value is likely to be overwritten from below at some point
598 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
600 // folders must not be displayed as added or deleted only as modified
601 if (s == git_wc_status_deleted || s == git_wc_status_added)
602 s = git_wc_status_modified;
604 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
605 if (cdir)
607 // This child directory is already in our cache!
608 // So ask this dir about its recursive status
609 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
610 AutoLocker lock(pThis->m_critSec);
611 pThis->m_childDirectories[gitPath] = st;
612 ATLTRACE(_T("call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
614 else
616 AutoLocker lock(pThis->m_critSec);
617 // the child directory is not in the cache. Create a new entry for it in the cache which is
618 // initially 'unversioned'. But we added that directory to the crawling list above, which
619 // means the cache will be updated soon.
620 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
622 pThis->m_childDirectories[gitPath] = s;
623 ATLTRACE(_T("call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
627 else /* gitpath is file*/
629 // Keep track of the most important status of all the files in this directory
630 // Don't include subdirectories in this figure, because they need to provide their
631 // own 'most important' value
632 if (status2->text_status == git_wc_status_deleted || status2->text_status == git_wc_status_added)
634 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
635 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
637 else
639 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
640 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
641 if (((status2->text_status == git_wc_status_unversioned)||(status2->text_status == git_wc_status_none))
642 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
644 // treat unversioned files as modified
645 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
646 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
652 pThis->AddEntry(gitPath, status2);
654 return FALSE;
657 bool
658 CCachedDirectory::IsOwnStatusValid() const
660 return m_ownStatus.HasBeenSet() &&
661 !m_ownStatus.HasExpired(GetTickCount());
664 void CCachedDirectory::Invalidate()
666 m_ownStatus.Invalidate();
669 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
671 // Combine our OWN folder status with the most important of our *FILES'* status.
672 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
674 // Now combine all our child-directorie's status
675 AutoLocker lock(m_critSec);
676 ChildDirStatus::const_iterator it;
677 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
679 retVal = GitStatus::GetMoreImportant(retVal, it->second);
682 return retVal;
685 // Update our composite status and deal with things if it's changed
686 void CCachedDirectory::UpdateCurrentStatus()
688 git_wc_status_kind newStatus = CalculateRecursiveStatus();
689 ATLTRACE(_T("UpdateCurrentStatus %s new:%d old: %d\n"),
690 m_directoryPath.GetWinPath(),
691 newStatus, m_currentFullStatus);
693 if ( this->m_ownStatus.GetEffectiveStatus() < git_wc_status_normal )
695 if (::PathFileExists(this->m_directoryPath.GetWinPathString()+_T("\\.git")))
697 //project root must be normal status at least.
698 ATLTRACE(_T("force update project root directory as normal status\n"));
699 this->m_ownStatus.ForceStatus(git_wc_status_normal);
703 if ((newStatus != m_currentFullStatus) && m_ownStatus.IsVersioned())
705 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
707 // Our status has changed - tell the shell
708 ATLTRACE(_T("Dir %s, status change from %d to %d, send shell notification\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
709 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
711 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
712 m_currentFullStatus = newStatus;
713 else
714 m_currentFullStatus = git_wc_status_missing;
716 // And tell our parent, if we've got one...
717 // we tell our parent *always* about our status, even if it hasn't
718 // changed. This is to make sure that the parent has really our current
719 // status - the parent can decide itself if our status has changed
720 // or not.
721 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
722 if(!parentPath.IsEmpty())
724 // We have a parent
725 // just version controled directory need to cache.
726 CString root1, root2;
727 if(parentPath.HasAdminDir(&root1) && m_directoryPath.HasAdminDir(&root2) && root1 == root2)
729 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
730 if (cachedDir)
731 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
737 // Receive a notification from a child that its status has changed
738 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
740 git_wc_status_kind currentStatus = git_wc_status_none;
742 AutoLocker lock(m_critSec);
743 currentStatus = m_childDirectories[childDir];
745 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
748 AutoLocker lock(m_critSec);
749 m_childDirectories[childDir] = childStatus;
751 UpdateCurrentStatus();
755 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
757 // Don't return recursive status if we're unversioned ourselves.
758 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned && m_ownStatus.GetEffectiveStatus() != git_wc_status_ignored)
760 CStatusCacheEntry recursiveStatus(m_ownStatus);
761 UpdateCurrentStatus();
762 if (m_currentFullStatus == git_wc_status_deleted || m_currentFullStatus == git_wc_status_added)
763 m_currentFullStatus = git_wc_status_modified;
764 recursiveStatus.ForceStatus(m_currentFullStatus);
765 return recursiveStatus;
767 else
769 return m_ownStatus;
773 void CCachedDirectory::RefreshStatus(bool bRecursive)
775 // Make sure that our own status is up-to-date
776 GetStatusForMember(m_directoryPath,bRecursive);
778 AutoLocker lock(m_critSec);
779 // We also need to check if all our file members have the right date on them
780 CacheEntryMap::iterator itMembers;
781 std::set<CTGitPath> refreshedpaths;
782 DWORD now = GetTickCount();
783 if (m_entryCache.size() == 0)
784 return;
785 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
787 if (itMembers->first)
789 CTGitPath filePath(m_directoryPath);
790 filePath.AppendPathString(itMembers->first);
791 std::set<CTGitPath>::iterator refr_it;
792 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
793 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
795 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
797 lock.Unlock();
798 // We need to request this item as well
799 GetStatusForMember(filePath,bRecursive);
800 // GetStatusForMember now has recreated the m_entryCache map.
801 // So start the loop again, but add this path to the refreshed paths set
802 // to make sure we don't refresh this path again. This is to make sure
803 // that we don't end up in an endless loop.
804 lock.Lock();
805 refreshedpaths.insert(refr_it, filePath);
806 itMembers = m_entryCache.begin();
807 if (m_entryCache.size()==0)
808 return;
809 continue;
811 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
813 // crawl all sub folders too! Otherwise a change deep inside the
814 // tree which has changed won't get propagated up the tree.
815 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
822 void CCachedDirectory::RefreshMostImportant()
824 CacheEntryMap::iterator itMembers;
825 git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();
826 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
828 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
829 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
830 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
832 // treat unversioned files as modified
833 if (newStatus != git_wc_status_added)
834 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
837 if (newStatus != m_mostImportantFileStatus)
839 ATLTRACE(_T("status change of path %s\n"), m_directoryPath.GetWinPath());
840 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
842 m_mostImportantFileStatus = newStatus;