diff --git a/src/git.rs b/src/git.rs index 3709e79f..88b53651 100644 --- a/src/git.rs +++ b/src/git.rs @@ -135,7 +135,11 @@ fn run_show(args: &[String], max_lines: Option, verbose: u8) -> Result<() .iter() .any(|arg| arg.starts_with("--pretty") || arg.starts_with("--format")); - if wants_stat_only || wants_format { + // `git show rev:path` prints a blob, not a commit diff. In this mode we should + // pass through directly to avoid duplicated output from compact-show steps. + let wants_blob_show = args.iter().any(|arg| is_blob_show_arg(arg)); + + if wants_stat_only || wants_format || wants_blob_show { let mut cmd = Command::new("git"); cmd.arg("show"); for arg in args { @@ -148,7 +152,11 @@ fn run_show(args: &[String], max_lines: Option, verbose: u8) -> Result<() std::process::exit(output.status.code().unwrap_or(1)); } let stdout = String::from_utf8_lossy(&output.stdout); - println!("{}", stdout.trim()); + if wants_blob_show { + print!("{}", stdout); + } else { + println!("{}", stdout.trim()); + } timer.track( &format!("git show {}", args.join(" ")), @@ -229,6 +237,11 @@ fn run_show(args: &[String], max_lines: Option, verbose: u8) -> Result<() Ok(()) } +fn is_blob_show_arg(arg: &str) -> bool { + // Detect `rev:path` style arguments while ignoring flags like `--pretty=format:...`. + !arg.starts_with('-') && arg.contains(':') +} + pub(crate) fn compact_diff(diff: &str, max_lines: usize) -> String { let mut result = Vec::new(); let mut current_file = String::new(); @@ -1342,6 +1355,15 @@ mod tests { assert!(result.contains("+")); } + #[test] + fn test_is_blob_show_arg() { + assert!(is_blob_show_arg("develop:modules/pairs_backtest.py")); + assert!(is_blob_show_arg("HEAD:src/main.rs")); + assert!(!is_blob_show_arg("--pretty=format:%h")); + assert!(!is_blob_show_arg("--format=short")); + assert!(!is_blob_show_arg("HEAD")); + } + #[test] fn test_filter_branch_output() { let output = "* main\n feature/auth\n fix/bug-123\n remotes/origin/HEAD -> origin/main\n remotes/origin/main\n remotes/origin/feature/auth\n remotes/origin/release/v2\n";