Skip to content

Commit c15be82

Browse files
committed
also implement inverse for subtree_prefix
1 parent 72a7a96 commit c15be82

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/filter/opt.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@ pub fn invert(filter: Filter) -> JoshResult<Filter> {
418418
Op::Subdir(path) => Some(Op::Prefix(path)),
419419
Op::File(path) => Some(Op::File(path)),
420420
Op::Prefix(path) => Some(Op::Subdir(path)),
421+
Op::SubtreePrefix {
422+
subtree_tip: _,
423+
path: _,
424+
} => {
425+
// We assume that new commits being added don't need the prefix fixup any more.
426+
// FIXME(RalfJung): does that make sense?
427+
Some(Op::Nop)
428+
}
421429
Op::Glob(pattern) => Some(Op::Glob(pattern)),
422430
_ => None,
423431
};

tests/filter/subtree_prefix.t

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
$ git init -q 1> /dev/null
1+
$ git init -q 1>/dev/null
22

33
Initial commit of main branch
44
$ echo contents1 > file1
55
$ git add .
6-
$ git commit -m "add file1" 1> /dev/null
6+
$ git commit -m "add file1" 1>/dev/null
77

88
Initial commit of subtree branch
99
$ git checkout --orphan subtree
1010
Switched to a new branch 'subtree'
1111
$ rm file*
1212
$ echo contents2 > file2
1313
$ git add .
14-
$ git commit -m "add file2 (in subtree)" 1> /dev/null
14+
$ git commit -m "add file2 (in subtree)" 1>/dev/null
15+
$ export SUBTREE_TIP=$(git rev-parse HEAD)
1516

1617
Articially create a subtree merge
1718
(merge commit has subtree files in subfolder but has subtree commit as a parent)
1819
$ git checkout master
1920
Switched to branch 'master'
20-
$ git merge subtree --allow-unrelated-histories 1> /dev/null
21+
$ git merge subtree --allow-unrelated-histories 1>/dev/null
2122
$ mkdir subtree
2223
$ git mv file2 subtree/
2324
$ git add subtree
24-
$ git commit -a --amend -m "subtree merge" 1> /dev/null
25+
$ git commit -a --amend -m "subtree merge" 1>/dev/null
2526
$ tree
2627
.
2728
|-- file1
@@ -37,11 +38,11 @@ Articially create a subtree merge
3738

3839
Change subtree file
3940
$ echo more contents >> subtree/file2
40-
$ git commit -a -m "subtree edit from main repo" 1> /dev/null
41+
$ git commit -a -m "subtree edit from main repo" 1>/dev/null
4142

4243
Rewrite the subtree part of the history
4344
FIXME(RalfJung): if I use /subtree or subtree/, the command still succeeds, but produces completely wrong output
44-
$ josh-filter -s :subtree_prefix=$(git rev-parse subtree),subtree refs/heads/master --update refs/heads/filtered
45+
$ josh-filter -s :subtree_prefix=$SUBTREE_TIP,subtree refs/heads/master --update refs/heads/filtered
4546
\[4\] :subtree_prefix=.*,subtree (re)
4647

4748
$ git log --graph --pretty=%s refs/heads/filtered
@@ -63,3 +64,42 @@ Compare input and result. ^^2 is the 2nd parent of the first parent, i.e., the '
6364
similarity index 100%
6465
rename from file2
6566
rename to subtree/file2
67+
68+
Extract the subtree history
69+
$ josh-filter -s :subtree_prefix=$SUBTREE_TIP,subtree:/subtree refs/heads/master --update refs/heads/subtree
70+
\[[0-9]\] :/subtree (re)
71+
\[[0-9]\] :subtree_prefix=.*,subtree (re)
72+
$ git checkout subtree
73+
Switched to branch 'subtree'
74+
$ cat file2
75+
contents2
76+
more contents
77+
78+
Work in the subtree, and sync that back.
79+
$ echo even more contents >> file2
80+
$ git commit -am "add even more content" 1>/dev/null
81+
$ josh-filter -s :subtree_prefix=$SUBTREE_TIP,subtree:/subtree refs/heads/master --update refs/heads/subtree --reverse
82+
\[[0-9]\] :/subtree (re)
83+
\[[0-9]\] :subtree_prefix=.*,subtree (re)
84+
$ git log --graph --pretty=%s refs/heads/master
85+
* add even more content
86+
* subtree edit from main repo
87+
* subtree merge
88+
|\
89+
| * add file2 (in subtree)
90+
* add file1
91+
$ git ls-tree --name-only -r refs/heads/master
92+
file1
93+
subtree/file2
94+
$ git checkout master
95+
Switched to branch 'master'
96+
$ cat subtree/file2
97+
contents2
98+
more contents
99+
even more contents
100+
101+
And then re-extract, which should re-construct the same subtree.
102+
$ josh-filter -s :subtree_prefix=$SUBTREE_TIP,subtree:/subtree refs/heads/master --update refs/heads/subtree2
103+
\[[0-9]\] :/subtree (re)
104+
\[[0-9]\] :subtree_prefix=.*,subtree (re)
105+
$ test $(git rev-parse subtree) = $(git rev-parse subtree2)

0 commit comments

Comments
 (0)