diff mbox series

[v2] show-index: fix uninitialized hash function

Message ID 20240715102344.182388-1-abhijeet.nkt@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v2] show-index: fix uninitialized hash function | expand

Commit Message

Abhijeet Sonar July 15, 2024, 10:23 a.m. UTC
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@gmail.com>
---
 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

Comments

brian m. carlson July 15, 2024, 10:07 p.m. UTC | #1
On 2024-07-15 at 10:23:43, Abhijeet Sonar wrote:
> 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
> +    )
> +'

I don't think this change is going to work.  If you run with
`GIT_TEST_DEFAULT_HASH=sha256`, as I do, you see this error:

  fatal: attempt to reinitialize repository with different hash

That's because the repository is already initialized as SHA-256 when you
do `git init`.

The reason that the `--object-format` option was added was to make this
configuration work outside a repository.  It would probably be better to
require the user to specify that option if we're outside of a repository
rather than just try to guess.  We want to have _fewer_ dependencies on
SHA-1 as the implicit algorithm, not more.
diff mbox series

Patch

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