fixed warnings
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob2d612e58735a0a4fd33012277a93f34cd4b101e2
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
4 // Copyright (C) 2008-2011 - 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 GetStatusFromGit(path, sProjectRoot);
385 return CStatusCacheEntry();
387 else
389 return GetStatusFromCache(path, bRecursive);
393 int CCachedDirectory::EnumFiles(CTGitPath *path , bool IsFull)
395 CString sProjectRoot;
396 if(path)
397 path->HasAdminDir(&sProjectRoot);
398 else
399 m_directoryPath.HasAdminDir(&sProjectRoot);
401 ATLTRACE(_T("EnumFiles %s\n"), path->GetWinPath());
403 ATLASSERT( !m_directoryPath.IsEmpty() );
405 CString sSubPath;
407 CString s;
408 if(path)
409 s=path->GetWinPath();
410 else
411 s=m_directoryPath.GetDirectory().GetWinPathString();
413 if (s.GetLength() > sProjectRoot.GetLength())
415 // skip initial slash if necessary
416 if(s[sProjectRoot.GetLength()] == _T('\\'))
417 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
418 else
419 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
422 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
423 UNREFERENCED_PARAMETER(pStatus);
424 git_wc_status_kind status;
426 if(!path->IsDirectory())
427 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false,true, GetStatusCallback,this);
428 else
430 m_mostImportantFileStatus = git_wc_status_unknown;
431 pStatus->EnumDirStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback,this);
434 m_mostImportantFileStatus = GitStatus::GetMoreImportant(m_mostImportantFileStatus, status);
436 return 0;
438 void
439 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
441 AutoLocker lock(m_critSec);
442 if(path.IsDirectory())
444 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
445 if (childDir)
447 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
449 if(pGitStatus)
451 if(childDir->GetCurrentFullStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
453 CGitStatusCache::Instance().UpdateShell(path);
454 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
455 childDir->m_ownStatus.SetStatus(pGitStatus);
456 childDir->m_ownStatus.SetKind(git_node_dir);
460 childDir->m_ownStatus.SetKind(git_node_dir);
465 else
467 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
468 bool bNotified = false;
470 if(!childDir)
471 return ;
473 CString cachekey = GetCacheKey(path);
474 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
475 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
477 if (pGitStatus)
479 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
480 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
483 bNotified =true;
488 else
490 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
491 bNotified = true;
494 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
495 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
496 entry_it->second.SetKind(git_node_file);
498 if(bNotified)
500 CGitStatusCache::Instance().UpdateShell(path);
501 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
504 //ATLTRACE(_T("Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
507 CCachedDirectory * parent = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
509 if(parent)
511 if ((parent->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
513 if(pGitStatus)
515 if(parent->GetCurrentFullStatus() < GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
517 CGitStatusCache::Instance().UpdateShell(parent->m_directoryPath);
518 //ATLTRACE(_T("shell update for %s\n"), parent->m_directoryPath.GetWinPathString());
519 parent->m_ownStatus.SetStatus(pGitStatus);
520 parent->m_ownStatus.SetKind(git_node_dir);
528 CString
529 CCachedDirectory::GetCacheKey(const CTGitPath& path)
531 // All we put into the cache as a key is just the end portion of the pathname
532 // There's no point storing the path of the containing directory for every item
533 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower();
536 CString
537 CCachedDirectory::GetFullPathString(const CString& cacheKey)
539 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
542 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *pUserData)
544 git_wc_status2_t _status;
545 git_wc_status2_t *status2 = &_status;
547 status2->prop_status = status2->text_status = status;
549 CTGitPath gitPath;
551 CString lowcasepath = path;
552 lowcasepath.MakeLower();
553 gitPath.SetFromUnknown(lowcasepath);
555 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
557 if(pThis == NULL)
558 return FALSE;
560 // if(status->entry)
562 if (isDir)
563 { /*gitpath is directory*/
564 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
566 if (!gitPath.Exists())
568 ATLTRACE(_T("Miss dir %s \n"), gitPath.GetWinPath());
569 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
572 if ( status < git_wc_status_normal)
574 if( ::PathFileExists(path+_T("\\.git")))
575 { // this is submodule
576 ATLTRACE(_T("skip submodule %s\n"), path);
577 return FALSE;
580 if (pThis->m_bRecursive)
582 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
583 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
584 if(status >= git_wc_status_normal)
585 if(status != git_wc_status_missing)
586 if(status != git_wc_status_deleted)
587 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
590 // Make sure we know about this child directory
591 // This initial status value is likely to be overwritten from below at some point
592 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
593 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
594 if (cdir)
596 // This child directory is already in our cache!
597 // So ask this dir about its recursive status
598 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
599 AutoLocker lock(pThis->m_critSec);
600 pThis->m_childDirectories[gitPath] = st;
601 ATLTRACE(_T("call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
603 else
605 AutoLocker lock(pThis->m_critSec);
606 // the child directory is not in the cache. Create a new entry for it in the cache which is
607 // initially 'unversioned'. But we added that directory to the crawling list above, which
608 // means the cache will be updated soon.
609 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
611 pThis->m_childDirectories[gitPath] = s;
612 ATLTRACE(_T("call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
616 else /* gitpath is file*/
618 // Keep track of the most important status of all the files in this directory
619 // Don't include subdirectories in this figure, because they need to provide their
620 // own 'most important' value
621 if (status2->text_status == git_wc_status_deleted || status2->text_status == git_wc_status_added)
623 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
624 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
626 else
628 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
629 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
630 if (((status2->text_status == git_wc_status_unversioned)||(status2->text_status == git_wc_status_none))
631 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
633 // treat unversioned files as modified
634 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
635 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
641 pThis->AddEntry(gitPath, status2);
643 return FALSE;
646 #if 0
647 git_error_t * CCachedDirectory::GetStatusCallback(void *baton, const char *path, git_wc_status2_t *status)
649 CCachedDirectory* pThis = (CCachedDirectory*)baton;
651 if (path == NULL)
652 return 0;
654 CTGitPath svnPath;
656 if(status->entry)
658 if ((status->text_status != git_wc_status_none)&&(status->text_status != git_wc_status_missing))
659 svnPath.SetFromSVN(path, (status->entry->kind == svn_node_dir));
660 else
661 svnPath.SetFromSVN(path);
663 if(svnPath.IsDirectory())
665 if(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))
667 if (pThis->m_bRecursive)
669 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
670 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
673 // Make sure we know about this child directory
674 // This initial status value is likely to be overwritten from below at some point
675 git_wc_status_kind s = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
676 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(svnPath);
677 if (cdir)
679 // This child directory is already in our cache!
680 // So ask this dir about its recursive status
681 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
682 AutoLocker lock(pThis->m_critSec);
683 pThis->m_childDirectories[svnPath] = st;
685 else
687 // the child directory is not in the cache. Create a new entry for it in the cache which is
688 // initially 'unversioned'. But we added that directory to the crawling list above, which
689 // means the cache will be updated soon.
690 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
691 AutoLocker lock(pThis->m_critSec);
692 pThis->m_childDirectories[svnPath] = s;
696 else
698 // Keep track of the most important status of all the files in this directory
699 // Don't include subdirectories in this figure, because they need to provide their
700 // own 'most important' value
701 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->text_status);
702 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->prop_status);
703 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_none))
704 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
706 // treat unversioned files as modified
707 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
708 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
712 else
714 svnPath.SetFromSVN(path);
715 // Subversion returns no 'entry' field for versioned folders if they're
716 // part of another working copy (nested layouts).
717 // So we have to make sure that such an 'unversioned' folder really
718 // is unversioned.
719 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_missing))&&(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))&&(svnPath.IsDirectory()))
721 if (svnPath.HasAdminDir())
723 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
724 // Mark the directory as 'versioned' (status 'normal' for now).
725 // This initial value will be overwritten from below some time later
727 AutoLocker lock(pThis->m_critSec);
728 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
730 // Make sure the entry is also in the cache
731 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
732 // also mark the status in the status object as normal
733 status->text_status = git_wc_status_normal;
736 else if (status->text_status == git_wc_status_external)
738 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);
739 // Mark the directory as 'versioned' (status 'normal' for now).
740 // This initial value will be overwritten from below some time later
742 AutoLocker lock(pThis->m_critSec);
743 pThis->m_childDirectories[svnPath] = git_wc_status_normal;
745 // we have added a directory to the child-directory list of this
746 // directory. We now must make sure that this directory also has
747 // an entry in the cache.
748 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);
749 // also mark the status in the status object as normal
750 status->text_status = git_wc_status_normal;
752 else
754 if (svnPath.IsDirectory())
756 AutoLocker lock(pThis->m_critSec);
757 pThis->m_childDirectories[svnPath] = GitStatus::GetMoreImportant(status->text_status, status->prop_status);
759 else if ((CGitStatusCache::Instance().IsUnversionedAsModified())&&(status->text_status != git_wc_status_missing))
761 // make this unversioned item change the most important status of this
762 // folder to modified if it doesn't already have another status
763 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
764 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
769 pThis->AddEntry(svnPath, status);
771 return 0;
773 #endif
775 bool
776 CCachedDirectory::IsOwnStatusValid() const
778 return m_ownStatus.HasBeenSet() &&
779 !m_ownStatus.HasExpired(GetTickCount()) &&
780 // 'external' isn't a valid status. That just
781 // means the folder is not part of the current working
782 // copy but it still has its own 'real' status
783 m_ownStatus.GetEffectiveStatus()!=git_wc_status_external &&
784 m_ownStatus.IsKindKnown();
787 void CCachedDirectory::Invalidate()
789 m_ownStatus.Invalidate();
792 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
794 // Combine our OWN folder status with the most important of our *FILES'* status.
795 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
797 // NOTE: TSVN marks dir as modified if it contains added/deleted/missing files, but we prefer the most important
798 // status to propagate upward in its original state
799 /*if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))
801 if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))
802 retVal = git_wc_status_modified;
805 // Now combine all our child-directorie's status
807 AutoLocker lock(m_critSec);
808 ChildDirStatus::const_iterator it;
809 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
811 retVal = GitStatus::GetMoreImportant(retVal, it->second);
812 /*if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))
814 if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))
815 retVal = git_wc_status_modified;
819 return retVal;
822 // Update our composite status and deal with things if it's changed
823 void CCachedDirectory::UpdateCurrentStatus()
825 git_wc_status_kind newStatus = CalculateRecursiveStatus();
826 ATLTRACE(_T("UpdateCurrentStatus %s new:%d old: %d\n"),
827 m_directoryPath.GetWinPath(),
828 newStatus, m_currentFullStatus);
830 if ( this->m_ownStatus.GetEffectiveStatus() < git_wc_status_normal )
832 if (::PathFileExists(this->m_directoryPath.GetWinPathString()+_T("\\.git")))
834 //project root must be normal status at least.
835 ATLTRACE(_T("force update project root directory as normal status\n"));
836 this->m_ownStatus.ForceStatus(git_wc_status_normal);
840 if ((newStatus != m_currentFullStatus) && m_ownStatus.IsVersioned())
842 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
844 // Our status has changed - tell the shell
845 ATLTRACE(_T("Dir %s, status change from %d to %d, send shell notification\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
846 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
848 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
849 m_currentFullStatus = newStatus;
850 else
851 m_currentFullStatus = git_wc_status_missing;
853 // And tell our parent, if we've got one...
854 // we tell our parent *always* about our status, even if it hasn't
855 // changed. This is to make sure that the parent has really our current
856 // status - the parent can decide itself if our status has changed
857 // or not.
858 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
859 if(!parentPath.IsEmpty())
861 // We have a parent
862 // just version controled directory need to cache.
863 CString root1, root2;
864 if(parentPath.HasAdminDir(&root1) && m_directoryPath.HasAdminDir(&root2) && root1 == root2)
866 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
867 if (cachedDir)
868 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
874 // Receive a notification from a child that its status has changed
875 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
877 git_wc_status_kind currentStatus = git_wc_status_none;
879 AutoLocker lock(m_critSec);
880 currentStatus = m_childDirectories[childDir];
882 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
885 AutoLocker lock(m_critSec);
886 m_childDirectories[childDir] = childStatus;
888 UpdateCurrentStatus();
892 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
894 // Don't return recursive status if we're unversioned ourselves.
895 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned)
897 CStatusCacheEntry recursiveStatus(m_ownStatus);
898 UpdateCurrentStatus();
899 recursiveStatus.ForceStatus(m_currentFullStatus);
900 return recursiveStatus;
902 else
904 return m_ownStatus;
908 void CCachedDirectory::RefreshStatus(bool bRecursive)
910 // Make sure that our own status is up-to-date
911 GetStatusForMember(m_directoryPath,bRecursive);
913 AutoLocker lock(m_critSec);
914 // We also need to check if all our file members have the right date on them
915 CacheEntryMap::iterator itMembers;
916 std::set<CTGitPath> refreshedpaths;
917 DWORD now = GetTickCount();
918 if (m_entryCache.size() == 0)
919 return;
920 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
922 if (itMembers->first)
924 CTGitPath filePath(m_directoryPath);
925 filePath.AppendPathString(itMembers->first);
926 std::set<CTGitPath>::iterator refr_it;
927 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
928 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
930 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
932 lock.Unlock();
933 // We need to request this item as well
934 GetStatusForMember(filePath,bRecursive);
935 // GetStatusForMember now has recreated the m_entryCache map.
936 // So start the loop again, but add this path to the refreshed paths set
937 // to make sure we don't refresh this path again. This is to make sure
938 // that we don't end up in an endless loop.
939 lock.Lock();
940 refreshedpaths.insert(refr_it, filePath);
941 itMembers = m_entryCache.begin();
942 if (m_entryCache.size()==0)
943 return;
944 continue;
946 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
948 // crawl all sub folders too! Otherwise a change deep inside the
949 // tree which has changed won't get propagated up the tree.
950 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
957 void CCachedDirectory::UpdateParentsStatus(const CTGitPath& path, git_wc_status_kind childStatus)
959 return ;
962 void CCachedDirectory::RefreshMostImportant()
964 CacheEntryMap::iterator itMembers;
965 git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();
966 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
968 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
969 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
970 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
972 // treat unversioned files as modified
973 if (newStatus != git_wc_status_added)
974 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
977 if (newStatus != m_mostImportantFileStatus)
979 ATLTRACE(_T("status change of path %s\n"), m_directoryPath.GetWinPath());
980 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
982 m_mostImportantFileStatus = newStatus;