Skip to content

Commit 34ccdc3

Browse files
Split author overwriting into it's own filter (#1096)
Change: author-filter
1 parent 207d79a commit 34ccdc3

File tree

6 files changed

+49
-38
lines changed

6 files changed

+49
-38
lines changed

josh-proxy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ fn split_changes(
403403
&vec![&parent],
404404
&new_tree,
405405
None,
406+
None,
406407
false,
407408
)?;
408409
changes[i].1 = new_commit;

src/bin/josh-filter.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,7 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {
194194
refs.push((reference.name().unwrap().to_string(), target));
195195
}
196196
}
197-
filterobj = josh::filter::chain(
198-
josh::filter::squash(Some((
199-
args.get_one::<String>("author").unwrap(),
200-
args.get_one::<String>("email").unwrap(),
201-
&ids,
202-
))),
203-
filterobj,
204-
);
197+
filterobj = josh::filter::chain(josh::filter::squash(Some(&ids)), filterobj);
205198
};
206199

207200
let odb = repo.odb()?;

src/filter/mod.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ pub fn empty() -> Filter {
6161
to_filter(Op::Empty)
6262
}
6363

64-
pub fn squash(ids: Option<(&str, &str, &[(git2::Oid, String)])>) -> Filter {
65-
if let Some((author, email, ids)) = ids {
64+
pub fn squash(ids: Option<&[(git2::Oid, String)]>) -> Filter {
65+
if let Some(ids) = ids {
6666
to_filter(Op::Squash(Some(
67-
ids.iter()
68-
.map(|(x, y)| (*x, (y.clone(), author.to_string(), email.to_string())))
69-
.collect(),
67+
ids.iter().map(|(x, y)| (*x, y.clone())).collect(),
7068
)))
7169
} else {
7270
to_filter(Op::Squash(None))
@@ -97,7 +95,8 @@ enum Op {
9795
Empty,
9896
Fold,
9997
Paths,
100-
Squash(Option<std::collections::HashMap<git2::Oid, (String, String, String)>>),
98+
Squash(Option<std::collections::HashMap<git2::Oid, String>>),
99+
Author(String, String),
101100
Rev(std::collections::HashMap<git2::Oid, Filter>),
102101
Linear,
103102
Unsign,
@@ -283,7 +282,7 @@ fn spec2(op: &Op) -> String {
283282
Op::Squash(Some(hs)) => {
284283
let mut v = hs
285284
.iter()
286-
.map(|(x, y)| format!("{}:{}:{}:{}", x, y.0, y.1, y.2))
285+
.map(|(x, y)| format!("{}:{}", x, y))
287286
.collect::<Vec<String>>();
288287
v.sort();
289288
let s = v.join(",");
@@ -297,6 +296,9 @@ fn spec2(op: &Op) -> String {
297296
Op::File(path) => format!("::{}", parse::quote_if(&path.to_string_lossy())),
298297
Op::Prefix(path) => format!(":prefix={}", parse::quote_if(&path.to_string_lossy())),
299298
Op::Glob(pattern) => format!("::{}", parse::quote_if(pattern)),
299+
Op::Author(author, email) => {
300+
format!(":author={};{}", parse::quote(author), parse::quote(email))
301+
}
300302
}
301303
}
302304

@@ -403,6 +405,7 @@ fn apply_to_commit2(
403405
&[],
404406
&commit.tree()?,
405407
None,
408+
None,
406409
true,
407410
))
408411
.transpose()
@@ -481,6 +484,7 @@ fn apply_to_commit2(
481484
transaction,
482485
filter,
483486
None,
487+
None,
484488
))
485489
.transpose();
486490
}
@@ -502,7 +506,6 @@ fn apply_to_commit2(
502506
commit.tree()?,
503507
transaction,
504508
filter,
505-
None,
506509
))
507510
.transpose();
508511
}
@@ -586,6 +589,7 @@ fn apply_to_commit2(
586589
transaction,
587590
filter,
588591
None,
592+
None,
589593
))
590594
.transpose();
591595
}
@@ -662,6 +666,11 @@ fn apply_to_commit2(
662666

663667
let filtered_parent_ids = some_or!(filtered_parent_ids, { return Ok(None) });
664668

669+
let author = match to_op(filter) {
670+
Op::Author(author, email) => Some((author.clone(), email.clone())),
671+
_ => None,
672+
};
673+
665674
let message = match to_op(filter) {
666675
Op::Squash(Some(ids)) => ids.get(&commit.id()).map(|x| x.clone()),
667676
_ => None,
@@ -673,6 +682,7 @@ fn apply_to_commit2(
673682
filtered_tree,
674683
transaction,
675684
filter,
685+
author,
676686
message,
677687
))
678688
.transpose()
@@ -698,6 +708,7 @@ fn apply2<'a>(
698708
Op::Empty => return Ok(tree::empty(repo)),
699709
Op::Fold => Ok(tree),
700710
Op::Squash(None) => Ok(tree),
711+
Op::Author(_, _) => Ok(tree),
701712
Op::Squash(Some(_)) => Err(josh_error("not applicable to tree")),
702713
Op::Linear => Ok(tree),
703714
Op::Unsign => Ok(tree),

src/filter/parse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn make_op(args: &[&str]) -> JoshResult<Op> {
77
["nop"] => Ok(Op::Nop),
88
["empty"] => Ok(Op::Empty),
99
["prefix", arg] => Ok(Op::Prefix(Path::new(arg).to_owned())),
10+
["author", author, email] => Ok(Op::Author(author.to_string(), email.to_string())),
1011
["workspace", arg] => Ok(Op::Workspace(Path::new(arg).to_owned())),
1112
["prefix"] => Err(josh_error(indoc!(
1213
r#"

src/history.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,20 @@ pub fn rewrite_commit(
181181
base: &git2::Commit,
182182
parents: &[&git2::Commit],
183183
tree: &git2::Tree,
184-
message: Option<(String, String, String)>,
184+
author: Option<(String, String)>,
185+
message: Option<String>,
185186
unsign: bool,
186187
) -> JoshResult<git2::Oid> {
187-
let b = if let Some((message, author, email)) = message {
188+
let message = message.unwrap_or(base.message_raw().unwrap_or("no message").to_string());
189+
190+
let b = if let Some((author, email)) = author {
188191
let a = base.author();
189192
let new_a = git2::Signature::new(&author, &email, &a.when())?;
190193
let c = base.committer();
191194
let new_c = git2::Signature::new(&author, &email, &c.when())?;
192195
repo.commit_create_buffer(&new_a, &new_c, &message, tree, parents)?
193196
} else {
194-
repo.commit_create_buffer(
195-
&base.author(),
196-
&base.committer(),
197-
&base.message_raw().unwrap_or("no message"),
198-
tree,
199-
parents,
200-
)?
197+
repo.commit_create_buffer(&base.author(), &base.committer(), &message, tree, parents)?
201198
};
202199

203200
if let (false, Ok((sig, _))) = (unsign, repo.extract_signature(&base.id(), None)) {
@@ -508,6 +505,7 @@ pub fn unapply_filter(
508505
&original_parents_refs,
509506
&new_tree,
510507
None,
508+
None,
511509
false,
512510
)?;
513511

@@ -556,14 +554,14 @@ pub fn remove_commit_signature<'a>(
556554
filtered_tree: git2::Tree<'a>,
557555
transaction: &cache::Transaction,
558556
filter: filter::Filter,
559-
message: Option<(String, String, String)>,
560557
) -> JoshResult<git2::Oid> {
561558
let (r, is_new) = create_filtered_commit2(
562559
transaction.repo(),
563560
original_commit,
564561
filtered_parent_ids,
565562
filtered_tree,
566-
message,
563+
None,
564+
None,
567565
true,
568566
)?;
569567

@@ -597,13 +595,15 @@ pub fn create_filtered_commit<'a>(
597595
filtered_tree: git2::Tree<'a>,
598596
transaction: &cache::Transaction,
599597
filter: filter::Filter,
600-
message: Option<(String, String, String)>,
598+
author: Option<(String, String)>,
599+
message: Option<String>,
601600
) -> JoshResult<git2::Oid> {
602601
let (r, is_new) = create_filtered_commit2(
603602
transaction.repo(),
604603
original_commit,
605604
filtered_parent_ids,
606605
filtered_tree,
606+
author,
607607
message,
608608
false,
609609
)?;
@@ -620,7 +620,8 @@ fn create_filtered_commit2<'a>(
620620
original_commit: &'a git2::Commit,
621621
filtered_parent_ids: Vec<git2::Oid>,
622622
filtered_tree: git2::Tree<'a>,
623-
message: Option<(String, String, String)>,
623+
author: Option<(String, String)>,
624+
message: Option<String>,
624625
unsign: bool,
625626
) -> JoshResult<(git2::Oid, bool)> {
626627
let filtered_parent_commits: Result<Vec<_>, _> = filtered_parent_ids
@@ -666,6 +667,7 @@ fn create_filtered_commit2<'a>(
666667
original_commit,
667668
&selected_filtered_parent_commits,
668669
&filtered_tree,
670+
author,
669671
message,
670672
unsign,
671673
)?,

tests/filter/squash.t

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
'git <command> [<revision>...] -- [<file>...]'
3939
[128]
4040
$ git tag tag_a 1d69b7d
41-
$ josh-filter -s --squash "refs/tags/*" --author "New Author" --email "new@e.mail" --update refs/heads/filtered
41+
$ josh-filter -s --squash "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered
4242
[1] :SQUASH=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
43-
[2] :SQUASH=e8e83b9c5d2f779f0cea83a6cad68b710a399c96
43+
[1] :author="New Author";"new@e.mail"
44+
[2] :SQUASH=10d465cdf297e8062eed54204414414faa63671e
4445
4546
$ git log --graph --decorate --pretty=oneline refs/heads/filtered
4647
* d8aa5a9937f4f0bd645dbc0b591bae5cd6b6d91b (tag: filtered/tag_a, filtered) refs/tags/tag_a
@@ -56,10 +57,11 @@
5657
|/
5758
* 0b4cf6c9efbbda1eada39fa9c1d21d2525b027bb (tag: tag_b) add file1
5859
59-
$ josh-filter -s --squash "refs/tags/*" --author "New Author" --email "new@e.mail" --update refs/heads/filtered
60+
$ josh-filter -s --squash "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered
6061
[1] :SQUASH=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
61-
[2] :SQUASH=e8e83b9c5d2f779f0cea83a6cad68b710a399c96
62-
[3] :SQUASH=3953063f3dc58661e9db16f9014aab1e8ec50bf8
62+
[2] :SQUASH=10d465cdf297e8062eed54204414414faa63671e
63+
[3] :SQUASH=dd8bdf1d78a6cb9ffc9e2a0644a8bf41de56ad36
64+
[4] :author="New Author";"new@e.mail"
6365
6466
$ git log --graph --decorate --pretty=oneline refs/heads/filtered
6567
* 5b1a753860ca124024f6dfb4fd018fe7df8beae4 (tag: filtered/tag_a, filtered) refs/tags/tag_a
@@ -81,11 +83,12 @@
8183
8284
$ git tag tag_c 975d4c4
8385
84-
$ josh-filter -s --squash "refs/tags/*" --author "New Author" --email "new@e.mail" --update refs/heads/filtered
86+
$ josh-filter -s --squash "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered
8587
[1] :SQUASH=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
86-
[2] :SQUASH=e8e83b9c5d2f779f0cea83a6cad68b710a399c96
87-
[3] :SQUASH=3953063f3dc58661e9db16f9014aab1e8ec50bf8
88-
[6] :SQUASH=6a132477d438779dbaeb0d68b9aab55786e28dd9
88+
[2] :SQUASH=10d465cdf297e8062eed54204414414faa63671e
89+
[3] :SQUASH=dd8bdf1d78a6cb9ffc9e2a0644a8bf41de56ad36
90+
[6] :SQUASH=b2a9a51df03600d3b5858fa7fca044741f88e521
91+
[9] :author="New Author";"new@e.mail"
8992
9093
$ git log --graph --decorate --pretty=oneline refs/heads/filtered
9194
* 9fe45cb2bead844630852ab338ecd8e073f8ba50 (tag: filtered/tag_a, filtered) refs/tags/tag_a

0 commit comments

Comments
 (0)