Skip to content

Commit

Permalink
fix(squash): preserve consensus author
Browse files Browse the repository at this point in the history
When all of the patches to be squashed have the same author, use that
author for the squashed commit.

If the patches to squash have heterogeneous authors, default to the
configured user for the repo. (This was the behavior for all squashes.)

The command line flags (--author, --authname, --authemail) still take
precedence.

Fixes: #490
  • Loading branch information
jpgrayson committed Sep 15, 2024
1 parent b691c04 commit 229b779
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/cmd/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,17 @@ fn try_squash(
) -> Result<Option<(PatchName, gix::ObjectId)>> {
let repo = trans.repo();
let base_commit = trans.get_patch_commit(&patchnames[0]);
let base_author = base_commit.author()?;
let mut use_base_author = true;
let base_commit_ref = base_commit.decode()?;
if let Some(tree_id) = repo.stupid().with_temp_index(|stupid_temp| {
stupid_temp.read_tree(base_commit_ref.tree())?;
for commit in patchnames[1..].iter().map(|pn| trans.get_patch_commit(pn)) {
let commit_ref = commit.decode()?;
let author = commit.author()?;
if author != base_author {
use_base_author = false;
}
let parent = commit.get_parent_commit()?;
let parent_commit_ref = parent.decode()?;
if parent_commit_ref.tree() != commit_ref.tree()
Expand Down Expand Up @@ -259,7 +265,14 @@ fn try_squash(
.allow_template_save(false)
.template_patchname(patchname)
.extra_allowed_patchnames(patchnames)
.default_author(repo.get_author()?.override_author(matches))
.default_author(
if use_base_author {
base_author
} else {
repo.get_author()?
}
.override_author(matches),
)
.default_message(prepare_message(trans, patchnames)?)
.edit(trans, repo, matches)?
{
Expand Down
48 changes: 48 additions & 0 deletions t/t2600-squash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,54 @@ test_expect_success 'Squash at stack top' '
[ "$(echo $(stg series --unapplied --noprefix))" = "" ]
'

test_expect_success 'Squash patches with all non-default author' '
echo "a" >>baz.txt &&
stg new -rm "a-patch" --author "Other Contributor <another@example.com>" &&
echo "b" >>baz.txt &&
stg new -rm "b-patch" --author "Other Contributor <another@example.com>" &&
echo "c" >>baz.txt &&
stg new -rm "c-patch" --author "Other Contributor <another@example.com>" &&
stg squash -m "abc-patch" a-patch b-patch c-patch &&
test_when_finished "stg delete abc-patch" &&
stg show abc-patch | grep "Author:" >out &&
cat >expected <<-\EOF &&
Author: Other Contributor <another@example.com>
EOF
test_cmp expected out
'

test_expect_success 'Squash patches with some non-default author' '
echo "a" >>baz.txt &&
stg new -rm "a-patch" &&
echo "b" >>baz.txt &&
stg new -rm "b-patch" --author "Other Contributor <another@example.com>" &&
echo "c" >>baz.txt &&
stg new -rm "c-patch" &&
stg squash -m "abc-patch" a-patch b-patch c-patch &&
test_when_finished "stg delete abc-patch" &&
stg show abc-patch | grep "Author:" >out &&
cat >expected <<-\EOF &&
Author: A Ú Thor <author@example.com>
EOF
test_cmp expected out
'

test_expect_success 'Squash patches with author override' '
echo "a" >>baz.txt &&
stg new -rm "a-patch" --author "Other Contributor <another@example.com>" &&
echo "b" >>baz.txt &&
stg new -rm "b-patch" --author "Other Contributor <another@example.com>" &&
echo "c" >>baz.txt &&
stg new -rm "c-patch" --author "Other Contributor <another@example.com>" &&
stg squash -m "abc-patch" --author "Override Author <override@example.com>" a-patch b-patch c-patch &&
test_when_finished "stg delete abc-patch" &&
stg show abc-patch | grep "Author:" >out &&
cat >expected <<-\EOF &&
Author: Override Author <override@example.com>
EOF
test_cmp expected out
'

test_expect_success 'Empty commit message aborts the squash' '
write_script fake-editor <<-\EOF &&
echo "" >"$1"
Expand Down

0 comments on commit 229b779

Please sign in to comment.