1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
use std::fs; use std::io::Write; use std::path::Path; fn main() { let hook_path = Path::new(".git/hooks/pre-commit"); let script = r#"#!/bin/sh echo "🔍 Running rust_checker before commit..." cargo run -- . || { echo " rust_checker validation failed. Commit aborted." exit 1 } "#; if let Some(parent) = hook_path.parent() { fs::create_dir_all(parent).expect("Failed to create hooks directory"); } let mut file = fs::File::create(hook_path).expect("Failed to create pre-commit hook"); file.write_all(script.as_bytes()) .expect("Failed to write pre-commit script"); // Make executable on Unix; no-op on Windows #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; let mut perms = fs::metadata(hook_path) .expect("Failed to read permissions") .permissions(); perms.set_mode(0o755); // executable fs::set_permissions(hook_path, perms).expect("Failed to set executable permissions"); } println!(" Git pre-commit hook installed successfully."); }