Skip to content

fix(git): propagate exit code when commit fails instead of reporting ok - #2496

Merged
aeppling merged 3 commits into
rtk-ai:developfrom
hgunduzoglu:fix/git-commit-false-success
Jun 26, 2026
Merged

aeppling merged 3 commits into
rtk-ai:developfrom
hgunduzoglu:fix/git-commit-false-success

Conversation

@hgunduzoglu

Copy link
Copy Markdown
Contributor

Summary

Fixes #2494. rtk git commit reported ok (...) and returned exit 0 when the commit actually failed — same masking-failure class as #1581 (push) and #1535 (stash).

Root cause

run_commit branched like this:

if output.status.success() { /* ok <hash> */ }
else if stderr/stdout contains "nothing to commit" { println!("ok (nothing to commit)"); }  // ← bug
else { /* surface output; return Ok(exit_code) */ }
Ok(0)   // ← the success AND "nothing to commit" branches both land here

The nothing to commit arm is only reachable when output.status.success() is false (git exits 1 when there's nothing staged), yet it printed ok and fell through to Ok(0). A pre-commit hook abort hit the same masking.

Fix

Gate strictly on output.status.success() via a small CommitOutcome helper. Success keeps the compact ok <hash> line; any failure surfaces git's own message and propagates its real exit code (with a FAILED: git commit header, matching run_add/run_pull/run_stash). No success path can return non-zero, no failure path can return 0.

# before
$ rtk git commit -m x      # nothing staged
ok (nothing to commit)      # exit 0  ✗

# after
$ rtk git commit -m x
FAILED: git commit
On branch main
nothing to commit, working tree clean
                            # exit 1  ✓  (matches native git)

Testing

  • TDD: 4 unit tests on the extracted classify_commit_outcome (success→ok <hash>, empty stdout→ok, nothing-to-commit→Failed(1), hook-abort→Failed(2)).
  • cargo fmt --all --check && cargo clippy --all-targets && cargo test — clean, 2203 passed.
  • Manual verification against native git for all three paths:
    • nothing staged → FAILED + exit 1 (was exit 0)
    • real commit → ok <hash> + exit 0 (no regression)
    • pre-commit hook abort → FAILED + exit 1

No output-format change on the success path, so token savings are unaffected.

run_commit had a branch that printed 'ok (nothing to commit)' and fell
through to Ok(0) whenever git exited non-zero with a 'nothing to commit'
message — so a no-op or hook-aborted commit was reported as success with
exit 0. Gate strictly on output.status.success() via a CommitOutcome
helper: any failure surfaces git's message and propagates the real exit
code, matching native git. Same bug class as rtk-ai#1581 (push) and rtk-ai#1535
(stash).

Fixes rtk-ai#2494
@aeppling

Copy link
Copy Markdown
Contributor

Otherwise LGTM :) Thanks for your contribution to make git filter more transparent and faithful

@aeppling aeppling mentioned this pull request Jun 26, 2026
Comment thread src/cmds/git/git.rs Outdated
if !stdout.trim().is_empty() {
eprint!("{}", stdout);
CommitOutcome::Failed(code) => {
eprintln!("FAILED: git commit");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop custom error messages, should return the git error message

Comment thread src/cmds/git/git.rs Outdated
enum CommitOutcome {
/// Commit succeeded; carries the compact one-line summary for stdout.
Ok(String),
/// Commit failed; carries git's real exit code to propagate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean up code comments, most of code is explicit enough

@aeppling
aeppling merged commit 2927248 into rtk-ai:develop Jun 26, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk git commit reports ok / exit 0 when the commit fails (nothing staged, or pre-commit hook abort)

2 participants