1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2007-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "StringUtils.h"
25 CHooks
* CHooks::m_pInstance
;
37 if (m_pInstance
== NULL
)
38 m_pInstance
= new CHooks();
39 CRegString reghooks
= CRegString(_T("Software\\TortoiseGit\\hooks"));
40 CString strhooks
= reghooks
;
41 // now fill the map with all the hooks defined in the string
42 // the string consists of multiple lines, where one hook script is defined
44 // line 1: the hook type
45 // line 2: path to working copy where to apply the hook script
46 // line 3: command line to execute
47 // line 4: 'true' or 'false' for waiting for the script to finish
48 // line 5: 'show' or 'hide' on how to start the hook script
52 while ((pos
= strhooks
.Find('\n')) >= 0)
55 key
.htype
= GetHookType(strhooks
.Mid(0, pos
));
56 if (pos
+1 < strhooks
.GetLength())
57 strhooks
= strhooks
.Mid(pos
+1);
60 bool bComplete
= false;
61 if ((pos
= strhooks
.Find('\n')) >= 0)
64 key
.path
= CTGitPath(strhooks
.Mid(0, pos
));
65 if (pos
+1 < strhooks
.GetLength())
66 strhooks
= strhooks
.Mid(pos
+1);
69 if ((pos
= strhooks
.Find('\n')) >= 0)
72 cmd
.commandline
= strhooks
.Mid(0, pos
);
73 if (pos
+1 < strhooks
.GetLength())
74 strhooks
= strhooks
.Mid(pos
+1);
77 if ((pos
= strhooks
.Find('\n')) >= 0)
80 cmd
.bWait
= (strhooks
.Mid(0, pos
).CompareNoCase(_T("true"))==0);
81 if (pos
+1 < strhooks
.GetLength())
82 strhooks
= strhooks
.Mid(pos
+1);
85 if ((pos
= strhooks
.Find('\n')) >= 0)
88 cmd
.bShow
= (strhooks
.Mid(0, pos
).CompareNoCase(_T("show"))==0);
89 if (pos
+1 < strhooks
.GetLength())
90 strhooks
= strhooks
.Mid(pos
+1);
100 m_pInstance
->insert(std::pair
<hookkey
, hookcmd
>(key
, cmd
));
106 CHooks
& CHooks::Instance()
111 void CHooks::Destroy()
119 for (hookiterator it
= begin(); it
!= end(); ++it
)
121 strhooks
+= GetHookTypeString(it
->first
.htype
);
123 strhooks
+= it
->first
.path
.GetWinPathString();
125 strhooks
+= it
->second
.commandline
;
127 strhooks
+= (it
->second
.bWait
? _T("true") : _T("false"));
129 strhooks
+= (it
->second
.bShow
? _T("show") : _T("hide"));
132 CRegString reghooks
= CRegString(_T("Software\\TortoiseGit\\hooks"));
134 if (reghooks
.GetLastError())
139 bool CHooks::Remove(hookkey key
)
141 return (erase(key
) > 0);
144 void CHooks::Add(hooktype ht
, const CTGitPath
& Path
, LPCTSTR szCmd
, bool bWait
, bool bShow
)
149 hookiterator it
= find(key
);
154 cmd
.commandline
= szCmd
;
157 insert(std::pair
<hookkey
, hookcmd
>(key
, cmd
));
160 CString
CHooks::GetHookTypeString(hooktype t
)
164 case start_commit_hook
:
165 return _T("start_commit_hook");
166 case pre_commit_hook
:
167 return _T("pre_commit_hook");
168 case post_commit_hook
:
169 return _T("post_commit_hook");
170 case start_update_hook
:
171 return _T("start_update_hook");
172 case pre_update_hook
:
173 return _T("pre_update_hook");
174 case post_update_hook
:
175 return _T("post_update_hook");
177 return _T("pre_push_hook");
179 return _T("post_push_hook");
184 hooktype
CHooks::GetHookType(const CString
& s
)
186 if (s
.Compare(_T("start_commit_hook"))==0)
187 return start_commit_hook
;
188 if (s
.Compare(_T("pre_commit_hook"))==0)
189 return pre_commit_hook
;
190 if (s
.Compare(_T("post_commit_hook"))==0)
191 return post_commit_hook
;
192 if (s
.Compare(_T("start_update_hook"))==0)
193 return start_update_hook
;
194 if (s
.Compare(_T("pre_update_hook"))==0)
195 return pre_update_hook
;
196 if (s
.Compare(_T("post_update_hook"))==0)
197 return post_update_hook
;
198 if (s
.Compare(_T("pre_push_hook"))==0)
199 return pre_push_hook
;
200 if (s
.Compare(_T("post_push_hook"))==0)
201 return post_push_hook
;
206 void CHooks::AddParam(CString
& sCmd
, const CString
& param
)
213 void CHooks::AddPathParam(CString
& sCmd
, const CTGitPathList
& pathList
)
215 CTGitPath temppath
= CTempFiles::Instance().GetTempFilePath(true);
216 pathList
.WriteToFile(temppath
.GetWinPathString(), true);
217 AddParam(sCmd
, temppath
.GetWinPathString());
220 void CHooks::AddCWDParam(CString
& sCmd
, const CTGitPathList
& pathList
)
222 AddParam(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPathString());
225 void CHooks::AddDepthParam(CString
& sCmd
, git_depth_t depth
)
228 sTemp
.Format(_T("%d"), depth
);
229 AddParam(sCmd
, sTemp
);
232 void CHooks::AddErrorParam(CString
& sCmd
, const CString
& error
)
235 tempPath
= CTempFiles::Instance().GetTempFilePath(true);
236 CStringUtils::WriteStringToTextFile(tempPath
.GetWinPath(), (LPCTSTR
)error
);
237 AddParam(sCmd
, tempPath
.GetWinPathString());
240 CTGitPath
CHooks::AddMessageFileParam(CString
& sCmd
, const CString
& message
)
243 tempPath
= CTempFiles::Instance().GetTempFilePath(true);
244 CStringUtils::WriteStringToTextFile(tempPath
.GetWinPath(), (LPCTSTR
)message
);
245 AddParam(sCmd
, tempPath
.GetWinPathString());
249 bool CHooks::StartCommit(const CTGitPathList
& pathList
, CString
& message
, DWORD
& exitcode
, CString
& error
)
251 hookiterator it
= FindItem(start_commit_hook
, pathList
);
254 CString sCmd
= it
->second
.commandline
;
255 AddPathParam(sCmd
, pathList
);
256 CTGitPath temppath
= AddMessageFileParam(sCmd
, message
);
257 AddCWDParam(sCmd
, pathList
);
258 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
259 if (!exitcode
&& !temppath
.IsEmpty())
261 CStringUtils::ReadStringFromTextFile(temppath
.GetWinPathString(), message
);
266 bool CHooks::PreCommit(const CTGitPathList
& pathList
, git_depth_t depth
, const CString
& message
, DWORD
& exitcode
, CString
& error
)
268 hookiterator it
= FindItem(pre_commit_hook
, pathList
);
271 CString sCmd
= it
->second
.commandline
;
272 AddPathParam(sCmd
, pathList
);
273 AddDepthParam(sCmd
, depth
);
274 AddMessageFileParam(sCmd
, message
);
275 AddCWDParam(sCmd
, pathList
);
276 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
280 bool CHooks::PostCommit(const CTGitPathList
& pathList
, git_depth_t depth
, GitRev rev
, const CString
& message
, DWORD
& exitcode
, CString
& error
)
282 hookiterator it
= FindItem(post_commit_hook
, pathList
);
285 CString sCmd
= it
->second
.commandline
;
286 AddPathParam(sCmd
, pathList
);
287 AddDepthParam(sCmd
, depth
);
288 AddMessageFileParam(sCmd
, message
);
289 AddParam(sCmd
, rev
.m_CommitHash
.ToString());
290 AddErrorParam(sCmd
, error
);
291 AddCWDParam(sCmd
, pathList
);
292 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
296 bool CHooks::StartUpdate(const CTGitPathList
& pathList
, DWORD
& exitcode
, CString
& error
)
298 hookiterator it
= FindItem(start_update_hook
, pathList
);
301 CString sCmd
= it
->second
.commandline
;
302 AddPathParam(sCmd
, pathList
);
303 AddCWDParam(sCmd
, pathList
);
304 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
308 bool CHooks::PreUpdate(const CTGitPathList
& pathList
, git_depth_t depth
, GitRev rev
, DWORD
& exitcode
, CString
& error
)
310 hookiterator it
= FindItem(pre_update_hook
, pathList
);
313 CString sCmd
= it
->second
.commandline
;
314 AddPathParam(sCmd
, pathList
);
315 AddDepthParam(sCmd
, depth
);
316 AddParam(sCmd
, rev
.m_CommitHash
.ToString());
317 AddCWDParam(sCmd
, pathList
);
318 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
322 bool CHooks::PostUpdate(const CTGitPathList
& pathList
, git_depth_t depth
, GitRev rev
, DWORD
& exitcode
, CString
& error
)
324 hookiterator it
= FindItem(post_update_hook
, pathList
);
327 CString sCmd
= it
->second
.commandline
;
328 AddPathParam(sCmd
, pathList
);
329 AddDepthParam(sCmd
, depth
);
330 AddParam(sCmd
, rev
.m_CommitHash
.ToString());
331 AddErrorParam(sCmd
, error
);
332 AddCWDParam(sCmd
, pathList
);
333 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
337 bool CHooks::PrePush(const CTGitPathList
& pathList
,DWORD
& exitcode
, CString
& error
)
339 hookiterator it
= FindItem(pre_push_hook
, pathList
);
342 CString sCmd
= it
->second
.commandline
;
343 AddPathParam(sCmd
, pathList
);
344 AddErrorParam(sCmd
, error
);
345 AddCWDParam(sCmd
, pathList
);
346 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
350 bool CHooks::PostPush(const CTGitPathList
& pathList
,DWORD
& exitcode
, CString
& error
)
352 hookiterator it
= FindItem(post_push_hook
, pathList
);
355 CString sCmd
= it
->second
.commandline
;
356 AddPathParam(sCmd
, pathList
);
357 AddErrorParam(sCmd
, error
);
358 AddCWDParam(sCmd
, pathList
);
359 exitcode
= RunScript(sCmd
, pathList
.GetCommonRoot().GetDirectory().GetWinPath(), error
, it
->second
.bWait
, it
->second
.bShow
);
363 hookiterator
CHooks::FindItem(hooktype t
, const CTGitPathList
& pathList
)
366 for (int i
=0; i
<pathList
.GetCount(); ++i
)
368 CTGitPath path
= pathList
[i
];
373 hookiterator it
= find(key
);
378 path
= path
.GetContainingDirectory();
379 } while(!path
.IsEmpty());
381 // look for a script with a path as '*'
383 key
.path
= CTGitPath(_T("*"));
384 hookiterator it
= find(key
);
393 DWORD
CHooks::RunScript(CString cmd
, LPCTSTR currentDir
, CString
& error
, bool bWait
, bool bShow
)
396 SECURITY_ATTRIBUTES sa
;
397 SecureZeroMemory(&sa
, sizeof(sa
));
398 sa
.nLength
= sizeof(sa
);
399 sa
.bInheritHandle
= TRUE
;
401 HANDLE hOut
= INVALID_HANDLE_VALUE
;
402 HANDLE hRedir
= INVALID_HANDLE_VALUE
;
403 HANDLE hErr
= INVALID_HANDLE_VALUE
;
405 // clear the error string
408 // Create Temp File for redirection
409 TCHAR szTempPath
[MAX_PATH
];
410 TCHAR szOutput
[MAX_PATH
];
411 TCHAR szErr
[MAX_PATH
];
412 GetTempPath(sizeof(szTempPath
)/sizeof(TCHAR
),szTempPath
);
413 GetTempFileName(szTempPath
, _T("svn"), 0, szErr
);
415 // setup redirection handles
416 // output handle must be WRITE mode, share READ
417 // redirect handle must be READ mode, share WRITE
418 hErr
= CreateFile(szErr
, GENERIC_WRITE
, FILE_SHARE_READ
, &sa
, CREATE_ALWAYS
, FILE_ATTRIBUTE_TEMPORARY
, 0);
420 if (hErr
== INVALID_HANDLE_VALUE
)
425 hRedir
= CreateFile(szErr
, GENERIC_READ
, FILE_SHARE_WRITE
, 0, OPEN_EXISTING
, 0, 0);
427 if (hRedir
== INVALID_HANDLE_VALUE
)
433 GetTempFileName(szTempPath
, _T("svn"), 0, szOutput
);
434 hOut
= CreateFile(szOutput
, GENERIC_WRITE
, FILE_SHARE_READ
, &sa
, CREATE_ALWAYS
, FILE_ATTRIBUTE_TEMPORARY
, 0);
436 if (hOut
== INVALID_HANDLE_VALUE
)
443 // setup startup info, set std out/err handles
446 SecureZeroMemory(&si
, sizeof(si
));
448 if (hOut
!= INVALID_HANDLE_VALUE
)
450 si
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
451 si
.hStdOutput
= hOut
;
453 si
.wShowWindow
= bShow
? SW_SHOW
: SW_HIDE
;
456 PROCESS_INFORMATION pi
;
457 SecureZeroMemory(&pi
, sizeof(pi
));
461 if (!CreateProcess(NULL
, cmd
.GetBuffer(), NULL
, NULL
, TRUE
, dwFlags
, NULL
, currentDir
, &si
, &pi
))
463 int err
= GetLastError(); // preserve the CreateProcess error
464 if (hErr
!= INVALID_HANDLE_VALUE
)
475 CloseHandle(pi
.hThread
);
477 // wait for process to finish, capture redirection and
478 // send it to the parent window/console
485 SecureZeroMemory(&buf
,sizeof(buf
));
486 while (ReadFile(hRedir
, &buf
, sizeof(buf
)-1, &dw
, NULL
))
490 error
+= CString(CStringA(buf
,dw
));
491 SecureZeroMemory(&buf
,sizeof(buf
));
493 } while (WaitForSingleObject(pi
.hProcess
, 0) != WAIT_OBJECT_0
);
495 // perform any final flushing
496 while (ReadFile(hRedir
, &buf
, sizeof(buf
)-1, &dw
, NULL
))
501 error
+= CString(CStringA(buf
, dw
));
502 SecureZeroMemory(&buf
,sizeof(buf
));
504 WaitForSingleObject(pi
.hProcess
, INFINITE
);
505 GetExitCodeProcess(pi
.hProcess
, &exitcode
);
507 CloseHandle(pi
.hProcess
);
511 DeleteFile(szOutput
);