Skip to content

Commit 37ee5cd

Browse files
Implement :from filter
Change: from-filter
1 parent 7e54cc9 commit 37ee5cd

File tree

6 files changed

+140
-4
lines changed

6 files changed

+140
-4
lines changed

docs/src/reference/filters.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ commits that don't match any of the other shas.
114114
Produce the history that would be the result of pushing the passed branches with the
115115
passed filters into the upstream.
116116

117+
### Start filtering from a specific commit **:from(<sha>:filter)**
118+
119+
Produce a history that keeps the original history leading up to the specified commit `<sha>` unchanged,
120+
but applies the given `:filter` to all commits from that commit onwards.
121+
117122
### Prune trivial merge commits **:prune=trivial-merge**
118123

119124
Produce a history that skips all merge commits whose tree is identical to the first parents

josh-core/src/filter/grammar.pest

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ filter_spec = { (
2424
filter_group
2525
| filter_message
2626
| filter_rev
27+
| filter_from
28+
| filter_concat
2729
| filter_join
2830
| filter_replace
2931
| filter_squash
@@ -51,6 +53,24 @@ filter_rev = {
5153
~ ")"
5254
}
5355

56+
filter_from = {
57+
CMD_START ~ "from" ~ "("
58+
~ NEWLINE*
59+
~ (rev ~ filter_spec)?
60+
~ (CMD_SEP+ ~ (rev ~ filter_spec))*
61+
~ NEWLINE*
62+
~ ")"
63+
}
64+
65+
filter_concat = {
66+
CMD_START ~ "from" ~ "("
67+
~ NEWLINE*
68+
~ (rev ~ filter_spec)?
69+
~ (CMD_SEP+ ~ (rev ~ filter_spec))*
70+
~ NEWLINE*
71+
~ ")"
72+
}
73+
5474
filter_join = {
5575
CMD_START ~ "join" ~ "("
5676
~ NEWLINE*
@@ -60,7 +80,6 @@ filter_join = {
6080
~ ")"
6181
}
6282

63-
6483
filter_replace = {
6584
CMD_START ~ "replace" ~ "("
6685
~ NEWLINE*

josh-core/src/filter/mod.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ enum Op {
293293
Pattern(String),
294294
Message(String),
295295

296+
HistoryConcat(LazyRef, Filter),
297+
296298
Compose(Vec<Filter>),
297299
Chain(Filter, Filter),
298300
Subtract(Filter, Filter),
@@ -442,6 +444,13 @@ fn lazy_refs2(op: &Op) -> Vec<String> {
442444
av
443445
}
444446
Op::Rev(filters) => lazy_refs2(&Op::Join(filters.clone())),
447+
Op::HistoryConcat(r, _) => {
448+
let mut lr = Vec::new();
449+
if let LazyRef::Lazy(s) = r {
450+
lr.push(s.to_owned());
451+
}
452+
lr
453+
}
445454
Op::Join(filters) => {
446455
let mut lr = lazy_refs2(&Op::Compose(filters.values().copied().collect()));
447456
lr.extend(filters.keys().filter_map(|x| {
@@ -501,6 +510,19 @@ fn resolve_refs2(refs: &std::collections::HashMap<String, git2::Oid>, op: &Op) -
501510
.collect();
502511
Op::Rev(lr)
503512
}
513+
Op::HistoryConcat(r, filter) => {
514+
let f = resolve_refs(refs, *filter);
515+
let resolved_ref = if let LazyRef::Lazy(s) = r {
516+
if let Some(res) = refs.get(s) {
517+
LazyRef::Resolved(*res)
518+
} else {
519+
r.clone()
520+
}
521+
} else {
522+
r.clone()
523+
};
524+
Op::HistoryConcat(resolved_ref, f)
525+
}
504526
Op::Join(filters) => {
505527
let lr = filters
506528
.iter()
@@ -636,6 +658,9 @@ fn spec2(op: &Op) -> String {
636658
Op::Message(m) => {
637659
format!(":{}", parse::quote(m))
638660
}
661+
Op::HistoryConcat(r, filter) => {
662+
format!(":concat({}{})", r.to_string(), spec(*filter))
663+
}
639664
Op::Hook(hook) => {
640665
format!(":hook={}", parse::quote(hook))
641666
}
@@ -828,6 +853,13 @@ fn as_tree2(repo: &git2::Repository, op: &Op) -> JoshResult<git2::Oid> {
828853
v.sort();
829854
builder.insert("rev", rev_params(repo, &v)?, git2::FileMode::Tree.into())?;
830855
}
856+
Op::HistoryConcat(r, f) => {
857+
builder.insert(
858+
"historyconcat",
859+
rev_params(repo, &vec![(r.to_string(), *f)])?,
860+
git2::FileMode::Tree.into(),
861+
)?;
862+
}
831863
Op::Join(filters) => {
832864
let mut v = filters
833865
.iter()
@@ -1678,6 +1710,19 @@ fn apply_to_commit2(
16781710
))
16791711
.transpose();
16801712
}
1713+
Op::HistoryConcat(r, f) => {
1714+
if let LazyRef::Resolved(c) = r {
1715+
let a = apply_to_commit2(&to_op(*f), &repo.find_commit(*c)?, transaction)?;
1716+
let a = some_or!(a, { return Ok(None) });
1717+
if commit.id() == a {
1718+
transaction.insert(filter, commit.id(), *c, true);
1719+
return Ok(Some(*c));
1720+
}
1721+
} else {
1722+
return Err(josh_error("unresolved lazy ref"));
1723+
}
1724+
Apply::from_commit(commit)?
1725+
}
16811726
_ => {
16821727
let filtered_parent_ids = commit
16831728
.parent_ids()
@@ -1729,7 +1774,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
17291774
Op::Nop => Ok(x),
17301775
Op::Empty => Ok(x.with_tree(tree::empty(repo))),
17311776
Op::Fold => Ok(x),
1732-
Op::Squash(None) => Ok(x),
1777+
Op::Squash(..) => Ok(x),
17331778
Op::Author(author, email) => Ok(x.with_author((author.clone(), email.clone()))),
17341779
Op::Committer(author, email) => Ok(x.with_committer((author.clone(), email.clone()))),
17351780
Op::Message(m) => Ok(x.with_message(
@@ -1739,7 +1784,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
17391784
&std::collections::HashMap::<String, &dyn strfmt::DisplayStr>::new(),
17401785
)?,
17411786
)),
1742-
Op::Squash(Some(_)) => Err(josh_error("not applicable to tree")),
1787+
Op::HistoryConcat(..) => Ok(x),
17431788
Op::Linear => Ok(x),
17441789
Op::Prune => Ok(x),
17451790
Op::Unsign => Ok(x),

josh-core/src/filter/parse.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ fn parse_item(pair: pest::iterators::Pair<Rule>) -> JoshResult<Op> {
145145

146146
Ok(Op::Rev(hm))
147147
}
148+
Rule::filter_from => {
149+
let v: Vec<_> = pair.into_inner().map(|x| x.as_str()).collect();
150+
151+
if v.len() == 2 {
152+
let oid = LazyRef::parse(v[0])?;
153+
let filter = parse(v[1])?;
154+
Ok(Op::Chain(
155+
filter,
156+
filter::to_filter(Op::HistoryConcat(oid, filter)),
157+
))
158+
} else {
159+
Err(josh_error("wrong argument count for :from"))
160+
}
161+
}
162+
Rule::filter_concat => {
163+
let v: Vec<_> = pair.into_inner().map(|x| x.as_str()).collect();
164+
165+
if v.len() == 2 {
166+
let oid = LazyRef::parse(v[0])?;
167+
let filter = parse(v[1])?;
168+
Ok(Op::HistoryConcat(oid, filter))
169+
} else {
170+
Err(josh_error("wrong argument count for :concat"))
171+
}
172+
}
148173
Rule::filter_replace => {
149174
let replacements = pair
150175
.into_inner()

tests/filter/concat.t

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
$ export TESTTMP=${PWD}
2+
3+
$ cd ${TESTTMP}
4+
$ git init -q libs 1> /dev/null
5+
$ cd libs
6+
7+
$ mkdir sub1
8+
$ echo contents1 > sub1/file1
9+
$ git add sub1
10+
$ git commit -m "add file1" 1> /dev/null
11+
12+
$ echo contents2 > sub1/file2
13+
$ git add sub1
14+
$ git commit -m "add file2" 1> /dev/null
15+
$ git update-ref refs/heads/from_here HEAD
16+
17+
18+
$ mkdir sub2
19+
$ echo contents1 > sub2/file3
20+
$ git add sub2
21+
$ git commit -m "add file3" 1> /dev/null
22+
23+
$ josh-filter ":\"x\""
24+
25+
$ git log --graph --pretty=%s:%H HEAD
26+
* add file3:667a912db7482f3c8023082c9b4c7b267792633a
27+
* add file2:81b10fb4984d20142cd275b89c91c346e536876a
28+
* add file1:bb282e9cdc1b972fffd08fd21eead43bc0c83cb8
29+
30+
$ git log --graph --pretty=%s:%H FILTERED_HEAD
31+
* x:9d117d96dfdba145df43ebe37d9e526acac4b17c
32+
* x:b232aa8eefaadfb5e38b3ad7355118aa59fb651e
33+
* x:6b4d1f87c2be08f7d0f9d40b6679aab612e259b1
34+
35+
$ josh-filter -p ":from(81b10fb4984d20142cd275b89c91c346e536876a:\"x\")"
36+
:"x":concat(81b10fb4984d20142cd275b89c91c346e536876a:"x")
37+
$ josh-filter ":from(81b10fb4984d20142cd275b89c91c346e536876a:\"x\")"
38+
39+
$ git log --graph --pretty=%s FILTERED_HEAD
40+
* x
41+
* add file2
42+
* add file1

tests/proxy/workspace_errors.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Error in filter
106106
remote: 1 | a/b = :b/sub2
107107
remote: | ^---
108108
remote: |
109-
remote: = expected EOI, filter_group, filter_subdir, filter_nop, filter_presub, filter, filter_noarg, filter_message, filter_rev, filter_join, filter_replace, or filter_squash
109+
remote: = expected EOI, filter_group, filter_subdir, filter_nop, filter_presub, filter, filter_noarg, filter_message, filter_rev, filter_from, filter_concat, filter_join, filter_replace, or filter_squash
110110
remote:
111111
remote: a/b = :b/sub2
112112
remote: c = :/sub1

0 commit comments

Comments
 (0)