SyncDlg: Use "Close" instead of "OK"
[TortoiseGit.git] / src / TGitCache / CachedDirectory.cpp
blob9311e93a03e46d7b70fb28e3c9dba7cdc49d793a
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 // only read non empty keys (just needed for transition from old TGit clients)
131 if (!sKey.IsEmpty())
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(value);
156 if (value > MAX_PATH)
157 return false;
158 if (value)
160 CString sPath;
161 if (fread(sPath.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)
163 sPath.ReleaseBuffer(0);
164 return false;
166 sPath.ReleaseBuffer(value);
167 // make sure paths do not end with backslash (just needed for transition from old TGit clients)
168 if (sPath.GetLength() > 3 && sPath[sPath.GetLength() - 1] == _T('\\'))
169 sPath.TrimRight(_T("\\"));
170 m_directoryPath.SetFromWin(sPath);
171 m_directoryPath.GetGitPathString(); // make sure git path string is set
173 if (!m_ownStatus.LoadFromDisk(pFile))
174 return false;
176 LOADVALUEFROMFILE(m_currentFullStatus);
177 LOADVALUEFROMFILE(m_mostImportantFileStatus);
179 catch ( CAtlException )
181 return false;
183 return true;
188 CStatusCacheEntry CCachedDirectory::GetStatusFromCache(const CTGitPath& path, bool bRecursive)
190 if(path.IsDirectory())
192 // We don't have directory status in our cache
193 // Ask the directory if it knows its own status
194 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
195 if( dirEntry)
197 if (dirEntry->IsOwnStatusValid())
198 return dirEntry->GetOwnStatus(bRecursive);
199 else
201 /* cache have outof date, need crawl again*/
203 /*AutoLocker lock(dirEntry->m_critSec);
204 ChildDirStatus::const_iterator it;
205 for(it = dirEntry->m_childDirectories.begin(); it != dirEntry->m_childDirectories.end(); ++it)
207 CGitStatusCache::Instance().AddFolderForCrawling(it->first);
210 CGitStatusCache::Instance().AddFolderForCrawling(path);
212 /*Return old status during crawling*/
213 return dirEntry->GetOwnStatus(bRecursive);
216 else
218 CGitStatusCache::Instance().AddFolderForCrawling(path);
220 return CStatusCacheEntry();
222 else
224 //All file ignored if under ignore directory
225 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_ignored)
226 return CStatusCacheEntry(git_wc_status_ignored);
227 if (m_ownStatus.GetEffectiveStatus() == git_wc_status_unversioned)
228 return CStatusCacheEntry(git_wc_status_unversioned);
230 // Look up a file in our own cache
231 AutoLocker lock(m_critSec);
232 CString strCacheKey = GetCacheKey(path);
233 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
234 if(itMap != m_entryCache.end())
236 // We've hit the cache - check for timeout
237 if(!itMap->second.HasExpired((long)GetTickCount()))
239 if(itMap->second.DoesFileTimeMatch(path.GetLastWriteTime()))
241 if ((itMap->second.GetEffectiveStatus()!=git_wc_status_missing)||(!PathFileExists(path.GetWinPath())))
243 // Note: the filetime matches after a modified has been committed too.
244 // So in that case, we would return a wrong status (e.g. 'modified' instead
245 // of 'normal') here.
246 return itMap->second;
252 CGitStatusCache::Instance().AddFolderForCrawling(path.GetContainingDirectory());
253 return CStatusCacheEntry();
258 CStatusCacheEntry CCachedDirectory::GetStatusFromGit(const CTGitPath &path, CString sProjectRoot)
260 CString subpaths = path.GetGitPathString();
261 if(subpaths.GetLength() >= sProjectRoot.GetLength())
263 if(subpaths[sProjectRoot.GetLength()] == _T('/'))
264 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength()-1);
265 else
266 subpaths=subpaths.Right(subpaths.GetLength() - sProjectRoot.GetLength());
269 GitStatus *pGitStatus = &CGitStatusCache::Instance().m_GitStatus;
270 UNREFERENCED_PARAMETER(pGitStatus);
272 bool isVersion =true;
273 pGitStatus->IsUnderVersionControl(sProjectRoot, subpaths, path.IsDirectory(), &isVersion);
274 if(!isVersion)
275 { //untracked file
276 bool isIgnoreFileChanged = pGitStatus->HasIgnoreFilesChanged(sProjectRoot, subpaths);
278 if( isIgnoreFileChanged)
280 pGitStatus->LoadIgnoreFile(sProjectRoot, subpaths);
283 if(path.IsDirectory())
286 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path,
287 false); /* we needn't watch untracked directory*/
289 if(dirEntry)
291 AutoLocker lock(dirEntry->m_critSec);
293 git_wc_status_kind dirstatus = dirEntry->GetCurrentFullStatus() ;
294 if( dirstatus == git_wc_status_none || dirstatus >= git_wc_status_normal || isIgnoreFileChanged )
295 {/* status have not initialized*/
296 git_wc_status2_t status2;
297 bool isignore = false;
298 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
299 status2.text_status = status2.prop_status =
300 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
302 dirEntry->m_ownStatus.SetKind(git_node_dir);
303 dirEntry->m_ownStatus.SetStatus(&status2);
306 return dirEntry->m_ownStatus;
310 else /* path is file */
312 AutoLocker lock(m_critSec);
313 CString strCacheKey = GetCacheKey(path);
315 if (strCacheKey.IsEmpty())
316 return CStatusCacheEntry();
318 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);
319 if(itMap == m_entryCache.end() || isIgnoreFileChanged)
321 git_wc_status2_t status2;
322 bool isignore = false;
323 pGitStatus->IsIgnore(sProjectRoot,subpaths,&isignore);
324 status2.text_status = status2.prop_status =
325 (isignore? git_wc_status_ignored:git_wc_status_unversioned);
326 AddEntry(path, &status2);
327 return m_entryCache[strCacheKey];
329 else
331 return itMap->second;
334 return CStatusCacheEntry();
337 else
339 EnumFiles((CTGitPath*)&path, TRUE);
340 UpdateCurrentStatus();
341 return CStatusCacheEntry(m_ownStatus);
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 sProjectRoot;
352 bool bIsVersionedPath;
354 bool bRequestForSelf = false;
355 if(path.IsEquivalentToWithoutCase(m_directoryPath))
357 bRequestForSelf = true;
358 AutoLocker lock(m_critSec);
359 // HasAdminDir might modify m_directoryPath, so we need to do it synchronized
360 bIsVersionedPath = m_directoryPath.HasAdminDir(&sProjectRoot);
362 else
363 bIsVersionedPath = path.HasAdminDir(&sProjectRoot);
365 // In all most circumstances, we ask for the status of a member of this directory.
366 ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);
368 //If is not version control path
369 if( !bIsVersionedPath)
371 ATLTRACE(_T("%s is not underversion control\n"), path.GetWinPath());
372 return CStatusCacheEntry();
375 // We've not got this item in the cache - let's add it
376 // We never bother asking SVN for the status of just one file, always for its containing directory
378 if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))
380 // We're being asked for the status of an .git directory
381 // It's not worth asking for this
382 return CStatusCacheEntry();
386 if(bFetch)
388 return GetStatusFromGit(path, sProjectRoot);
390 else
392 return GetStatusFromCache(path, bRecursive);
396 CStatusCacheEntry CCachedDirectory::GetCacheStatusForMember(const CTGitPath& path)
398 // no disk access!
399 AutoLocker lock(m_critSec);
400 CacheEntryMap::iterator itMap = m_entryCache.find(GetCacheKey(path));
401 if(itMap != m_entryCache.end())
402 return itMap->second;
404 return CStatusCacheEntry();
407 int CCachedDirectory::EnumFiles(CTGitPath *path , bool IsFull)
409 CString sProjectRoot;
410 if(path)
411 path->HasAdminDir(&sProjectRoot);
412 else
413 m_directoryPath.HasAdminDir(&sProjectRoot);
415 ATLTRACE(_T("EnumFiles %s\n"), path->GetWinPath());
417 ATLASSERT( !m_directoryPath.IsEmpty() );
419 CString sSubPath;
421 CString s;
422 if(path)
423 s=path->GetWinPath();
424 else
425 s=m_directoryPath.GetDirectory().GetWinPathString();
427 if (s.GetLength() > sProjectRoot.GetLength())
429 // skip initial slash if necessary
430 if(s[sProjectRoot.GetLength()] == _T('\\'))
431 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() -1);
432 else
433 sSubPath = s.Right(s.GetLength() - sProjectRoot.GetLength() );
436 // strip "\" at the end, otherwise cache lookups for drives do not work correctly
437 sProjectRoot.TrimRight(_T("\\"));
439 GitStatus *pStatus = &CGitStatusCache::Instance().m_GitStatus;
440 UNREFERENCED_PARAMETER(pStatus);
441 git_wc_status_kind status = git_wc_status_none;
443 if(!path->IsDirectory())
445 bool assumeValid = false;
446 bool skipWorktree = false;
447 pStatus->GetFileStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback, this, &assumeValid, &skipWorktree);
449 else
451 m_mostImportantFileStatus = git_wc_status_none;
452 pStatus->EnumDirStatus(sProjectRoot, sSubPath, &status, IsFull, false, true, GetStatusCallback,this);
454 // folders must not be displayed as added or deleted only as modified
455 if (status == git_wc_status_deleted || status == git_wc_status_added)
456 status = git_wc_status_modified;
457 // this is necessary, even if the m_mostImportantFileStatus is set in GetStatusCallback,
458 // however, if it's missing, deleted subdirectories won't mark parents as modified
459 m_mostImportantFileStatus = GitStatus::GetMoreImportant(m_mostImportantFileStatus, status);
461 if (sSubPath.IsEmpty())
463 // request a shell notification for working trees which have not been examined before
464 if (m_currentFullStatus == git_wc_status_none)
466 m_currentFullStatus = git_wc_status_normal;
467 CGitStatusCache::Instance().UpdateShell(sProjectRoot);
469 m_ownStatus = git_wc_status_normal;
471 // otherwise folders which contain at least one ignored item gets displayed as ignored
472 // or: folders which were displayed as conflicted never recover from that state
473 else if (m_ownStatus.GetEffectiveStatus() == git_wc_status_ignored || m_ownStatus.GetEffectiveStatus() == git_wc_status_conflicted)
474 m_ownStatus = git_wc_status_normal;
477 return 0;
479 void
480 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)
482 AutoLocker lock(m_critSec);
483 if(path.IsDirectory())
485 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);
486 if (childDir)
488 if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
490 if(pGitStatus)
492 if(childDir->GetCurrentFullStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
494 CGitStatusCache::Instance().UpdateShell(path);
495 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
496 childDir->m_ownStatus.SetKind(git_node_dir);
497 childDir->m_ownStatus.SetStatus(pGitStatus);
501 childDir->m_ownStatus.SetKind(git_node_dir);
506 else
508 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
509 bool bNotified = false;
511 if(!childDir)
512 return ;
514 AutoLocker lock(childDir->m_critSec);
515 CString cachekey = GetCacheKey(path);
516 CacheEntryMap::iterator entry_it = childDir->m_entryCache.lower_bound(cachekey);
517 if (entry_it != childDir->m_entryCache.end() && entry_it->first == cachekey)
519 if (pGitStatus)
521 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&
522 entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status)
525 bNotified =true;
530 else
532 entry_it = childDir->m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));
533 bNotified = true;
536 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);
537 // TEMP(?): git status doesn't not have "entry" that contains node type, so manually set as file
538 entry_it->second.SetKind(git_node_file);
540 if(bNotified)
542 CGitStatusCache::Instance().UpdateShell(path);
543 //ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());
546 //ATLTRACE(_T("Path Entry Add %s %s %s %d\n"), path.GetWinPath(), cachekey, m_directoryPath.GetWinPath(), pGitStatus->text_status);
549 CCachedDirectory * parent = CGitStatusCache::Instance().GetDirectoryCacheEntry(path.GetContainingDirectory());
551 if(parent)
553 if ((parent->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))
555 if(pGitStatus)
557 if(parent->GetCurrentFullStatus() < GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))
559 CGitStatusCache::Instance().UpdateShell(parent->m_directoryPath);
560 //ATLTRACE(_T("shell update for %s\n"), parent->m_directoryPath.GetWinPathString());
561 parent->m_ownStatus.SetKind(git_node_dir);
562 parent->m_ownStatus.SetStatus(pGitStatus);
570 CString
571 CCachedDirectory::GetCacheKey(const CTGitPath& path)
573 // All we put into the cache as a key is just the end portion of the pathname
574 // There's no point storing the path of the containing directory for every item
575 return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength()).MakeLower().TrimLeft(_T("\\"));
578 CString
579 CCachedDirectory::GetFullPathString(const CString& cacheKey)
581 return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;
584 BOOL CCachedDirectory::GetStatusCallback(const CString & path, git_wc_status_kind status,bool isDir, void *, bool assumeValid, bool skipWorktree)
586 git_wc_status2_t _status;
587 git_wc_status2_t *status2 = &_status;
589 status2->prop_status = status2->text_status = status;
590 status2->assumeValid = assumeValid;
591 status2->skipWorktree = skipWorktree;
593 CTGitPath gitPath(path);
595 CCachedDirectory *pThis = CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath.GetContainingDirectory());
597 if(pThis == NULL)
598 return FALSE;
600 // if(status->entry)
602 if (isDir)
603 { /*gitpath is directory*/
604 //if ( !gitPath.IsEquivalentToWithoutCase(pThis->m_directoryPath) )
606 if (!gitPath.Exists())
608 ATLTRACE(_T("Miss dir %s \n"), gitPath.GetWinPath());
609 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_deleted);
612 if ( status < git_wc_status_normal)
614 if( ::PathFileExists(path+_T("\\.git")))
615 { // this is submodule
616 ATLTRACE(_T("skip submodule %s\n"), path);
617 return FALSE;
620 if (pThis->m_bRecursive)
622 // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated
623 //OutputDebugStringA("AddFolderCrawl: ");OutputDebugStringW(svnPath.GetWinPathString());OutputDebugStringA("\r\n");
624 if(status >= git_wc_status_normal)
625 CGitStatusCache::Instance().AddFolderForCrawling(gitPath);
628 // Make sure we know about this child directory
629 // This initial status value is likely to be overwritten from below at some point
630 git_wc_status_kind s = GitStatus::GetMoreImportant(status2->text_status, status2->prop_status);
632 // folders must not be displayed as added or deleted only as modified
633 if (s == git_wc_status_deleted || s == git_wc_status_added)
634 s = git_wc_status_modified;
636 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(gitPath);
637 if (cdir)
639 // This child directory is already in our cache!
640 // So ask this dir about its recursive status
641 git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());
642 AutoLocker lock(pThis->m_critSec);
643 pThis->m_childDirectories[gitPath] = st;
644 ATLTRACE(_T("call 1 Update dir %s %d\n"), gitPath.GetWinPath(), st);
646 else
648 AutoLocker lock(pThis->m_critSec);
649 // the child directory is not in the cache. Create a new entry for it in the cache which is
650 // initially 'unversioned'. But we added that directory to the crawling list above, which
651 // means the cache will be updated soon.
652 CGitStatusCache::Instance().GetDirectoryCacheEntry(gitPath);
654 pThis->m_childDirectories[gitPath] = s;
655 ATLTRACE(_T("call 2 Update dir %s %d\n"), gitPath.GetWinPath(), s);
659 else /* gitpath is file*/
661 // Keep track of the most important status of all the files in this directory
662 // Don't include subdirectories in this figure, because they need to provide their
663 // own 'most important' value
664 if (status2->text_status == git_wc_status_deleted || status2->text_status == git_wc_status_added)
666 // if just a file in a folder is deleted or added report back that the folder is modified and not deleted or added
667 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
669 else
671 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->text_status);
672 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status2->prop_status);
673 if ((status2->text_status == git_wc_status_unversioned)
674 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
676 // treat unversioned files as modified
677 if (pThis->m_mostImportantFileStatus != git_wc_status_added)
678 pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);
684 pThis->AddEntry(gitPath, status2);
686 return FALSE;
689 bool
690 CCachedDirectory::IsOwnStatusValid() const
692 return m_ownStatus.HasBeenSet() &&
693 !m_ownStatus.HasExpired(GetTickCount());
696 void CCachedDirectory::Invalidate()
698 m_ownStatus.Invalidate();
701 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()
703 // Combine our OWN folder status with the most important of our *FILES'* status.
704 git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());
706 // Now combine all our child-directorie's status
707 AutoLocker lock(m_critSec);
708 ChildDirStatus::const_iterator it;
709 for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)
711 retVal = GitStatus::GetMoreImportant(retVal, it->second);
714 return retVal;
717 // Update our composite status and deal with things if it's changed
718 void CCachedDirectory::UpdateCurrentStatus()
720 git_wc_status_kind newStatus = CalculateRecursiveStatus();
721 ATLTRACE(_T("UpdateCurrentStatus %s new:%d old: %d\n"),
722 m_directoryPath.GetWinPath(),
723 newStatus, m_currentFullStatus);
725 if ( this->m_ownStatus.GetEffectiveStatus() < git_wc_status_normal )
727 if (::PathFileExists(this->m_directoryPath.GetWinPathString()+_T("\\.git")))
729 //project root must be normal status at least.
730 ATLTRACE(_T("force update project root directory as normal status\n"));
731 this->m_ownStatus.ForceStatus(git_wc_status_normal);
735 if ((newStatus != m_currentFullStatus) && m_ownStatus.IsVersioned())
737 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))
739 // Our status has changed - tell the shell
740 ATLTRACE(_T("Dir %s, status change from %d to %d, send shell notification\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);
741 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
743 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)
744 m_currentFullStatus = newStatus;
745 else
746 m_currentFullStatus = git_wc_status_missing;
748 // And tell our parent, if we've got one...
749 // we tell our parent *always* about our status, even if it hasn't
750 // changed. This is to make sure that the parent has really our current
751 // status - the parent can decide itself if our status has changed
752 // or not.
753 CTGitPath parentPath = m_directoryPath.GetContainingDirectory();
754 if(!parentPath.IsEmpty())
756 // We have a parent
757 // just version controled directory need to cache.
758 CString root1, root2;
759 if(parentPath.HasAdminDir(&root1) && m_directoryPath.HasAdminDir(&root2) && root1 == root2)
761 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);
762 if (cachedDir)
763 cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);
769 // Receive a notification from a child that its status has changed
770 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)
772 git_wc_status_kind currentStatus = git_wc_status_none;
774 AutoLocker lock(m_critSec);
775 currentStatus = m_childDirectories[childDir];
777 if ((currentStatus != childStatus)||(!IsOwnStatusValid()))
780 AutoLocker lock(m_critSec);
781 m_childDirectories[childDir] = childStatus;
783 UpdateCurrentStatus();
787 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)
789 // Don't return recursive status if we're unversioned ourselves.
790 if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned && m_ownStatus.GetEffectiveStatus() != git_wc_status_ignored)
792 CStatusCacheEntry recursiveStatus(m_ownStatus);
793 UpdateCurrentStatus();
794 if (m_currentFullStatus == git_wc_status_deleted || m_currentFullStatus == git_wc_status_added)
795 m_currentFullStatus = git_wc_status_modified;
796 recursiveStatus.ForceStatus(m_currentFullStatus);
797 return recursiveStatus;
799 else
801 return m_ownStatus;
805 void CCachedDirectory::RefreshStatus(bool bRecursive)
807 // Make sure that our own status is up-to-date
808 GetStatusForMember(m_directoryPath,bRecursive);
810 AutoLocker lock(m_critSec);
811 // We also need to check if all our file members have the right date on them
812 CacheEntryMap::iterator itMembers;
813 std::set<CTGitPath> refreshedpaths;
814 DWORD now = GetTickCount();
815 if (m_entryCache.empty())
816 return;
817 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
819 if (itMembers->first)
821 CTGitPath filePath(m_directoryPath);
822 filePath.AppendPathString(itMembers->first);
823 std::set<CTGitPath>::iterator refr_it;
824 if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&
825 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))
827 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))
829 lock.Unlock();
830 // We need to request this item as well
831 GetStatusForMember(filePath,bRecursive);
832 // GetStatusForMember now has recreated the m_entryCache map.
833 // So start the loop again, but add this path to the refreshed paths set
834 // to make sure we don't refresh this path again. This is to make sure
835 // that we don't end up in an endless loop.
836 lock.Lock();
837 refreshedpaths.insert(refr_it, filePath);
838 itMembers = m_entryCache.begin();
839 if (m_entryCache.empty())
840 return;
841 continue;
843 else if ((bRecursive)&&(itMembers->second.IsDirectory()))
845 // crawl all sub folders too! Otherwise a change deep inside the
846 // tree which has changed won't get propagated up the tree.
847 CGitStatusCache::Instance().AddFolderForCrawling(filePath);
854 void CCachedDirectory::RefreshMostImportant()
856 AutoLocker lock(m_critSec);
857 CacheEntryMap::iterator itMembers;
858 git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();
859 for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)
861 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());
862 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))
863 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))
865 // treat unversioned files as modified
866 if (newStatus != git_wc_status_added)
867 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);
870 if (newStatus != m_mostImportantFileStatus)
872 ATLTRACE(_T("status change of path %s\n"), m_directoryPath.GetWinPath());
873 CGitStatusCache::Instance().UpdateShell(m_directoryPath);
875 m_mostImportantFileStatus = newStatus;