Implement commands to (un)block paths from getting crawled
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob7dd7b84c923a7526f0e88f77dd3e85d2a06ee910
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;
31 m_indexFileTime = 0;
34 CCachedDirectory::~CCachedDirectory(void)
38 CCachedDirectory::CCachedDirectory(const CTGitPath& directoryPath)
40 ATLASSERT(directoryPath.IsDirectory() || !PathFileExists(directoryPath.GetWinPath()));
42 m_directoryPath = directoryPath;
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_indexFileTime);
87 WRITEVALUETOFILE(m_Head.m_hash);
88 // WRITEVALUETOFILE(m_propsFileTime);
89 value = m_directoryPath.GetWinPathString().GetLength();
90 WRITEVALUETOFILE(value);
91 if (value)
93 if (fwrite(m_directoryPath.GetWinPath(), sizeof(TCHAR), value, pFile)!=value)
94 return false;
96 if (!m_ownStatus.SaveToDisk(pFile))
97 return false;
98 WRITEVALUETOFILE(m_currentFullStatus);
99 WRITEVALUETOFILE(m_mostImportantFileStatus);
100 return true;
103 BOOL CCachedDirectory::LoadFromDisk(FILE * pFile)
105 AutoLocker lock(m_critSec);
106 #define LOADVALUEFROMFILE(x) if (fread(&x, sizeof(x), 1, pFile)!=1) return false;
109 unsigned int value = 0;
110 LOADVALUEFROMFILE(value);
111 if (value != GIT_CACHE_VERSION)
112 return false; // not the correct version
113 int mapsize = 0;
114 LOADVALUEFROMFILE(mapsize);
115 for (int i=0; i<mapsize; ++i)
117 LOADVALUEFROMFILE(value);
118 if (value > MAX_PATH)
119 return false;
120 if (value)
122 CString sKey;
123 if (fread(sKey.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
125 sKey.ReleaseBuffer(0);
126 return false;
128 sKey.ReleaseBuffer(value);
129 CStatusCacheEntry entry;
130 if (!entry.LoadFromDisk(pFile))
131 return false;
132 m_entryCache[sKey] = entry;
135 LOADVALUEFROMFILE(mapsize);
136 for (int i=0; i<mapsize; ++i)
138 LOADVALUEFROMFILE(value);
139 if (value > MAX_PATH)
140 return false;
141 if (value)
143 CString sPath;
144 if (fread(sPath.GetBuffer(value), sizeof(TCHAR), value, pFile)!=value)
146 sPath.ReleaseBuffer(0);
147 return false;
149 sPath.ReleaseBuffer(value);
150 git_wc_status_kind status;
151 LOADVALUEFROMFILE(status);
152 m_childDirectories[CTGitPath(sPath)] = status;
155 LOADVALUEFROMFILE(m_indexFileTime);
156 LOADVALUEFROMFILE(m_Head.m_hash);
157 // LOADVALUEFROMFILE(m_propsFileTime);
158 LOADVALUEFROMFILE(value);
159 if (value > MAX_PATH)
160 return false;
161 if (value)
163 CString sPath;
164 if (fread(sPath.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
166 sPath.ReleaseBuffer(0);
167 return false;
169 sPath.ReleaseBuffer(value);
170 m_directoryPath.SetFromWin(sPath);
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 // 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;
244 else
246 //All file ignored if under ignore directory
247 if( m_currentFullStatus == git_wc_status_ignored)
248 return CStatusCacheEntry(git_wc_status_ignored);
251 CGitStatusCache::Instance().AddFolderForCrawling(path);
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;
270 CGitHash head;
272 pGitStatus->GetHeadHash(sProjectRoot,head);
274 bool isVersion =true;
275 pGitStatus->IsUnderVersionControl(sProjectRoot, subpaths, path.IsDirectory(), &isVersion);
276 if(!isVersion)
277 { //untracked file
278 bool isIgnoreFileChanged=false;
280 isIgnoreFileChanged = pGitStatus->IsGitReposChanged(sProjectRoot, subpaths, GIT_MODE_IGNORE);
282 if( isIgnoreFileChanged)
284 pGitStatus->LoadIgnoreFile(sProjectRoot, subpaths);
287 if(path.IsDirectory())
290 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path,
291 false); /* we needn't watch untracked directory*/
293 if(dirEntry)
295 AutoLocker lock(dirEntry->m_critSec);
297 git_wc_status_kind dirstatus = dirEntry->GetCurrentFullStatus() ;
298 if( dirstatus == git_wc_status_none || dirstatus >= git_wc_status_normal || isIgnoreFileChanged )
299 {/* status have not initialized*/
300 git_wc_status2_t status2;
301 bool isignore = false;
302 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
303 status2.text_status = status2.prop_status =
304 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
306 dirEntry->m_ownStatus.SetStatus(&status2);
307 dirEntry->m_ownStatus.SetKind(git_node_dir);
310 return dirEntry->m_ownStatus;
314 else /* path is file */
316 AutoLocker lock(m_critSec);
317 CString strCacheKey = GetCacheKey(path);
319 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
320 if(itMap == m_entryCache.end() || isIgnoreFileChanged)
322 git_wc_status2_t status2;
323 bool isignore = false;
324 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
325 status2.text_status = status2.prop_status =
326 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
327 AddEntry(path, &status2);
328 return m_entryCache[strCacheKey];
330 else
332 return itMap->second;
335 return CStatusCacheEntry();
338 else
340 EnumFiles((CTGitPath*)&path, TRUE);
341 return CStatusCacheEntry(git_wc_status_normal);
346 /// bFetch is true, fetch all status, call by crawl.
347 /// bFetch is false, get cache status, return quickly.
349 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive, bool bFetch /* = true */)
351 CString strCacheKey;
352 bool bRequestForSelf = false;
353 if(path.IsEquivalentToWithoutCase(m_directoryPath))
355 bRequestForSelf = true;
357 //OutputDebugStringA("GetStatusForMember: ");OutputDebugStringW(path.GetWinPathString());OutputDebugStringA("\r\n");
358 // In all most circumstances, we ask for the status of a member of this directory.
359 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
361 CString sProjectRoot;
362 const BOOL bIsVersionedPath = path.HasAdminDir(&sProjectRoot);
364 //If is not version control path
365 if( !bIsVersionedPath)
367 ATLTRACE(_T("%s is not underversion control\n"), path.GetWinPath());
368 return CStatusCacheEntry();
371 // We've not got this item in the cache - let's add it
372 // We never bother asking SVN for the status of just one file, always for its containing directory
374 if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))
376 // We're being asked for the status of an .git directory
377 // It's not worth asking for this
378 return CStatusCacheEntry();
382 if(bFetch)
384 return GetStatusFromGit(path, sProjectRoot);
386 else
388 return GetStatusFromCache(path, bRecursive);
392 int CCachedDirectory::EnumFiles(CTGitPath *path , bool IsFull)
394 CString sProjectRoot;
395 if(path)
396 path->HasAdminDir(&sProjectRoot);
397 else
398 m_directoryPath.HasAdminDir(&sProjectRoot);
400 ATLTRACE(_T("EnumFiles %s\n"), path->GetWinPath());
402 ATLASSERT( !m_directoryPath.IsEmpty() );
404 CString sSubPath;
406 CString s;
407 if(path)
408 s=path->GetWinPath();
409 else
410 s=m_directoryPath.GetDirectory().GetWinPathString();
412 if (s.GetLength() > sProjectRoot.GetLength())
414 // skip initial slash if necessary
415 if(s[sProjectRoot.GetLength()] == _T('\\'))
416 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
417 else
418 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
421 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
422 UNREFERENCED_PARAMETER(pStatus);
423 git_wc_status_kind status;
425 if(!path->IsDirectory())
426 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false,true, GetStatusCallback,this);
427 else
429 m_mostImportantFileStatus = git_wc_status_normal;
430 pStatus->EnumDirStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback,this);
432 m_ownStatus = git_wc_status_normal;
435 m_mostImportantFileStatus = GitStatus::GetMoreImportant(m_mostImportantFileStatus, status);
437 return 0;
439 void
440 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
442 AutoLocker lock(m_critSec);
443 if(path.IsDirectory())
445 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
446 if (childDir)
448 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
450 if(pGitStatus)
452 if(childDir->GetCurrentFullStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
454 CGitStatusCache::Instance().UpdateShell(path);
455 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
456 childDir->m_ownStatus.SetStatus(pGitStatus);
457 childDir->m_ownStatus.SetKind(git_node_dir);
461 childDir->m_ownStatus.SetKind(git_node_dir);
466 else
468 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
469 bool bNotified = false;
471 if(!childDir)
472 return ;
474 CString cachekey = GetCacheKey(path);
475 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
476 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
478 if (pGitStatus)
480 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
481 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
484 bNotified =true;
489 else
491 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
492 bNotified = true;
495 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
496 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
497 entry_it->second.SetKind(git_node_file);
499 if(bNotified)
501 CGitStatusCache::Instance().UpdateShell(path);
502 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
505 //ATLTRACE(_T("Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
508 CCachedDirectory * parent = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
510 if(parent)
512 if ((parent->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
514 if(pGitStatus)
516 if(parent->GetCurrentFullStatus() < GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
518 CGitStatusCache::Instance().UpdateShell(parent->m_directoryPath);
519 //ATLTRACE(_T("shell update for %s\n"), parent->m_directoryPath.GetWinPathString());
520 parent->m_ownStatus.SetStatus(pGitStatus);
521 parent->m_ownStatus.SetKind(git_node_dir);
529 CString
530 CCachedDirectory::GetCacheKey(const CTGitPath& path)
532 // All we put into the cache as a key is just the end portion of the pathname
533 // There's no point storing the path of the containing directory for every item
534 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower();
537 CString
538 CCachedDirectory::GetFullPathString(const CString& cacheKey)
540 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
543 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *pUserData)
545 git_wc_status2_t _status;
546 git_wc_status2_t *status2 = &_status;
548 status2->prop_status = status2->text_status = status;
550 CTGitPath gitPath;
552 CString lowcasepath = path;
553 lowcasepath.MakeLower();
554 gitPath.SetFromUnknown(lowcasepath);
556 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
558 if(pThis == NULL)
559 return FALSE;
561 // if(status->entry)
563 if (isDir)
564 { /*gitpath is directory*/
565 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
567 if (!gitPath.Exists())
569 ATLTRACE(_T("Miss dir %s \n"), gitPath.GetWinPath());
570 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
573 if ( status < git_wc_status_normal)
575 if( ::PathFileExists(path+_T("\\.git")))
576 { // this is submodule
577 ATLTRACE(_T("skip submodule %s\n"), path);
578 return FALSE;
581 if (pThis->m_bRecursive)
583 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
584 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
585 if(status >= git_wc_status_normal)
586 if(status != git_wc_status_missing)
587 if(status != git_wc_status_deleted)
588 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
591 // Make sure we know about this child directory
592 // This initial status value is likely to be overwritten from below at some point
593 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
594 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
595 if (cdir)
597 // This child directory is already in our cache!
598 // So ask this dir about its recursive status
599 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
600 AutoLocker lock(pThis->m_critSec);
601 pThis->m_childDirectories[gitPath] = st;
602 ATLTRACE(_T("call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
604 else
606 AutoLocker lock(pThis->m_critSec);
607 // the child directory is not in the cache. Create a new entry for it in the cache which is
608 // initially 'unversioned'. But we added that directory to the crawling list above, which
609 // means the cache will be updated soon.
610 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
612 pThis->m_childDirectories[gitPath] = s;
613 ATLTRACE(_T("call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
617 else /* gitpath is file*/
619 // Keep track of the most important status of all the files in this directory
620 // Don't include subdirectories in this figure, because they need to provide their
621 // own 'most important' value
622 if (status2->text_status == git_wc_status_deleted || status2->text_status == git_wc_status_added)
624 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
625 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
627 else
629 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
630 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
631 if (((status2->text_status == git_wc_status_unversioned)||(status2->text_status == git_wc_status_none))
632 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
634 // treat unversioned files as modified
635 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
636 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
642 pThis->AddEntry(gitPath, status2);
644 return FALSE;
647 #if 0
648 git_error_t * CCachedDirectory::GetStatusCallback(void *baton, const char *path, git_wc_status2_t *status)
650 CCachedDirectory* pThis = (CCachedDirectory*)baton;
652 if (path == NULL)
653 return 0;
655 CTGitPath svnPath;
657 if(status->entry)
659 if ((status->text_status != git_wc_status_none)&&(status->text_status != git_wc_status_missing))
660 svnPath.SetFromSVN(path, (status->entry->kind == svn_node_dir));
661 else
662 svnPath.SetFromSVN(path);
664 if(svnPath.IsDirectory())
666 if(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))
668 if (pThis->m_bRecursive)
670 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
671 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
674 // Make sure we know about this child directory
675 // This initial status value is likely to be overwritten from below at some point
676 git_wc_status_kind s = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
677 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(svnPath);
678 if (cdir)
680 // This child directory is already in our cache!
681 // So ask this dir about its recursive status
682 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
683 AutoLocker lock(pThis->m_critSec);
684 pThis->m_childDirectories[svnPath] = st;
686 else
688 // the child directory is not in the cache. Create a new entry for it in the cache which is
689 // initially 'unversioned'. But we added that directory to the crawling list above, which
690 // means the cache will be updated soon.
691 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
692 AutoLocker lock(pThis->m_critSec);
693 pThis->m_childDirectories[svnPath] = s;
697 else
699 // Keep track of the most important status of all the files in this directory
700 // Don't include subdirectories in this figure, because they need to provide their
701 // own 'most important' value
702 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->text_status);
703 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->prop_status);
704 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_none))
705 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
707 // treat unversioned files as modified
708 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
709 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
713 else
715 svnPath.SetFromSVN(path);
716 // Subversion returns no 'entry' field for versioned folders if they're
717 // part of another working copy (nested layouts).
718 // So we have to make sure that such an 'unversioned' folder really
719 // is unversioned.
720 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_missing))&&(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))&&(svnPath.IsDirectory()))
722 if (svnPath.HasAdminDir())
724 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
725 // Mark the directory as 'versioned' (status 'normal' for now).
726 // This initial value will be overwritten from below some time later
728 AutoLocker lock(pThis->m_critSec);
729 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
731 // Make sure the entry is also in the cache
732 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
733 // also mark the status in the status object as normal
734 status->text_status = git_wc_status_normal;
737 else if (status->text_status == git_wc_status_external)
739 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
740 // Mark the directory as 'versioned' (status 'normal' for now).
741 // This initial value will be overwritten from below some time later
743 AutoLocker lock(pThis->m_critSec);
744 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
746 // we have added a directory to the child-directory list of this
747 // directory. We now must make sure that this directory also has
748 // an entry in the cache.
749 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
750 // also mark the status in the status object as normal
751 status->text_status = git_wc_status_normal;
753 else
755 if (svnPath.IsDirectory())
757 AutoLocker lock(pThis->m_critSec);
758 pThis->m_childDirectories[svnPath] = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
760 else if ((CGitStatusCache::Instance().IsUnversionedAsModified())&&(status->text_status != git_wc_status_missing))
762 // make this unversioned item change the most important status of this
763 // folder to modified if it doesn't already have another status
764 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
765 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
770 pThis->AddEntry(svnPath, status);
772 return 0;
774 #endif
776 bool
777 CCachedDirectory::IsOwnStatusValid() const
779 return m_ownStatus.HasBeenSet() &&
780 !m_ownStatus.HasExpired(GetTickCount());
783 void CCachedDirectory::Invalidate()
785 m_ownStatus.Invalidate();
788 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
790 // Combine our OWN folder status with the most important of our *FILES'* status.
791 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
793 // NOTE: TSVN marks dir as modified if it contains added/deleted/missing files, but we prefer the most important
794 // status to propagate upward in its original state
795 /*if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))
797 if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))
798 retVal = git_wc_status_modified;
801 // Now combine all our child-directorie's status
803 AutoLocker lock(m_critSec);
804 ChildDirStatus::const_iterator it;
805 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
807 retVal = GitStatus::GetMoreImportant(retVal, it->second);
808 /*if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))
810 if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))
811 retVal = git_wc_status_modified;
815 return retVal;
818 // Update our composite status and deal with things if it's changed
819 void CCachedDirectory::UpdateCurrentStatus()
821 git_wc_status_kind newStatus = CalculateRecursiveStatus();
822 ATLTRACE(_T("UpdateCurrentStatus %s new:%d old: %d\n"),
823 m_directoryPath.GetWinPath(),
824 newStatus, m_currentFullStatus);
826 if ( this->m_ownStatus.GetEffectiveStatus() < git_wc_status_normal )
828 if (::PathFileExists(this->m_directoryPath.GetWinPathString()+_T("\\.git")))
830 //project root must be normal status at least.
831 ATLTRACE(_T("force update project root directory as normal status\n"));
832 this->m_ownStatus.ForceStatus(git_wc_status_normal);
836 if ((newStatus != m_currentFullStatus) && m_ownStatus.IsVersioned())
838 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
840 // Our status has changed - tell the shell
841 ATLTRACE(_T("Dir %s, status change from %d to %d, send shell notification\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
842 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
844 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
845 m_currentFullStatus = newStatus;
846 else
847 m_currentFullStatus = git_wc_status_missing;
849 // And tell our parent, if we've got one...
850 // we tell our parent *always* about our status, even if it hasn't
851 // changed. This is to make sure that the parent has really our current
852 // status - the parent can decide itself if our status has changed
853 // or not.
854 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
855 if(!parentPath.IsEmpty())
857 // We have a parent
858 // just version controled directory need to cache.
859 CString root1, root2;
860 if(parentPath.HasAdminDir(&root1) && m_directoryPath.HasAdminDir(&root2) && root1 == root2)
862 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
863 if (cachedDir)
864 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
870 // Receive a notification from a child that its status has changed
871 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
873 git_wc_status_kind currentStatus = git_wc_status_none;
875 AutoLocker lock(m_critSec);
876 currentStatus = m_childDirectories[childDir];
878 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
881 AutoLocker lock(m_critSec);
882 m_childDirectories[childDir] = childStatus;
884 UpdateCurrentStatus();
888 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
890 // Don't return recursive status if we're unversioned ourselves.
891 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned)
893 CStatusCacheEntry recursiveStatus(m_ownStatus);
894 UpdateCurrentStatus();
895 recursiveStatus.ForceStatus(m_currentFullStatus);
896 return recursiveStatus;
898 else
900 return m_ownStatus;
904 void CCachedDirectory::RefreshStatus(bool bRecursive)
906 // Make sure that our own status is up-to-date
907 GetStatusForMember(m_directoryPath,bRecursive);
909 AutoLocker lock(m_critSec);
910 // We also need to check if all our file members have the right date on them
911 CacheEntryMap::iterator itMembers;
912 std::set<CTGitPath> refreshedpaths;
913 DWORD now = GetTickCount();
914 if (m_entryCache.size() == 0)
915 return;
916 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
918 if (itMembers->first)
920 CTGitPath filePath(m_directoryPath);
921 filePath.AppendPathString(itMembers->first);
922 std::set<CTGitPath>::iterator refr_it;
923 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
924 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
926 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
928 lock.Unlock();
929 // We need to request this item as well
930 GetStatusForMember(filePath,bRecursive);
931 // GetStatusForMember now has recreated the m_entryCache map.
932 // So start the loop again, but add this path to the refreshed paths set
933 // to make sure we don't refresh this path again. This is to make sure
934 // that we don't end up in an endless loop.
935 lock.Lock();
936 refreshedpaths.insert(refr_it, filePath);
937 itMembers = m_entryCache.begin();
938 if (m_entryCache.size()==0)
939 return;
940 continue;
942 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
944 // crawl all sub folders too! Otherwise a change deep inside the
945 // tree which has changed won't get propagated up the tree.
946 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
953 void CCachedDirectory::UpdateParentsStatus(const CTGitPath& path, git_wc_status_kind childStatus)
955 return ;
958 void CCachedDirectory::RefreshMostImportant()
960 CacheEntryMap::iterator itMembers;
961 git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();
962 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
964 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
965 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
966 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
968 // treat unversioned files as modified
969 if (newStatus != git_wc_status_added)
970 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
973 if (newStatus != m_mostImportantFileStatus)
975 ATLTRACE(_T("status change of path %s\n"), m_directoryPath.GetWinPath());
976 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
978 m_mostImportantFileStatus = newStatus;