Skip to content

Commit 11e4b4f

Browse files
committed
Merge branch 'js/maint-merge-use-prepare-commit-msg-hook'
* js/maint-merge-use-prepare-commit-msg-hook: merge: honor prepare-commit-msg hook
2 parents fc7ae9c + 65969d4 commit 11e4b4f

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
@@ -797,6 +797,32 @@ static void add_strategies(const char *string, unsigned attr)
797797

798798
}
799799

800+
static void write_merge_msg(void)
801+
{
802+
int fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
803+
if (fd < 0)
804+
die_errno("Could not open '%s' for writing",
805+
git_path("MERGE_MSG"));
806+
if (write_in_full(fd, merge_msg.buf, merge_msg.len) != merge_msg.len)
807+
die_errno("Could not write to '%s'", git_path("MERGE_MSG"));
808+
close(fd);
809+
}
810+
811+
static void read_merge_msg(void)
812+
{
813+
strbuf_reset(&merge_msg);
814+
if (strbuf_read_file(&merge_msg, git_path("MERGE_MSG"), 0) < 0)
815+
die_errno("Could not read from '%s'", git_path("MERGE_MSG"));
816+
}
817+
818+
static void run_prepare_commit_msg(void)
819+
{
820+
write_merge_msg();
821+
run_hook(get_index_file(), "prepare-commit-msg",
822+
git_path("MERGE_MSG"), "merge", NULL, NULL);
823+
read_merge_msg();
824+
}
825+
800826
static int merge_trivial(void)
801827
{
802828
unsigned char result_tree[20], result_commit[20];
@@ -808,6 +834,7 @@ static int merge_trivial(void)
808834
parent->next = xmalloc(sizeof(*parent->next));
809835
parent->next->item = remoteheads->item;
810836
parent->next->next = NULL;
837+
run_prepare_commit_msg();
811838
commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
812839
finish(result_commit, "In-index merge");
813840
drop_save();
@@ -837,6 +864,7 @@ static int finish_automerge(struct commit_list *common,
837864
}
838865
free_commit_list(remoteheads);
839866
strbuf_addch(&merge_msg, '\n');
867+
run_prepare_commit_msg();
840868
commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
841869
strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
842870
finish(result_commit, buf.buf);
@@ -1318,14 +1346,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13181346
die_errno("Could not write to '%s'", git_path("MERGE_HEAD"));
13191347
close(fd);
13201348
strbuf_addch(&merge_msg, '\n');
1321-
fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
1322-
if (fd < 0)
1323-
die_errno("Could not open '%s' for writing",
1324-
git_path("MERGE_MSG"));
1325-
if (write_in_full(fd, merge_msg.buf, merge_msg.len) !=
1326-
merge_msg.len)
1327-
die_errno("Could not write to '%s'", git_path("MERGE_MSG"));
1328-
close(fd);
1349+
write_merge_msg();
13291350
fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
13301351
if (fd < 0)
13311352
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)