Skip to content

Commit 7050edb

Browse files
Implement :from filter
1 parent 833abc2 commit 7050edb

File tree

7 files changed

+159
-4
lines changed

7 files changed

+159
-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: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ enum Op {
286286
Pattern(String),
287287
Message(String),
288288

289+
HistoryConcat(LazyRef, Filter),
290+
289291
Compose(Vec<Filter>),
290292
Chain(Filter, Filter),
291293
Subtract(Filter, Filter),
@@ -410,6 +412,13 @@ fn lazy_refs2(op: &Op) -> Vec<String> {
410412
av
411413
}
412414
Op::Rev(filters) => lazy_refs2(&Op::Join(filters.clone())),
415+
Op::HistoryConcat(r, _) => {
416+
let mut lr = Vec::new();
417+
if let LazyRef::Lazy(s) = r {
418+
lr.push(s.to_owned());
419+
}
420+
lr
421+
}
413422
Op::Join(filters) => {
414423
let mut lr = lazy_refs2(&Op::Compose(filters.values().copied().collect()));
415424
lr.extend(filters.keys().filter_map(|x| {
@@ -470,6 +479,19 @@ fn resolve_refs2(refs: &std::collections::HashMap<String, git2::Oid>, op: &Op) -
470479
.collect();
471480
Op::Rev(lr)
472481
}
482+
Op::HistoryConcat(r, filter) => {
483+
let f = resolve_refs(refs, *filter);
484+
let resolved_ref = if let LazyRef::Lazy(s) = r {
485+
if let Some(res) = refs.get(s) {
486+
LazyRef::Resolved(*res)
487+
} else {
488+
r.clone()
489+
}
490+
} else {
491+
r.clone()
492+
};
493+
Op::HistoryConcat(resolved_ref, f)
494+
}
473495
Op::Join(filters) => {
474496
let lr = filters
475497
.iter()
@@ -611,6 +633,9 @@ fn spec2(op: &Op) -> String {
611633
Op::Message(m) => {
612634
format!(":{}", parse::quote(m))
613635
}
636+
Op::HistoryConcat(r, filter) => {
637+
format!(":concat({}{})", r.to_string(), spec(*filter))
638+
}
614639
Op::Hook(hook) => {
615640
format!(":hook={}", parse::quote(hook))
616641
}
@@ -1025,6 +1050,19 @@ fn apply_to_commit2(
10251050

10261051
return per_rev_filter(transaction, commit, filter, commit_filter, parent_filters);
10271052
}
1053+
Op::HistoryConcat(r, f) => {
1054+
if let LazyRef::Resolved(c) = r {
1055+
let a = apply_to_commit2(&to_op(*f), &repo.find_commit(*c)?, transaction)?;
1056+
let a = some_or!(a, { return Ok(None) });
1057+
if commit.id() == a {
1058+
transaction.insert(filter, commit.id(), *c, true);
1059+
return Ok(Some(*c));
1060+
}
1061+
} else {
1062+
return Err(josh_error("unresolved lazy ref"));
1063+
}
1064+
Apply::from_commit(commit)?
1065+
}
10281066
_ => apply(transaction, filter, Apply::from_commit(commit)?)?,
10291067
};
10301068

@@ -1065,7 +1103,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
10651103
Op::Nop => Ok(x),
10661104
Op::Empty => Ok(x.with_tree(tree::empty(repo))),
10671105
Op::Fold => Ok(x),
1068-
Op::Squash(None) => Ok(x),
1106+
Op::Squash(..) => Ok(x),
10691107
Op::Author(author, email) => Ok(x.with_author((author.clone(), email.clone()))),
10701108
Op::Committer(author, email) => Ok(x.with_committer((author.clone(), email.clone()))),
10711109
Op::Message(m) => Ok(x.with_message(
@@ -1075,7 +1113,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
10751113
&std::collections::HashMap::<String, &dyn strfmt::DisplayStr>::new(),
10761114
)?,
10771115
)),
1078-
Op::Squash(Some(_)) => Err(josh_error("not applicable to tree")),
1116+
Op::HistoryConcat(..) => Ok(x),
10791117
Op::Linear => Ok(x),
10801118
Op::Prune => Ok(x),
10811119
Op::Unsign => Ok(x),

josh-core/src/filter/parse.rs

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

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

josh-core/src/filter/persist.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ impl InMemoryBuilder {
275275
let params_tree = self.build_rev_params(&v)?;
276276
push_tree_entries(&mut entries, [("join", params_tree)]);
277277
}
278+
Op::HistoryConcat(lr, f) => {
279+
let params_tree = self.build_rev_params(&[(lr.to_string(), *f)])?;
280+
push_tree_entries(&mut entries, [("concat", params_tree)]);
281+
}
278282
Op::Squash(Some(ids)) => {
279283
let mut v = ids
280284
.iter()
@@ -572,6 +576,28 @@ fn from_tree2(repo: &git2::Repository, tree_oid: git2::Oid) -> JoshResult<Op> {
572576
}
573577
Ok(Op::Join(filters))
574578
}
579+
"concat" => {
580+
let concat_tree = repo.find_tree(entry.id())?;
581+
let entry = concat_tree
582+
.get(0)
583+
.ok_or_else(|| josh_error("concat: missing entry"))?;
584+
let inner_tree = repo.find_tree(entry.id())?;
585+
let key_blob = repo.find_blob(
586+
inner_tree
587+
.get_name("o")
588+
.ok_or_else(|| josh_error("concat: missing key"))?
589+
.id(),
590+
)?;
591+
let filter_tree = repo.find_tree(
592+
inner_tree
593+
.get_name("f")
594+
.ok_or_else(|| josh_error("concat: missing filter"))?
595+
.id(),
596+
)?;
597+
let key = std::str::from_utf8(key_blob.content())?.to_string();
598+
let filter = from_tree2(repo, filter_tree.id())?;
599+
Ok(Op::HistoryConcat(LazyRef::parse(&key)?, to_filter(filter)))
600+
}
575601
"squash" => {
576602
// blob -> Squash(None), tree -> Squash(Some(...))
577603
if let Some(kind) = entry.kind() {

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)