Skip to content

Commit 668cdc0

Browse files
pks-tgitster
authored andcommitted
refs: propagate errno when reading special refs fails
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8f61321 commit 668cdc0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

refs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,8 +1806,10 @@ static int refs_read_special_head(struct ref_store *ref_store,
18061806
int result = -1;
18071807
strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname);
18081808

1809-
if (strbuf_read_file(&content, full_path.buf, 0) < 0)
1809+
if (strbuf_read_file(&content, full_path.buf, 0) < 0) {
1810+
*failure_errno = errno;
18101811
goto done;
1812+
}
18111813

18121814
result = parse_loose_ref_contents(content.buf, oid, referent, type,
18131815
failure_errno);

t/t1403-show-ref.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,14 @@ test_expect_success '--exists with directory fails with generic error' '
266266
test_cmp expect err
267267
'
268268

269+
test_expect_success '--exists with non-existent special ref' '
270+
test_expect_code 2 git show-ref --exists FETCH_HEAD
271+
'
272+
273+
test_expect_success '--exists with existing special ref' '
274+
test_when_finished "rm .git/FETCH_HEAD" &&
275+
git rev-parse HEAD >.git/FETCH_HEAD &&
276+
git show-ref --exists FETCH_HEAD
277+
'
278+
269279
test_done

0 commit comments

Comments
 (0)