OSDN Git Service

Fix 'Explore To' in context menu in Commit Dialog
[tortoisegit/TortoiseGitJp.git] / src / TGitCache / CachedDirectory.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include ".\cacheddirectory.h"\r
21 //#include "SVNHelpers.h"\r
22 #include "GitStatusCache.h"\r
23 #include "GitStatus.h"\r
24 #include <set>\r
25 \r
26 CCachedDirectory::CCachedDirectory(void)\r
27 {\r
28         m_entriesFileTime = 0;\r
29         m_propsFileTime = 0;\r
30         m_currentStatusFetchingPathTicks = 0;\r
31         m_bCurrentFullStatusValid = false;\r
32         m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;\r
33         m_bRecursive = true;\r
34 }\r
35 \r
36 CCachedDirectory::~CCachedDirectory(void)\r
37 {\r
38 }\r
39 \r
40 CCachedDirectory::CCachedDirectory(const CTGitPath& directoryPath)\r
41 {\r
42         ATLASSERT(directoryPath.IsDirectory() || !PathFileExists(directoryPath.GetWinPath()));\r
43 \r
44         m_directoryPath = directoryPath;\r
45         m_entriesFileTime = 0;\r
46         m_propsFileTime = 0;\r
47         m_currentStatusFetchingPathTicks = 0;\r
48         m_bCurrentFullStatusValid = false;\r
49         m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;\r
50         m_bRecursive = true;\r
51 }\r
52 \r
53 BOOL CCachedDirectory::SaveToDisk(FILE * pFile)\r
54 {\r
55         AutoLocker lock(m_critSec);\r
56 #define WRITEVALUETOFILE(x) if (fwrite(&x, sizeof(x), 1, pFile)!=1) return false;\r
57 \r
58         unsigned int value = 1;\r
59         WRITEVALUETOFILE(value);        // 'version' of this save-format\r
60         value = (int)m_entryCache.size();\r
61         WRITEVALUETOFILE(value);        // size of the cache map\r
62         // now iterate through the maps and save every entry.\r
63         for (CacheEntryMap::iterator I = m_entryCache.begin(); I != m_entryCache.end(); ++I)\r
64         {\r
65                 const CString& key = I->first;\r
66                 value = key.GetLength();\r
67                 WRITEVALUETOFILE(value);\r
68                 if (value)\r
69                 {\r
70                         if (fwrite((LPCTSTR)key, sizeof(TCHAR), value, pFile)!=value)\r
71                                 return false;\r
72                         if (!I->second.SaveToDisk(pFile))\r
73                                 return false;\r
74                 }\r
75         }\r
76         value = (int)m_childDirectories.size();\r
77         WRITEVALUETOFILE(value);\r
78         for (ChildDirStatus::iterator I = m_childDirectories.begin(); I != m_childDirectories.end(); ++I)\r
79         {\r
80                 const CString& path = I->first.GetWinPathString();\r
81                 value = path.GetLength();\r
82                 WRITEVALUETOFILE(value);\r
83                 if (value)\r
84                 {\r
85                         if (fwrite((LPCTSTR)path, sizeof(TCHAR), value, pFile)!=value)\r
86                                 return false;\r
87                         git_wc_status_kind status = I->second;\r
88                         WRITEVALUETOFILE(status);\r
89                 }\r
90         }\r
91         WRITEVALUETOFILE(m_entriesFileTime);\r
92         WRITEVALUETOFILE(m_propsFileTime);\r
93         value = m_directoryPath.GetWinPathString().GetLength();\r
94         WRITEVALUETOFILE(value);\r
95         if (value)\r
96         {\r
97                 if (fwrite(m_directoryPath.GetWinPath(), sizeof(TCHAR), value, pFile)!=value)\r
98                         return false;\r
99         }\r
100         if (!m_ownStatus.SaveToDisk(pFile))\r
101                 return false;\r
102         WRITEVALUETOFILE(m_currentFullStatus);\r
103         WRITEVALUETOFILE(m_mostImportantFileStatus);\r
104         return true;\r
105 }\r
106 \r
107 BOOL CCachedDirectory::LoadFromDisk(FILE * pFile)\r
108 {\r
109         AutoLocker lock(m_critSec);\r
110 #define LOADVALUEFROMFILE(x) if (fread(&x, sizeof(x), 1, pFile)!=1) return false;\r
111         try\r
112         {\r
113                 unsigned int value = 0;\r
114                 LOADVALUEFROMFILE(value);\r
115                 if (value != 1)\r
116                         return false;           // not the correct version\r
117                 int mapsize = 0;\r
118                 LOADVALUEFROMFILE(mapsize);\r
119                 for (int i=0; i<mapsize; ++i)\r
120                 {\r
121                         LOADVALUEFROMFILE(value);\r
122                         if (value > MAX_PATH)\r
123                                 return false;\r
124                         if (value)\r
125                         {\r
126                                 CString sKey;\r
127                                 if (fread(sKey.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)\r
128                                 {\r
129                                         sKey.ReleaseBuffer(0);\r
130                                         return false;\r
131                                 }\r
132                                 sKey.ReleaseBuffer(value);\r
133                                 CStatusCacheEntry entry;\r
134                                 if (!entry.LoadFromDisk(pFile))\r
135                                         return false;\r
136                                 m_entryCache[sKey] = entry;\r
137                         }\r
138                 }\r
139                 LOADVALUEFROMFILE(mapsize);\r
140                 for (int i=0; i<mapsize; ++i)\r
141                 {\r
142                         LOADVALUEFROMFILE(value);\r
143                         if (value > MAX_PATH)\r
144                                 return false;\r
145                         if (value)\r
146                         {\r
147                                 CString sPath;\r
148                                 if (fread(sPath.GetBuffer(value), sizeof(TCHAR), value, pFile)!=value)\r
149                                 {\r
150                                         sPath.ReleaseBuffer(0);\r
151                                         return false;\r
152                                 }\r
153                                 sPath.ReleaseBuffer(value);\r
154                                 git_wc_status_kind status;\r
155                                 LOADVALUEFROMFILE(status);\r
156                                 m_childDirectories[CTGitPath(sPath)] = status;\r
157                         }\r
158                 }\r
159                 LOADVALUEFROMFILE(m_entriesFileTime);\r
160                 LOADVALUEFROMFILE(m_propsFileTime);\r
161                 LOADVALUEFROMFILE(value);\r
162                 if (value > MAX_PATH)\r
163                         return false;\r
164                 if (value)\r
165                 {\r
166                         CString sPath;\r
167                         if (fread(sPath.GetBuffer(value+1), sizeof(TCHAR), value, pFile)!=value)\r
168                         {\r
169                                 sPath.ReleaseBuffer(0);\r
170                                 return false;\r
171                         }\r
172                         sPath.ReleaseBuffer(value);\r
173                         m_directoryPath.SetFromWin(sPath);\r
174                 }\r
175                 if (!m_ownStatus.LoadFromDisk(pFile))\r
176                         return false;\r
177 \r
178                 LOADVALUEFROMFILE(m_currentFullStatus);\r
179                 LOADVALUEFROMFILE(m_mostImportantFileStatus);\r
180         }\r
181         catch ( CAtlException )\r
182         {\r
183                 return false;\r
184         }\r
185         return true;\r
186 \r
187 }\r
188 \r
189 CStatusCacheEntry CCachedDirectory::GetStatusForMember(const CTGitPath& path, bool bRecursive,  bool bFetch /* = true */)\r
190 {\r
191         CString strCacheKey;\r
192         bool bThisDirectoryIsUnversioned = false;\r
193         bool bRequestForSelf = false;\r
194         if(path.IsEquivalentToWithoutCase(m_directoryPath))\r
195         {\r
196                 bRequestForSelf = true;\r
197         }\r
198 \r
199         // In all most circumstances, we ask for the status of a member of this directory.\r
200         ATLASSERT(m_directoryPath.IsEquivalentToWithoutCase(path.GetContainingDirectory()) || bRequestForSelf);\r
201 \r
202         // Check if the entries file has been changed\r
203         CTGitPath entriesFilePath(m_directoryPath);\r
204         CTGitPath propsDirPath(m_directoryPath);\r
205         if (g_GitAdminDir.IsVSNETHackActive())\r
206         {\r
207                 entriesFilePath.AppendPathString(g_GitAdminDir.GetVSNETAdminDirName() + _T("\\entries"));\r
208                 propsDirPath.AppendPathString(g_GitAdminDir.GetVSNETAdminDirName() + _T("\\dir-props"));\r
209         }\r
210         else\r
211         {\r
212                 entriesFilePath.AppendPathString(g_GitAdminDir.GetAdminDirName() + _T("\\entries"));\r
213                 propsDirPath.AppendPathString(g_GitAdminDir.GetAdminDirName() + _T("\\dir-props"));\r
214         }\r
215         if ( (m_entriesFileTime == entriesFilePath.GetLastWriteTime()) && ((entriesFilePath.GetLastWriteTime() == 0) || (m_propsFileTime == propsDirPath.GetLastWriteTime())) )\r
216         {\r
217                 m_entriesFileTime = entriesFilePath.GetLastWriteTime();\r
218                 if (m_entriesFileTime)\r
219                         m_propsFileTime = propsDirPath.GetLastWriteTime();\r
220 \r
221                 if(m_entriesFileTime == 0)\r
222                 {\r
223                         // We are a folder which is not in a working copy\r
224                         bThisDirectoryIsUnversioned = true;\r
225                         m_ownStatus.SetStatus(NULL);\r
226 \r
227                         // If a user removes the .svn directory, we get here with m_entryCache\r
228                         // not being empty, but still us being unversioned\r
229                         if (!m_entryCache.empty())\r
230                         {\r
231                                 m_entryCache.clear();\r
232                         }\r
233                         ATLASSERT(m_entryCache.empty());\r
234                         \r
235                         // However, a member *DIRECTORY* might be the top of WC\r
236                         // so we need to ask them to get their own status\r
237                         if(!path.IsDirectory())\r
238                         {\r
239                                 if ((PathFileExists(path.GetWinPath()))||(bRequestForSelf))\r
240                                         return CStatusCacheEntry();\r
241                                 // the entry doesn't exist anymore! \r
242                                 // but we can't remove it from the cache here:\r
243                                 // the GetStatusForMember() method is called only with a read\r
244                                 // lock and not a write lock!\r
245                                 // So mark it for crawling, and let the crawler remove it\r
246                                 // later\r
247                                 CGitStatusCache::Instance().AddFolderForCrawling(path.GetContainingDirectory());\r
248                                 return CStatusCacheEntry();\r
249                         }\r
250                         else\r
251                         {\r
252                                 // If we're in the special case of a directory being asked for its own status\r
253                                 // and this directory is unversioned, then we should just return that here\r
254                                 if(bRequestForSelf)\r
255                                 {\r
256                                         return CStatusCacheEntry();\r
257                                 }\r
258                         }\r
259                 }\r
260 \r
261                 if(path.IsDirectory())\r
262                 {\r
263                         // We don't have directory status in our cache\r
264                         // Ask the directory if it knows its own status\r
265                         CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);\r
266                         if ((dirEntry)&&(dirEntry->IsOwnStatusValid()))\r
267                         {\r
268                                 // To keep recursive status up to date, we'll request that children are all crawled again\r
269                                 // This will be very quick if nothings changed, because it will all be cache hits\r
270                                 if (bRecursive)\r
271                                 {\r
272                                         AutoLocker lock(dirEntry->m_critSec);\r
273                                         ChildDirStatus::const_iterator it;\r
274                                         for(it = dirEntry->m_childDirectories.begin(); it != dirEntry->m_childDirectories.end(); ++it)\r
275                                         {\r
276                                                 CGitStatusCache::Instance().AddFolderForCrawling(it->first);\r
277                                         }\r
278                                 }\r
279 \r
280                                 return dirEntry->GetOwnStatus(bRecursive);\r
281                         }\r
282                 }\r
283                 else\r
284                 {\r
285                         {\r
286                                 // if we currently are fetching the status of the directory\r
287                                 // we want the status for, we just return an empty entry here\r
288                                 // and don't wait for that fetching to finish.\r
289                                 // That's because fetching the status can take a *really* long\r
290                                 // time (e.g. if a commit is also in progress on that same\r
291                                 // directory), and we don't want to make the explorer appear\r
292                                 // to hang.\r
293                                 AutoLocker pathlock(m_critSecPath);\r
294                                 if ((!bFetch)&&(!m_currentStatusFetchingPath.IsEmpty()))\r
295                                 {\r
296                                         if ((m_currentStatusFetchingPath.IsAncestorOf(path))&&((m_currentStatusFetchingPathTicks + 1000)<GetTickCount()))\r
297                                         {\r
298                                                 ATLTRACE(_T("returning empty status (status fetch in progress) for %s\n"), path.GetWinPath());\r
299                                                 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;\r
300                                                 return CStatusCacheEntry();\r
301                                         }\r
302                                 }\r
303                         }\r
304                         // Look up a file in our own cache\r
305                         AutoLocker lock(m_critSec);\r
306                         strCacheKey = GetCacheKey(path);\r
307                         CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);\r
308                         if(itMap != m_entryCache.end())\r
309                         {\r
310                                 // We've hit the cache - check for timeout\r
311                                 if(!itMap->second.HasExpired((long)GetTickCount()))\r
312                                 {\r
313                                         if(itMap->second.DoesFileTimeMatch(path.GetLastWriteTime()))\r
314                                         {\r
315                                                 if ((itMap->second.GetEffectiveStatus()!=git_wc_status_missing)||(!PathFileExists(path.GetWinPath())))\r
316                                                 {\r
317                                                         // Note: the filetime matches after a modified has been committed too.\r
318                                                         // So in that case, we would return a wrong status (e.g. 'modified' instead\r
319                                                         // of 'normal') here.\r
320                                                         return itMap->second;\r
321                                                 }\r
322                                         }\r
323                                 }\r
324                         }\r
325                 }\r
326         }\r
327         else\r
328         {\r
329                 AutoLocker pathlock(m_critSecPath);\r
330                 if ((!bFetch)&&(!m_currentStatusFetchingPath.IsEmpty()))\r
331                 {\r
332                         if ((m_currentStatusFetchingPath.IsAncestorOf(path))&&((m_currentStatusFetchingPathTicks + 1000)<GetTickCount()))\r
333                         {\r
334                                 ATLTRACE(_T("returning empty status (status fetch in progress) for %s\n"), path.GetWinPath());\r
335                                 m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;\r
336                                 return CStatusCacheEntry();\r
337                         }\r
338                 }\r
339                 // if we're fetching the status for the explorer,\r
340                 // we don't refresh the status but use the one\r
341                 // we already have (to save time and make the explorer\r
342                 // more responsive in stress conditions).\r
343                 // We leave the refreshing to the crawler.\r
344                 if ((!bFetch)&&(m_entriesFileTime))\r
345                 {\r
346                         CGitStatusCache::Instance().AddFolderForCrawling(path.GetDirectory());\r
347                         return CStatusCacheEntry();\r
348                 }\r
349                 AutoLocker lock(m_critSec);\r
350                 m_entriesFileTime = entriesFilePath.GetLastWriteTime();\r
351                 m_propsFileTime = propsDirPath.GetLastWriteTime();\r
352                 m_entryCache.clear();\r
353                 strCacheKey = GetCacheKey(path);\r
354         }\r
355 \r
356 //      svn_opt_revision_t revision;\r
357 //      revision.kind = svn_opt_revision_unspecified;\r
358 \r
359         // We've not got this item in the cache - let's add it\r
360         // We never bother asking SVN for the status of just one file, always for its containing directory\r
361 \r
362         if (g_GitAdminDir.IsAdminDirPath(path.GetWinPathString()))\r
363         {\r
364                 // We're being asked for the status of an .SVN directory\r
365                 // It's not worth asking for this\r
366                 return CStatusCacheEntry();\r
367         }\r
368 \r
369         {\r
370                 {\r
371                         AutoLocker pathlock(m_critSecPath);\r
372                         if ((!bFetch)&&(!m_currentStatusFetchingPath.IsEmpty()))\r
373                         {\r
374                                 if ((m_currentStatusFetchingPath.IsAncestorOf(path))&&((m_currentStatusFetchingPathTicks + 1000)<GetTickCount()))\r
375                                 {\r
376                                         ATLTRACE(_T("returning empty status (status fetch in progress) for %s\n"), path.GetWinPath());\r
377                                         m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;\r
378                                         return CStatusCacheEntry();\r
379                                 }\r
380                         }\r
381                 }\r
382 //              SVNPool subPool(CGitStatusCache::Instance().m_svnHelp.Pool());\r
383                 {\r
384                         AutoLocker lock(m_critSec);\r
385                         m_mostImportantFileStatus = git_wc_status_none;\r
386                         m_childDirectories.clear();\r
387                         m_entryCache.clear();\r
388                         m_ownStatus.SetStatus(NULL);\r
389                         m_bRecursive = bRecursive;\r
390                 }\r
391                 if(!bThisDirectoryIsUnversioned)\r
392                 {\r
393                         {\r
394                                 AutoLocker pathlock(m_critSecPath);\r
395                                 m_currentStatusFetchingPath = m_directoryPath;\r
396                                 m_currentStatusFetchingPathTicks = GetTickCount();\r
397                         }\r
398                         ATLTRACE(_T("svn_cli_stat for '%s' (req %s)\n"), m_directoryPath.GetWinPath(), path.GetWinPath());\r
399                         git_error_t* pErr;\r
400 #if 0\r
401                          = svn_client_status4 (\r
402                                 NULL,\r
403                                 m_directoryPath.GetSVNApiPath(subPool),\r
404                                 &revision,\r
405                                 GetStatusCallback,\r
406                                 this,\r
407                                 svn_depth_immediates,\r
408                                 TRUE,                                                                   //getall\r
409                                 FALSE,\r
410                                 TRUE,                                                                   //noignore\r
411                                 FALSE,                                                                  //ignore externals\r
412                                 NULL,                                                                   //changelists\r
413                                 CGitStatusCache::Instance().m_svnHelp.ClientContext(),\r
414                                 subPool\r
415                                 );\r
416 #endif\r
417                         {\r
418                                 AutoLocker pathlock(m_critSecPath);\r
419                                 m_currentStatusFetchingPath.Reset();\r
420                         }\r
421                         ATLTRACE(_T("svn_cli_stat finished for '%s'\n"), m_directoryPath.GetWinPath(), path.GetWinPath());\r
422                         if(pErr)\r
423                         {\r
424                                 // Handle an error\r
425                                 // The most likely error on a folder is that it's not part of a WC\r
426                                 // In most circumstances, this will have been caught earlier,\r
427                                 // but in some situations, we'll get this error.\r
428                                 // If we allow ourselves to fall on through, then folders will be asked\r
429                                 // for their own status, and will set themselves as unversioned, for the \r
430                                 // benefit of future requests\r
431 //                              ATLTRACE("svn_cli_stat err: '%s'\n", pErr->message);\r
432 //                              svn_error_clear(pErr);\r
433                                 // No assert here! Since we _can_ get here, an assertion is not an option!\r
434                                 // Reasons to get here: \r
435                                 // - renaming a folder with many sub folders --> results in "not a working copy" if the revert\r
436                                 //   happens between our checks and the svn_client_status() call.\r
437                                 // - reverting a move/copy --> results in "not a working copy" (as above)\r
438                                 if (!m_directoryPath.HasAdminDir())\r
439                                 {\r
440                                         m_currentFullStatus = m_mostImportantFileStatus = git_wc_status_none;\r
441                                         return CStatusCacheEntry();\r
442                                 }\r
443                                 else\r
444                                 {\r
445                                         ATLTRACE("svn_cli_stat error, assume none status\n");\r
446                                         // Since we only assume a none status here due to svn_client_status()\r
447                                         // returning an error, make sure that this status times out soon.\r
448                                         CGitStatusCache::Instance().m_folderCrawler.BlockPath(m_directoryPath, 2000);\r
449                                         CGitStatusCache::Instance().AddFolderForCrawling(m_directoryPath);\r
450                                         return CStatusCacheEntry();\r
451                                 }\r
452                         }\r
453                 }\r
454                 else\r
455                 {\r
456                         ATLTRACE("Skipped SVN status for unversioned folder\n");\r
457                 }\r
458         }\r
459         // Now that we've refreshed our SVN status, we can see if it's \r
460         // changed the 'most important' status value for this directory.\r
461         // If it has, then we should tell our parent\r
462         UpdateCurrentStatus();\r
463 \r
464         if (path.IsDirectory())\r
465         {\r
466                 CCachedDirectory * dirEntry = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);\r
467                 if ((dirEntry)&&(dirEntry->IsOwnStatusValid()))\r
468                 {\r
469                         CGitStatusCache::Instance().AddFolderForCrawling(path);\r
470                         return dirEntry->GetOwnStatus(bRecursive);\r
471                 }\r
472 \r
473                 // If the status *still* isn't valid here, it means that \r
474                 // the current directory is unversioned, and we shall need to ask its children for info about themselves\r
475                 if (dirEntry)\r
476                         return dirEntry->GetStatusForMember(path,bRecursive);\r
477                 CGitStatusCache::Instance().AddFolderForCrawling(path);\r
478                 return CStatusCacheEntry();\r
479         }\r
480         else\r
481         {\r
482                 CacheEntryMap::iterator itMap = m_entryCache.find(strCacheKey);\r
483                 if(itMap != m_entryCache.end())\r
484                 {\r
485                         return itMap->second;\r
486                 }\r
487         }\r
488 \r
489         AddEntry(path, NULL);\r
490         return CStatusCacheEntry();\r
491 }\r
492 \r
493 void \r
494 CCachedDirectory::AddEntry(const CTGitPath& path, const git_wc_status2_t* pGitStatus, DWORD validuntil /* = 0*/)\r
495 {\r
496         AutoLocker lock(m_critSec);\r
497         if(path.IsDirectory())\r
498         {\r
499                 CCachedDirectory * childDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(path);\r
500                 if (childDir)\r
501                 {\r
502                         if ((childDir->GetCurrentFullStatus() != git_wc_status_missing)||(pGitStatus==NULL)||(pGitStatus->text_status != git_wc_status_unversioned))\r
503                                 childDir->m_ownStatus.SetStatus(pGitStatus);\r
504 //                      childDir->m_ownStatus.SetKind(svn_node_dir);\r
505                 }\r
506         }\r
507         else\r
508         {\r
509                 CString cachekey = GetCacheKey(path);\r
510                 CacheEntryMap::iterator entry_it = m_entryCache.lower_bound(cachekey);\r
511                 if (entry_it != m_entryCache.end() && entry_it->first == cachekey)\r
512                 {\r
513                         if (pGitStatus)\r
514                         {\r
515                                 if (entry_it->second.GetEffectiveStatus() > git_wc_status_none &&\r
516                                         entry_it->second.GetEffectiveStatus() != GitStatus::GetMoreImportant(pGitStatus->prop_status, pGitStatus->text_status))\r
517                                 {\r
518                                         CGitStatusCache::Instance().UpdateShell(path);\r
519                                         ATLTRACE(_T("shell update for %s\n"), path.GetWinPath());\r
520                                 }\r
521                         }\r
522                 }\r
523                 else\r
524                 {\r
525                         entry_it = m_entryCache.insert(entry_it, std::make_pair(cachekey, CStatusCacheEntry()));\r
526                 }\r
527                 entry_it->second = CStatusCacheEntry(pGitStatus, path.GetLastWriteTime(), path.IsReadOnly(), validuntil);\r
528         }\r
529 }\r
530 \r
531 \r
532 CString \r
533 CCachedDirectory::GetCacheKey(const CTGitPath& path)\r
534 {\r
535         // All we put into the cache as a key is just the end portion of the pathname\r
536         // There's no point storing the path of the containing directory for every item\r
537         return path.GetWinPathString().Mid(m_directoryPath.GetWinPathString().GetLength());\r
538 }\r
539 \r
540 CString \r
541 CCachedDirectory::GetFullPathString(const CString& cacheKey)\r
542 {\r
543         return m_directoryPath.GetWinPathString() + _T("\\") + cacheKey;\r
544 }\r
545 \r
546 git_error_t * CCachedDirectory::GetStatusCallback(void *baton, const char *path, git_wc_status2_t *status)\r
547 {\r
548         CCachedDirectory* pThis = (CCachedDirectory*)baton;\r
549 \r
550         if (path == NULL)\r
551                 return 0;\r
552                 \r
553         CTGitPath svnPath;\r
554 \r
555 #if 0\r
556         if(status->entry)\r
557         {\r
558                 if ((status->text_status != git_wc_status_none)&&(status->text_status != git_wc_status_missing))\r
559                         svnPath.SetFromSVN(path, (status->entry->kind == svn_node_dir));\r
560                 else\r
561                         svnPath.SetFromSVN(path);\r
562 \r
563                 if(svnPath.IsDirectory())\r
564                 {\r
565                         if(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))\r
566                         {\r
567                                 if (pThis->m_bRecursive)\r
568                                 {\r
569                                         // Add any versioned directory, which is not our 'self' entry, to the list for having its status updated\r
570                                         CGitStatusCache::Instance().AddFolderForCrawling(svnPath);\r
571                                 }\r
572 \r
573                                 // Make sure we know about this child directory\r
574                                 // This initial status value is likely to be overwritten from below at some point\r
575                                 git_wc_status_kind s = GitStatus::GetMoreImportant(status->text_status, status->prop_status);\r
576                                 CCachedDirectory * cdir = CGitStatusCache::Instance().GetDirectoryCacheEntryNoCreate(svnPath);\r
577                                 if (cdir)\r
578                                 {\r
579                                         // This child directory is already in our cache!\r
580                                         // So ask this dir about its recursive status\r
581                                         git_wc_status_kind st = GitStatus::GetMoreImportant(s, cdir->GetCurrentFullStatus());\r
582                                         AutoLocker lock(pThis->m_critSec);\r
583                                         pThis->m_childDirectories[svnPath] = st;\r
584                                 }\r
585                                 else\r
586                                 {\r
587                                         // the child directory is not in the cache. Create a new entry for it in the cache which is\r
588                                         // initially 'unversioned'. But we added that directory to the crawling list above, which\r
589                                         // means the cache will be updated soon.\r
590                                         CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);\r
591                                         AutoLocker lock(pThis->m_critSec);\r
592                                         pThis->m_childDirectories[svnPath] = s;\r
593                                 }\r
594                         }\r
595                 }\r
596                 else\r
597                 {\r
598                         // Keep track of the most important status of all the files in this directory\r
599                         // Don't include subdirectories in this figure, because they need to provide their \r
600                         // own 'most important' value\r
601                         pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->text_status);\r
602                         pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, status->prop_status);\r
603                         if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_none))\r
604                                 &&(CGitStatusCache::Instance().IsUnversionedAsModified()))\r
605                         {\r
606                                 // treat unversioned files as modified\r
607                                 if (pThis->m_mostImportantFileStatus != git_wc_status_added)\r
608                                         pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);\r
609                         }\r
610                 }\r
611         }\r
612         else\r
613         {\r
614                 svnPath.SetFromSVN(path);\r
615                 // Subversion returns no 'entry' field for versioned folders if they're\r
616                 // part of another working copy (nested layouts).\r
617                 // So we have to make sure that such an 'unversioned' folder really\r
618                 // is unversioned.\r
619                 if (((status->text_status == git_wc_status_unversioned)||(status->text_status == git_wc_status_missing))&&(!svnPath.IsEquivalentToWithoutCase(pThis->m_directoryPath))&&(svnPath.IsDirectory()))\r
620                 {\r
621                         if (svnPath.HasAdminDir())\r
622                         {\r
623                                 CGitStatusCache::Instance().AddFolderForCrawling(svnPath);\r
624                                 // Mark the directory as 'versioned' (status 'normal' for now).\r
625                                 // This initial value will be overwritten from below some time later\r
626                                 {\r
627                                         AutoLocker lock(pThis->m_critSec);\r
628                                         pThis->m_childDirectories[svnPath] = git_wc_status_normal;\r
629                                 }\r
630                                 // Make sure the entry is also in the cache\r
631                                 CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);\r
632                                 // also mark the status in the status object as normal\r
633                                 status->text_status = git_wc_status_normal;\r
634                         }\r
635                 }\r
636                 else if (status->text_status == git_wc_status_external)\r
637                 {\r
638                         CGitStatusCache::Instance().AddFolderForCrawling(svnPath);\r
639                         // Mark the directory as 'versioned' (status 'normal' for now).\r
640                         // This initial value will be overwritten from below some time later\r
641                         {\r
642                                 AutoLocker lock(pThis->m_critSec);\r
643                                 pThis->m_childDirectories[svnPath] = git_wc_status_normal;\r
644                         }\r
645                         // we have added a directory to the child-directory list of this\r
646                         // directory. We now must make sure that this directory also has\r
647                         // an entry in the cache.\r
648                         CGitStatusCache::Instance().GetDirectoryCacheEntry(svnPath);\r
649                         // also mark the status in the status object as normal\r
650                         status->text_status = git_wc_status_normal;\r
651                 }\r
652                 else\r
653                 {\r
654                         if (svnPath.IsDirectory())\r
655                         {\r
656                                 AutoLocker lock(pThis->m_critSec);\r
657                                 pThis->m_childDirectories[svnPath] = GitStatus::GetMoreImportant(status->text_status, status->prop_status);\r
658                         }\r
659                         else if ((CGitStatusCache::Instance().IsUnversionedAsModified())&&(status->text_status != git_wc_status_missing))\r
660                         {\r
661                                 // make this unversioned item change the most important status of this\r
662                                 // folder to modified if it doesn't already have another status\r
663                                 if (pThis->m_mostImportantFileStatus != git_wc_status_added)\r
664                                         pThis->m_mostImportantFileStatus = GitStatus::GetMoreImportant(pThis->m_mostImportantFileStatus, git_wc_status_modified);\r
665                         }\r
666                 }\r
667         }\r
668 #endif\r
669         pThis->AddEntry(svnPath, status);\r
670 \r
671         return 0;\r
672 }\r
673 \r
674 bool \r
675 CCachedDirectory::IsOwnStatusValid() const\r
676 {\r
677         return m_ownStatus.HasBeenSet() && \r
678                    !m_ownStatus.HasExpired(GetTickCount()) &&\r
679                    // 'external' isn't a valid status. That just\r
680                    // means the folder is not part of the current working\r
681                    // copy but it still has its own 'real' status\r
682                    m_ownStatus.GetEffectiveStatus()!=git_wc_status_external &&\r
683                    m_ownStatus.IsKindKnown();\r
684 }\r
685 \r
686 void CCachedDirectory::Invalidate()\r
687 {\r
688         m_ownStatus.Invalidate();\r
689 }\r
690 \r
691 git_wc_status_kind CCachedDirectory::CalculateRecursiveStatus()\r
692 {\r
693         // Combine our OWN folder status with the most important of our *FILES'* status.\r
694         git_wc_status_kind retVal = GitStatus::GetMoreImportant(m_mostImportantFileStatus, m_ownStatus.GetEffectiveStatus());\r
695 \r
696         if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))\r
697         {\r
698                 if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))\r
699                         retVal = git_wc_status_modified;\r
700         }\r
701 \r
702         // Now combine all our child-directorie's status\r
703         \r
704         AutoLocker lock(m_critSec);\r
705         ChildDirStatus::const_iterator it;\r
706         for(it = m_childDirectories.begin(); it != m_childDirectories.end(); ++it)\r
707         {\r
708                 retVal = GitStatus::GetMoreImportant(retVal, it->second);\r
709                 if ((retVal != git_wc_status_modified)&&(retVal != m_ownStatus.GetEffectiveStatus()))\r
710                 {\r
711                         if ((retVal == git_wc_status_added)||(retVal == git_wc_status_deleted)||(retVal == git_wc_status_missing))\r
712                                 retVal = git_wc_status_modified;\r
713                 }\r
714         }\r
715         \r
716         return retVal;\r
717 }\r
718 \r
719 // Update our composite status and deal with things if it's changed\r
720 void CCachedDirectory::UpdateCurrentStatus()\r
721 {\r
722         git_wc_status_kind newStatus = CalculateRecursiveStatus();\r
723 \r
724         if ((newStatus != m_currentFullStatus)&&(m_ownStatus.IsVersioned()))\r
725         {\r
726                 if ((m_currentFullStatus != git_wc_status_none)&&(m_ownStatus.GetEffectiveStatus() != git_wc_status_missing))\r
727                 {\r
728                         // Our status has changed - tell the shell\r
729                         ATLTRACE(_T("Dir %s, status change from %d to %d, send shell notification\n"), m_directoryPath.GetWinPath(), m_currentFullStatus, newStatus);           \r
730                         CGitStatusCache::Instance().UpdateShell(m_directoryPath);\r
731                 }\r
732                 if (m_ownStatus.GetEffectiveStatus() != git_wc_status_missing)\r
733                         m_currentFullStatus = newStatus;\r
734                 else\r
735                         m_currentFullStatus = git_wc_status_missing;\r
736         }\r
737         // And tell our parent, if we've got one...\r
738         // we tell our parent *always* about our status, even if it hasn't\r
739         // changed. This is to make sure that the parent has really our current\r
740         // status - the parent can decide itself if our status has changed\r
741         // or not.\r
742         CTGitPath parentPath = m_directoryPath.GetContainingDirectory();\r
743         if(!parentPath.IsEmpty())\r
744         {\r
745                 // We have a parent\r
746                 CCachedDirectory * cachedDir = CGitStatusCache::Instance().GetDirectoryCacheEntry(parentPath);\r
747                 if (cachedDir)\r
748                         cachedDir->UpdateChildDirectoryStatus(m_directoryPath, m_currentFullStatus);\r
749         }\r
750 }\r
751 \r
752 \r
753 // Receive a notification from a child that its status has changed\r
754 void CCachedDirectory::UpdateChildDirectoryStatus(const CTGitPath& childDir, git_wc_status_kind childStatus)\r
755 {\r
756         git_wc_status_kind currentStatus = git_wc_status_none;\r
757         {\r
758                 AutoLocker lock(m_critSec);\r
759                 currentStatus = m_childDirectories[childDir];\r
760         }\r
761         if ((currentStatus != childStatus)||(!IsOwnStatusValid()))\r
762         {\r
763                 {\r
764                         AutoLocker lock(m_critSec);\r
765                         m_childDirectories[childDir] = childStatus;\r
766                 }\r
767                 UpdateCurrentStatus();\r
768         }\r
769 }\r
770 \r
771 CStatusCacheEntry CCachedDirectory::GetOwnStatus(bool bRecursive)\r
772 {\r
773         // Don't return recursive status if we're unversioned ourselves.\r
774         if(bRecursive && m_ownStatus.GetEffectiveStatus() > git_wc_status_unversioned)\r
775         {\r
776                 CStatusCacheEntry recursiveStatus(m_ownStatus);\r
777                 UpdateCurrentStatus();\r
778                 recursiveStatus.ForceStatus(m_currentFullStatus);\r
779                 return recursiveStatus;                         \r
780         }\r
781         else\r
782         {\r
783                 return m_ownStatus;\r
784         }\r
785 }\r
786 \r
787 void CCachedDirectory::RefreshStatus(bool bRecursive)\r
788 {\r
789         // Make sure that our own status is up-to-date\r
790         GetStatusForMember(m_directoryPath,bRecursive);\r
791 \r
792         AutoLocker lock(m_critSec);\r
793         // We also need to check if all our file members have the right date on them\r
794         CacheEntryMap::iterator itMembers;\r
795         std::set<CTGitPath> refreshedpaths;\r
796         DWORD now = GetTickCount();\r
797         if (m_entryCache.size() == 0)\r
798                 return;\r
799         for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)\r
800         {\r
801                 if (itMembers->first)\r
802                 {\r
803                         CTGitPath filePath(m_directoryPath);\r
804                         filePath.AppendPathString(itMembers->first);\r
805                         std::set<CTGitPath>::iterator refr_it;\r
806                         if ((!filePath.IsEquivalentToWithoutCase(m_directoryPath))&&\r
807                                 (((refr_it = refreshedpaths.lower_bound(filePath)) == refreshedpaths.end()) || !filePath.IsEquivalentToWithoutCase(*refr_it)))\r
808                         {\r
809                                 if ((itMembers->second.HasExpired(now))||(!itMembers->second.DoesFileTimeMatch(filePath.GetLastWriteTime())))\r
810                                 {\r
811                                         lock.Unlock();\r
812                                         // We need to request this item as well\r
813                                         GetStatusForMember(filePath,bRecursive);\r
814                                         // GetStatusForMember now has recreated the m_entryCache map.\r
815                                         // So start the loop again, but add this path to the refreshed paths set\r
816                                         // to make sure we don't refresh this path again. This is to make sure\r
817                                         // that we don't end up in an endless loop.\r
818                                         lock.Lock();\r
819                                         refreshedpaths.insert(refr_it, filePath);\r
820                                         itMembers = m_entryCache.begin();\r
821                                         if (m_entryCache.size()==0)\r
822                                                 return;\r
823                                         continue;\r
824                                 }\r
825                                 else if ((bRecursive)&&(itMembers->second.IsDirectory()))\r
826                                 {\r
827                                         // crawl all sub folders too! Otherwise a change deep inside the\r
828                                         // tree which has changed won't get propagated up the tree.\r
829                                         CGitStatusCache::Instance().AddFolderForCrawling(filePath);\r
830                                 }\r
831                         }\r
832                 }\r
833         }\r
834 }\r
835 \r
836 void CCachedDirectory::RefreshMostImportant()\r
837 {\r
838         CacheEntryMap::iterator itMembers;\r
839         git_wc_status_kind newStatus = m_ownStatus.GetEffectiveStatus();\r
840         for (itMembers = m_entryCache.begin(); itMembers != m_entryCache.end(); ++itMembers)\r
841         {\r
842                 newStatus = GitStatus::GetMoreImportant(newStatus, itMembers->second.GetEffectiveStatus());\r
843                 if (((itMembers->second.GetEffectiveStatus() == git_wc_status_unversioned)||(itMembers->second.GetEffectiveStatus() == git_wc_status_none))\r
844                         &&(CGitStatusCache::Instance().IsUnversionedAsModified()))\r
845                 {\r
846                         // treat unversioned files as modified\r
847                         if (newStatus != git_wc_status_added)\r
848                                 newStatus = GitStatus::GetMoreImportant(newStatus, git_wc_status_modified);\r
849                 }\r
850         }\r
851         if (newStatus != m_mostImportantFileStatus)\r
852         {\r
853                 ATLTRACE(_T("status change of path %s\n"), m_directoryPath.GetWinPath());\r
854                 CGitStatusCache::Instance().UpdateShell(m_directoryPath);\r
855         }\r
856         m_mostImportantFileStatus = newStatus;\r
857 }\r