Skip to content

Commit 65969d4

Browse files
jaysoffiangitster
authored andcommitted
merge: honor prepare-commit-msg hook
When a merge is stopped due to conflicts or --no-commit, the subsequent commit calls the prepare-commit-msg hook. However, it is not called after a clean merge. Fix this inconsistency by invoking the hook after clean merges as well. Signed-off-by: Jay Soffian <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ed863a commit 65969d4

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

builtin/merge.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,32 @@ static void add_strategies(const char *string, unsigned attr)
795795

796796
}
797797

798+
static void write_merge_msg(void)
799+
{
800+
int fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
801+
if (fd < 0)
802+
die_errno("Could not open '%s' for writing",
803+
git_path("MERGE_MSG"));
804+
if (write_in_full(fd, merge_msg.buf, merge_msg.len) != merge_msg.len)
805+
die_errno("Could not write to '%s'", git_path("MERGE_MSG"));
806+
close(fd);
807+
}
808+
809+
static void read_merge_msg(void)
810+
{
811+
strbuf_reset(&merge_msg);
812+
if (strbuf_read_file(&merge_msg, git_path("MERGE_MSG"), 0) < 0)
813+
die_errno("Could not read from '%s'", git_path("MERGE_MSG"));
814+
}
815+
816+
static void run_prepare_commit_msg(void)
817+
{
818+
write_merge_msg();
819+
run_hook(get_index_file(), "prepare-commit-msg",
820+
git_path("MERGE_MSG"), "merge", NULL, NULL);
821+
read_merge_msg();
822+
}
823+
798824
static int merge_trivial(void)
799825
{
800826
unsigned char result_tree[20], result_commit[20];
@@ -806,6 +832,7 @@ static int merge_trivial(void)
806832
parent->next = xmalloc(sizeof(*parent->next));
807833
parent->next->item = remoteheads->item;
808834
parent->next->next = NULL;
835+
run_prepare_commit_msg();
809836
commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
810837
finish(result_commit, "In-index merge");
811838
drop_save();
@@ -835,6 +862,7 @@ static int finish_automerge(struct commit_list *common,
835862
}
836863
free_commit_list(remoteheads);
837864
strbuf_addch(&merge_msg, '\n');
865+
run_prepare_commit_msg();
838866
commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
839867
strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
840868
finish(result_commit, buf.buf);
@@ -1316,14 +1344,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13161344
die_errno("Could not write to '%s'", git_path("MERGE_HEAD"));
13171345
close(fd);
13181346
strbuf_addch(&merge_msg, '\n');
1319-
fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
1320-
if (fd < 0)
1321-
die_errno("Could not open '%s' for writing",
1322-
git_path("MERGE_MSG"));
1323-
if (write_in_full(fd, merge_msg.buf, merge_msg.len) !=
1324-
merge_msg.len)
1325-
die_errno("Could not write to '%s'", git_path("MERGE_MSG"));
1326-
close(fd);
1347+
write_merge_msg();
13271348
fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
13281349
if (fd < 0)
13291350
die_errno("Could not open '%s' for writing",

t/t7505-prepare-commit-msg-hook.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ test_expect_success 'with hook (-c)' '
132132
133133
'
134134

135+
test_expect_success 'with hook (merge)' '
136+
137+
head=`git rev-parse HEAD` &&
138+
git checkout -b other HEAD@{1} &&
139+
echo "more" >> file &&
140+
git add file &&
141+
git commit -m other &&
142+
git checkout - &&
143+
git merge other &&
144+
test "`git log -1 --pretty=format:%s`" = merge
145+
'
146+
135147
cat > "$HOOK" <<'EOF'
136148
#!/bin/sh
137149
exit 1

0 commit comments

Comments
 (0)