mbox series

[v2,0/3] sample hooks: become hash agnostic

Message ID cover.1600853895.git.liu.denton@gmail.com (mailing list archive)
Headers show
Series sample hooks: become hash agnostic | expand

Message

Denton Liu Sept. 23, 2020, 9:38 a.m. UTC
There are currently two hooks that have hardcoded 40 zeros as the null
OID and, thus, are not hash-agnostic. Rewrite these to get the zero OID using

	git hash-object --stdin </dev/null | tr '[0-9a-f]' '0'

so that the zero OID is hash-agnostic.

This was initially done by introducing `git rev-parse --null-oid` to get the
zero OID but that seems like overkill. From a cursory search of Github, the
only instances of the zero OID being used come from clones of the git.git
repository (tests and sample hooks). Since we don't want to introduce an option
that no one will use, don't go this route and just do the easiest thing.

If in the future, someone decides that `git rev-parse --zero-oid` is useful,
they can also adjust these hooks accordingly.

Changes since v1:

* Don't implement `git rev-parse --null-oid`

Denton Liu (3):
  hooks--pre-push.sample: modernize script
  hooks--pre-push.sample: use hash-agnostic zero OID
  hooks--update.sample: use hash-agnostic zero OID

 templates/hooks--pre-push.sample | 18 +++++++++---------
 templates/hooks--update.sample   |  2 +-
 2 files changed, 10 insertions(+), 10 deletions(-)

Range-diff against v1:
1:  ed1ade7328 < -:  ---------- hooks--pre-push.sample: prefer $() for command substitution
2:  004f2e4c92 < -:  ---------- builtin/rev-parse: learn --null-oid
3:  9d6c2951ab < -:  ---------- hooks--pre-push.sample: use hash-agnostic null OID
-:  ---------- > 1:  95dd0b19ba hooks--pre-push.sample: modernize script
-:  ---------- > 2:  afb460d9fd hooks--pre-push.sample: use hash-agnostic zero OID
4:  42d2829889 ! 3:  784135549f hooks--update.sample: use hash-agnostic null OID
    @@ Metadata
     Author: Denton Liu <liu.denton@gmail.com>
     
      ## Commit message ##
    -    hooks--update.sample: use hash-agnostic null OID
    +    hooks--update.sample: use hash-agnostic zero OID
     
    -    The update sample hook has the null OID hardcoded as 40 zeros. However,
    +    The update sample hook has the zero OID hardcoded as 40 zeros. However,
         with the introduction of SHA-256 support, this assumption no longer
         holds true. Replace the hardcoded $z40 with a call to
    -    `git rev-parse --null-oid` so the sample hook becomes hash-agnostic.
    +
    +            git hash-object --stdin </dev/null | tr '[0-9a-f]' '0'
    +
    +    so the sample hook becomes hash-agnostic.
     
      ## templates/hooks--update.sample ##
     @@ templates/hooks--update.sample: esac
    @@ templates/hooks--update.sample: esac
      # --- Check types
      # if $newrev is 0000...0000, it's a commit to delete a ref.
     -zero="0000000000000000000000000000000000000000"
    -+zero="$(git rev-list --null-oid)"
    ++zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
      if [ "$newrev" = "$zero" ]; then
      	newrev_type=delete
      else

Comments

Junio C Hamano Sept. 23, 2020, 4:34 p.m. UTC | #1
Denton Liu <liu.denton@gmail.com> writes:

> There are currently two hooks that have hardcoded 40 zeros as the null
> OID and, thus, are not hash-agnostic. Rewrite these to get the zero OID using
>
> 	git hash-object --stdin </dev/null | tr '[0-9a-f]' '0'
>
> so that the zero OID is hash-agnostic.
>
> This was initially done by introducing `git rev-parse --null-oid` to get the
> zero OID but that seems like overkill. From a cursory search of Github, the
> only instances of the zero OID being used come from clones of the git.git
> repository (tests and sample hooks). Since we don't want to introduce an option
> that no one will use, don't go this route and just do the easiest thing.

Patches 2&3/3 look quite sensible.

Patch 1/3 is a borderline Meh, as we do not need to force _our_
coding convention to the end-users, even though we do do so on
ourselves.  But let's take it to make things more consistent.

Thanks.