Skip to content

Commit 87020cb

Browse files
nasamuffingitster
authored andcommitted
commit: use config-based hooks
As part of the adoption of config-based hooks, teach run_commit_hook() to call hook.h instead of run-command.h. This covers 'pre-commit', 'commit-msg', and 'prepare-commit-msg'. Additionally, ask the hook library - not run-command - whether any hooks will be run, as it's possible hooks may exist in the config but not the hookdir. Signed-off-by: Emily Shaffer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b1c2b59 commit 87020cb

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "help.h"
3737
#include "commit-reach.h"
3838
#include "commit-graph.h"
39+
#include "hook.h"
3940

4041
static const char * const builtin_commit_usage[] = {
4142
N_("git commit [<options>] [--] <pathspec>..."),
@@ -983,7 +984,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
983984
return 0;
984985
}
985986

986-
if (!no_verify && find_hook("pre-commit")) {
987+
if (!no_verify && hook_exists("pre-commit")) {
987988
/*
988989
* Re-read the index as pre-commit hook could have updated it,
989990
* and write it out as a tree. We must do this before we invoke

builtin/merge.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "commit-reach.h"
4242
#include "wt-status.h"
4343
#include "commit-graph.h"
44+
#include "hook.h"
4445

4546
#define DEFAULT_TWOHEAD (1<<0)
4647
#define DEFAULT_OCTOPUS (1<<1)
@@ -829,7 +830,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
829830
* and write it out as a tree. We must do this before we invoke
830831
* the editor and after we invoke run_status above.
831832
*/
832-
if (find_hook("pre-merge-commit"))
833+
if (hook_exists("pre-merge-commit"))
833834
discard_cache();
834835
read_cache_from(index_file);
835836
strbuf_addbuf(&msg, &merge_msg);

commit.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "commit-reach.h"
2222
#include "run-command.h"
2323
#include "shallow.h"
24+
#include "hook.h"
2425

2526
static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
2627

@@ -1635,8 +1636,13 @@ int run_commit_hook(int editor_is_used, const char *index_file,
16351636
{
16361637
struct strvec hook_env = STRVEC_INIT;
16371638
va_list args;
1639+
const char *arg;
1640+
struct strvec hook_args = STRVEC_INIT;
1641+
struct strbuf hook_name = STRBUF_INIT;
16381642
int ret;
16391643

1644+
strbuf_addstr(&hook_name, name);
1645+
16401646
strvec_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
16411647

16421648
/*
@@ -1646,9 +1652,14 @@ int run_commit_hook(int editor_is_used, const char *index_file,
16461652
strvec_push(&hook_env, "GIT_EDITOR=:");
16471653

16481654
va_start(args, name);
1649-
ret = run_hook_ve(hook_env.v, name, args);
1655+
while ((arg = va_arg(args, const char *)))
1656+
strvec_push(&hook_args, arg);
16501657
va_end(args);
1658+
1659+
ret = run_hooks(hook_env.v, &hook_name, &hook_args);
16511660
strvec_clear(&hook_env);
1661+
strvec_clear(&hook_args);
1662+
strbuf_release(&hook_name);
16521663

16531664
return ret;
16541665
}

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ test_expect_success 'with succeeding hook' '
103103
test_cmp expected_hooks actual_hooks
104104
'
105105

106+
# NEEDSWORK: when 'git hook add' and 'git hook remove' have been added, use that
107+
# instead
108+
test_expect_success 'with succeeding hook (config-based)' '
109+
test_when_finished "git config --unset hook.pre-commit.command success.sample" &&
110+
test_when_finished "rm -f expected_hooks actual_hooks" &&
111+
git config hook.pre-commit.command "$HOOKDIR/success.sample" &&
112+
echo "$HOOKDIR/success.sample" >expected_hooks &&
113+
echo "more" >>file &&
114+
git add file &&
115+
git commit -m "more" &&
116+
test_cmp expected_hooks actual_hooks
117+
'
118+
106119
test_expect_success 'with succeeding hook (merge)' '
107120
test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
108121
cp "$HOOKDIR/success.sample" "$PREMERGE" &&

0 commit comments

Comments
 (0)