Extend the hook scripts with the ability to use information from the project properti...
[TortoiseGit.git] / src / Utils / Hooks.h
blobead0d536332f3cb162b6fb2855ee24f7fb06aa81
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"
23 #include "ProjectProperties.h"
25 /**
26 * \ingroup TortoiseProc
27 * enumeration of all client hook types
29 typedef enum hooktype
31 unknown_hook,
32 start_commit_hook,
33 pre_commit_hook,
34 post_commit_hook,
35 issue_tracker_hook,
36 pre_push_hook,
37 post_push_hook,
38 pre_rebase_hook,
39 } hooktype;
41 /**
42 * \ingroup TortoiseProc
43 * helper class, used as the key to the std::map we store
44 * the data for the client hook scripts in.
46 class hookkey
48 public:
49 hooktype htype;
50 CTGitPath path;
52 bool operator < (const hookkey& hk) const
54 if (htype == hk.htype)
55 return (path < hk.path);
56 else
57 return htype < hk.htype;
61 /**
62 * \ingroup TortoiseProc
63 * helper struct, used as the value to the std::map we
64 * store the data for the client hook scripts in.
66 typedef struct hookcmd
68 CString commandline;
69 bool bWait;
70 bool bShow;
71 bool bEnabled;
72 bool bLocal;
73 } hookcmd;
75 typedef std::map<hookkey, hookcmd>::iterator hookiterator;
76 typedef std::map<hookkey, hookcmd>::const_iterator const_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 // prevent cloning
88 CHooks(const CHooks&) = delete;
89 CHooks& operator=(const CHooks&) = delete;
91 static void AddPathParam(CString& sCmd, const CTGitPathList& pathList);
92 static void AddCWDParam(CString& sCmd, const CString& workingTree);
93 static void AddErrorParam(CString& sCmd, const CString& error);
94 static void AddParam(CString& sCmd, const CString& param);
95 static CTGitPath AddMessageFileParam(CString& sCmd, const CString& message);
96 public:
97 /// Create the singleton. Call this at the start of the program.
98 static bool Create();
99 /// Returns the singleton instance
100 static CHooks& Instance();
101 /// Destroys the singleton object. Call this at the end of the program.
102 static void Destroy();
104 public:
105 /// Saves the hook script information to the registry.
106 bool Save();
108 * Removes the hook script identified by \c key. To make the change persistent
109 * call Save().
111 bool Remove(const hookkey &key);
113 * Adds a new hook script. To make the change persistent, call Save().
115 void Add(hooktype ht, const CTGitPath& Path, LPCTSTR szCmd,
116 bool bWait, bool bShow, bool bEnabled, bool bLocal);
119 * Toggles the hook script identified by \c key. Returns whether the status has changed.
120 * To make the change persistent call Save().
122 bool SetEnabled(const hookkey& key, bool bEnabled);
124 /// returns the string representation of the hook type.
125 static CString GetHookTypeString(hooktype t);
126 /// returns the hooktype from a string representation of the same.
127 static hooktype GetHookType(const CString& s);
129 /// Add hook script data from project properties
130 void SetProjectProperties(const CTGitPath& Path, const ProjectProperties& pp);
133 * Executes the Start-Commit-Hook that first matches the path in
134 * \c workingTree.
135 * \param workingTree working tree root directory
136 * \param pathList a list of paths to look for the hook scripts
137 * \param message a commit message
138 * \param exitcode on return, contains the exit code of the hook script
139 * \param error the data the hook script outputs to stderr
140 * \remark the string "%PATHS% in the command line of the hook script is
141 * replaced with the path to a temporary file which contains a list of files
142 * in \c pathList, separated by newlines. The hook script can parse this
143 * file to get all the paths the commit is about to be done on.
144 * The string %MESSAGEFILE% is replaced with path to temporary file containing
145 * \c message. If the script finishes successfully, contents of this file
146 * is read back into \c message parameter.
148 bool StartCommit(const CString& workingTree, const CTGitPathList& pathList, CString& message,
149 DWORD& exitcode, CString& error);
151 * Executes the Pre-Commit-Hook that first matches the path in
152 * \c workingTree.
153 * \param workingTree working tree root directory
154 * \param pathList a list of paths to look for the hook scripts
155 * \param message the commit message
156 * \param exitcode on return, contains the exit code of the hook script
157 * \param error the data the hook script outputs to stderr
158 * \remark the string "%PATHS% in the command line of the hook script is
159 * replaced with the path to a temporary file which contains a list of files
160 * in \c pathList, separated by newlines. The hook script can parse this
161 * file to get all the paths the update is about to be done on.
162 * If the script finishes successfully, contents of this file is read back
163 * into \c message parameter.
165 bool PreCommit(const CString& workingTree, const CTGitPathList& pathList,
166 CString& message, DWORD& exitcode,
167 CString& error);
169 * Executes the Post-Commit-Hook that first matches the path in
170 * \c workingTree.
171 * \param workingTree working tree root directory
172 * \param amend commit was amend
174 bool PostCommit(const CString& workingTree, bool amend, 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 PreRebase(const CString& workingTree, const CString& upstream, const CString& rebasedBranch, DWORD& exitcode, CString& error);
181 bool IsHookPresent(hooktype t, const CString& workingTree) const;
183 private:
185 * Starts a new process, specified in \c cmd.
186 * \param error the data the process writes to stderr
187 * \param bWait if true, then this method waits until the created process has finished. If false, then the return
188 * value will always be 0 and \c error will be an empty string.
189 * \param bShow set to true if the process should be started visible.
190 * \return the exit code of the process if \c bWait is true, zero otherwise.
192 static DWORD RunScript(CString cmd, LPCTSTR currentDir, CString& error, bool bWait, bool bShow);
194 * Find the hook script information for the hook type \c t which matches the
195 * path in \c workingTree.
197 const_hookiterator FindItem(hooktype t, const CString& workingTree) const;
199 static void ParseHookString(CString strhooks, bool bLocal);
201 static CHooks * m_pInstance;
202 static CTGitPath m_RootPath;