Skip to content

Commit b2a93b5

Browse files
committed
new: added --ignore filter to sign and verify sub commands
1 parent 0cc87e1 commit b2a93b5

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/cli/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub(crate) struct SignArgs {
8787
/// Output signature file. If not set the original file name will be used as base name.
8888
#[clap(long, short = 'O')]
8989
output: Option<PathBuf>,
90+
/// Ignore files and folders matching this pattern.
91+
#[clap(long, short = 'I')]
92+
ignore: Option<String>,
9093
}
9194

9295
#[derive(Debug, Args)]
@@ -102,6 +105,9 @@ pub(crate) struct VerifyArgs {
102105
/// Signature file. If not set the file name will be used as base name.
103106
#[clap(long, short = 'S')]
104107
signature: Option<PathBuf>,
108+
/// Ignore files and folders matching this pattern.
109+
#[clap(long, short = 'I')]
110+
ignore: Option<String>,
105111
}
106112

107113
#[derive(Debug, Args)]

src/cli/signing.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn get_paths_for(format: Option<FileType>, file_path: &Path) -> anyhow::Result<V
3333
fn get_paths_of_interest(
3434
format: Option<FileType>,
3535
file_path: &Path,
36+
ignore: Option<String>,
3637
) -> anyhow::Result<Vec<PathBuf>> {
3738
let paths = if file_path.is_file() {
3839
// single file case
@@ -55,6 +56,15 @@ fn get_paths_of_interest(
5556
unique.into_iter().collect::<Vec<PathBuf>>()
5657
};
5758

59+
let paths = if let Some(ignore_pattern) = ignore {
60+
paths
61+
.into_iter()
62+
.filter(|path| !path.to_string_lossy().contains(&ignore_pattern))
63+
.collect()
64+
} else {
65+
paths
66+
};
67+
5868
if paths.is_empty() {
5969
return Err(anyhow!("no compatible paths found"));
6070
}
@@ -79,7 +89,7 @@ pub(crate) fn sign(args: SignArgs) -> anyhow::Result<()> {
7989
// load the private key for signing
8090
let signing_key = crate::core::signing::load_key(&args.key_path)?;
8191
// get the paths to sign
82-
let mut paths_to_sign = get_paths_of_interest(args.format, &args.file_path)?;
92+
let mut paths_to_sign = get_paths_of_interest(args.format, &args.file_path, args.ignore)?;
8393
let base_path = if args.file_path.is_file() {
8494
args.file_path.parent().unwrap().to_path_buf()
8595
} else {
@@ -119,7 +129,7 @@ pub(crate) fn verify(args: VerifyArgs) -> anyhow::Result<()> {
119129
// load the public key to verify against
120130
let mut manifest = Manifest::from_public_key_path(&base_path, &args.key_path)?;
121131
// get the paths to verify
122-
let mut paths_to_verify = get_paths_of_interest(args.format, &args.file_path)?;
132+
let mut paths_to_verify = get_paths_of_interest(args.format, &args.file_path, args.ignore)?;
123133
// remove the signature file from the list
124134
paths_to_verify.retain(|p| p != &signature_path);
125135

@@ -144,7 +154,7 @@ mod tests {
144154

145155
File::create(&file_path)?;
146156

147-
let paths = get_paths_of_interest(None, &file_path)?;
157+
let paths = get_paths_of_interest(None, &file_path, None)?;
148158
assert_eq!(paths.len(), 1);
149159
assert_eq!(paths[0], file_path.canonicalize()?);
150160

@@ -160,7 +170,7 @@ mod tests {
160170
File::create(temp_dir.path().join("model.bin"))?;
161171
File::create(temp_dir.path().join("other.txt"))?;
162172

163-
let paths = get_paths_of_interest(None, temp_dir.path())?;
173+
let paths = get_paths_of_interest(None, temp_dir.path(), None)?;
164174
assert_eq!(paths.len(), 3);
165175

166176
// Sort paths for consistent comparison
@@ -186,6 +196,7 @@ mod tests {
186196
let paths = get_paths_of_interest(
187197
Some(FileType::SafeTensors),
188198
&temp_dir.path().join("model.custom"),
199+
None,
189200
)?;
190201
assert_eq!(paths.len(), 1);
191202
assert!(paths[0].to_string_lossy().ends_with("model.custom"));
@@ -202,7 +213,7 @@ mod tests {
202213
File::create(temp_dir.path().join("model-00002-of-00002.safetensors"))?;
203214
File::create(temp_dir.path().join("other.txt"))?;
204215

205-
let paths = get_paths_of_interest(None, temp_dir.path())?;
216+
let paths = get_paths_of_interest(None, temp_dir.path(), None)?;
206217
assert_eq!(paths.len(), 3);
207218

208219
let mut paths: Vec<String> = paths
@@ -225,7 +236,7 @@ mod tests {
225236

226237
#[test]
227238
fn test_get_paths_nonexistent() {
228-
let result = get_paths_of_interest(None, &PathBuf::from("/nonexistent/path"));
239+
let result = get_paths_of_interest(None, &PathBuf::from("/nonexistent/path"), None);
229240
assert!(result.is_err());
230241
}
231242

@@ -246,7 +257,7 @@ mod tests {
246257
File::create(deep_dir.join(".very_hidden"))?;
247258
File::create(deep_dir.join("deep.dat"))?;
248259

249-
let paths = get_paths_of_interest(None, temp_dir.path())?;
260+
let paths = get_paths_of_interest(None, temp_dir.path(), None)?;
250261
assert_eq!(paths.len(), 6); // Should find all 6 files
251262

252263
let mut paths: Vec<String> = paths
@@ -277,7 +288,7 @@ mod tests {
277288
File::create(&file_path)?;
278289

279290
// Get paths using absolute path first to verify it works
280-
let paths = get_paths_of_interest(None, &file_path)?;
291+
let paths = get_paths_of_interest(None, &file_path, None)?;
281292
assert_eq!(paths.len(), 1);
282293
let canonical_path = file_path.canonicalize()?;
283294
assert_eq!(&paths[0], &canonical_path);
@@ -287,7 +298,7 @@ mod tests {
287298

288299
// Get paths using relative path
289300
let relative_path = PathBuf::from("test.txt");
290-
let paths = get_paths_of_interest(None, &relative_path)?;
301+
let paths = get_paths_of_interest(None, &relative_path, None)?;
291302

292303
assert_eq!(paths.len(), 1);
293304
let returned_path = &paths[0];
@@ -298,4 +309,28 @@ mod tests {
298309

299310
Ok(())
300311
}
312+
313+
#[test]
314+
fn test_paths_with_huggingface_cache_ignore() -> anyhow::Result<()> {
315+
let temp_dir = TempDir::new()?;
316+
317+
// Create a .cache/huggingface directory with a file
318+
let cache_dir = temp_dir.path().join(".cache").join("huggingface");
319+
std::fs::create_dir_all(&cache_dir)?;
320+
File::create(cache_dir.join("cached_file.bin"))?;
321+
322+
// Create a regular file
323+
let regular_file = temp_dir.path().join("model.onnx");
324+
File::create(&regular_file)?;
325+
326+
// Get paths with huggingface cache ignore pattern
327+
let paths =
328+
get_paths_of_interest(None, &regular_file, Some(".cache/huggingface".to_string()))?;
329+
330+
// Should only return the regular file
331+
assert_eq!(paths.len(), 1);
332+
assert_eq!(&paths[0], &regular_file.canonicalize()?);
333+
334+
Ok(())
335+
}
301336
}

0 commit comments

Comments
 (0)