Refactor out ParseHookString
[TortoiseGit.git] / src / Utils / Hooks.h
blob6829eaa6cae4520ac4a753403f65e5b40242db2a
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2018 - 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 "registry.h"
22 #include "TGitPath.h"
24 /**
25 * \ingroup TortoiseProc
26 * enumeration of all client hook types
28 typedef enum hooktype
30 unknown_hook,
31 start_commit_hook,
32 pre_commit_hook,
33 post_commit_hook,
34 issue_tracker_hook,
35 pre_push_hook,
36 post_push_hook,
37 pre_rebase_hook,
38 } hooktype;
40 /**
41 * \ingroup TortoiseProc
42 * helper class, used as the key to the std::map we store
43 * the data for the client hook scripts in.
45 class hookkey
47 public:
48 hooktype htype;
49 CTGitPath path;
51 bool operator < (const hookkey& hk) const
53 if (htype == hk.htype)
54 return (path < hk.path);
55 else
56 return htype < hk.htype;
60 /**
61 * \ingroup TortoiseProc
62 * helper struct, used as the value to the std::map we
63 * store the data for the client hook scripts in.
65 typedef struct hookcmd
67 CString commandline;
68 bool bWait;
69 bool bShow;
70 bool bEnabled;
71 } hookcmd;
73 typedef std::map<hookkey, hookcmd>::iterator hookiterator;
74 typedef std::map<hookkey, hookcmd>::const_iterator const_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 // prevent cloning
86 CHooks(const CHooks&) = delete;
87 CHooks& operator=(const CHooks&) = delete;
89 static void AddPathParam(CString& sCmd, const CTGitPathList& pathList);
90 static void AddCWDParam(CString& sCmd, const CString& workingTree);
91 static void AddErrorParam(CString& sCmd, const CString& error);
92 static void AddParam(CString& sCmd, const CString& param);
93 static CTGitPath AddMessageFileParam(CString& sCmd, const CString& message);
94 public:
95 /// Create the singleton. Call this at the start of the program.
96 static bool Create();
97 /// Returns the singleton instance
98 static CHooks& Instance();
99 /// Destroys the singleton object. Call this at the end of the program.
100 static void Destroy();
102 public:
103 /// Saves the hook script information to the registry.
104 bool Save();
106 * Removes the hook script identified by \c key. To make the change persistent
107 * call Save().
109 bool Remove(const hookkey &key);
111 * Adds a new hook script. To make the change persistent, call Save().
113 void Add(hooktype ht, const CTGitPath& Path, LPCTSTR szCmd,
114 bool bWait, bool bShow, bool bEnabled);
117 * Toggles the hook script identified by \c key. Returns whether the status has changed.
118 * To make the change persistent call Save().
120 bool SetEnabled(const hookkey& key, bool bEnabled);
122 /// returns the string representation of the hook type.
123 static CString GetHookTypeString(hooktype t);
124 /// returns the hooktype from a string representation of the same.
125 static hooktype GetHookType(const CString& s);
128 * Executes the Start-Commit-Hook that first matches the path in
129 * \c workingTree.
130 * \param workingTree working tree root directory
131 * \param pathList a list of paths to look for the hook scripts
132 * \param message a commit message
133 * \param exitcode on return, contains the exit code of the hook script
134 * \param error the data the hook script outputs to stderr
135 * \remark the string "%PATHS% in the command line of the hook script is
136 * replaced with the path to a temporary file which contains a list of files
137 * in \c pathList, separated by newlines. The hook script can parse this
138 * file to get all the paths the commit is about to be done on.
139 * The string %MESSAGEFILE% is replaced with path to temporary file containing
140 * \c message. If the script finishes successfully, contents of this file
141 * is read back into \c message parameter.
143 bool StartCommit(const CString& workingTree, const CTGitPathList& pathList, CString& message,
144 DWORD& exitcode, CString& error);
146 * Executes the Pre-Commit-Hook that first matches the path in
147 * \c workingTree.
148 * \param workingTree working tree root directory
149 * \param pathList a list of paths to look for the hook scripts
150 * \param message the commit message
151 * \param exitcode on return, contains the exit code of the hook script
152 * \param error the data the hook script outputs to stderr
153 * \remark the string "%PATHS% in the command line of the hook script is
154 * replaced with the path to a temporary file which contains a list of files
155 * in \c pathList, separated by newlines. The hook script can parse this
156 * file to get all the paths the update is about to be done on.
157 * If the script finishes successfully, contents of this file is read back
158 * into \c message parameter.
160 bool PreCommit(const CString& workingTree, const CTGitPathList& pathList,
161 CString& message, DWORD& exitcode,
162 CString& error);
164 * Executes the Post-Commit-Hook that first matches the path in
165 * \c workingTree.
166 * \param workingTree working tree root directory
167 * \param amend commit was amend
169 bool PostCommit(const CString& workingTree, bool amend, DWORD& exitcode, CString& error);
171 bool PrePush(const CString& workingTree, DWORD& exitcode, CString& error);
172 bool PostPush(const CString& workingTree, DWORD& exitcode, CString& error);
174 bool PreRebase(const CString& workingTree, const CString& upstream, const CString& rebasedBranch, DWORD& exitcode, CString& error);
176 bool IsHookPresent(hooktype t, const CString& workingTree) const;
178 private:
180 * Starts a new process, specified in \c cmd.
181 * \param error the data the process writes to stderr
182 * \param bWait if true, then this method waits until the created process has finished. If false, then the return
183 * value will always be 0 and \c error will be an empty string.
184 * \param bShow set to true if the process should be started visible.
185 * \return the exit code of the process if \c bWait is true, zero otherwise.
187 static DWORD RunScript(CString cmd, LPCTSTR currentDir, CString& error, bool bWait, bool bShow);
189 * Find the hook script information for the hook type \c t which matches the
190 * path in \c workingTree.
192 const_hookiterator FindItem(hooktype t, const CString& workingTree) const;
194 static void ParseHookString(CString strhooks);
196 static CHooks * m_pInstance;