]> Kevux Git Server - rit/commit
hash: stop depending on `the_repository` in `null_oid()`
authorPatrick Steinhardt <ps@pks.im>
Mon, 10 Mar 2025 07:13:31 +0000 (08:13 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Mar 2025 20:16:20 +0000 (13:16 -0700)
commit7d70b29c4f0b2fd3c6698956d9fb4026632d9c6e
treee089b1c2654d7dcb48ad39c93ef849efc811b45a
parent8ca9fa60a63c97bb9397f5de294aef53df90e7a0
hash: stop depending on `the_repository` in `null_oid()`

The `null_oid()` function returns the object ID that only consists of
zeroes. Naturally, this ID also depends on the hash algorithm used, as
the number of zeroes is different between SHA1 and SHA256. Consequently,
the function returns the hash-algorithm-specific null object ID.

This is currently done by depending on `the_hash_algo`, which implicitly
makes us depend on `the_repository`. Refactor the function to instead
pass in the hash algorithm for which we want to retrieve the null object
ID. Adapt callsites accordingly by passing in `the_repository`, thus
bubbling up the dependency on that global variable by one layer.

There are a couple of trivial exceptions for subsystems that already got
rid of `the_repository`. These subsystems instead use the repository
that is available via the calling context:

  - "builtin/grep.c"
  - "grep.c"
  - "refs/debug.c"

There are also two non-trivial exceptions:

  - "diff-no-index.c": Here we know that we may not have a repository
    initialized at all, so we cannot rely on `the_repository`. Instead,
    we adapt `diff_no_index()` to get a `struct git_hash_algo` as
    parameter. The only caller is located in "builtin/diff.c", where we
    know to call `repo_set_hash_algo()` in case we're running outside of
    a Git repository. Consequently, it is fine to continue passing
    `the_repository->hash_algo` even in this case.

  - "builtin/ls-files.c": There is an in-flight patch series that drops
    `USE_THE_REPOSITORY_VARIABLE` in this file, which causes a semantic
    conflict because we use `null_oid()` in `show_submodule()`. The
    value is passed to `repo_submodule_init()`, which may use the object
    ID to resolve a tree-ish in the superproject from which we want to
    read the submodule config. As such, the object ID should refer to an
    object in the superproject, and consequently we need to use its hash
    algorithm.

    This means that we could in theory just not bother about this edge
    case at all and just use `the_repository` in "diff-no-index.c". But
    doing so would feel misdesigned.

Remove the `USE_THE_REPOSITORY_VARIABLE` preprocessor define in
"hash.c".

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
48 files changed:
archive.c
blame.c
branch.c
builtin/checkout.c
builtin/clone.c
builtin/describe.c
builtin/diff.c
builtin/fast-export.c
builtin/fsck.c
builtin/grep.c
builtin/ls-files.c
builtin/rebase.c
builtin/receive-pack.c
builtin/submodule--helper.c
builtin/unpack-objects.c
builtin/update-ref.c
builtin/worktree.c
combine-diff.c
diff-lib.c
diff-no-index.c
diff.c
diff.h
dir.c
grep.c
hash.c
hash.h
log-tree.c
merge-ort.c
merge-recursive.c
notes-merge.c
notes.c
object-file.c
parse-options-cb.c
range-diff.c
read-cache.c
refs.c
refs/debug.c
refs/files-backend.c
reset.c
sequencer.c
submodule-config.c
submodule.c
t/helper/test-ref-store.c
t/helper/test-submodule-nested-repo-config.c
tree-diff.c
upload-pack.c
wt-status.c
xdiff-interface.c