diff --git a/src/cmd/squash.rs b/src/cmd/squash.rs index 3ea63e27..001ad82d 100644 --- a/src/cmd/squash.rs +++ b/src/cmd/squash.rs @@ -222,11 +222,17 @@ fn try_squash( ) -> Result> { 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() @@ -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)? { diff --git a/t/t2600-squash.sh b/t/t2600-squash.sh index 8d7749b1..848bea33 100755 --- a/t/t2600-squash.sh +++ b/t/t2600-squash.sh @@ -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 " && + echo "b" >>baz.txt && + stg new -rm "b-patch" --author "Other Contributor " && + echo "c" >>baz.txt && + stg new -rm "c-patch" --author "Other Contributor " && + 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 + 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 " && + 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 + 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 " && + echo "b" >>baz.txt && + stg new -rm "b-patch" --author "Other Contributor " && + echo "c" >>baz.txt && + stg new -rm "c-patch" --author "Other Contributor " && + stg squash -m "abc-patch" --author "Override Author " 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 + EOF + test_cmp expected out +' + test_expect_success 'Empty commit message aborts the squash' ' write_script fake-editor <<-\EOF && echo "" >"$1"