Skip to content

Commit bc00e81

Browse files
Implement :from filter
1 parent ab022bd commit bc00e81

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

docs/src/reference/filters.md

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

128+
### Start filtering from a specific commit **:from(<sha>:filter)**
129+
130+
Produce a history that keeps the original history leading up to the specified commit `<sha>` unchanged,
131+
but applies the given `:filter` to all commits from that commit onwards.
132+
128133
### Prune trivial merge commits **:prune=trivial-merge**
129134

130135
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ enum Op {
316316
Pattern(String),
317317
Message(String, regex::Regex),
318318

319+
HistoryConcat(LazyRef, Filter),
320+
319321
Compose(Vec<Filter>),
320322
Chain(Filter, Filter),
321323
Subtract(Filter, Filter),
@@ -440,6 +442,13 @@ fn lazy_refs2(op: &Op) -> Vec<String> {
440442
av
441443
}
442444
Op::Rev(filters) => lazy_refs2(&Op::Join(filters.clone())),
445+
Op::HistoryConcat(r, _) => {
446+
let mut lr = Vec::new();
447+
if let LazyRef::Lazy(s) = r {
448+
lr.push(s.to_owned());
449+
}
450+
lr
451+
}
443452
Op::Join(filters) => {
444453
let mut lr = lazy_refs2(&Op::Compose(filters.values().copied().collect()));
445454
lr.extend(filters.keys().filter_map(|x| {
@@ -500,6 +509,19 @@ fn resolve_refs2(refs: &std::collections::HashMap<String, git2::Oid>, op: &Op) -
500509
.collect();
501510
Op::Rev(lr)
502511
}
512+
Op::HistoryConcat(r, filter) => {
513+
let f = resolve_refs(refs, *filter);
514+
let resolved_ref = if let LazyRef::Lazy(s) = r {
515+
if let Some(res) = refs.get(s) {
516+
LazyRef::Resolved(*res)
517+
} else {
518+
r.clone()
519+
}
520+
} else {
521+
r.clone()
522+
};
523+
Op::HistoryConcat(resolved_ref, f)
524+
}
503525
Op::Join(filters) => {
504526
let lr = filters
505527
.iter()
@@ -654,6 +676,9 @@ fn spec2(op: &Op) -> String {
654676
Op::Message(m, r) => {
655677
format!(":{};{}", parse::quote(m), parse::quote(r.as_str()))
656678
}
679+
Op::HistoryConcat(r, filter) => {
680+
format!(":concat({}{})", r.to_string(), spec(*filter))
681+
}
657682
Op::Hook(hook) => {
658683
format!(":hook={}", parse::quote(hook))
659684
}
@@ -1057,6 +1082,19 @@ fn apply_to_commit2(
10571082

10581083
return per_rev_filter(transaction, commit, filter, commit_filter, parent_filters);
10591084
}
1085+
Op::HistoryConcat(r, f) => {
1086+
if let LazyRef::Resolved(c) = r {
1087+
let a = apply_to_commit2(&to_op(*f), &repo.find_commit(*c)?, transaction)?;
1088+
let a = some_or!(a, { return Ok(None) });
1089+
if commit.id() == a {
1090+
transaction.insert(filter, commit.id(), *c, true);
1091+
return Ok(Some(*c));
1092+
}
1093+
} else {
1094+
return Err(josh_error("unresolved lazy ref"));
1095+
}
1096+
Apply::from_commit(commit)?
1097+
}
10601098
_ => apply(transaction, filter, Apply::from_commit(commit)?)?,
10611099
};
10621100

@@ -1121,6 +1159,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
11211159

11221160
Ok(x.with_message(text::transform_with_template(&r, &m, &message, &hm)?))
11231161
}
1162+
Op::HistoryConcat(..) => Ok(x),
11241163
Op::Linear => Ok(x),
11251164
Op::Prune => Ok(x),
11261165
Op::Unsign => Ok(x),

josh-core/src/filter/parse.rs

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

177177
Ok(Op::Rev(hm))
178178
}
179+
Rule::filter_from => {
180+
let v: Vec<_> = pair.into_inner().map(|x| x.as_str()).collect();
181+
182+
if v.len() == 2 {
183+
let oid = LazyRef::parse(v[0])?;
184+
let filter = parse(v[1])?;
185+
Ok(Op::Chain(
186+
filter,
187+
filter::to_filter(Op::HistoryConcat(oid, filter)),
188+
))
189+
} else {
190+
Err(josh_error("wrong argument count for :from"))
191+
}
192+
}
193+
Rule::filter_concat => {
194+
let v: Vec<_> = pair.into_inner().map(|x| x.as_str()).collect();
195+
196+
if v.len() == 2 {
197+
let oid = LazyRef::parse(v[0])?;
198+
let filter = parse(v[1])?;
199+
Ok(Op::HistoryConcat(oid, filter))
200+
} else {
201+
Err(josh_error("wrong argument count for :concat"))
202+
}
203+
}
179204
Rule::filter_replace => {
180205
let replacements = pair
181206
.into_inner()

josh-core/src/filter/persist.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ impl InMemoryBuilder {
286286
let params_tree = self.build_rev_params(&v)?;
287287
push_tree_entries(&mut entries, [("join", params_tree)]);
288288
}
289+
Op::HistoryConcat(lr, f) => {
290+
let params_tree = self.build_rev_params(&[(lr.to_string(), *f)])?;
291+
push_tree_entries(&mut entries, [("concat", params_tree)]);
292+
}
289293
Op::Squash(Some(ids)) => {
290294
let mut v = ids
291295
.iter()
@@ -626,6 +630,28 @@ fn from_tree2(repo: &git2::Repository, tree_oid: git2::Oid) -> JoshResult<Op> {
626630
}
627631
Ok(Op::Join(filters))
628632
}
633+
"concat" => {
634+
let concat_tree = repo.find_tree(entry.id())?;
635+
let entry = concat_tree
636+
.get(0)
637+
.ok_or_else(|| josh_error("concat: missing entry"))?;
638+
let inner_tree = repo.find_tree(entry.id())?;
639+
let key_blob = repo.find_blob(
640+
inner_tree
641+
.get_name("o")
642+
.ok_or_else(|| josh_error("concat: missing key"))?
643+
.id(),
644+
)?;
645+
let filter_tree = repo.find_tree(
646+
inner_tree
647+
.get_name("f")
648+
.ok_or_else(|| josh_error("concat: missing filter"))?
649+
.id(),
650+
)?;
651+
let key = std::str::from_utf8(key_blob.content())?.to_string();
652+
let filter = from_tree2(repo, filter_tree.id())?;
653+
Ok(Op::HistoryConcat(LazyRef::parse(&key)?, to_filter(filter)))
654+
}
629655
"squash" => {
630656
// blob -> Squash(None), tree -> Squash(Some(...))
631657
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

0 commit comments

Comments
 (0)