Ray's Knowledge Base

rgit stage --hunk reports success for a hunk it did not stage

PitfallVerified 28 Sep 2026Holds project: rgit, until: 5d47390
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

rgit stage <file> --hunk N, with an N that matches no hunk, prints staged part of <file> and exits 0, but the index does not change. A commit made next goes in without that hunk. In the first case git diff placed the hunk at 1021 and rgit's own diff at 1022.

Cause#

--hunk looks each N up among rgit's own hunk start lines. stage_hunk applied the diff with a libgit2 hunk callback that matched no hunk, so repo.apply wrote nothing and returned success. unstage, discard and --lines already failed on a missing hunk, but their error did not list the valid start lines.

Fix#

Fixed in rgit 5d47390. stage, unstage, discard and --lines now fail with exit 1 when N matches no hunk, stage nothing, and list the start lines:

error: "no hunk at line 99 of f.txt; hunks start at: 1, 22"
help: Run `rgit diff --patch` to see the current hunks

On an older build, check the index with git diff --cached --stat before you commit. Take hunk numbers from rgit diff --patch, not from git diff.

Evidence#

  • rkb repository, 2026-09-27: the matching commit was first made without its test after --hunk 1021, then amended with rgit's number.
  • Scratch repository with rgit built at 3699e76, 2026-09-27: rgit stage f.txt --hunk 99 on a file whose only hunk starts at line 1 printed staged part of f.txt, exited 0, and git diff --cached --name-only did not list f.txt.
  • rgit 5d47390, 2026-09-27: the same command printed the error above and exited 1. The test a_line_that_starts_no_hunk_is_an_error in crates/rgit-cli/tests/hunks.rs covers it.