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 <jaysoffian@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jay Soffian committed
Feb 14, 2011 at 20:07 UTC
65969d43d10512a44f5e7248e4404b946d1d0d9f
2 files changed
+41
-8
builtin/merge.c
+29
-8
@@ -795,6 +795,32 @@ static void add_strategies(const char *string, unsigned attr)
795
796
}
797
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
+
824
static int merge_trivial(void)
825
{
826
unsigned char result_tree[20], result_commit[20];
@@ -806,6 +832,7 @@ static int merge_trivial(void)
832
parent->next = xmalloc(sizeof(*parent->next));
833
parent->next->item = remoteheads->item;
834
parent->next->next = NULL;
835
+ run_prepare_commit_msg();
836
commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
837
finish(result_commit, "In-index merge");
838
drop_save();
@@ -835,6 +862,7 @@ static int finish_automerge(struct commit_list *common,
862
}
863
free_commit_list(remoteheads);
864
strbuf_addch(&merge_msg, '\n');
865
+ run_prepare_commit_msg();
866
commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
867
strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
868
finish(result_commit, buf.buf);
@@ -1316,14 +1344,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1344
die_errno("Could not write to '%s'", git_path("MERGE_HEAD"));
1345
close(fd);
1346
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();
1348
fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
1349
if (fd < 0)
1350
die_errno("Could not open '%s' for writing",
t/t7505-prepare-commit-msg-hook.sh
+12
@@ -132,6 +132,18 @@ test_expect_success 'with hook (-c)' '
132
133
'
134
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
+
147
cat > "$HOOK" <<'EOF'
148
#!/bin/sh
149
exit 1