Skip to content

Commit

Permalink
refs: propagate errno when reading special refs fails
Browse files Browse the repository at this point in the history
Some refs in Git are more special than others due to reasons explained
in the next commit. These refs are read via `refs_read_special_head()`,
but this function doesn't behave the same as when we try to read a
normal ref. Most importantly, we do not propagate `failure_errno` in the
case where the reference does not exist, which is behaviour that we rely
on in many parts of Git.

Fix this bug by propagating errno when `strbuf_read_file()` fails.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pks-t authored and gitster committed Dec 14, 2023
1 parent 8f61321 commit 668cdc0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1806,8 +1806,10 @@ static int refs_read_special_head(struct ref_store *ref_store,
int result = -1;
strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname);

if (strbuf_read_file(&content, full_path.buf, 0) < 0)
if (strbuf_read_file(&content, full_path.buf, 0) < 0) {
*failure_errno = errno;
goto done;
}

result = parse_loose_ref_contents(content.buf, oid, referent, type,
failure_errno);
Expand Down
10 changes: 10 additions & 0 deletions t/t1403-show-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,14 @@ test_expect_success '--exists with directory fails with generic error' '
test_cmp expect err
'

test_expect_success '--exists with non-existent special ref' '
test_expect_code 2 git show-ref --exists FETCH_HEAD
'

test_expect_success '--exists with existing special ref' '
test_when_finished "rm .git/FETCH_HEAD" &&
git rev-parse HEAD >.git/FETCH_HEAD &&
git show-ref --exists FETCH_HEAD
'

test_done

0 comments on commit 668cdc0

Please sign in to comment.