Skip to content

Commit

Permalink
Add :start=sha[...] filter
Browse files Browse the repository at this point in the history
Change: start-filter
  • Loading branch information
christian-schilling committed Oct 2, 2022
1 parent 6f667d4 commit 99a5743
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/filter/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ char = {

filter_spec = { (
filter_group
| filter_group_arg
| filter_presub
| filter_subdir
| filter_nop
Expand All @@ -26,6 +27,7 @@ filter_spec = { (
)+ }

filter_group = { CMD_START ~ cmd? ~ GROUP_START ~ compose ~ GROUP_END }
filter_group_arg = { CMD_START ~ cmd ~ "=" ~ argument ~ GROUP_START ~ compose ~ GROUP_END }
filter_subdir = { CMD_START ~ "/" ~ argument }
filter_nop = { CMD_START ~ "/" }
filter_presub = { CMD_START ~ ":" ~ argument }
Expand Down
13 changes: 13 additions & 0 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ enum Op {
Fold,
Paths,
Squash(Option<std::collections::HashMap<git2::Oid, (String, String, String)>>),
Start(git2::Oid, Filter),
Linear,

RegexReplace(regex::Regex, String),
Expand Down Expand Up @@ -223,6 +224,9 @@ fn spec2(op: &Op) -> String {
Op::Exclude(b) => {
format!(":exclude[{}]", spec(*b))
}
Op::Start(id, b) => {
format!(":start={}[{}]", id, spec(*b))
}
Op::Workspace(path) => {
format!(":workspace={}", parse::quote(&path.to_string_lossy()))
}
Expand Down Expand Up @@ -374,6 +378,14 @@ fn apply_to_commit2(
))
.transpose()
}
Op::Start(id,filter) => {
if *id == commit.id() {
return apply_to_commit2(&to_op(*filter), commit, transaction);
} else {

return apply_to_commit2(&Op::Nop, commit, transaction);
}
},
_ => {
if let Some(oid) = transaction.get(filter, commit.id()) {
return Ok(Some(oid));
Expand Down Expand Up @@ -619,6 +631,7 @@ fn apply2<'a>(
Op::Squash(None) => Ok(tree),
Op::Squash(Some(_)) => Err(josh_error("not applicable to tree")),
Op::Linear => Ok(tree),
Op::Start(_,_) => Err(josh_error("not applicable to tree")),

Op::RegexReplace(regex, replacement) => {
tree::regex_replace(tree.id(), &regex, &replacement, transaction)
Expand Down
1 change: 1 addition & 0 deletions src/filter/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ fn step(filter: Filter) -> Filter {
Op::Prefix(path)
}
}
Op::Start(id, filter) => Op::Start(id, step(filter)),
Op::Compose(filters) if filters.is_empty() => Op::Empty,
Op::Compose(filters) if filters.len() == 1 => to_op(filters[0]),
Op::Compose(mut filters) => {
Expand Down
14 changes: 14 additions & 0 deletions src/filter/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ fn parse_item(pair: pest::iterators::Pair<Rule>) -> JoshResult<Op> {
_ => Err(josh_error("parse_item: no match {:?}")),
}
}
Rule::filter_group_arg => {
let v: Vec<_> = pair.into_inner().map(|x| unquote(x.as_str())).collect();

match v.as_slice() {
[cmd, arg, args] => {
let g = parse_group(args)?;
match *cmd {
"start" => Ok(Op::Start(git2::Oid::from_str(arg)?, to_filter(Op::Compose(g)))),
_ => Err(josh_error("parse_item: no match")),
}
}
_ => Err(josh_error("parse_item: no match {:?}")),
}
}
_ => Err(josh_error("parse_item: no match")),
}
}
Expand Down
49 changes: 49 additions & 0 deletions tests/filter/start.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
$ export RUST_BACKTRACE=1
$ git init -q 1> /dev/null

$ echo contents1 > file1
$ git add .
$ git commit -m "add file1" 1> /dev/null

$ git log --graph --pretty=%s
* add file1

$ git checkout -b branch2
Switched to a new branch 'branch2'

$ echo contents2 > file1
$ git add .
$ git commit -m "mod file1" 1> /dev/null

$ echo contents3 > file3
$ git add .
$ git commit -m "mod file3" 1> /dev/null

$ git checkout master
Switched to branch 'master'

$ echo contents3 > file2
$ git add .
$ git commit -m "add file2" 1> /dev/null

$ git merge -q branch2 --no-ff

$ git log --graph --pretty=%H
* 1d69b7d2651f744be3416f2ad526aeccefb99310
|\
| * 86871b8775ad3baca86484337d1072aa1d386f7e
| * 975d4c4975912729482cc864d321c5196a969271
* | e707f76bb6a1390f28b2162da5b5eb6933009070
|/
* 0b4cf6c9efbbda1eada39fa9c1d21d2525b027bb

$ josh-filter -s :start=0b4cf6c9efbbda1eada39fa9c1d21d2525b027bb[:prefix=x] --update refs/heads/filtered

$ git log --graph --decorate --pretty=%H refs/heads/filtered
* 1d69b7d2651f744be3416f2ad526aeccefb99310
|\
| * 86871b8775ad3baca86484337d1072aa1d386f7e
| * 975d4c4975912729482cc864d321c5196a969271
* | e707f76bb6a1390f28b2162da5b5eb6933009070
|/
* 0b4cf6c9efbbda1eada39fa9c1d21d2525b027bb

0 comments on commit 99a5743

Please sign in to comment.