|
| 1 | +#!/usr/bin/env rust-script |
| 2 | +//! ```cargo |
| 3 | +//! [package] |
| 4 | +//! name = "directory-comparison" |
| 5 | +//! version = "0.1.0" |
| 6 | +//! edition = "2021" |
| 7 | +//! description = "A tool to compare directory contents using Merkle trees" |
| 8 | +//! |
| 9 | +//! [dependencies] |
| 10 | +//! merkle_hash = "3.7" |
| 11 | +//! clap = { version = "4.0", features = ["derive", "env"] } |
| 12 | +//! ``` |
| 13 | +
|
| 14 | +use std::collections::HashMap; |
| 15 | +use std::error::Error; |
| 16 | +use std::path::Path; |
| 17 | +use std::process; |
| 18 | +use merkle_hash::{MerkleTree, Encodable}; |
| 19 | +use clap::Parser; |
| 20 | + |
| 21 | +#[derive(Parser)] |
| 22 | +#[command(name = "directory-comparison")] |
| 23 | +#[command(about = "Compare directory contents using Merkle trees")] |
| 24 | +struct Cli { |
| 25 | + /// First directory to compare |
| 26 | + dir1: String, |
| 27 | + |
| 28 | + /// Second directory to compare |
| 29 | + dir2: String, |
| 30 | + |
| 31 | + /// Pattern to ignore during comparison |
| 32 | + #[arg(long, env = "IGNORE_PATTERN")] |
| 33 | + ignore: Option<String>, |
| 34 | +} |
| 35 | + |
| 36 | +fn main() { |
| 37 | + let cli = Cli::parse(); |
| 38 | + |
| 39 | + match directories_match(&cli.dir1, &cli.dir2, cli.ignore.as_deref()) { |
| 40 | + Ok(true) => process::exit(0), |
| 41 | + Ok(false) => process::exit(1), |
| 42 | + Err(e) => { |
| 43 | + eprintln!("Error: {}", e); |
| 44 | + process::exit(1); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fn directories_match(dir1: &str, dir2: &str, ignore: Option<&str>) -> Result<bool, Box<dyn Error>> { |
| 50 | + let tree1 = MerkleTree::builder(dir1).build()?; |
| 51 | + let tree2 = MerkleTree::builder(dir2).build()?; |
| 52 | + |
| 53 | + let files1: HashMap<_, _> = tree1.iter() |
| 54 | + .filter(|i| !should_ignore_file(dir1, i, ignore)) |
| 55 | + .map(|i| (i.path.relative.to_string(), i.hash.to_hex_string())) |
| 56 | + .collect(); |
| 57 | + |
| 58 | + let files2: HashMap<_, _> = tree2.iter() |
| 59 | + .filter(|i| !should_ignore_file(dir2, i, ignore)) |
| 60 | + .map(|i| (i.path.relative.to_string(), i.hash.to_hex_string())) |
| 61 | + .collect(); |
| 62 | + |
| 63 | + Ok(files1 == files2) |
| 64 | +} |
| 65 | + |
| 66 | +fn should_ignore_file(base_dir: &str, item: &merkle_hash::tree::Item, ignore: Option<&str>) -> bool { |
| 67 | + let path_str = item.path.relative.to_string(); |
| 68 | + path_str.is_empty() || |
| 69 | + ignore.map_or(false, |p| path_str.contains(p)) || |
| 70 | + !Path::new(base_dir).join(&path_str).is_file() |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::*; |
| 76 | + use std::fs::{self, File}; |
| 77 | + use std::io::Write; |
| 78 | + use std::path::Path; |
| 79 | + |
| 80 | + fn write_file(dir: &Path, path: &str, content: &str) { |
| 81 | + let file_path = dir.join(path); |
| 82 | + if let Some(parent) = file_path.parent() { |
| 83 | + fs::create_dir_all(parent).unwrap(); |
| 84 | + } |
| 85 | + File::create(file_path).unwrap().write_all(content.as_bytes()).unwrap(); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_identical_files() { |
| 90 | + let temp = std::env::temp_dir(); |
| 91 | + let dir1 = temp.join("test1"); |
| 92 | + let dir2 = temp.join("test2"); |
| 93 | + |
| 94 | + fs::create_dir_all(&dir1).unwrap(); |
| 95 | + fs::create_dir_all(&dir2).unwrap(); |
| 96 | + |
| 97 | + write_file(&dir1, "file.txt", "content"); |
| 98 | + write_file(&dir2, "file.txt", "content"); |
| 99 | + |
| 100 | + assert!(directories_match(dir1.to_str().unwrap(), dir2.to_str().unwrap(), None).unwrap()); |
| 101 | + |
| 102 | + fs::remove_dir_all(&dir1).ok(); |
| 103 | + fs::remove_dir_all(&dir2).ok(); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_different_files() { |
| 108 | + let temp = std::env::temp_dir(); |
| 109 | + let dir1 = temp.join("test3"); |
| 110 | + let dir2 = temp.join("test4"); |
| 111 | + |
| 112 | + fs::create_dir_all(&dir1).unwrap(); |
| 113 | + fs::create_dir_all(&dir2).unwrap(); |
| 114 | + |
| 115 | + write_file(&dir1, "file.txt", "content A"); |
| 116 | + write_file(&dir2, "file.txt", "content B"); |
| 117 | + |
| 118 | + assert!(!directories_match(dir1.to_str().unwrap(), dir2.to_str().unwrap(), None).unwrap()); |
| 119 | + |
| 120 | + fs::remove_dir_all(&dir1).ok(); |
| 121 | + fs::remove_dir_all(&dir2).ok(); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn test_ignore_pattern() { |
| 126 | + let temp = std::env::temp_dir(); |
| 127 | + let dir1 = temp.join("test5"); |
| 128 | + let dir2 = temp.join("test6"); |
| 129 | + |
| 130 | + fs::create_dir_all(&dir1).unwrap(); |
| 131 | + fs::create_dir_all(&dir2).unwrap(); |
| 132 | + |
| 133 | + write_file(&dir1, "keep.txt", "same"); |
| 134 | + write_file(&dir2, "keep.txt", "same"); |
| 135 | + write_file(&dir1, "build-info/ignore.txt", "diff1"); |
| 136 | + write_file(&dir2, "build-info/ignore.txt", "diff2"); |
| 137 | + |
| 138 | + assert!(directories_match(dir1.to_str().unwrap(), dir2.to_str().unwrap(), Some("build-info")).unwrap()); |
| 139 | + |
| 140 | + fs::remove_dir_all(&dir1).ok(); |
| 141 | + fs::remove_dir_all(&dir2).ok(); |
| 142 | + } |
| 143 | +} |
0 commit comments