Skip to content

Commit ea7fefb

Browse files
committed
Merge branch 'ss/commit-squash-msg' into maint
When "git merge --squash" stopped due to conflict, the concluding "git commit" failed to read in the SQUASH_MSG that shows the log messages from all the squashed commits. * ss/commit-squash-msg: commit: do not lose SQUASH_MSG contents
2 parents 8cad7fc + b64c1e0 commit ea7fefb

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

builtin/commit.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,18 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
726726
&sb, &ctx);
727727
hook_arg1 = "message";
728728
} else if (!stat(git_path_merge_msg(), &statbuf)) {
729+
/*
730+
* prepend SQUASH_MSG here if it exists and a
731+
* "merge --squash" was originally performed
732+
*/
733+
if (!stat(git_path_squash_msg(), &statbuf)) {
734+
if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0)
735+
die_errno(_("could not read SQUASH_MSG"));
736+
hook_arg1 = "squash";
737+
} else
738+
hook_arg1 = "merge";
729739
if (strbuf_read_file(&sb, git_path_merge_msg(), 0) < 0)
730740
die_errno(_("could not read MERGE_MSG"));
731-
hook_arg1 = "merge";
732741
} else if (!stat(git_path_squash_msg(), &statbuf)) {
733742
if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0)
734743
die_errno(_("could not read SQUASH_MSG"));

t/t7600-merge.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ printf '%s\n' 1 2 3 4 5 6 7 8 9 >file
3333
printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >file.1
3434
printf '%s\n' 1 2 3 4 '5 X' 6 7 8 9 >file.5
3535
printf '%s\n' 1 2 3 4 5 6 7 8 '9 X' >file.9
36+
printf '%s\n' 1 2 3 4 5 6 7 8 '9 Y' >file.9y
3637
printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >result.1
3738
printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
3839
printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
40+
printf '%s\n' 1 2 3 4 5 6 7 8 '9 Z' >result.9z
3941
>empty
4042

4143
create_merge_msgs () {
@@ -128,6 +130,12 @@ test_expect_success 'setup' '
128130
git tag c2 &&
129131
c2=$(git rev-parse HEAD) &&
130132
git reset --hard "$c0" &&
133+
cp file.9y file &&
134+
git add file &&
135+
test_tick &&
136+
git commit -m "commit 7" &&
137+
git tag c7 &&
138+
git reset --hard "$c0" &&
131139
cp file.9 file &&
132140
git add file &&
133141
test_tick &&
@@ -218,6 +226,26 @@ test_expect_success 'merge c1 with c2' '
218226
verify_parents $c1 $c2
219227
'
220228

229+
test_expect_success 'merge --squash c3 with c7' '
230+
git reset --hard c3 &&
231+
test_must_fail git merge --squash c7 &&
232+
cat result.9z >file &&
233+
git commit --no-edit -a &&
234+
235+
{
236+
cat <<-EOF
237+
Squashed commit of the following:
238+
239+
$(git show -s c7)
240+
241+
# Conflicts:
242+
# file
243+
EOF
244+
} >expect &&
245+
git cat-file commit HEAD | sed -e '1,/^$/d' >actual &&
246+
test_cmp expect actual
247+
'
248+
221249
test_debug 'git log --graph --decorate --oneline --all'
222250

223251
test_expect_success 'merge c1 with c2 and c3' '

0 commit comments

Comments
 (0)