Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ fn run_show(args: &[String], max_lines: Option<usize>, 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 {
Expand All @@ -148,7 +152,11 @@ fn run_show(args: &[String], max_lines: Option<usize>, 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(" ")),
Expand Down Expand Up @@ -229,6 +237,11 @@ fn run_show(args: &[String], max_lines: Option<usize>, 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();
Expand Down Expand Up @@ -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";
Expand Down