1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-2017 - TortoiseGit
4 // Copyright (C) 2003-2008, 2017 - TortoiseSVN
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.
23 #include "PreserveChdir.h"
24 #include "UnicodeUtils.h"
25 #include "GitStatus.h"
26 #include "../TGitCache/CacheInterface.h"
27 #include "GitAdminDir.h"
28 #include "StringUtils.h"
30 // "The Shell calls IShellIconOverlayIdentifier::GetOverlayInfo to request the
31 // location of the handler's icon overlay. The icon overlay handler returns
32 // the name of the file containing the overlay image, and its index within
33 // that file. The Shell then adds the icon overlay to the system image list."
35 STDMETHODIMP
CShellExt::GetOverlayInfo(LPWSTR pwszIconFile
, int cchMax
, int* pIndex
, DWORD
* pdwFlags
)
39 return GetOverlayInfo_Wrap(pwszIconFile
, cchMax
, pIndex
, pdwFlags
);
41 __except(CCrashReport::Instance().SendReport(GetExceptionInformation()))
47 STDMETHODIMP
CShellExt::GetOverlayInfo_Wrap(LPWSTR pwszIconFile
, int cchMax
, int* pIndex
, DWORD
* pdwFlags
)
58 // Set "out parameters" since we return S_OK later.
63 // Now here's where we can find out if due to lack of enough overlay
64 // slots some of our overlays won't be shown.
65 // To do that we have to mark every overlay handler that's successfully
66 // loaded, so we can later check if some are missing
69 case FileStateVersioned
: g_normalovlloaded
= true; break;
70 case FileStateModified
: g_modifiedovlloaded
= true; break;
71 case FileStateConflict
: g_conflictedovlloaded
= true; break;
72 case FileStateDeleted
: g_deletedovlloaded
= true; break;
73 case FileStateReadOnly
: g_readonlyovlloaded
= true; break;
74 case FileStateLockedOverlay
: g_lockedovlloaded
= true; break;
75 case FileStateAddedOverlay
: g_addedovlloaded
= true; break;
76 case FileStateIgnoredOverlay
: g_ignoredovlloaded
= true; break;
77 case FileStateUnversionedOverlay
: g_unversionedovlloaded
= true; break;
80 // we don't have to set the icon file and/or the index here:
81 // the icons are handled by the TortoiseOverlays dll.
85 STDMETHODIMP
CShellExt::GetPriority(int *pPriority
)
89 return GetPriority_Wrap(pPriority
);
91 __except(CCrashReport::Instance().SendReport(GetExceptionInformation()))
97 STDMETHODIMP
CShellExt::GetPriority_Wrap(int *pPriority
)
104 case FileStateConflict
:
107 case FileStateModified
:
110 case FileStateDeleted
:
113 case FileStateReadOnly
:
116 case FileStateLockedOverlay
:
119 case FileStateAddedOverlay
:
122 case FileStateVersioned
:
132 // "Before painting an object's icon, the Shell passes the object's name to
133 // each icon overlay handler's IShellIconOverlayIdentifier::IsMemberOf
134 // method. If a handler wants to have its icon overlay displayed,
135 // it returns S_OK. The Shell then calls the handler's
136 // IShellIconOverlayIdentifier::GetOverlayInfo method to determine which icon
139 STDMETHODIMP
CShellExt::IsMemberOf(LPCWSTR pwszPath
, DWORD dwAttrib
)
143 return IsMemberOf_Wrap(pwszPath
, dwAttrib
);
145 __except(CCrashReport::Instance().SendReport(GetExceptionInformation()))
151 STDMETHODIMP
CShellExt::IsMemberOf_Wrap(LPCWSTR pwszPath
, DWORD
/*dwAttrib*/)
155 const TCHAR
* pPath
= pwszPath
;
156 // the shell sometimes asks overlays for invalid paths, e.g. for network
157 // printers (in that case the path is "0", at least for me here).
158 if (wcslen(pPath
) < 2)
160 PreserveChdir preserveChdir
;
161 git_wc_status_kind status
= git_wc_status_none
;
162 bool readonlyoverlay
= false;
163 bool lockedoverlay
= false;
165 // since the shell calls each and every overlay handler with the same filepath
166 // we use a small 'fast' cache of just one path here.
167 // To make sure that cache expires, clear it as soon as one handler is used.
169 AutoLocker
lock(g_csGlobalCOMGuard
);
170 if (wcscmp(pPath
, g_filepath
.c_str()) == 0)
172 status
= g_filestatus
;
173 readonlyoverlay
= g_readonlyoverlay
;
174 lockedoverlay
= g_lockedoverlay
;
178 if (!g_ShellCache
.IsPathAllowed(pPath
))
180 if ((m_State
== FileStateVersioned
) && g_ShellCache
.ShowExcludedAsNormal() &&
181 (PathGetDriveNumber(pPath
)>1) &&
182 PathIsDirectory(pPath
) && g_ShellCache
.HasGITAdminDir(pPath
, true))
189 auto cacheType
= g_ShellCache
.GetCacheType();
190 if (g_ShellCache
.IsOnlyNonElevated() && g_ShellCache
.IsProcessElevated())
192 cacheType
= ShellCache::none
;
193 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": elevated overlays blocked\n");
197 case ShellCache::exe
:
199 CTGitPath
tpath(pPath
);
200 if(!tpath
.HasAdminDir())
202 status
= git_wc_status_none
;
205 if(tpath
.IsAdminDir())
207 status
= git_wc_status_none
;
210 TGITCacheResponse itemStatus
;
211 SecureZeroMemory(&itemStatus
, sizeof(itemStatus
));
212 if (m_remoteCacheLink
.GetStatusFromRemoteCache(tpath
, &itemStatus
, true))
214 if (itemStatus
.m_bAssumeValid
)
215 readonlyoverlay
= true;
216 if (itemStatus
.m_bSkipWorktree
)
217 lockedoverlay
= true;
218 status
= (git_wc_status_kind
)itemStatus
.m_status
;
222 case ShellCache::dll
:
223 case ShellCache::dllFull
:
225 // Look in our caches for this item
226 const FileStatusCacheEntry
* s
= m_CachedStatus
.GetCachedItem(CTGitPath(pPath
));
231 readonlyoverlay
= true;
233 lockedoverlay
= true;
237 // No cached status available
239 // since the dwAttrib param of the IsMemberOf() function does not
240 // have the SFGAO_FOLDER flag set at all (it's 0 for files and folders!)
241 // we have to check if the path is a folder ourselves :(
242 if (PathIsDirectory(pPath
))
244 if (g_ShellCache
.HasGITAdminDir(pPath
, TRUE
))
246 if ((!g_ShellCache
.IsRecursive()) && (!g_ShellCache
.IsFolderOverlay()))
247 status
= git_wc_status_normal
;
250 s
= m_CachedStatus
.GetFullStatus(CTGitPath(pPath
), TRUE
);
255 status
= git_wc_status_none
;
257 else if (CStringUtils::EndsWith(pPath
, GitAdminDir::GetAdminDirName()))
258 status
= git_wc_status_none
;
261 s
= m_CachedStatus
.GetFullStatus(CTGitPath(pPath
), FALSE
);
264 readonlyoverlay
= true;
266 lockedoverlay
= true;
273 case ShellCache::none
:
275 // no cache means we only show a 'versioned' overlay on folders
276 // with an admin directory
277 if (PathIsDirectory(pPath
))
279 if (g_ShellCache
.HasGITAdminDir(pPath
, TRUE
))
280 status
= git_wc_status_normal
;
282 status
= git_wc_status_none
;
285 status
= git_wc_status_none
;
289 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Status %d for file %s\n", status
, pwszPath
);
293 g_filestatus
= status
;
294 g_readonlyoverlay
= readonlyoverlay
;
295 g_lockedoverlay
= lockedoverlay
;
297 //the priority system of the shell doesn't seem to work as expected (or as I expected):
298 //as it seems that if one handler returns S_OK then that handler is used, no matter
299 //if other handlers would return S_OK too (they're never called on my machine!)
300 //So we return S_OK for ONLY ONE handler!
304 // note: we can show other overlays if due to lack of enough free overlay
305 // slots some of our overlays aren't loaded. But we assume that
306 // at least the 'normal' overlay is available.
307 // if the 'modified' overlay isn't available, we show the 'normal' overlay,
308 // but in this case the overlays don't really provide anything useful anymore.
309 case git_wc_status_none
:
311 case git_wc_status_unversioned
:
312 if (g_ShellCache
.ShowUnversionedOverlay() && g_unversionedovlloaded
&& (m_State
== FileStateUnversionedOverlay
))
318 case git_wc_status_ignored
:
319 if (g_ShellCache
.ShowIgnoredOverlay() && g_ignoredovlloaded
&& (m_State
== FileStateIgnoredOverlay
))
325 case git_wc_status_normal
:
326 // skip-worktree aka locked has higher priority than assume-valid
327 if ((lockedoverlay
)&&(g_lockedovlloaded
))
329 if (m_State
== FileStateLockedOverlay
)
337 else if ((readonlyoverlay
)&&(g_readonlyovlloaded
))
339 if (m_State
== FileStateReadOnly
)
347 else if (m_State
== FileStateVersioned
)
354 case git_wc_status_deleted
:
355 if (g_deletedovlloaded
)
357 if (m_State
== FileStateDeleted
)
367 // the 'deleted' overlay isn't available (due to lack of enough
368 // overlay slots). So just show the 'modified' overlay instead.
369 if (m_State
== FileStateModified
)
377 case git_wc_status_modified
:
378 if (g_modifiedovlloaded
)
380 if (m_State
== FileStateModified
)
390 if (m_State
== FileStateVersioned
)
398 case git_wc_status_added
:
399 if (g_addedovlloaded
)
401 if (m_State
== FileStateAddedOverlay
)
411 // the 'added' overlay isn't available (due to lack of enough
412 // overlay slots). So just show the 'modified' overlay instead.
413 if (m_State
== FileStateModified
)
421 case git_wc_status_conflicted
:
422 if (g_conflictedovlloaded
)
424 if (m_State
== FileStateConflict
)
434 // the 'conflicted' overlay isn't available (due to lack of enough
435 // overlay slots). So just show the 'modified' overlay instead.
436 if (m_State
== FileStateModified
)