Skip to content
Merged
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
36 changes: 33 additions & 3 deletions src/cmds/git/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,15 @@ fn run_status(args: &[String], verbose: u8, global_args: &[String]) -> Result<i3
let mut cmd = build_status_command(args, global_args);
let result = exec_capture(&mut cmd).context("Failed to run git status")?;

if !result.stderr.is_empty() && result.stderr.contains("not a git repository") {
let message = "Not a git repository".to_string();
eprintln!("{}", message);
if !result.success() {
let message = if result.stderr.contains("not a git repository") {
"Not a git repository".to_string()
} else {
result.stderr.trim().to_string()
};
if !message.is_empty() {
eprintln!("{}", message);
}
let original_cmd = if args.is_empty() {
"git status".to_string()
} else {
Expand Down Expand Up @@ -1894,6 +1900,30 @@ mod tests {
assert_eq!(cmd_args, vec!["status", "--porcelain", "-uno"]);
}

#[test]
fn test_run_status_compact_propagates_non_repo_failure() {
// #2497: a `git status` failure other than "not a git repository"
// (here: a corrupt index) must propagate a non-zero exit, not be
// flattened into "Clean working tree" + exit 0.
let dir = tempfile::tempdir().expect("tempdir");
let p = dir.path().to_string_lossy().into_owned();
assert!(
Command::new("git")
.args(["-C", &p, "init", "-q"])
.status()
.expect("git init")
.success(),
"git init should succeed"
);
std::fs::write(dir.path().join(".git/index"), "corrupt-index").expect("corrupt index");
let global = vec!["-C".to_string(), p];
let code = run_status(&[], 0, &global).expect("run_status");
assert_ne!(
code, 0,
"corrupt-index git status must not be reported as success"
);
}

#[test]
fn test_compact_diff() {
let diff = r#"diff --git a/foo.rs b/foo.rs
Expand Down
Loading