Update libgit2
[TortoiseGit.git] / src / TortoiseShell / TortoiseGIT.cpp
blobb97a9ceab604069170d22d54853f10456c58b68d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2010, 2012 - TortoiseSVN
4 // Copyright (C) 2008-2012,2014 - 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"
25 #include "gitindex.h"
27 volatile LONG g_cRefThisDll = 0; ///< reference count of this DLL.
28 HINSTANCE g_hmodThisDll = NULL; ///< handle to this DLL itself.
29 ShellCache g_ShellCache; ///< caching of registry entries, ...
30 DWORD g_langid;
31 DWORD g_langTimeout = 0;
32 HINSTANCE g_hResInst = NULL;
33 stdstring g_filepath;
34 git_wc_status_kind g_filestatus = git_wc_status_none; ///< holds the corresponding status to the file/dir above
35 bool g_readonlyoverlay = false;
36 bool g_lockedoverlay = false;
38 bool g_normalovlloaded = false;
39 bool g_modifiedovlloaded = false;
40 bool g_conflictedovlloaded = false;
41 bool g_readonlyovlloaded = false;
42 bool g_deletedovlloaded = false;
43 bool g_lockedovlloaded = false;
44 bool g_addedovlloaded = false;
45 bool g_ignoredovlloaded = false;
46 bool g_unversionedovlloaded = false;
47 CComCriticalSection g_csGlobalCOMGuard;
49 LPCTSTR g_MenuIDString = _T("TortoiseGit");
51 ShellObjects g_shellObjects;
52 CGitIndexFileMap g_IndexFileMap;
54 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
56 extern "C" int APIENTRY
57 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */)
59 #ifdef _DEBUG
60 // if no debugger is present, then don't load the dll.
61 // this prevents other apps from loading the dll and locking
62 // it.
64 bool bInShellTest = false;
65 TCHAR buf[MAX_PATH + 1] = {0}; // MAX_PATH ok, the test really is for debugging anyway.
66 DWORD pathLength = GetModuleFileName(NULL, buf, MAX_PATH);
67 if(pathLength >= 14)
69 if (pathLength >= 24 && _tcsicmp(&buf[pathLength - 24], _T("\\TortoiseGitExplorer.exe")) == 0)
71 bInShellTest = true;
73 if ((_tcsicmp(&buf[pathLength-14], _T("\\ShellTest.exe"))) == 0)
75 bInShellTest = true;
77 if ((_tcsicmp(&buf[pathLength-13], _T("\\verclsid.exe"))) == 0)
79 bInShellTest = true;
83 if (!::IsDebuggerPresent() && !bInShellTest)
85 ATLTRACE("In debug load preventer\n");
86 return FALSE;
88 #endif
90 // NOTE: Do *NOT* call apr_initialize() or apr_terminate() here in DllMain(),
91 // because those functions call LoadLibrary() indirectly through malloc().
92 // And LoadLibrary() inside DllMain() is not allowed and can lead to unexpected
93 // behavior and even may create dependency loops in the dll load order.
94 if (dwReason == DLL_PROCESS_ATTACH)
96 if (g_hmodThisDll == NULL)
98 g_csGlobalCOMGuard.Init();
101 // Extension DLL one-time initialization
102 g_hmodThisDll = hInstance;
104 else if (dwReason == DLL_PROCESS_DETACH)
106 // do not clean up memory here:
107 // if an application doesn't release all COM objects
108 // but still unloads the dll, cleaning up ourselves
109 // will lead to crashes.
110 // better to leak some memory than to crash other apps.
111 // sometimes an application doesn't release all COM objects
112 // but still unloads the dll.
113 // in that case, we do it ourselves
114 g_csGlobalCOMGuard.Term();
116 return 1; // ok
119 STDAPI DllCanUnloadNow(void)
121 return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
124 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
126 if (ppvOut == 0)
127 return E_POINTER;
128 *ppvOut = NULL;
130 FileState state = FileStateInvalid;
131 if (IsEqualIID(rclsid, CLSID_Tortoisegit_UPTODATE))
132 state = FileStateVersioned;
133 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_MODIFIED))
134 state = FileStateModified;
135 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_CONFLICTING))
136 state = FileStateConflict;
137 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_UNCONTROLLED))
138 state = FileStateUncontrolled;
139 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_DROPHANDLER))
140 state = FileStateDropHandler;
141 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_DELETED))
142 state = FileStateDeleted;
143 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_READONLY))
144 state = FileStateReadOnly;
145 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_LOCKED))
146 state = FileStateLockedOverlay;
147 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_ADDED))
148 state = FileStateAddedOverlay;
149 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_IGNORED))
150 state = FileStateIgnoredOverlay;
151 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_UNVERSIONED))
152 state = FileStateUnversionedOverlay;
154 if (state != FileStateInvalid)
156 CShellExtClassFactory *pcf = new (std::nothrow) CShellExtClassFactory(state);
157 if (pcf == NULL)
158 return E_OUTOFMEMORY;
159 // refcount currently set to 0
160 git_libgit2_init();
161 const HRESULT hr = pcf->QueryInterface(riid, ppvOut);
162 if(FAILED(hr))
163 delete pcf;
164 return hr;
167 return CLASS_E_CLASSNOTAVAILABLE;