From a8ed502ac089fbabcef345172931079e13a9511c Mon Sep 17 00:00:00 2001 From: Christian Schilling Date: Fri, 21 Nov 2025 19:01:15 +0100 Subject: [PATCH] Add support for printing filters from trees This is cherry-picked from a later change to enable easier rebasing of the changes in between. --- josh-filter/src/bin/josh-filter.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/josh-filter/src/bin/josh-filter.rs b/josh-filter/src/bin/josh-filter.rs index 20cd5e682..73ddcdd92 100644 --- a/josh-filter/src/bin/josh-filter.rs +++ b/josh-filter/src/bin/josh-filter.rs @@ -171,16 +171,25 @@ fn run_filter(args: Vec) -> josh_core::JoshResult { return Ok(0); } let specstr = args.get_one::("filter").unwrap(); + let is_from_file = args.get_one::("file").is_some(); let specstr = args .get_one::("file") .and_then(|f| read_to_string(f).ok()) .unwrap_or(specstr.to_string()); - let mut filterobj = josh_core::filter::parse(&specstr)?; + let repo = git2::Repository::open_from_env()?; + let repo_path = repo.path().to_path_buf(); - let repo_path = { - let repo = git2::Repository::open_from_env()?; - repo.path().to_path_buf() + // If the filter spec doesn't contain a colon and it's not from a file, + // treat it as a SHA and read from tree + let mut filterobj = if specstr.contains(':') || is_from_file { + josh_core::filter::parse(&specstr)? + } else { + // Try to parse as SHA and read filter from tree + let tree_oid = git2::Oid::from_str(&specstr.trim()).map_err(|_| { + josh_core::josh_error(&format!("Invalid filter spec or SHA: {}", specstr)) + })?; + josh_core::filter::persist::from_tree(&repo, tree_oid)? }; if !args.get_flag("no-cache") {