Skip to content

Commit 108f92e

Browse files
nasamuffingitster
authored andcommitted
run_commit_hook: take strvec instead of varargs
Taking varargs in run_commit_hook() led to some bizarre patterns, like callers using two string variables (which may or may not be filled) to express different argument lists for the commit hooks. Because run_commit_hook() no longer needs to call a variadic function for the hook run itself, we can use strvec to make the calling code more conventional. Signed-off-by: Emily Shaffer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 87020cb commit 108f92e

File tree

5 files changed

+52
-47
lines changed

5 files changed

+52
-47
lines changed

builtin/commit.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
691691
struct strbuf committer_ident = STRBUF_INIT;
692692
int committable;
693693
struct strbuf sb = STRBUF_INIT;
694-
const char *hook_arg1 = NULL;
695-
const char *hook_arg2 = NULL;
694+
struct strvec hook_args = STRVEC_INIT;
696695
int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
697696
int old_display_comment_prefix;
698697
int merge_contains_scissors = 0;
699698

700699
/* This checks and barfs if author is badly specified */
701700
determine_author_info(author_ident);
702701

703-
if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
702+
if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit",
703+
&hook_args))
704704
return 0;
705705

706706
if (squash_message) {
@@ -722,27 +722,28 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
722722
}
723723
}
724724

725+
strvec_push(&hook_args, git_path_commit_editmsg());
726+
725727
if (have_option_m && !fixup_message) {
726728
strbuf_addbuf(&sb, &message);
727-
hook_arg1 = "message";
729+
strvec_push(&hook_args, "message");
728730
} else if (logfile && !strcmp(logfile, "-")) {
729731
if (isatty(0))
730732
fprintf(stderr, _("(reading log message from standard input)\n"));
731733
if (strbuf_read(&sb, 0, 0) < 0)
732734
die_errno(_("could not read log from standard input"));
733-
hook_arg1 = "message";
735+
strvec_push(&hook_args, "message");
734736
} else if (logfile) {
735737
if (strbuf_read_file(&sb, logfile, 0) < 0)
736738
die_errno(_("could not read log file '%s'"),
737739
logfile);
738-
hook_arg1 = "message";
740+
strvec_push(&hook_args, "message");
739741
} else if (use_message) {
740742
char *buffer;
741743
buffer = strstr(use_message_buffer, "\n\n");
742744
if (buffer)
743745
strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
744-
hook_arg1 = "commit";
745-
hook_arg2 = use_message;
746+
strvec_pushl(&hook_args, "commit", use_message, NULL);
746747
} else if (fixup_message) {
747748
struct pretty_print_context ctx = {0};
748749
struct commit *commit;
@@ -754,7 +755,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
754755
&sb, &ctx);
755756
if (have_option_m)
756757
strbuf_addbuf(&sb, &message);
757-
hook_arg1 = "message";
758+
strvec_push(&hook_args, "message");
758759
} else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
759760
size_t merge_msg_start;
760761

@@ -765,9 +766,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
765766
if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
766767
if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
767768
die_errno(_("could not read SQUASH_MSG"));
768-
hook_arg1 = "squash";
769+
strvec_push(&hook_args, "squash");
769770
} else
770-
hook_arg1 = "merge";
771+
strvec_push(&hook_args, "merge");
771772

772773
merge_msg_start = sb.len;
773774
if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
@@ -781,11 +782,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
781782
} else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
782783
if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
783784
die_errno(_("could not read SQUASH_MSG"));
784-
hook_arg1 = "squash";
785+
strvec_push(&hook_args, "squash");
785786
} else if (template_file) {
786787
if (strbuf_read_file(&sb, template_file, 0) < 0)
787788
die_errno(_("could not read '%s'"), template_file);
788-
hook_arg1 = "template";
789+
strvec_push(&hook_args, "template");
789790
clean_message_contents = 0;
790791
}
791792

@@ -794,20 +795,18 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
794795
* just set the argument(s) to the prepare-commit-msg hook.
795796
*/
796797
else if (whence == FROM_MERGE)
797-
hook_arg1 = "merge";
798-
else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
799-
hook_arg1 = "commit";
800-
hook_arg2 = "CHERRY_PICK_HEAD";
801-
}
798+
strvec_push(&hook_args, "merge");
799+
else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK)
800+
strvec_pushl(&hook_args, "commit", "CHERRY_PICK_HEAD", NULL);
802801

803802
if (squash_message) {
804803
/*
805804
* If squash_commit was used for the commit subject,
806805
* then we're possibly hijacking other commit log options.
807806
* Reset the hook args to tell the real story.
808807
*/
809-
hook_arg1 = "message";
810-
hook_arg2 = "";
808+
strvec_clear(&hook_args);
809+
strvec_pushl(&hook_args, git_path_commit_editmsg(), "message", NULL);
811810
}
812811

813812
s->fp = fopen_for_writing(git_path_commit_editmsg());
@@ -999,8 +998,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
999998
return 0;
1000999
}
10011000

1002-
if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
1003-
git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
1001+
if (run_commit_hook(use_editor, index_file, "prepare-commit-msg", &hook_args))
10041002
return 0;
10051003

10061004
if (use_editor) {
@@ -1015,8 +1013,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10151013
strvec_clear(&env);
10161014
}
10171015

1016+
strvec_clear(&hook_args);
1017+
strvec_push(&hook_args, git_path_commit_editmsg());
10181018
if (!no_verify &&
1019-
run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) {
1019+
run_commit_hook(use_editor, index_file, "commit-msg", &hook_args)) {
10201020
return 0;
10211021
}
10221022

