Skip to content

Commit 229b779

Browse files
committed
fix(squash): preserve consensus author
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
1 parent b691c04 commit 229b779

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/cmd/squash.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,17 @@ fn try_squash(
222222
) -> Result<Option<(PatchName, gix::ObjectId)>> {
223223
let repo = trans.repo();
224224
let base_commit = trans.get_patch_commit(&patchnames[0]);
225+
let base_author = base_commit.author()?;
226+
let mut use_base_author = true;
225227
let base_commit_ref = base_commit.decode()?;
226228
if let Some(tree_id) = repo.stupid().with_temp_index(|stupid_temp| {
227229
stupid_temp.read_tree(base_commit_ref.tree())?;
228230
for commit in patchnames[1..].iter().map(|pn| trans.get_patch_commit(pn)) {
229231
let commit_ref = commit.decode()?;
232+
let author = commit.author()?;
233+
if author != base_author {
234+
use_base_author = false;
235+
}
230236
let parent = commit.get_parent_commit()?;
231237
let parent_commit_ref = parent.decode()?;
232238
if parent_commit_ref.tree() != commit_ref.tree()
@@ -259,7 +265,14 @@ fn try_squash(
259265
.allow_template_save(false)
260266
.template_patchname(patchname)
261267
.extra_allowed_patchnames(patchnames)
262-
.default_author(repo.get_author()?.override_author(matches))
268+
.default_author(
269+
if use_base_author {
270+
base_author
271+
} else {
272+
repo.get_author()?
273+
}
274+
.override_author(matches),
275+
)
263276
.default_message(prepare_message(trans, patchnames)?)
264277
.edit(trans, repo, matches)?
265278
{

t/t2600-squash.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,54 @@ test_expect_success 'Squash at stack top' '
7171
[ "$(echo $(stg series --unapplied --noprefix))" = "" ]
7272
'
7373

74+
test_expect_success 'Squash patches with all non-default author' '
75+
echo "a" >>baz.txt &&
76+
stg new -rm "a-patch" --author "Other Contributor <[email protected]>" &&
77+
echo "b" >>baz.txt &&
78+
stg new -rm "b-patch" --author "Other Contributor <[email protected]>" &&
79+
echo "c" >>baz.txt &&
80+
stg new -rm "c-patch" --author "Other Contributor <[email protected]>" &&
81+
stg squash -m "abc-patch" a-patch b-patch c-patch &&
82+
test_when_finished "stg delete abc-patch" &&
83+
stg show abc-patch | grep "Author:" >out &&
84+
cat >expected <<-\EOF &&
85+
Author: Other Contributor <[email protected]>
86+
EOF
87+
test_cmp expected out
88+
'
89+
90+
test_expect_success 'Squash patches with some non-default author' '
91+
echo "a" >>baz.txt &&
92+
stg new -rm "a-patch" &&
93+
echo "b" >>baz.txt &&
94+
stg new -rm "b-patch" --author "Other Contributor <[email protected]>" &&
95+
echo "c" >>baz.txt &&
96+
stg new -rm "c-patch" &&
97+
stg squash -m "abc-patch" a-patch b-patch c-patch &&
98+
test_when_finished "stg delete abc-patch" &&
99+
stg show abc-patch | grep "Author:" >out &&
100+
cat >expected <<-\EOF &&
101+
Author: A Ú Thor <[email protected]>
102+
EOF
103+
test_cmp expected out
104+
'
105+
106+
test_expect_success 'Squash patches with author override' '
107+
echo "a" >>baz.txt &&
108+
stg new -rm "a-patch" --author "Other Contributor <[email protected]>" &&
109+
echo "b" >>baz.txt &&
110+
stg new -rm "b-patch" --author "Other Contributor <[email protected]>" &&
111+
echo "c" >>baz.txt &&
112+
stg new -rm "c-patch" --author "Other Contributor <[email protected]>" &&
113+
stg squash -m "abc-patch" --author "Override Author <[email protected]>" a-patch b-patch c-patch &&
114+
test_when_finished "stg delete abc-patch" &&
115+
stg show abc-patch | grep "Author:" >out &&
116+
cat >expected <<-\EOF &&
117+
Author: Override Author <[email protected]>
118+
EOF
119+
test_cmp expected out
120+
'
121+
74122
test_expect_success 'Empty commit message aborts the squash' '
75123
write_script fake-editor <<-\EOF &&
76124
echo "" >"$1"

0 commit comments

Comments
 (0)