Show GUI friendly diffstat after pull
[TortoiseGit.git] / src / Utils / Hooks.h
blobb72fd829e13b03b62af94de051ea3552204191e5
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 } hooktype;
42 /**
43 * \ingroup TortoiseProc
44 * helper class, used as the key to the std::map we store
45 * the data for the client hook scripts in.
47 class hookkey
49 public:
50 hooktype htype;
51 CTGitPath path;
53 bool operator < (const hookkey& hk) const
55 if (htype == hk.htype)
56 return (path < hk.path);
57 else
58 return htype < hk.htype;
62 /**
63 * \ingroup TortoiseProc
64 * helper struct, used as the value to the std::map we
65 * store the data for the client hook scripts in.
67 typedef struct hookcmd
69 CString commandline;
70 bool bWait;
71 bool bShow;
72 } hookcmd;
74 typedef std::map<hookkey, hookcmd>::iterator hookiterator;
76 /**
77 * \ingroup TortoiseProc
78 * Singleton class which deals with the client hook scripts.
80 class CHooks : public std::map<hookkey, hookcmd>
82 private:
83 CHooks();
84 ~CHooks();
85 void AddPathParam(CString& sCmd, const CTGitPathList& pathList);
86 void AddDepthParam(CString& sCmd, git_depth_t depth);
87 void AddCWDParam(CString& sCmd, const CTGitPathList& pathList);
88 void AddErrorParam(CString& sCmd, const CString& error);
89 void AddParam(CString& sCmd, const CString& param);
90 CTGitPath AddMessageFileParam(CString& sCmd, const CString& message);
91 public:
92 /// Create the singleton. Call this at the start of the program.
93 static bool Create();
94 /// Returns the singleton instance
95 static CHooks& Instance();
96 /// Destroys the singleton object. Call this at the end of the program.
97 static void Destroy();
99 public:
100 /// Saves the hook script information to the registry.
101 bool Save();
103 * Removes the hook script identified by \c key. To make the change persistent
104 * call Save().
106 bool Remove(hookkey key);
108 * Adds a new hook script. To make the change persistent, call Save().
110 void Add(hooktype ht, const CTGitPath& Path, LPCTSTR szCmd,
111 bool bWait, bool bShow);
113 /// returns the string representation of the hook type.
114 static CString GetHookTypeString(hooktype t);
115 /// returns the hooktype from a string representation of the same.
116 static hooktype GetHookType(const CString& s);
119 * Executes the Start-Update-Hook that first matches one of the paths in
120 * \c pathList.
121 * \param pathList a list of paths to look for the hook scripts
122 * \param exitcode on return, contains the exit code of the hook script
123 * \param error the data the hook script outputs to stderr
124 * \remark the string "%PATHS% in the command line of the hook script is
125 * replaced with the path to a temporary file which contains a list of files
126 * in \c pathList, separated by newlines. The hook script can parse this
127 * file to get all the paths the update is about to be done on.
129 bool StartUpdate(const CTGitPathList& pathList, DWORD& exitcode,
130 CString& error);
132 * Executes the Pre-Update-Hook that first matches one of the paths in
133 * \c pathList.
134 * \param pathList a list of paths to look for the hook scripts
135 * \param depth the depth of the commit
136 * \param rev the revision the update is done to
137 * \param exitcode on return, contains the exit code of the hook script
138 * \param error the data the hook script outputs to stderr
139 * \remark the string "%PATHS% in the command line of the hook script is
140 * replaced with the path to a temporary file which contains a list of files
141 * in \c pathList, separated by newlines. The hook script can parse this
142 * file to get all the paths the update is about to be done on.
143 * The string "%RECURSIVE%" is replaced with either "recursive" or "nonrecursive" according
144 * to the \c bRecursive parameter. And the string "%REVISION%" is replaced with
145 * the string representation of \c rev.
147 bool PreUpdate(const CTGitPathList& pathList, git_depth_t depth,
148 GitRev rev, DWORD& exitcode, CString& error);
150 * Executes the Post-Update-Hook that first matches one of the paths in
151 * \c pathList.
152 * \param pathList a list of paths to look for the hook scripts
153 * \param depth the depth of the commit
154 * \param rev the revision the update was done to
155 * \param exitcode on return, contains the exit code of the hook script
156 * \param error the data the hook script outputs to stderr
157 * \remark the string "%PATHS% in the command line of the hook script is
158 * replaced with the path to a temporary file which contains a list of files
159 * in \c pathList, separated by newlines. The hook script can parse this
160 * file to get all the paths the update is about to be done on.
161 * The string "%RECURSIVE%" is replaced with either "recursive" or "nonrecursive" according
162 * to the \c bRecursive parameter. And the string "%REVISION%" is replaced with
163 * the string representation of \c rev.
165 bool PostUpdate(const CTGitPathList& pathList, git_depth_t depth,
166 GitRev rev, DWORD& exitcode, CString& error);
169 * Executes the Start-Commit-Hook that first matches one of the paths in
170 * \c pathList.
171 * \param pathList a list of paths to look for the hook scripts
172 * \param message a commit message
173 * \param exitcode on return, contains the exit code of the hook script
174 * \param error the data the hook script outputs to stderr
175 * \remark the string "%PATHS% in the command line of the hook script is
176 * replaced with the path to a temporary file which contains a list of files
177 * in \c pathList, separated by newlines. The hook script can parse this
178 * file to get all the paths the commit is about to be done on.
179 * The string %MESSAGEFILE% is replaced with path to temporary file containing
180 * \c message. If the script finishes successfully, contents of this file
181 * is read back into \c message parameter.
183 bool StartCommit(const CTGitPathList& pathList, CString& message,
184 DWORD& exitcode, CString& error);
186 * Executes the Pre-Commit-Hook that first matches one of the paths in
187 * \c pathList.
188 * \param pathList a list of paths to look for the hook scripts
189 * \param depth the depth of the commit
190 * \param message the commit message
191 * \param exitcode on return, contains the exit code of the hook script
192 * \param error the data the hook script outputs to stderr
193 * \remark the string "%PATHS% in the command line of the hook script is
194 * replaced with the path to a temporary file which contains a list of files
195 * in \c pathList, separated by newlines. The hook script can parse this
196 * file to get all the paths the update is about to be done on.
197 * The string "%DEPTH%" is replaced with the numerical value (string) of the
198 * Git_depth_t parameter. See the Subversion source documentation about the
199 * values.
201 bool PreCommit(const CTGitPathList& pathList, git_depth_t depth,
202 const CString& message, DWORD& exitcode,
203 CString& error);
205 * Executes the Post-Commit-Hook that first matches one of the paths in
206 * \c pathList.
207 * \param pathList a list of paths to look for the hook scripts
208 * \param depth the depth of the commit
209 * \param message the commit message
210 * \param rev the revision the commit was done to
211 * \param exitcode on return, contains the exit code of the hook script
212 * \param error the data the hook script outputs to stderr
213 * \remark the string "%PATHS% in the command line of the hook script is
214 * replaced with the path to a temporary file which contains a list of files
215 * in \c pathList, separated by newlines. The hook script can parse this
216 * file to get all the paths the commit is about to be done on.
217 * The string "%DEPTH%" is replaced with the numerical value (string) of the
218 * Git_depth_t parameter. See the Subversion source documentation about the
219 * values.
221 bool PostCommit(const CTGitPathList& pathList, git_depth_t depth,
222 GitRev rev, const CString& message,
223 DWORD& exitcode, CString& error);
225 private:
227 * Starts a new process, specified in \c cmd.
228 * \param error the data the process writes to stderr
229 * \param bWait if true, then this method waits until the created process has finished. If false, then the return
230 * value will always be 0 and \c error will be an empty string.
231 * \param bShow set to true if the process should be started visible.
232 * \return the exit code of the process if \c bWait is true, zero otherwise.
234 DWORD RunScript(CString cmd, LPCTSTR currentDir, CString& error, bool bWait, bool bShow);
236 * Find the hook script information for the hook type \c t which matches a
237 * path in \c pathList.
239 hookiterator FindItem(hooktype t, const CTGitPathList& pathList);
240 static CHooks * m_pInstance;