[PATCH v7 5/5] history: re-edit a squash with every message
From: Harald Nordgren via GitGitGadget <hidden>
Date: 2026-07-06 08:51:07
Subsystem:
documentation, the rest · Maintainers:
Jonathan Corbet, Linus Torvalds
From: Harald Nordgren <redacted> By default "git history squash" reuses the oldest commit's message. When --reedit-message is given it only reopened that one message, so the messages of the folded-in commits were lost. Gather the messages of every commit in the range, oldest first, and build the same editor template that "git rebase -i" shows for a squash, using add_squash_combination_header(), add_squash_message_header() and squash_subject_comment_len(). Only the message text differs, the changes are always folded in. Following autosquash, a fixup!'s message is commented out in full under a "will be skipped" header, while a squash! or amend! keeps its body with only the marker subject commented. Signed-off-by: Harald Nordgren <redacted> --- Documentation/git-history.adoc | 12 +++- builtin/history.c | 73 ++++++++++++++++++++- t/t3455-history-squash.sh | 115 +++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc
index 783ddf4a51..f51332e731 100644
--- a/Documentation/git-history.adoc
+++ b/Documentation/git-history.adoc@@ -117,15 +117,21 @@ like the arguments to linkgit:git-rev-list[1], so several arguments may be given, for example `HEAD~3..HEAD ^topic` to additionally exclude what is already on `topic`. + -The oldest commit's message and authorship are preserved by default, -unless you specify `--reedit-message`. A merge commit inside the range is +The oldest commit's message and authorship are preserved by default. With +`--reedit-message`, an editor opens pre-filled with the messages of all the +folded commits so you can combine them. A merge commit inside the range is folded like any other, but the range must have a single base, so a range that reaches more than one entry point (for example a side branch that forked before the range and was later merged into it) is rejected. + Because the oldest commit's message is reused, the range may not begin with a `fixup!`, `squash!`, or `amend!` commit, whose target is -necessarily outside the range. +necessarily outside the range. The changes from every commit in the range +are always folded in. Only the message text differs. With +`--reedit-message` the template mirrors `git rebase -i`: the message of a +`fixup!` elsewhere in the range is commented out in full, while a +`squash!` or `amend!` keeps its message body with only the marker subject +commented, so you can fold the remark into the result. + A branch or tag that points at a commit inside the range would be left dangling once those commits are folded away, so with the default
diff --git a/builtin/history.c b/builtin/history.c
index 63911a493d..8999ba9533 100644
--- a/builtin/history.c
+++ b/builtin/history.c@@ -1114,6 +1114,68 @@ static int find_interior_ref(const struct reference *ref, void *cb_data) return 0; } +static int build_squash_message(struct repository *repo, + struct commit *base, + struct commit *tip, + struct strbuf *out) +{ + struct commit_list *commits = NULL, **tail = &commits, *c; + struct rev_info revs; + struct commit *commit; + struct strvec args = STRVEC_INIT; + int n = 0, total, ret; + + repo_init_revisions(repo, &revs, NULL); + strvec_push(&args, "ignored"); + strvec_push(&args, "--reverse"); + strvec_push(&args, "--topo-order"); + strvec_pushf(&args, "%s..%s", oid_to_hex(&base->object.oid), + oid_to_hex(&tip->object.oid)); + setup_revisions_from_strvec(&args, &revs, NULL); + + if (prepare_revision_walk(&revs) < 0) { + ret = error(_("error preparing revisions")); + goto out; + } + + while ((commit = get_revision(&revs))) + tail = &commit_list_insert(commit, tail)->next; + total = commit_list_count(commits); + + for (c = commits; c; c = c->next) { + const char *message, *body; + size_t commented_len; + int skip; + + message = repo_logmsg_reencode(repo, c->item, NULL, NULL); + find_commit_subject(message, &body); + + skip = starts_with(body, "fixup! "); + commented_len = skip ? strlen(body) : + squash_subject_comment_len(body, 1); + + if (!n) + add_squash_combination_header(out, total); + strbuf_addch(out, '\n'); + add_squash_message_header(out, ++n, skip); + strbuf_addstr(out, "\n\n"); + strbuf_add_commented_lines(out, body, commented_len, comment_line_str); + strbuf_addstr(out, body + commented_len); + strbuf_complete_line(out); + + repo_unuse_commit_buffer(repo, c->item, message); + } + + ret = 0; + +out: + commit_list_free(commits); + reset_revision_walk(); + release_revisions(&revs); + strvec_clear(&args); + return ret; +} + static int cmd_history_squash(int argc, const char **argv, const char *prefix,
@@ -1138,6 +1200,7 @@ static int cmd_history_squash(int argc, OPT_END(), }; struct strbuf reflog_msg = STRBUF_INIT; + struct strbuf message = STRBUF_INIT; struct oidset interior = OIDSET_INIT; struct commit *base, *oldest, *tip, *rewritten; const struct object_id *base_tree_oid, *tip_tree_oid;
@@ -1181,6 +1244,12 @@ static int cmd_history_squash(int argc, } } + if (flags & COMMIT_TREE_EDIT_MESSAGE) { + ret = build_squash_message(repo, base, tip, &message); + if (ret < 0) + goto out; + } + ret = setup_revwalk(repo, action, tip, &revs); if (ret < 0) goto out;
@@ -1189,7 +1258,8 @@ static int cmd_history_squash(int argc, tip_tree_oid = &repo_get_commit_tree(repo, tip)->object.oid; commit_list_append(base, &parents); - ret = commit_tree_ext(repo, "squash", oldest, NULL, parents, + ret = commit_tree_ext(repo, "squash", oldest, + message.len ? message.buf : NULL, parents, base_tree_oid, tip_tree_oid, &rewritten, flags); if (ret < 0) { ret = error(_("failed writing squashed commit"));
@@ -1210,6 +1280,7 @@ static int cmd_history_squash(int argc, out: strbuf_release(&reflog_msg); + strbuf_release(&message); oidset_clear(&interior); commit_list_free(parents); release_revisions(&revs);
diff --git a/t/t3455-history-squash.sh b/t/t3455-history-squash.sh
index 6598649971..1985c83fbb 100755
--- a/t/t3455-history-squash.sh
+++ b/t/t3455-history-squash.sh@@ -186,6 +186,121 @@ test_expect_success 'preserves authorship of the oldest commit' ' test_cmp expect actual ' +test_expect_success '--reedit-message offers every folded-in message' ' + git reset --hard start && + echo b >file && + git add file && + git commit -m "re-one subject" -m "re-one body line" && + test_commit --no-tag re-two file c && + test_commit re-three file d && + + write_script editor <<-\EOF && + cat "$1" >edited && + echo combined >"$1" + EOF + test_set_editor "$(pwd)/editor" && + git history squash --reedit-message start.. && + + cat >expect <<-EOF && + # This is a combination of 3 commits. + # This is the 1st commit message: + + re-one subject + + re-one body line + + # This is the commit message #2: + + re-two + + # This is the commit message #3: + + re-three + + # Please enter the commit message for the squash changes. Lines starting + # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit. + # Changes to be committed: + # modified: file + # + EOF + test_cmp expect edited && + echo combined >expect && + git log --format="%s" -1 >actual && + test_cmp expect actual +' + +test_expect_success '--reedit-message handles fixup!, squash! and amend! like rebase' ' + git reset --hard start && + test_commit --no-tag mark-base file b && + printf "fixup! mark-base\n\nfixup body\n" >msg && + echo c >file && + git add file && + git commit -qF msg && + printf "squash! mark-base\n\nsquash remark\n" >msg && + echo d >file && + git add file && + git commit -qF msg && + printf "amend! mark-base\n\namended message\n" >msg && + echo e >file && + git add file && + git commit -qF msg && + + write_script editor <<-\EOF && + cat "$1" >edited + EOF + test_set_editor "$(pwd)/editor" && + git history squash --reedit-message start.. && + + cat >expect <<-EOF && + # This is a combination of 4 commits. + # This is the 1st commit message: + + mark-base + + # The commit message #2 will be skipped: + + # fixup! mark-base + # + # fixup body + + # This is the commit message #3: + + # squash! mark-base + + squash remark + + # This is the commit message #4: + + # amend! mark-base + + amended message + + # Please enter the commit message for the squash changes. Lines starting + # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit. + # Changes to be committed: + # modified: file + # + EOF + test_cmp expect edited && + git log -1 --format="%B" >final && + test_grep ! "fixup body" final && + test_grep "squash remark" final && + test_grep "amended message" final +' + +test_expect_success '--reedit-message aborts on an empty message' ' + git reset --hard three && + head_before=$(git rev-parse HEAD) && + + write_script editor <<-\EOF && + >"$1" + EOF + test_set_editor "$(pwd)/editor" && + test_must_fail git history squash --reedit-message start.. && + + test_cmp_rev "$head_before" HEAD +' + test_expect_success '--update-refs=head only moves HEAD' ' git reset --hard three && git branch -f other HEAD &&
--
gitgitgadget