Merge branch 'bare-repo'
[TortoiseGit.git] / src / Utils / Hooks.h
blobf6faedaad6d33fd0c350018c9f6c3db67be0125f
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2006-2008 - TortoiseGit
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.
19 #pragma once
20 #include <map>
21 #include "registry.h"
22 #include "TGitPath.h"
23 #include "GitRev.h"
24 #include "GitStatus.h"
26 /**
27 * \ingroup TortoiseProc
28 * enumeration of all client hook types
30 typedef enum hooktype
32 unknown_hook,
33 start_commit_hook,
34 pre_commit_hook,
35 post_commit_hook,
36 start_update_hook,
37 pre_update_hook,
38 post_update_hook,
39 issue_tracker_hook,
40 pre_push_hook,
41 post_push_hook,
42 } hooktype;
44 /**
45 * \ingroup TortoiseProc
46 * helper class, used as the key to the std::map we store
47 * the data for the client hook scripts in.
49 class hookkey
51 public:
52 hooktype htype;
53 CTGitPath path;
55 bool operator < (const hookkey& hk) const
57 if (htype == hk.htype)
58 return (path < hk.path);
59 else
60 return htype < hk.htype;
64 /**
65 * \ingroup TortoiseProc
66 * helper struct, used as the value to the std::map we
67 * store the data for the client hook scripts in.
69 typedef struct hookcmd
71 CString commandline;
72 bool bWait;
73 bool bShow;
74 } hookcmd;
76 typedef std::map<hookkey, hookcmd>::iterator hookiterator;
78 /**
79 * \ingroup TortoiseProc
80 * Singleton class which deals with the client hook scripts.
82 class CHooks : public std::map<hookkey, hookcmd>
84 private:
85 CHooks();
86 ~CHooks();
87 void AddPathParam(CString& sCmd, const CTGitPathList& pathList);
88 void AddDepthParam(CString& sCmd, git_depth_t depth);
89 void AddCWDParam(CString& sCmd, const CTGitPathList& pathList);
90 void AddErrorParam(CString& sCmd, const CString& error);
91 void AddParam(CString& sCmd, const CString& param);
92 CTGitPath AddMessageFileParam(CString& sCmd, const CString& message);
93 public:
94 /// Create the singleton. Call this at the start of the program.
95 static bool Create();
96 /// Returns the singleton instance
97 static CHooks& Instance();
98 /// Destroys the singleton object. Call this at the end of the program.
99 static void Destroy();
101 public:
102 /// Saves the hook script information to the registry.
103 bool Save();
105 * Removes the hook script identified by \c key. To make the change persistent
106 * call Save().
108 bool Remove(hookkey key);
110 * Adds a new hook script. To make the change persistent, call Save().
112 void Add(hooktype ht, const CTGitPath& Path, LPCTSTR szCmd,
113 bool bWait, bool bShow);
115 /// returns the string representation of the hook type.
116 static CString GetHookTypeString(hooktype t);
117 /// returns the hooktype from a string representation of the same.
118 static hooktype GetHookType(const CString& s);
121 * Executes the Start-Update-Hook that first matches one of the paths in
122 * \c pathList.
123 * \param pathList a list of paths to look for the hook scripts
124 * \param exitcode on return, contains the exit code of the hook script
125 * \param error the data the hook script outputs to stderr
126 * \remark the string "%PATHS% in the command line of the hook script is
127 * replaced with the path to a temporary file which contains a list of files
128 * in \c pathList, separated by newlines. The hook script can parse this
129 * file to get all the paths the update is about to be done on.
131 bool StartUpdate(const CTGitPathList& pathList, DWORD& exitcode,
132 CString& error);
134 * Executes the Pre-Update-Hook that first matches one of the paths in
135 * \c pathList.
136 * \param pathList a list of paths to look for the hook scripts
137 * \param depth the depth of the commit
138 * \param rev the revision the update is done to
139 * \param exitcode on return, contains the exit code of the hook script
140 * \param error the data the hook script outputs to stderr
141 * \remark the string "%PATHS% in the command line of the hook script is
142 * replaced with the path to a temporary file which contains a list of files
143 * in \c pathList, separated by newlines. The hook script can parse this
144 * file to get all the paths the update is about to be done on.
145 * The string "%RECURSIVE%" is replaced with either "recursive" or "nonrecursive" according
146 * to the \c bRecursive parameter. And the string "%REVISION%" is replaced with
147 * the string representation of \c rev.
149 bool PreUpdate(const CTGitPathList& pathList, git_depth_t depth,
150 GitRev rev, DWORD& exitcode, CString& error);
152 * Executes the Post-Update-Hook that first matches one of the paths in
153 * \c pathList.
154 * \param pathList a list of paths to look for the hook scripts
155 * \param depth the depth of the commit
156 * \param rev the revision the update was done to
157 * \param exitcode on return, contains the exit code of the hook script
158 * \param error the data the hook script outputs to stderr
159 * \remark the string "%PATHS% in the command line of the hook script is
160 * replaced with the path to a temporary file which contains a list of files
161 * in \c pathList, separated by newlines. The hook script can parse this
162 * file to get all the paths the update is about to be done on.
163 * The string "%RECURSIVE%" is replaced with either "recursive" or "nonrecursive" according
164 * to the \c bRecursive parameter. And the string "%REVISION%" is replaced with
165 * the string representation of \c rev.
167 bool PostUpdate(const CTGitPathList& pathList, git_depth_t depth,
168 GitRev rev, DWORD& exitcode, CString& error);
171 * Executes the Start-Commit-Hook that first matches one of the paths in
172 * \c pathList.
173 * \param pathList a list of paths to look for the hook scripts
174 * \param message a commit message
175 * \param exitcode on return, contains the exit code of the hook script
176 * \param error the data the hook script outputs to stderr
177 * \remark the string "%PATHS% in the command line of the hook script is
178 * replaced with the path to a temporary file which contains a list of files
179 * in \c pathList, separated by newlines. The hook script can parse this
180 * file to get all the paths the commit is about to be done on.
181 * The string %MESSAGEFILE% is replaced with path to temporary file containing
182 * \c message. If the script finishes successfully, contents of this file
183 * is read back into \c message parameter.
185 bool StartCommit(const CTGitPathList& pathList, CString& message,
186 DWORD& exitcode, CString& error);
188 * Executes the Pre-Commit-Hook that first matches one of the paths in
189 * \c pathList.
190 * \param pathList a list of paths to look for the hook scripts
191 * \param depth the depth of the commit
192 * \param message the commit message
193 * \param exitcode on return, contains the exit code of the hook script
194 * \param error the data the hook script outputs to stderr
195 * \remark the string "%PATHS% in the command line of the hook script is
196 * replaced with the path to a temporary file which contains a list of files
197 * in \c pathList, separated by newlines. The hook script can parse this
198 * file to get all the paths the update is about to be done on.
199 * The string "%DEPTH%" is replaced with the numerical value (string) of the
200 * Git_depth_t parameter. See the Subversion source documentation about the
201 * values.
203 bool PreCommit(const CTGitPathList& pathList, git_depth_t depth,
204 const CString& message, DWORD& exitcode,
205 CString& error);
207 * Executes the Post-Commit-Hook that first matches one of the paths in
208 * \c pathList.
209 * \param pathList a list of paths to look for the hook scripts
210 * \param depth the depth of the commit
211 * \param message the commit message
212 * \param rev the revision the commit was done to
213 * \param exitcode on return, contains the exit code of the hook script
214 * \param error the data the hook script outputs to stderr
215 * \remark the string "%PATHS% in the command line of the hook script is
216 * replaced with the path to a temporary file which contains a list of files
217 * in \c pathList, separated by newlines. The hook script can parse this
218 * file to get all the paths the commit is about to be done on.
219 * The string "%DEPTH%" is replaced with the numerical value (string) of the
220 * Git_depth_t parameter. See the Subversion source documentation about the
221 * values.
223 bool PostCommit(const CTGitPathList& pathList, git_depth_t depth,
224 GitRev rev, const CString& message,
225 DWORD& exitcode, CString& error);
227 bool PrePush(const CTGitPathList& pathList,DWORD& exitcode, CString& error);
228 bool PostPush(const CTGitPathList& pathList,DWORD& exitcode, CString& error);
229 private:
231 * Starts a new process, specified in \c cmd.
232 * \param error the data the process writes to stderr
233 * \param bWait if true, then this method waits until the created process has finished. If false, then the return
234 * value will always be 0 and \c error will be an empty string.
235 * \param bShow set to true if the process should be started visible.
236 * \return the exit code of the process if \c bWait is true, zero otherwise.
238 DWORD RunScript(CString cmd, LPCTSTR currentDir, CString& error, bool bWait, bool bShow);
240 * Find the hook script information for the hook type \c t which matches a
241 * path in \c pathList.
243 hookiterator FindItem(hooktype t, const CTGitPathList& pathList);
244 static CHooks * m_pInstance;