forked from alansbraga/NPPGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPPGit.cpp
More file actions
299 lines (254 loc) · 7.01 KB
/
NPPGit.cpp
File metadata and controls
299 lines (254 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
///----------------------------------------------------------------------------
/// Copyright (c) 2008-2010
/// Brandon Cannaday
/// Paranoid Ferret Productions (support@paranoidferret.com)
///
/// This program is free software; you can redistribute it and/or
/// modify it under the terms of the GNU General Public License
/// as published by the Free Software Foundation; either
/// version 2 of the License, or (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program; if not, write to the Free Software
/// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
///----------------------------------------------------------------------------
#include "stdafx.h"
#include "PluginInterface.h"
#include <string>
#include <vector>
const TCHAR PLUGIN_NAME[] = TEXT("Git");
static NppData nppData;
static std::vector<FuncItem> funcItems;
//
// Required plugin methods
//
extern "C" __declspec(dllexport) BOOL isUnicode()
{
return TRUE;
}
extern "C" __declspec(dllexport) void setInfo(NppData notpadPlusData)
{
nppData = notpadPlusData;
}
extern "C" __declspec(dllexport) const TCHAR * getName()
{
return PLUGIN_NAME;
}
extern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode)
{
}
extern "C" __declspec(dllexport) LRESULT messageProc(UINT Message, WPARAM wParam, LPARAM lParam)
{
return TRUE;
}
extern "C" __declspec(dllexport) FuncItem * getFuncsArray(int *nbF)
{
*nbF = funcItems.size();
return &funcItems.front();
}
///
/// Gets the path to the current file.
///
/// @return Current file path.
///
std::wstring getCurrentFile()
{
TCHAR path[MAX_PATH];
::SendMessage(nppData._nppHandle, NPPM_GETFULLCURRENTPATH, MAX_PATH, (LPARAM)path);
return std::wstring(path);
}
///
/// Gets the full path to every opened file.
///
/// @param numFiles [out] Returns the number of opened files.
///
/// @return Vector of filenames.
///
std::vector<std::wstring> getAllFiles()
{
//get the number of opened files
//notepad++ always returns an extra "new 1", remove it by subtracting 1
int numFiles = (::SendMessage(nppData._nppHandle, NPPM_GETNBOPENFILES, 0, ALL_OPEN_FILES)) - 1;
//allocate memory to hold filenames
TCHAR** files = new TCHAR*[numFiles];
for(int i = 0; i < numFiles; i++)
files[i] = new TCHAR[MAX_PATH];
//get filenames
::SendMessage(nppData._nppHandle, NPPM_GETOPENFILENAMES, (WPARAM)files, (LPARAM)numFiles);
std::vector<std::wstring> filePaths;
for(int i = 0; i < numFiles; i++)
filePaths.push_back(files[i]);
return filePaths;
}
///
/// Gets the path to the TortioseGit executable from the registry.
///
/// @param loc [out] Location of Tortoise executable
///
/// @return Whether or not the path was successfully retrieved.
/// If false, Tortoise is most likely not installed.
bool getTortoiseLocation(std::wstring &loc)
{
HKEY hKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\TortoiseGit"), 0,
KEY_READ | KEY_WOW64_64KEY, &hKey) != ERROR_SUCCESS)
{
return false;
}
TCHAR procPath[MAX_PATH];
DWORD length = MAX_PATH;
// Modified Douglas Phillips <doug@sbscomp.com> 2008-12-29 to
// support 32-bit and Non-Vista operating Systems.
if(RegQueryValueEx(
hKey,
TEXT("ProcPath"),
NULL,
NULL,
(LPBYTE)procPath,
&length) != ERROR_SUCCESS)
{
return false;
}
loc = loc.append(procPath);
return true;
}
///
/// Launches TortoiseGit using the supplied command
///
/// @param Command line string to execute.
///
/// @return Whether or not Tortoise could be launched.
///
bool launchTortoise(std::wstring &command)
{
STARTUPINFOW si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
return CreateProcess(
NULL,
const_cast<LPWSTR>(command.c_str()),
NULL,
NULL,
FALSE,
CREATE_DEFAULT_ERROR_MODE,
NULL,
NULL,
&si,
&pi) != 0;
}
///
/// Builds and executes command line string to send to CreateProcess
/// See http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-automation.html
/// for TortoiseSVN command line parameters.
///
/// @param cmd Command name to execute.
/// @param all Execute command on all files, or just the current file.
///
void ExecCommand(const std::wstring &cmd, bool all = false)
{
std::wstring tortoiseLoc;
bool tortoiseInstalled = getTortoiseLocation(tortoiseLoc);
if(!tortoiseInstalled)
{
MessageBox(NULL, TEXT("Could not locate TortoiseGit"), TEXT("Update Failed"), 0);
return;
}
std::vector<std::wstring> files;
if (all)
files = getAllFiles();
else
files.push_back(getCurrentFile());
std::wstring command = tortoiseLoc;
command += TEXT(" /command:") + cmd + TEXT(" /path:\"");
for(std::vector<std::wstring>::iterator itr = files.begin();
itr != files.end(); itr++)
{
command += (*itr);
if(itr != files.end() - 1)
command += TEXT("*");
}
command += TEXT("\" /closeonend:0");
if(!launchTortoise(command))
MessageBox(NULL, TEXT("Could not launch TortoiseGit."), TEXT("Update Failed"), 0);
}
void AddCommand(PFUNCPLUGINCMD func, LPCTSTR sMenuItemCaption, UCHAR cShortCut)
{
FuncItem item;
item._pFunc = func;
lstrcpy(item._itemName, sMenuItemCaption);
item._init2Check = false;
item._pShKey = new ShortcutKey;
item._pShKey->_isAlt = true;
item._pShKey->_isCtrl = true;
item._pShKey->_isShift = false;
item._pShKey->_key = cShortCut;
funcItems.push_back(item);
}
////////////////////////////////////////////////////////////////////////////
///
/// Execution commands:
///
void commitFile()
{
ExecCommand(TEXT("commit"));
}
void commitAllFiles()
{
ExecCommand(TEXT("commit"), true);
}
void addFile()
{
ExecCommand(TEXT("add"));
}
void diffFile()
{
ExecCommand(TEXT("diff"));
}
void revertFile()
{
ExecCommand(TEXT("revert"));
}
void revertAllFiles()
{
ExecCommand(TEXT("revert"),true);
}
void showFileLog()
{
ExecCommand(TEXT("log"));
}
void blameFile()
{
ExecCommand(TEXT("blame"));
}
////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
AddCommand(commitFile, TEXT("Commit File"), 'C');
AddCommand(commitAllFiles, TEXT("Commit All Open Files"), 0);
AddCommand(addFile, TEXT("Add File"), 'A');
AddCommand(diffFile, TEXT("Diff File"), 'D');
AddCommand(revertFile, TEXT("Revert File"), 'R');
AddCommand(revertAllFiles, TEXT("Revert All Open Files"), 0);
AddCommand(showFileLog, TEXT("Show File Log"), 'L');
AddCommand(blameFile, TEXT("Blame File"), 0);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}