Prepare release and bump version numbers to 2.17.0.2
[TortoiseGit.git] / src / TortoiseShell / TortoiseGIT.cpp
blobeb2db3c0c4822ca56e63552d2d566e6dbd131819
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2010, 2012, 2021-2023 - TortoiseSVN
4 // Copyright (C) 2008-2012, 2014, 2016-2017 - 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 "ShellExt.h"
22 #include "Guids.h"
23 #include "ShellExtClassFactory.h"
24 #include "ShellObjects.h"
26 volatile LONG g_cRefThisDll = 0; ///< reference count of this DLL.
27 HINSTANCE g_hmodThisDll = nullptr; ///< handle to this DLL itself.
28 ShellCache g_ShellCache; ///< caching of registry entries, ...
29 DWORD g_langid;
30 ULONGLONG g_langTimeout = 0;
31 HINSTANCE g_hResInst = nullptr;
32 std::wstring g_filepath;
33 git_wc_status_kind g_filestatus = git_wc_status_none; ///< holds the corresponding status to the file/dir above
34 bool g_readonlyoverlay = false;
35 bool g_lockedoverlay = false;
37 bool g_normalovlloaded = false;
38 bool g_modifiedovlloaded = false;
39 bool g_conflictedovlloaded = false;
40 bool g_readonlyovlloaded = false;
41 bool g_deletedovlloaded = false;
42 bool g_lockedovlloaded = false;
43 bool g_addedovlloaded = false;
44 bool g_ignoredovlloaded = false;
45 bool g_unversionedovlloaded = false;
46 CComCriticalSection g_csGlobalCOMGuard;
48 LPCWSTR g_MenuIDString = L"TortoiseGit";
50 ShellObjects g_shellObjects;
52 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
54 extern "C" int APIENTRY
55 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */)
57 #ifdef _DEBUG
58 // if no debugger is present, then don't load the dll.
59 // this prevents other apps from loading the dll and locking
60 // it.
62 bool bInShellTest = false;
63 wchar_t buf[MAX_PATH + 1] = { 0 }; // MAX_PATH ok, the test really is for debugging anyway.
64 const DWORD pathLength = GetModuleFileName(nullptr, buf, _countof(buf) - 1);
65 if(pathLength >= 14)
67 if (pathLength >= 24 && _wcsicmp(&buf[pathLength - 24], L"\\TortoiseGitExplorer.exe") == 0)
68 bInShellTest = true;
69 if ((_wcsicmp(&buf[pathLength-14], L"\\ShellTest.exe")) == 0)
70 bInShellTest = true;
71 if ((_wcsicmp(&buf[pathLength-13], L"\\verclsid.exe")) == 0)
72 bInShellTest = true;
75 if (!::IsDebuggerPresent() && !bInShellTest)
77 ATLTRACE("In debug load preventer\n");
78 return FALSE;
80 #endif
82 // NOTE: Do *NOT* call apr_initialize() or apr_terminate() here in DllMain(),
83 // because those functions call LoadLibrary() indirectly through malloc().
84 // And LoadLibrary() inside DllMain() is not allowed and can lead to unexpected
85 // behavior and even may create dependency loops in the dll load order.
86 if (dwReason == DLL_PROCESS_ATTACH)
88 DisableThreadLibraryCalls(hInstance);
89 if (!g_hmodThisDll)
90 g_csGlobalCOMGuard.Init();
92 // Extension DLL one-time initialization
93 g_hmodThisDll = hInstance;
95 else if (dwReason == DLL_PROCESS_DETACH)
97 // do not clean up memory here:
98 // if an application doesn't release all COM objects
99 // but still unloads the dll, cleaning up ourselves
100 // will lead to crashes.
101 // better to leak some memory than to crash other apps.
102 // sometimes an application doesn't release all COM objects
103 // but still unloads the dll.
104 // in that case, we do it ourselves
105 g_csGlobalCOMGuard.Term();
107 return 1; // ok
110 STDAPI DllCanUnloadNow()
112 return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
115 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
117 if (!ppvOut)
118 return E_POINTER;
119 *ppvOut = nullptr;
121 FileState state = FileStateInvalid;
122 if (IsEqualIID(rclsid, CLSID_Tortoisegit_UPTODATE))
123 state = FileStateVersioned;
124 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_MODIFIED))
125 state = FileStateModified;
126 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_CONFLICTING))
127 state = FileStateConflict;
128 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_UNCONTROLLED))
129 state = FileStateUncontrolled;
130 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_DROPHANDLER))
131 state = FileStateDropHandler;
132 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_DELETED))
133 state = FileStateDeleted;
134 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_READONLY))
135 state = FileStateReadOnly;
136 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_LOCKED))
137 state = FileStateLockedOverlay;
138 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_ADDED))
139 state = FileStateAddedOverlay;
140 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_IGNORED))
141 state = FileStateIgnoredOverlay;
142 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_UNVERSIONED))
143 state = FileStateUnversionedOverlay;
144 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_EXPLORERCOMMAND))
145 state = FileStateVersioned;
147 if (state != FileStateInvalid)
149 CShellExtClassFactory *pcf = new (std::nothrow) CShellExtClassFactory(state);
150 if (!pcf)
151 return E_OUTOFMEMORY;
152 // refcount currently set to 0
153 const HRESULT hr = pcf->QueryInterface(riid, ppvOut);
154 if(FAILED(hr))
155 delete pcf;
156 return hr;
159 return CLASS_E_CLASSNOTAVAILABLE;