Skip to content

Commit

Permalink
Add support to read list of refs for squash from a file (#1107)
Browse files Browse the repository at this point in the history
Change: squash-file
  • Loading branch information
christian-schilling authored Jan 19, 2023
1 parent 42e5064 commit 160f097
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
28 changes: 25 additions & 3 deletions src/bin/josh-filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ fn make_app() -> clap::Command {
.default_value("FILTERED_HEAD"),
)
.arg(
clap::Arg::new("squash")
clap::Arg::new("squash-pattern")
.help("Produce a history that contains only commits pointed to by references matching the given pattern")
.long("squash")
.long("squash-pattern")
)
.arg(
clap::Arg::new("squash-file")
.help("Produce a history that contains only commits listed in the given file")
.long("squash-file")
)
.arg(
clap::Arg::new("author")
Expand Down Expand Up @@ -172,7 +177,7 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {
filterobj = josh::filter::chain(josh::filter::squash(None), filterobj);
}

if let Some(pattern) = args.get_one::<String>("squash") {
if let Some(pattern) = args.get_one::<String>("squash-pattern") {
let pattern = pattern.to_string();
for reference in repo.references_glob(&pattern).unwrap() {
let reference = reference?;
Expand All @@ -183,6 +188,23 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {
filterobj = josh::filter::chain(josh::filter::squash(Some(&ids)), filterobj);
};

if let Some(filename) = args.get_one::<String>("squash-file") {
let reflist = read_to_string(filename)?;

for line in reflist.lines() {
let split = line.split(" ").collect::<Vec<_>>();
if let [sha, name] = split.as_slice() {
let target = git2::Oid::from_str(sha)?;
let target = repo.find_object(target, None)?.peel_to_commit()?.id();
ids.push((target, name.to_string()));
refs.push((name.to_string(), target));
} else if split.len() != 0 {
eprintln!("Warning: malformed line: {:?}", line);
}
}
filterobj = josh::filter::chain(josh::filter::squash(Some(&ids)), filterobj);
};

if args.get_flag("print-filter") {
let filterobj = if args.get_flag("reverse") {
josh::filter::invert(filterobj)?
Expand Down
21 changes: 17 additions & 4 deletions tests/filter/squash.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

$ git merge -q branch2 --no-ff

$ josh-filter -s --squash "refs/tags/*" --author "New Author" --email "new@e.mail" --update refs/heads/filtered
$ josh-filter -s --squash-pattern "refs/tags/*" --author "New Author" --email "new@e.mail" --update refs/heads/filtered
Warning: reference refs/heads/filtered wasn't updated
[1] :squash(

Expand All @@ -42,7 +42,7 @@

This one tag is an annotated tag, to make sure those are handled as well
$ git tag -a tag_a -m "created a tag" 1d69b7d
$ josh-filter -s --squash "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered
$ josh-filter -s --squash-pattern "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered
[1] :author="New Author";"new@e.mail"
[1] :squash(
Expand All @@ -65,7 +65,7 @@ This one tag is an annotated tag, to make sure those are handled as well
|/
* 0b4cf6c9efbbda1eada39fa9c1d21d2525b027bb (tag: tag_b) add file1
$ josh-filter -s --squash "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered
$ josh-filter -s --squash-pattern "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered
[1] :squash(
)
Expand Down Expand Up @@ -99,7 +99,20 @@ This one tag is an annotated tag, to make sure those are handled as well
$ git tag tag_c 975d4c4
$ josh-filter -s --squash "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered -p > filter.josh
$ git show-ref | grep refs/heads > squashlist
$ cat squashlist
86871b8775ad3baca86484337d1072aa1d386f7e refs/heads/branch2
5b1a753860ca124024f6dfb4fd018fe7df8beae4 refs/heads/filtered
1d69b7d2651f744be3416f2ad526aeccefb99310 refs/heads/master
$ josh-filter -s --squash-file squashlist :author=\"John\ Doe\"\;\"new@e.mail\" --update refs/heads/filtered -p > filter.josh
$ cat filter.josh
:squash(
1d69b7d2651f744be3416f2ad526aeccefb99310:"refs/heads/master"
5b1a753860ca124024f6dfb4fd018fe7df8beae4:"refs/heads/filtered"
86871b8775ad3baca86484337d1072aa1d386f7e:"refs/heads/branch2"
):author="John Doe";"new@e.mail"
$ josh-filter -s --squash-pattern "refs/tags/*" :author=\"New\ Author\"\;\"new@e.mail\" --update refs/heads/filtered -p > filter.josh
$ cat filter.josh
:squash(
0b4cf6c9efbbda1eada39fa9c1d21d2525b027bb:"refs/tags/tag_b"
Expand Down

0 comments on commit 160f097

Please sign in to comment.