git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 3/3] read_loose_refs(): treat NULL_SHA1 loose references as broken

From
Stefan Beller <sbeller@google.com>
Date
Jun 2, 2015, 17:28 UTC
Message-ID
<CAGZ79kZG95HB-siR_jMORPuqcL2J_YEwg0VPHE=8XN7gWD4JkA@mail.gmail.com>
In-Reply-To
<1433260647-18181-4-git-send-email-mhagger@alum.mit.edu>
On Tue, Jun 2, 2015 at 8:57 AM, Michael Haggerty <mhagger@alum.mit.edu> wrote:
Show 6 quoted lines
> NULL_SHA1 is never a valid value for a reference. If a loose reference
> has that value, mark it as broken.
>
> Why check NULL_SHA1 and not the nearly 2^160 other SHA-1s that are
> also invalid in a given repository? Because (a) it is cheap to test
> for NULL_SHA1, and (b) NULL_SHA1 is often used as a "SHA-1 is invalid"

I don't quite agree with the reasoning here. Just because it's cheap doesn't mean it's right. ;) But I fully agree with (b) so this still makes sense.

Show 20 quoted lines
> value inside of Git client source code (not only ours!), and
> accidentally writing it to a loose reference file would be an easy
> mistake to make.
>
> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> ---
>  refs.c                         | 7 +++++++
>  t/t6301-for-each-ref-errors.sh | 2 +-
>  2 files changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/refs.c b/refs.c
> index 47e4e53..c28fde1 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -1321,6 +1321,13 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
>                                 hashclr(sha1);
>                                 flag |= REF_ISBROKEN;
>                         }
> +
> +                       if (!(flag & REF_ISBROKEN) && is_null_sha1(sha1)) {
Why do we do the extra check for !(flag & REF_ISBROKEN) here?
> +                               /* NULL_SHA1 is never a valid reference value. ...

... *by our definition*, because we believe it helps detecting errors/mistakes in the future.

*/
> +                               hashclr(sha1);

While this code looks consistent to the rest around, at closer inspection this feels a bit redundant to me. If is_null_sha1(sha1) is true, then hashclr(sha1); doesn't change the state. Or did I miss a subtle side effect?

Show 22 quoted lines
> +                               flag |= REF_ISBROKEN;
> +                       }
> +
>                         if (check_refname_format(refname.buf,
>                                                  REFNAME_ALLOW_ONELEVEL)) {
>                                 hashclr(sha1);
> diff --git a/t/t6301-for-each-ref-errors.sh b/t/t6301-for-each-ref-errors.sh
> index 72d2397..cdb67a0 100755
> --- a/t/t6301-for-each-ref-errors.sh
> +++ b/t/t6301-for-each-ref-errors.sh
> @@ -24,7 +24,7 @@ test_expect_success 'Broken refs are reported correctly' '
>         test_cmp broken-err err
>  '
>
> -test_expect_failure 'NULL_SHA1 refs are reported correctly' '
> +test_expect_success 'NULL_SHA1 refs are reported correctly' '
>         r=refs/heads/zeros &&
>         echo $ZEROS >.git/$r &&
>         test_when_finished "rm -f .git/$r" &&
> --
> 2.1.4
>
Previous: Michael HaggertyNext: Michael Haggerty
Message 5 of 8 in “Fix how for-each-ref handles broken loose references”
  1. 0/3 Fix how for-each-ref handles broken loose referencesMichael Haggerty, Jun 2, 2015
  2. 1/3 t6301: new tests of for-each-ref error handlingMichael Haggerty, Jun 2, 2015
  3. 2/3 for-each-ref: report broken references correctlyMichael Haggerty, Jun 2, 2015
  4. 3/3 read_loose_refs(): treat NULL_SHA1 loose references as brokenMichael Haggerty, Jun 2, 2015
  5. Stefan BellerJun 2, 2015
  6. Michael HaggertyJun 2, 2015
  7. Jeff KingJun 3, 2015
  8. Junio C HamanoJun 2, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.