]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: propagate errno when reading special refs fails
authorPatrick Steinhardt <ps@pks.im>
Thu, 14 Dec 2023 13:37:02 +0000 (14:37 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 Dec 2023 17:25:26 +0000 (09:25 -0800)
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>
refs.c
t/t1403-show-ref.sh

diff --git a/refs.c b/refs.c
index fcae5dddc6050627668048e966384b7f9fa70171..00e72a2abfe6c718e07d807dad4d18175ab51872 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -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);
index b50ae6fcf115abe384444db1ef0ddbf04ce247d3..66e6e77fa9e71163e32a4b1a5846327bb996c4ba 100755 (executable)
@@ -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