Skip to content

Commit 8286df4

Browse files
Add tree to filter tests
1 parent ca4920b commit 8286df4

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

josh-filter/src/bin/josh-filter.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,25 @@ fn run_filter(args: Vec<String>) -> josh_core::JoshResult<i32> {
171171
return Ok(0);
172172
}
173173
let specstr = args.get_one::<String>("filter").unwrap();
174+
let is_from_file = args.get_one::<String>("file").is_some();
174175
let specstr = args
175176
.get_one::<String>("file")
176177
.and_then(|f| read_to_string(f).ok())
177178
.unwrap_or(specstr.to_string());
178179

179-
let mut filterobj = josh_core::filter::parse(&specstr)?;
180+
let repo = git2::Repository::open_from_env()?;
181+
let repo_path = repo.path().to_path_buf();
180182

181-
let repo_path = {
182-
let repo = git2::Repository::open_from_env()?;
183-
repo.path().to_path_buf()
183+
// If the filter spec doesn't contain a colon and it's not from a file,
184+
// treat it as a SHA and read from tree
185+
let mut filterobj = if specstr.contains(':') || is_from_file {
186+
josh_core::filter::parse(&specstr)?
187+
} else {
188+
// Try to parse as SHA and read filter from tree
189+
let tree_oid = git2::Oid::from_str(&specstr.trim()).map_err(|_| {
190+
josh_core::josh_error(&format!("Invalid filter spec or SHA: {}", specstr))
191+
})?;
192+
josh_core::filter::persist::from_tree(&repo, tree_oid)?
184193
};
185194

186195
if !args.get_flag("no-cache") {

tests/filter/filter_id.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
$ git commit -q --allow-empty -m "empty"
55

66
$ FILTER_HASH=$(josh-filter -i :[:/a,:/b])
7+
$ josh-filter -p ${FILTER_HASH}
8+
:[
9+
:/a
10+
:/b
11+
]
712
$ git read-tree --reset -u ${FILTER_HASH}
813
$ find . -type f -not -path './.git/*' -exec echo "-- {}" \; -exec cat {} \;
914
-- ./compose/0/subdir/0
@@ -56,6 +61,11 @@
5661
$ josh-filter --reverse -p :[:empty,:/a]
5762
:prefix=a
5863
$ FILTER_HASH=$(josh-filter -i :[x=:/a:/b:/d,y=:/a:/c:/d])
64+
$ josh-filter -p ${FILTER_HASH}
65+
:/a:[
66+
x = :/b/d
67+
y = :/c/d
68+
]
5969
$ git read-tree --reset -u ${FILTER_HASH}
6070
$ tree
6171
.
@@ -415,6 +425,8 @@ Test File filter tree representations
415425

416426
Test ::file.txt (single argument, no trailing slash, no =, no *)
417427
$ FILTER_HASH=$(josh-filter -i ::file.txt)
428+
$ josh-filter -p ${FILTER_HASH}
429+
::file.txt
418430
$ git read-tree --reset -u ${FILTER_HASH}
419431
$ tree
420432
.
@@ -443,6 +455,8 @@ Test ::file.txt (single argument, no trailing slash, no =, no *)
443455

444456
Test ::dest.txt=src.txt (with =, destination=source)
445457
$ FILTER_HASH=$(josh-filter -i ::dest.txt=src.txt)
458+
$ josh-filter -p ${FILTER_HASH}
459+
::dest.txt=src.txt
446460
$ git read-tree --reset -u ${FILTER_HASH}
447461
$ tree
448462
.
@@ -471,6 +485,8 @@ Test ::dest.txt=src.txt (with =, destination=source)
471485

472486
Test ::*.txt (with *, pattern)
473487
$ FILTER_HASH=$(josh-filter -i ::*.txt)
488+
$ josh-filter -p ${FILTER_HASH}
489+
::*.txt
474490
$ git read-tree --reset -u ${FILTER_HASH}
475491
$ tree
476492
.
@@ -490,6 +506,8 @@ Test ::*.txt (with *, pattern)
490506

491507
Test ::dir/ (with trailing slash, directory)
492508
$ FILTER_HASH=$(josh-filter -i ::dir/)
509+
$ josh-filter -p ${FILTER_HASH}
510+
::dir/
493511
$ git read-tree --reset -u ${FILTER_HASH}
494512
$ tree
495513
.
@@ -522,6 +540,8 @@ Test ::dir/ (with trailing slash, directory)
522540

523541
Test ::a/b/c/ (nested directory path with trailing slash)
524542
$ FILTER_HASH=$(josh-filter -i ::a/b/c/)
543+
$ josh-filter -p ${FILTER_HASH}
544+
::a/b/c/
525545
$ git read-tree --reset -u ${FILTER_HASH}
526546
$ tree
527547
.

0 commit comments

Comments
 (0)