builtin/merge.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,17 +821,22 @@ static void write_merge_heads(struct commit_list *);
821821
static void prepare_to_commit(struct commit_list *remoteheads)
822822
{
823823
struct strbuf msg = STRBUF_INIT;
824+
struct strvec hook_args = STRVEC_INIT;
825+
struct strbuf hook_name = STRBUF_INIT;
824826
const char *index_file = get_index_file();
825827

826-
if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
828+
if (!no_verify && run_commit_hook(0 < option_edit, index_file,
829+
"pre-merge-commit", &hook_args))
827830
abort_commit(remoteheads, NULL);
831+
828832
/*
829833
* Re-read the index as pre-merge-commit hook could have updated it,
830834
* and write it out as a tree. We must do this before we invoke
831835
* the editor and after we invoke run_status above.
832836
*/
833837
if (hook_exists("pre-merge-commit"))
834838
discard_cache();
839+
835840
read_cache_from(index_file);
836841
strbuf_addbuf(&msg, &merge_msg);
837842
if (squash)
@@ -851,17 +856,22 @@ static void prepare_to_commit(struct commit_list *remoteheads)
851856
append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
852857
write_merge_heads(remoteheads);
853858
write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
859+
860+
strvec_clear(&hook_args);
861+
strvec_pushl(&hook_args, git_path_merge_msg(the_repository), "merge", NULL);
854862
if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
855-
git_path_merge_msg(the_repository), "merge", NULL))
863+
&hook_args))
856864
abort_commit(remoteheads, NULL);
865+
857866
if (0 < option_edit) {
858867
if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
859868
abort_commit(remoteheads, NULL);
860869
}
861870

871+
strvec_clear(&hook_args);
872+
strvec_push(&hook_args, git_path_merge_msg(the_repository));
862873
if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
863-
"commit-msg",
864-
git_path_merge_msg(the_repository), NULL))
874+
"commit-msg", &hook_args))
865875
abort_commit(remoteheads, NULL);
866876

867877
read_merge_msg(&msg);
@@ -871,6 +881,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
871881
strbuf_release(&merge_msg);
872882
strbuf_addbuf(&merge_msg, &msg);
873883
strbuf_release(&msg);
884+
strbuf_release(&hook_name);
885+
strvec_clear(&hook_args);
874886
}
875887

876888
static int merge_trivial(struct commit *head, struct commit_list *remoteheads)

commit.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,12 +1632,9 @@ size_t ignore_non_trailer(const char *buf, size_t len)
16321632
}
16331633

16341634
int run_commit_hook(int editor_is_used, const char *index_file,
1635-
const char *name, ...)
1635+
const char *name, struct strvec *args)
16361636
{
16371637
struct strvec hook_env = STRVEC_INIT;
1638-
va_list args;
1639-
const char *arg;
1640-
struct strvec hook_args = STRVEC_INIT;
16411638
struct strbuf hook_name = STRBUF_INIT;
16421639
int ret;
16431640

@@ -1651,14 +1648,8 @@ int run_commit_hook(int editor_is_used, const char *index_file,
16511648
if (!editor_is_used)
16521649
strvec_push(&hook_env, "GIT_EDITOR=:");
16531650

1654-
va_start(args, name);
1655-
while ((arg = va_arg(args, const char *)))
1656-
strvec_push(&hook_args, arg);
1657-
va_end(args);
1658-
1659-
ret = run_hooks(hook_env.v, &hook_name, &hook_args);
1651+
ret = run_hooks(hook_env.v, &hook_name, args);
16601652
strvec_clear(&hook_env);
1661-
strvec_clear(&hook_args);
16621653
strbuf_release(&hook_name);
16631654

16641655
return ret;

commit.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "string-list.h"
1010
#include "pretty.h"
1111
#include "commit-slab.h"
12+
#include "strvec.h"
1213

1314
#define COMMIT_NOT_FROM_GRAPH 0xFFFFFFFF
1415
#define GENERATION_NUMBER_INFINITY 0xFFFFFFFF
@@ -352,7 +353,7 @@ void verify_merge_signature(struct commit *commit, int verbose,
352353
int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
353354
int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused);
354355

355-
LAST_ARG_MUST_BE_NULL
356-
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
356+
int run_commit_hook(int editor_is_used, const char *index_file,
357+
const char *name, struct strvec *args);
357358

358359
#endif /* COMMIT_H */

sequencer.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,22 +1153,23 @@ static int run_prepare_commit_msg_hook(struct repository *r,
11531153
const char *commit)
11541154
{
11551155
int ret = 0;
1156-
const char *name, *arg1 = NULL, *arg2 = NULL;
1156+
struct strvec args = STRVEC_INIT;
1157+
const char *name = git_path_commit_editmsg();
11571158

1158-
name = git_path_commit_editmsg();
1159+
strvec_push(&args, name);
11591160
if (write_message(msg->buf, msg->len, name, 0))
11601161
return -1;
11611162

11621163
if (commit) {
1163-
arg1 = "commit";
1164-
arg2 = commit;
1164+
strvec_push(&args, "commit");
1165+
strvec_push(&args, commit);
11651166
} else {
1166-
arg1 = "message";
1167+
strvec_push(&args, "message");
11671168
}
1168-
if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
1169-
arg1, arg2, NULL))
1169+
if (run_commit_hook(0, r->index_file, "prepare-commit-msg", &args))
11701170
ret = error(_("'prepare-commit-msg' hook failed"));
11711171

1172+
strvec_clear(&args);
11721173
return ret;
11731174
}
11741175

0 commit comments

Comments
 (0)