Skip to content

Commit 3eb2bd0

Browse files
committed
hook: extract the internal hook replacement logic
Before making the post-index-change logic more advanced for the postCommand.strategy=post-index-change feature, extract the code into a new method that will be easier to update in the future. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent e7d349d commit 3eb2bd0

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

hook.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ static int post_index_change_sentinel_exists(struct repository *r)
233233
return res;
234234
}
235235

236+
/**
237+
* See if we can replace the requested hook with an internal behavior.
238+
* Returns 0 if the real hook should run. Returns nonzero if we instead
239+
* executed custom internal behavior and the value to return is set to
240+
* 'result'.
241+
*/
242+
static int handle_hook_replacement(struct repository *r,
243+
const char *hook_name,
244+
int *result)
245+
{
246+
const char *strval;
247+
if (repo_config_get_string_tmp(r, "postcommand.strategy", &strval) ||
248+
strcasecmp(strval, "post-index-change"))
249+
return 0;
250+
251+
if (!strcmp(hook_name, "post-index-change")) {
252+
*result = write_post_index_change_sentinel(r);
253+
return 1;
254+
}
255+
if (!strcmp(hook_name, "post-command") &&
256+
!post_index_change_sentinel_exists(r)) {
257+
*result = 0;
258+
return 1;
259+
}
260+
261+
return 0;
262+
}
263+
236264
int run_hooks_opt(struct repository *r, const char *hook_name,
237265
struct run_hooks_opt *options)
238266
{
@@ -260,15 +288,9 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
260288

261289
/* Interject hook behavior depending on strategy. */
262290
if (r && r->gitdir) {
263-
const char *strval;
264-
if (!repo_config_get_string_tmp(r, "postcommand.strategy", &strval) &&
265-
!strcasecmp(strval, "post-index-change")) {
266-
if (!strcmp(hook_name, "post-index-change"))
267-
return write_post_index_change_sentinel(r);
268-
if (!strcmp(hook_name, "post-command") &&
269-
!post_index_change_sentinel_exists(r))
270-
return 0;
271-
}
291+
int result = 0;
292+
if (handle_hook_replacement(r, hook_name, &result))
293+
return result;
272294
}
273295

274296
hook_path = find_hook(r, hook_name);

0 commit comments

Comments
 (0)