1
// TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2019, 2021, 2023 - 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.
23 #include "ProjectProperties.h"
26 * \ingroup TortoiseProc
27 * enumeration of all client hook types
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.
49 HookType htype
= HookType::unknown_hook
;
53 bool operator < (const hookkey
& hk
) const
55 if (htype
== hk
.htype
)
57 if (local
!= hk
.local
)
58 return local
< hk
.local
;
60 return (path
< hk
.path
);
63 return htype
< hk
.htype
;
68 * \ingroup TortoiseProc
69 * helper struct, used as the value to the std::map we
70 * store the data for the client hook scripts in.
77 bool bEnabled
= false;
79 bool bApproved
= false; ///< user explicitly approved
80 bool bStored
= false; ///< use decision is stored in reg
84 using hookiterator
= std::map
<hookkey
, hookcmd
>::iterator
;
85 using const_hookiterator
= std::map
<hookkey
, hookcmd
>::const_iterator
;
88 * \ingroup TortoiseProc
89 * Singleton class which deals with the client hook scripts.
91 class CHooks
: private std::map
<hookkey
, hookcmd
>
97 CHooks(const CHooks
&) = delete;
98 CHooks
& operator=(const CHooks
&) = delete;
100 static void AddPathParam(CString
& sCmd
, const CTGitPathList
& pathList
);
101 static void AddCWDParam(CString
& sCmd
, const CString
& workingTree
);
102 static void AddErrorParam(CString
& sCmd
, const CString
& error
);
103 static void AddParam(CString
& sCmd
, const CString
& param
);
104 static CTGitPath
AddMessageFileParam(CString
& sCmd
, const CString
& message
);
106 /// Create the singleton. Call this at the start of the program.
107 static bool Create();
108 /// Returns the singleton instance
109 static CHooks
& Instance();
110 /// Destroys the singleton object. Call this at the end of the program.
111 static void Destroy();
113 using std::map
<hookkey
, hookcmd
>::begin
;
114 using std::map
<hookkey
, hookcmd
>::end
;
115 using std::map
<hookkey
, hookcmd
>::empty
;
118 /// Saves the hook script information to the registry.
121 * Removes the hook script identified by \c key. To make the change persistent
124 bool Remove(const hookkey
&key
);
126 * Adds a new hook script. To make the change persistent, call Save().
128 void Add(HookType ht
, const CTGitPath
& Path
, LPCWSTR szCmd
,
129 bool bWait
, bool bShow
, bool bEnabled
, bool bLocal
);
132 * Toggles the hook script identified by \c key. Returns whether the status has changed.
133 * To make the change persistent call Save().
135 bool SetEnabled(const hookkey
& key
, bool bEnabled
);
137 /// returns the string representation of the hook type.
138 static CString
GetHookTypeString(HookType t
);
139 /// returns the hooktype from a string representation of the same.
140 static HookType
GetHookType(const CString
& s
);
142 /// Add hook script data from project properties
143 void SetProjectProperties(const CTGitPath
& Path
, const ProjectProperties
& pp
);
146 * Executes the Start-Commit-Hook that first matches the path in
148 * \param workingTree working tree root directory
149 * \param pathList a list of paths to look for the hook scripts
150 * \param message a 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 commit is about to be done on.
157 * The string %MESSAGEFILE% is replaced with path to temporary file containing
158 * \c message. If the script finishes successfully, contents of this file
159 * is read back into \c message parameter.
161 bool StartCommit(HWND hWnd
, const CString
& workingTree
, const CTGitPathList
& pathList
, CString
& message
,
162 DWORD
& exitcode
, CString
& error
);
164 * Executes the Pre-Commit-Hook that first matches the path in
166 * \param workingTree working tree root directory
167 * \param pathList a list of paths to look for the hook scripts
168 * \param message the commit message
169 * \param exitcode on return, contains the exit code of the hook script
170 * \param error the data the hook script outputs to stderr
171 * \remark the string "%PATHS% in the command line of the hook script is
172 * replaced with the path to a temporary file which contains a list of files
173 * in \c pathList, separated by newlines. The hook script can parse this
174 * file to get all the paths the update is about to be done on.
175 * If the script finishes successfully, contents of this file is read back
176 * into \c message parameter.
178 bool PreCommit(HWND hWnd
, const CString
& workingTree
, const CTGitPathList
& pathList
,
179 CString
& message
, DWORD
& exitcode
,
182 * Executes the Post-Commit-Hook that first matches the path in
184 * \param workingTree working tree root directory
185 * \param amend commit was amend
187 bool PostCommit(HWND hWnd
, const CString
& workingTree
, bool amend
, DWORD
& exitcode
, CString
& error
);
189 bool PrePush(HWND hWnd
, const CString
& workingTree
, DWORD
& exitcode
, CString
& error
);
190 bool PostPush(HWND hWnd
, const CString
& workingTree
, DWORD
& exitcode
, CString
& error
);
192 bool PreRebase(HWND hWnd
, const CString
& workingTree
, const CString
& upstream
, const CString
& rebasedBranch
, DWORD
& exitcode
, CString
& error
);
194 bool IsHookPresent(HookType t
, const CString
& workingTree
);
198 * Starts a new process, specified in \c cmd.
199 * \param error the data the process writes to stderr
200 * \param bWait if true, then this method waits until the created process has finished. If false, then the return
201 * value will always be 0 and \c error will be an empty string.
202 * \param bShow set to true if the process should be started visible.
203 * \return the exit code of the process if \c bWait is true, zero otherwise.
205 static DWORD
RunScript(CString cmd
, LPCWSTR currentDir
, CString
& error
, bool bWait
, bool bShow
);
207 * Find the hook script information for the hook type \c t which matches the
208 * path in \c workingTree.
210 hookiterator
FindItem(HookType t
, const CString
& workingTree
);
212 static void ParseHookString(CString strhooks
, bool bLocal
);
215 * Checks whether the hook script has been validated already and
216 * if not, asks the user to validate it.
218 bool ApproveHook(HWND hWnd
, hookiterator it
, DWORD
& exitcode
);
220 static CHooks
* m_pInstance
;
221 static CTGitPath m_RootPath
;