As stated in the docs, show-index should use SHA1 as the default hash algorithm when run outsize of a repository. However, 'the_hash_algo' is currently left uninitialized if we are not in a repository and no explicit hash function is specified, causing a crash. Fix it by falling back to SHA1 when it is found uninitialized. Also add test that verifies this behaviour. Signed-off-by: Abhijeet Sonar <abhijeet.nkt@xxxxxxxxx> --- builtin/show-index.c | 3 +++ t/t8101-show-index-hash-function.sh | 15 +++++++++++++++ 2 files changed, 18 insertions(+) create mode 100755 t/t8101-show-index-hash-function.sh diff --git a/builtin/show-index.c b/builtin/show-index.c index 540dc3dad1..bb6d9e3c40 100644 --- a/builtin/show-index.c +++ b/builtin/show-index.c @@ -35,6 +35,9 @@ int cmd_show_index(int argc, const char **argv, const char *prefix) repo_set_hash_algo(the_repository, hash_algo); } + if (!the_hash_algo) + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); + hashsz = the_hash_algo->rawsz; if (fread(top_index, 2 * 4, 1, stdin) != 1) diff --git a/t/t8101-show-index-hash-function.sh b/t/t8101-show-index-hash-function.sh new file mode 100755 index 0000000000..2e9308f73c --- /dev/null +++ b/t/t8101-show-index-hash-function.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +test_description='git show-index' + +. ./test-lib.sh + +test_expect_success 'show-index: should not fail outside a repository' ' + git init --object-format=sha1 && ( + echo "" | git hash-object -w --stdin | git pack-objects test && + rm -rf .git && + cat test-*.idx | git show-index + ) +' + +test_done -- 2.45.2.827.g557ae147e6