Skip to content

Commit 8d058b8

Browse files
pks-tgitster
authored andcommitted
builtin/hash-object: fix uninitialized hash function
The git-hash-object(1) command allows users to hash an object even without a repository. Starting with c8aed5e (repository: stop setting SHA1 as the default object hash, 2024-05-07), this will make us hit an uninitialized hash function, which subsequently leads to a segfault. Fix this by falling back to SHA-1 explicitly when running outside of a Git repository. Users can use GIT_DEFAULT_HASH environment to specify what hash algorithm they want, so arguably this code should not be needed. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4a1c959 commit 8d058b8

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

builtin/hash-object.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
123123
else
124124
prefix = setup_git_directory_gently(&nongit);
125125

126+
if (nongit && !the_hash_algo)
127+
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
128+
126129
if (vpath && prefix) {
127130
vpath_free = prefix_filename(prefix, vpath);
128131
vpath = vpath_free;

t/t1007-hash-object.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,10 @@ test_expect_success '--literally with extra-long type' '
260260
echo example | git hash-object -t $t --literally --stdin
261261
'
262262

263+
test_expect_success '--stdin outside of repository (uses SHA-1)' '
264+
nongit git hash-object --stdin <hello >actual &&
265+
echo "$(test_oid --hash=sha1 hello)" >expect &&
266+
test_cmp expect actual
267+
'
268+
263269
test_done

t/t1517-outside-repo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_expect_success 'compute a patch-id outside repository (uses SHA-1)' '
2929
test_cmp patch-id.expect patch-id.actual
3030
'
3131

32-
test_expect_failure 'hash-object outside repository (uses SHA-1)' '
32+
test_expect_success 'hash-object outside repository (uses SHA-1)' '
3333
nongit env GIT_DEFAULT_HASH=sha1 \
3434
git hash-object --stdin <sample.patch >hash.expect &&
3535
nongit \

0 commit comments

Comments
 (0)