Right align column contents
[TortoiseGit.git] / src / Utils / Hooks.h
blobeb05ced27ea18364cc2156b710e541c46f43c685
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2015 - TortoiseGit
4 // Copyright (C) 2006-2008, 2015 - 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 #pragma once
21 #include <map>
22 #include "registry.h"
23 #include "TGitPath.h"
24 #include "GitRev.h"
25 #include "GitStatus.h"
27 /**
28 * \ingroup TortoiseProc
29 * enumeration of all client hook types
31 typedef enum hooktype
33 unknown_hook,
34 start_commit_hook,
35 pre_commit_hook,
36 post_commit_hook,
37 issue_tracker_hook,
38 pre_push_hook,
39 post_push_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;
75 typedef std::map<hookkey, hookcmd>::const_iterator const_hookiterator;
77 /**
78 * \ingroup TortoiseProc
79 * Singleton class which deals with the client hook scripts.
81 class CHooks : public std::map<hookkey, hookcmd>
83 private:
84 CHooks();
85 ~CHooks();
86 // prevent cloning
87 CHooks(const CHooks&) = delete;
88 CHooks& operator=(const CHooks&) = delete;
90 static void AddPathParam(CString& sCmd, const CTGitPathList& pathList);
91 static void AddCWDParam(CString& sCmd, const CString& workingTree);
92 static void AddErrorParam(CString& sCmd, const CString& error);
93 static void AddParam(CString& sCmd, const CString& param);
94 static CTGitPath AddMessageFileParam(CString& sCmd, const CString& message);
95 public:
96 /// Create the singleton. Call this at the start of the program.
97 static bool Create();
98 /// Returns the singleton instance
99 static CHooks& Instance();
100 /// Destroys the singleton object. Call this at the end of the program.
101 static void Destroy();
103 public:
104 /// Saves the hook script information to the registry.
105 bool Save();
107 * Removes the hook script identified by \c key. To make the change persistent
108 * call Save().
110 bool Remove(const hookkey &key);
112 * Adds a new hook script. To make the change persistent, call Save().
114 void Add(hooktype ht, const CTGitPath& Path, LPCTSTR szCmd,
115 bool bWait, bool bShow);
117 /// returns the string representation of the hook type.
118 static CString GetHookTypeString(hooktype t);
119 /// returns the hooktype from a string representation of the same.
120 static hooktype GetHookType(const CString& s);
123 * Executes the Start-Commit-Hook that first matches the path in
124 * \c workingTree.
125 * \param workingTree working tree root directory
126 * \param pathList a list of paths to look for the hook scripts
127 * \param message a commit message
128 * \param exitcode on return, contains the exit code of the hook script
129 * \param error the data the hook script outputs to stderr
130 * \remark the string "%PATHS% in the command line of the hook script is
131 * replaced with the path to a temporary file which contains a list of files
132 * in \c pathList, separated by newlines. The hook script can parse this
133 * file to get all the paths the commit is about to be done on.
134 * The string %MESSAGEFILE% is replaced with path to temporary file containing
135 * \c message. If the script finishes successfully, contents of this file
136 * is read back into \c message parameter.
138 bool StartCommit(const CString& workingTree, const CTGitPathList& pathList, CString& message,
139 DWORD& exitcode, CString& error);
141 * Executes the Pre-Commit-Hook that first matches the path in
142 * \c workingTree.
143 * \param workingTree working tree root directory
144 * \param pathList a list of paths to look for the hook scripts
145 * \param message the commit message
146 * \param exitcode on return, contains the exit code of the hook script
147 * \param error the data the hook script outputs to stderr
148 * \remark the string "%PATHS% in the command line of the hook script is
149 * replaced with the path to a temporary file which contains a list of files
150 * in \c pathList, separated by newlines. The hook script can parse this
151 * file to get all the paths the update is about to be done on.
152 * If the script finishes successfully, contents of this file is read back
153 * into \c message parameter.
155 bool PreCommit(const CString& workingTree, const CTGitPathList& pathList,
156 CString& message, DWORD& exitcode,
157 CString& error);
159 * Executes the Post-Commit-Hook that first matches the path in
160 * \c workingTree.
161 * \param workingTree working tree root directory
162 * \param pathList a list of paths to look for the hook scripts
163 * \param message the commit message
164 * \param rev the revision the commit was done to
165 * \param exitcode on return, contains the exit code of the hook script
166 * \param error the data the hook script outputs to stderr
167 * \remark the string "%PATHS% in the command line of the hook script is
168 * replaced with the path to a temporary file which contains a list of files
169 * in \c pathList, separated by newlines. The hook script can parse this
170 * file to get all the paths the commit is about to be done on.
172 bool PostCommit(const CString& workingTree, const CTGitPathList& pathList,
173 const GitRev& rev, const CString& message,
174 DWORD& exitcode, CString& error);
176 bool PrePush(const CString& workingTree, DWORD& exitcode, CString& error);
177 bool PostPush(const CString& workingTree, DWORD& exitcode, CString& error);
179 bool IsHookPresent(hooktype t, const CString& workingTree) const;
181 private:
183 * Starts a new process, specified in \c cmd.
184 * \param error the data the process writes to stderr
185 * \param bWait if true, then this method waits until the created process has finished. If false, then the return
186 * value will always be 0 and \c error will be an empty string.
187 * \param bShow set to true if the process should be started visible.
188 * \return the exit code of the process if \c bWait is true, zero otherwise.
190 static DWORD RunScript(CString cmd, LPCTSTR currentDir, CString& error, bool bWait, bool bShow);
192 * Find the hook script information for the hook type \c t which matches the
193 * path in \c workingTree.
195 const_hookiterator FindItem(hooktype t, const CString& workingTree) const;
196 static CHooks * m_pInstance;