Skip to content

Commit 9ead7f6

Browse files
Implement :from filter
Change: from-filter
1 parent da01944 commit 9ead7f6

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
@@ -302,6 +302,8 @@ enum Op {
302302
Pattern(String),
303303
Message(String),
304304

305+
HistoryConcat(LazyRef, Filter),
306+
305307
Compose(Vec<Filter>),
306308
Chain(Filter, Filter),
307309
Subtract(Filter, Filter),
@@ -458,6 +460,13 @@ fn lazy_refs2(op: &Op) -> Vec<String> {
458460
av
459461
}
460462
Op::Rev(filters) => lazy_refs2(&Op::Join(filters.clone())),
463+
Op::HistoryConcat(r, _) => {
464+
let mut lr = Vec::new();
465+
if let LazyRef::Lazy(s) = r {
466+
lr.push(s.to_owned());
467+
}
468+
lr
469+
}
461470
Op::Join(filters) => {
462471
let mut lr = lazy_refs2(&Op::Compose(filters.values().copied().collect()));
463472
lr.extend(filters.keys().filter_map(|x| {
@@ -518,6 +527,19 @@ fn resolve_refs2(refs: &std::collections::HashMap<String, git2::Oid>, op: &Op) -
518527
.collect();
519528
Op::Rev(lr)
520529
}
530+
Op::HistoryConcat(r, filter) => {
531+
let f = resolve_refs(refs, *filter);
532+
let resolved_ref = if let LazyRef::Lazy(s) = r {
533+
if let Some(res) = refs.get(s) {
534+
LazyRef::Resolved(*res)
535+
} else {
536+
r.clone()
537+
}
538+
} else {
539+
r.clone()
540+
};
541+
Op::HistoryConcat(resolved_ref, f)
542+
}
521543
Op::Join(filters) => {
522544
let lr = filters
523545
.iter()
@@ -665,6 +687,9 @@ fn spec2(op: &Op) -> String {
665687
Op::Message(m) => {
666688
format!(":{}", parse::quote(m))
667689
}
690+
Op::HistoryConcat(r, filter) => {
691+
format!(":concat({}{})", r.to_string(), spec(*filter))
692+
}
668693
Op::Hook(hook) => {
669694
format!(":hook={}", parse::quote(hook))
670695
}
@@ -874,6 +899,13 @@ fn as_tree2(repo: &git2::Repository, op: &Op) -> JoshResult<git2::Oid> {
874899
v.sort();
875900
builder.insert("rev", rev_params(repo, &v)?, git2::FileMode::Tree.into())?;
876901
}
902+
Op::HistoryConcat(r, f) => {
903+
builder.insert(
904+
"historyconcat",
905+
rev_params(repo, &vec![(r.to_string(), *f)])?,
906+
git2::FileMode::Tree.into(),
907+
)?;
908+
}
877909
Op::Join(filters) => {
878910
let mut v = filters
879911
.iter()
@@ -1809,6 +1841,19 @@ fn apply_to_commit2(
18091841
))
18101842
.transpose();
18111843
}
1844+
Op::HistoryConcat(r, f) => {
1845+
if let LazyRef::Resolved(c) = r {
1846+
let a = apply_to_commit2(&to_op(*f), &repo.find_commit(*c)?, transaction)?;
1847+
let a = some_or!(a, { return Ok(None) });
1848+
if commit.id() == a {
1849+
transaction.insert(filter, commit.id(), *c, true);
1850+
return Ok(Some(*c));
1851+
}
1852+
} else {
1853+
return Err(josh_error("unresolved lazy ref"));
1854+
}
1855+
Apply::from_commit(commit)?
1856+
}
18121857
_ => {
18131858
let filtered_parent_ids = commit
18141859
.parent_ids()
@@ -1860,7 +1905,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
18601905
Op::Nop => Ok(x),
18611906
Op::Empty => Ok(x.with_tree(tree::empty(repo))),
18621907
Op::Fold => Ok(x),
1863-
Op::Squash(None) => Ok(x),
1908+
Op::Squash(..) => Ok(x),
18641909
Op::Author(author, email) => Ok(x.with_author((author.clone(), email.clone()))),
18651910
Op::Committer(author, email) => Ok(x.with_committer((author.clone(), email.clone()))),
18661911
Op::Message(m) => Ok(x.with_message(
@@ -1870,7 +1915,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
18701915
&std::collections::HashMap::<String, &dyn strfmt::DisplayStr>::new(),
18711916
)?,
18721917
)),
1873-
Op::Squash(Some(_)) => Err(josh_error("not applicable to tree")),
1918+
Op::HistoryConcat(..) => Ok(x),
18741919
Op::Linear => Ok(x),
18751920
Op::Prune => Ok(x),
18761921
Op::Unsign => Ok(x),

josh-core/src/filter/parse.rs

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

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