Drop depth parameter
[TortoiseGit.git] / src / Utils / Hooks.cpp
blob998674665ba663c9f4c45b9175d1447f019c660e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2014 - TortoiseGit
4 // Copyright (C) 2007-2008 - 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.
20 #include "stdafx.h"
21 #include "Hooks.h"
22 #include "registry.h"
23 #include "StringUtils.h"
24 #include "TempFile.h"
25 #include "SmartHandle.h"
27 CHooks* CHooks::m_pInstance;
29 CHooks::CHooks()
33 CHooks::~CHooks()
37 bool CHooks::Create()
39 if (m_pInstance == NULL)
40 m_pInstance = new CHooks();
41 CRegString reghooks = CRegString(_T("Software\\TortoiseGit\\hooks"));
42 CString strhooks = reghooks;
43 // now fill the map with all the hooks defined in the string
44 // the string consists of multiple lines, where one hook script is defined
45 // as four lines:
46 // line 1: the hook type
47 // line 2: path to working copy where to apply the hook script
48 // line 3: command line to execute
49 // line 4: 'true' or 'false' for waiting for the script to finish
50 // line 5: 'show' or 'hide' on how to start the hook script
51 hookkey key;
52 int pos = 0;
53 hookcmd cmd;
54 while ((pos = strhooks.Find('\n')) >= 0)
56 // line 1
57 key.htype = GetHookType(strhooks.Mid(0, pos));
58 if (pos+1 < strhooks.GetLength())
59 strhooks = strhooks.Mid(pos+1);
60 else
61 strhooks.Empty();
62 bool bComplete = false;
63 if ((pos = strhooks.Find('\n')) >= 0)
65 // line 2
66 key.path = CTGitPath(strhooks.Mid(0, pos));
67 if (pos+1 < strhooks.GetLength())
68 strhooks = strhooks.Mid(pos+1);
69 else
70 strhooks.Empty();
71 if ((pos = strhooks.Find('\n')) >= 0)
73 // line 3
74 cmd.commandline = strhooks.Mid(0, pos);
75 if (pos+1 < strhooks.GetLength())
76 strhooks = strhooks.Mid(pos+1);
77 else
78 strhooks.Empty();
79 if ((pos = strhooks.Find('\n')) >= 0)
81 // line 4
82 cmd.bWait = (strhooks.Mid(0, pos).CompareNoCase(_T("true"))==0);
83 if (pos+1 < strhooks.GetLength())
84 strhooks = strhooks.Mid(pos+1);
85 else
86 strhooks.Empty();
87 if ((pos = strhooks.Find('\n')) >= 0)
89 // line 5
90 cmd.bShow = (strhooks.Mid(0, pos).CompareNoCase(_T("show"))==0);
91 if (pos+1 < strhooks.GetLength())
92 strhooks = strhooks.Mid(pos+1);
93 else
94 strhooks.Empty();
95 bComplete = true;
100 if (bComplete)
102 m_pInstance->insert(std::pair<hookkey, hookcmd>(key, cmd));
105 return true;
108 CHooks& CHooks::Instance()
110 return *m_pInstance;
113 void CHooks::Destroy()
115 delete m_pInstance;
118 bool CHooks::Save()
120 CString strhooks;
121 for (hookiterator it = begin(); it != end(); ++it)
123 strhooks += GetHookTypeString(it->first.htype);
124 strhooks += '\n';
125 strhooks += it->first.path.GetWinPathString();
126 strhooks += '\n';
127 strhooks += it->second.commandline;
128 strhooks += '\n';
129 strhooks += (it->second.bWait ? _T("true") : _T("false"));
130 strhooks += '\n';
131 strhooks += (it->second.bShow ? _T("show") : _T("hide"));
132 strhooks += '\n';
134 CRegString reghooks = CRegString(_T("Software\\TortoiseGit\\hooks"));
135 reghooks = strhooks;
136 if (reghooks.GetLastError())
137 return false;
138 return true;
141 bool CHooks::Remove(const hookkey &key)
143 return (erase(key) > 0);
146 void CHooks::Add(hooktype ht, const CTGitPath& Path, LPCTSTR szCmd, bool bWait, bool bShow)
148 hookkey key;
149 key.htype = ht;
150 key.path = Path;
151 hookiterator it = find(key);
152 if (it!=end())
153 erase(it);
155 hookcmd cmd;
156 cmd.commandline = szCmd;
157 cmd.bWait = bWait;
158 cmd.bShow = bShow;
159 insert(std::pair<hookkey, hookcmd>(key, cmd));
162 CString CHooks::GetHookTypeString(hooktype t)
164 switch (t)
166 case start_commit_hook:
167 return _T("start_commit_hook");
168 case pre_commit_hook:
169 return _T("pre_commit_hook");
170 case post_commit_hook:
171 return _T("post_commit_hook");
172 case pre_push_hook:
173 return _T("pre_push_hook");
174 case post_push_hook:
175 return _T("post_push_hook");
177 return _T("");
180 hooktype CHooks::GetHookType(const CString& s)
182 if (s.Compare(_T("start_commit_hook"))==0)
183 return start_commit_hook;
184 if (s.Compare(_T("pre_commit_hook"))==0)
185 return pre_commit_hook;
186 if (s.Compare(_T("post_commit_hook"))==0)
187 return post_commit_hook;
188 if (s.Compare(_T("pre_push_hook"))==0)
189 return pre_push_hook;
190 if (s.Compare(_T("post_push_hook"))==0)
191 return post_push_hook;
193 return unknown_hook;
196 void CHooks::AddParam(CString& sCmd, const CString& param)
198 sCmd += _T(" \"");
199 sCmd += param;
200 sCmd += _T("\"");
203 void CHooks::AddPathParam(CString& sCmd, const CTGitPathList& pathList)
205 CTGitPath temppath = CTempFiles::Instance().GetTempFilePath(true);
206 pathList.WriteToFile(temppath.GetWinPathString(), true);
207 AddParam(sCmd, temppath.GetWinPathString());
210 void CHooks::AddCWDParam(CString& sCmd, const CTGitPathList& pathList)
212 AddParam(sCmd, pathList.GetCommonRoot().GetDirectory().GetWinPathString());
215 void CHooks::AddErrorParam(CString& sCmd, const CString& error)
217 CTGitPath tempPath;
218 tempPath = CTempFiles::Instance().GetTempFilePath(true);
219 CStringUtils::WriteStringToTextFile(tempPath.GetWinPath(), (LPCTSTR)error);
220 AddParam(sCmd, tempPath.GetWinPathString());
223 CTGitPath CHooks::AddMessageFileParam(CString& sCmd, const CString& message)
225 CTGitPath tempPath;
226 tempPath = CTempFiles::Instance().GetTempFilePath(true);
227 CStringUtils::WriteStringToTextFile(tempPath.GetWinPath(), (LPCTSTR)message);
228 AddParam(sCmd, tempPath.GetWinPathString());
229 return tempPath;
232 bool CHooks::StartCommit(const CTGitPathList& pathList, CString& message, DWORD& exitcode, CString& error)
234 hookiterator it = FindItem(start_commit_hook, pathList);
235 if (it == end())
236 return false;
237 CString sCmd = it->second.commandline;
238 AddPathParam(sCmd, pathList);
239 CTGitPath temppath = AddMessageFileParam(sCmd, message);
240 AddCWDParam(sCmd, pathList);
241 exitcode = RunScript(sCmd, pathList.GetCommonRoot().GetDirectory().GetWinPath(), error, it->second.bWait, it->second.bShow);
242 if (!exitcode && !temppath.IsEmpty())
244 CStringUtils::ReadStringFromTextFile(temppath.GetWinPathString(), message);
246 return true;
249 bool CHooks::PreCommit(const CTGitPathList& pathList, const CString& message, DWORD& exitcode, CString& error)
251 hookiterator it = FindItem(pre_commit_hook, pathList);
252 if (it == end())
253 return false;
254 CString sCmd = it->second.commandline;
255 AddPathParam(sCmd, pathList);
256 AddMessageFileParam(sCmd, message);
257 AddCWDParam(sCmd, pathList);
258 exitcode = RunScript(sCmd, pathList.GetCommonRoot().GetDirectory().GetWinPath(), error, it->second.bWait, it->second.bShow);
259 return true;
262 bool CHooks::PostCommit(const CTGitPathList& pathList, const GitRev& rev, const CString& message, DWORD& exitcode, CString& error)
264 hookiterator it = FindItem(post_commit_hook, pathList);
265 if (it == end())
266 return false;
267 CString sCmd = it->second.commandline;
268 AddPathParam(sCmd, pathList);
269 AddMessageFileParam(sCmd, message);
270 AddParam(sCmd, rev.m_CommitHash.ToString());
271 AddErrorParam(sCmd, error);
272 AddCWDParam(sCmd, pathList);
273 exitcode = RunScript(sCmd, pathList.GetCommonRoot().GetDirectory().GetWinPath(), error, it->second.bWait, it->second.bShow);
274 return true;
277 bool CHooks::PrePush(const CTGitPathList& pathList,DWORD& exitcode, CString& error)
279 hookiterator it = FindItem(pre_push_hook, pathList);
280 if (it == end())
281 return false;
282 CString sCmd = it->second.commandline;
283 AddPathParam(sCmd, pathList);
284 AddErrorParam(sCmd, error);
285 AddCWDParam(sCmd, pathList);
286 exitcode = RunScript(sCmd, pathList.GetCommonRoot().GetDirectory().GetWinPath(), error, it->second.bWait, it->second.bShow);
287 return true;
290 bool CHooks::PostPush(const CTGitPathList& pathList,DWORD& exitcode, CString& error)
292 hookiterator it = FindItem(post_push_hook, pathList);
293 if (it == end())
294 return false;
295 CString sCmd = it->second.commandline;
296 AddPathParam(sCmd, pathList);
297 AddErrorParam(sCmd, error);
298 AddCWDParam(sCmd, pathList);
299 exitcode = RunScript(sCmd, pathList.GetCommonRoot().GetDirectory().GetWinPath(), error, it->second.bWait, it->second.bShow);
300 return true;
303 hookiterator CHooks::FindItem(hooktype t, const CTGitPathList& pathList)
305 hookkey key;
306 for (int i=0; i<pathList.GetCount(); ++i)
308 CTGitPath path = pathList[i];
311 key.htype = t;
312 key.path = path;
313 hookiterator it = find(key);
314 if (it != end())
316 return it;
318 path = path.GetContainingDirectory();
319 } while(!path.IsEmpty());
321 // look for a script with a path as '*'
322 key.htype = t;
323 key.path = CTGitPath(_T("*"));
324 hookiterator it = find(key);
325 if (it != end())
327 return it;
330 return end();
333 DWORD CHooks::RunScript(CString cmd, LPCTSTR currentDir, CString& error, bool bWait, bool bShow)
335 DWORD exitcode = 0;
336 SECURITY_ATTRIBUTES sa;
337 SecureZeroMemory(&sa, sizeof(sa));
338 sa.nLength = sizeof(sa);
339 sa.bInheritHandle = TRUE;
341 CAutoFile hOut ;
342 CAutoFile hRedir;
343 CAutoFile hErr;
345 // clear the error string
346 error.Empty();
348 // Create Temp File for redirection
349 TCHAR szTempPath[MAX_PATH] = {0};
350 TCHAR szOutput[MAX_PATH] = {0};
351 TCHAR szErr[MAX_PATH] = {0};
352 GetTortoiseGitTempPath(_countof(szTempPath), szTempPath);
353 GetTempFileName(szTempPath, _T("git"), 0, szErr);
355 // setup redirection handles
356 // output handle must be WRITE mode, share READ
357 // redirect handle must be READ mode, share WRITE
358 hErr = CreateFile(szErr, GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0);
360 if (!hErr)
361 return (DWORD)-1;
363 hRedir = CreateFile(szErr, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
365 if (!hRedir)
366 return (DWORD)-1;
368 GetTempFileName(szTempPath, _T("git"), 0, szOutput);
369 hOut = CreateFile(szOutput, GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0);
371 if (!hOut)
372 return (DWORD)-1;
374 // setup startup info, set std out/err handles
375 // hide window
376 STARTUPINFO si;
377 SecureZeroMemory(&si, sizeof(si));
378 si.cb = sizeof(si);
379 if (hOut != INVALID_HANDLE_VALUE)
381 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
382 si.hStdOutput = hOut;
383 si.hStdError = hErr;
384 si.wShowWindow = bShow ? SW_SHOW : SW_HIDE;
387 PROCESS_INFORMATION pi;
388 SecureZeroMemory(&pi, sizeof(pi));
390 DWORD dwFlags = 0;
392 if (!CreateProcess(NULL, cmd.GetBuffer(), NULL, NULL, TRUE, dwFlags, NULL, currentDir, &si, &pi))
394 int err = GetLastError(); // preserve the CreateProcess error
395 error = CFormatMessageWrapper(err);
396 SetLastError(err);
397 cmd.ReleaseBuffer();
398 return (DWORD)-1;
400 cmd.ReleaseBuffer();
402 CloseHandle(pi.hThread);
404 // wait for process to finish, capture redirection and
405 // send it to the parent window/console
406 if (bWait)
408 DWORD dw;
409 char buf[256] = { 0 };
412 while (ReadFile(hRedir, &buf, sizeof(buf)-1, &dw, NULL))
414 if (dw == 0)
415 break;
416 error += CString(CStringA(buf,dw));
418 } while (WaitForSingleObject(pi.hProcess, 0) != WAIT_OBJECT_0);
420 // perform any final flushing
421 while (ReadFile(hRedir, &buf, sizeof(buf)-1, &dw, NULL))
423 if (dw == 0)
424 break;
426 error += CString(CStringA(buf, dw));
428 WaitForSingleObject(pi.hProcess, INFINITE);
429 GetExitCodeProcess(pi.hProcess, &exitcode);
431 CloseHandle(pi.hProcess);
432 DeleteFile(szOutput);
433 DeleteFile(szErr);
435 return exitcode;