Skip to contentSkip to conversation

show-index: fix uninitialized hash function

PATCH Superseded Abhijeet Sonar 6 participants 27 messages Jul 12, 2024 – Dec 16, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 show-index: fix uninitialized hash function

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 funciton is specified, causing a crash. Fix it by falling back to SHA1 when it is found uninitialized.

---
 builtin/show-index.c | 3 +++
 1 file changed, 3 insertions(+)
builtin/show-index.c +3 -0
index 540dc3dad1..bb6d9e3c40 100644
@@ -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)
Signature · 1 line
2.45.2.827.g557ae147e6
#1 lore raw
Quoted 10 lines · Abhijeet Sonar

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 funciton is specified, causing a crash. Fix it by falling back to SHA1 when it is found uninitialized.

---
 builtin/show-index.c | 3 +++
 1 file changed, 3 insertions(+)

Nicely described.

We'd probably want to protect this with a new test, so that regardless of the choice of GIT_TEST_DEFAULT_HASH, the command should behave as advertised.

Having said that, I am not sure if --object-format specified on the command line, or picked up from the repository, makes much sense in the context of the command, especially for the longer term [*]. The command is designed to read from its standard input a byte-stream, which is assumed to be an .idx file of _any_ origin, so ideally it should be able to tell what hash the incoming data uses and use that hash algorithm, without being told from the command line?

But that longer-term worry has nothing to do with the validity of this patch (but the lack of test does). Thanks.

[Footnote]

* Perhaps the file format does not make it obvious what hash algorithm it uses, so it may be hard to auto-detect without additional code. But if that is the case, it would be something we may want to eventually fix.

#2 lore raw

PATCH v2 1/1 show-index: fix uninitialized hash function

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.

---
 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
builtin/show-index.c +3 -0
index 540dc3dad1..bb6d9e3c40 100644
@@ -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)
t/t8101-show-index-hash-function.sh new file +15 -0
new file mode 100755
index 0000000000..2e9308f73c
@@ -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
Signature · 1 line
2.45.2.827.g557ae147e6
#3 lore raw

Re* [PATCH v2] show-index: fix uninitialized hash function

Quoted 3 lines · Abhijeet Sonar
 t/t8101-show-index-hash-function.sh | 15 +++++++++++++++
 2 files changed, 18 insertions(+)
 create mode 100755 t/t8101-show-index-hash-function.sh

Thanks. But let's not waste the scarce resource that is a test number for a single oddball test (and as t/README says t8xxx series is for forensics Porcelains).

Review

Quoted 3 lines
(quoted diff) +3 -0
+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 &&

Our tests run in an already initialized repository, and some test configuration would use sha256 to initialize that repository. So the above is not a good idea, unless you use a new directory. We often create a new directory inside the initial directory the test begins in, and then in a subshell chdir into the directory.

We frown upon a pipeline that has "git" as an upstream, because the exit status from such invocation of "git" will be hidden.

	git init --object-format=sha1 sample &&
	(
		cd sample &&
		O=$(git hash-object -w /dev/null) &&
		T=$(echo "$O" | git pack-objects test) &&

would give you a pair of files "test-$T.idx" and "test-$T.pack" and it will notice if hash-object or pack-objects fail.

Quoted 1 line

+ rm -rf .git &&

This alone does *not* necessarily make the directory you are using for test completely unassociated with any repository. If you are working with the source code of Git from a repository (as opposed to extracted tar archive), with that "rm -fr", you may have made the directory not a Git repository, but then that directory is now a mere subdirectory "t/trash directory.t8101-show-index-hash-function" of the repository that houses the Git source code (unless you are using the --root=<directory> option to run the tests).

When we test behaviour of commands outside a repository, we use the GIT_CEILING_DIRECTORIES feature, often via the nongit helper function that is defined in t/test-lib-functions.sh (which becomes available to tests by doing ". ./test-lib.sh".

In t5300-pack-object.sh we see these bits already.

    test_expect_success 'index-pack --stdin complains of non-repo' '
            nongit test_must_fail git index-pack \
                    --object-format=$(test_oid algo) --stdin <foo.pack &&
            test_path_is_missing non-repo/.git
    '

    test_expect_success 'index-pack <pack> works in non-repo' '
            nongit git index-pack \
                    --object-format=$(test_oid algo) ../foo.pack &&
            test_path_is_file foo.idx
    '

I wonder if it is sufficient to add a new test after these two steps, something like

test_expect_success SHA1 'show-index works OK outside a repository' '
nongit git show-index <foo.idx
'

perhaps?

With that, your patch would become like so:

------------ >8 ----------------------- >8 ------------
From: Abhijeet Sonar <[email protected]>
Date: Mon, 15 Jul 2024 15:53:43 +0530
Subject: [PATCH] show-index: fix uninitialized hash function

As stated in the docs, show-index should use SHA1 as the default hash algorithm when run outside a repository.

However, 'the_hash_algo' is 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.

[jc: fixed up the test]
---
 builtin/show-index.c   | 3 +++
 t/t5300-pack-object.sh | 4 ++++
 2 files changed, 7 insertions(+)
builtin/show-index.c +3 -0
index 540dc3dad1..bb6d9e3c40 100644
@@ -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)
t/t5300-pack-object.sh +4 -0
index 4ad023c846..83933eca5d 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
Signature · 1 line
2.46.0-rc0-140-g824782812f
#4 lore raw

PATCH v3 1/1 show-index: fix uninitialized hash function

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.

---
 builtin/show-index.c   | 3 +++
 t/t5300-pack-object.sh | 4 ++++
 2 files changed, 7 insertions(+)
builtin/show-index.c +3 -0
index f164c01bbe..978ae70470 100644
@@ -38,6 +38,9 @@ int cmd_show_index(int argc,
 		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)
t/t5300-pack-object.sh +4 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
Signature · 1 line
2.47.0.107.g34b6ce9b30
#5 lore raw
Quoted 5 lines · Abhijeet Sonar

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.

This commit description is good, and would benefit further from a bisection showing where the regression began. I don't think that it is a prerequisite for us moving this patch forward, though.

Quoted 36 lines
---
 builtin/show-index.c   | 3 +++
 t/t5300-pack-object.sh | 4 ++++
 2 files changed, 7 insertions(+)
builtin/show-index.c +3 -0
index f164c01bbe..978ae70470 100644
@@ -38,6 +38,9 @@ int cmd_show_index(int argc,
 		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)
t/t5300-pack-object.sh +4 -1
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
--
Quoted 1 line

2.47.0.107.g34b6ce9b30

These all look reasonable and as-expected to me. Patrick (CC'd) has been reviewing similar changes elsewhere, so I'd like him to chime in as well on whether or not this looks good to go.

Thanks,
Taylor

#6 lore raw
Quoted 11 lines · Taylor Blau

builtin/show-index.c int cmd_show_index(int argc, · line 42

Quoted 10 lines · Abhijeet Sonar
builtin/show-index.c +2 -0
index f164c01bbe..978ae70470 100644
@@ -38,6 +38,9 @@ int cmd_show_index(int argc,
 		repo_set_hash_algo(the_repository, hash_algo);
 	}
 
+	if (!the_hash_algo)
+		repo_set_hash_algo(the_repository, GIT_HASH_SHA1);

Let's add a todo-comment here. The behaviour with this patch is somewhat broken as you cannot inspect indices that use any other object hash than SHA256 outside of a repository. This is fine from my point of view and nothing that you have to fix here, as you simply fix up the broken behaviour. But in the future, we should either:

- Add logic to detect the format of the passed-in index and set that up as the hash algorithm.

- If that is impossible, add a command line option to pick the hash algo.

Quoted 11 lines
Quoted 11 lines
builtin/show-index.c +0 -0
 	hashsz = the_hash_algo->rawsz;
 
 	if (fread(top_index, 2 * 4, 1, stdin) != 1)
t/t5300-pack-object.sh +0 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
Quoted 3 lines

t/t5300-pack-object.sh test_expect_success 'index-pack --strict <pack> works in non-repo' ' · line 528

Quoted 3 lines
t/t5300-pack-object.sh +3 -0
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'

So how does this behave with SHA256? Does it raise an error? Does it segfault?

I think it's okay to fail with SHA256 for now, but I'd like the failure behaviour to be cleanish. So I'd prefer to not skip the test completely, but adapt our expectations based on the hash algo. Or have two separate tests, one for each hash, that explicitly init the repo with git init --ref-format=$hash, and then exercise the behaviour for each of them.

Quoted 9 lines
Quoted 5 lines
t/t5300-pack-object.sh +0 -1
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
--

2.47.0.107.g34b6ce9b30

These all look reasonable and as-expected to me. Patrick (CC'd) has been reviewing similar changes elsewhere, so I'd like him to chime in as well on whether or not this looks good to go.

Ah, thanks. I've missed this topic somehow.

Patrick

#7 lore raw
Quoted 11 lines · Patrick Steinhardt
Quoted 9 lines
Quoted 5 lines

test_expect_success !PTHREADS,!FAIL_PREREQS \

t/t5300-pack-object.sh +0 -1
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
--

2.47.0.107.g34b6ce9b30

These all look reasonable and as-expected to me. Patrick (CC'd) has been reviewing similar changes elsewhere, so I'd like him to chime in as well on whether or not this looks good to go.

Ah, thanks. I've missed this topic somehow.

Not a problem at all. Thanks for a very helpful review.

Abhijeet: I've gone ahead and marked this in my notes as "expecting another round" to address the feedback from Patrick. I'll keep my eyes out for the new version of this patch. Thanks!

Thanks,
Taylor

#8 lore raw

PATCH v4 1/1 show-index: fix uninitialized hash function

In c8aed5e8da (repository: stop setting SHA1 as the default object hash), we got rid of the default hash algorithm for the_repository. Due to this change, it is now the responsibility of the callers to set thier own default when this is not present.

As stated in the docs, show-index should use SHA1 as the default hash algorithm when ran outsize of a repository. Make sure this promise is met by falling back to SHA1 when the_hash_algo is not present (i.e. when the command is ran outside of a repository). Also add a test that verifies this behaviour.

---
 builtin/show-index.c   | 6 ++++++
 rm                     | 3 +++
 t/t5300-pack-object.sh | 4 ++++
 3 files changed, 13 insertions(+)
 create mode 100755 rm
builtin/show-index.c +6 -0
index f164c01bbe..645c2548fb 100644
@@ -38,6 +38,12 @@ int cmd_show_index(int argc,
 		repo_set_hash_algo(the_repository, hash_algo);
 	}
 
+	// Fallback to SHA1 if we are running outside of a repository.
+	// TODO: Figure out and implement a way to detect the hash algorithm in use by the
+	//       the index file passed in and use that instead.
+	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)
rm new file +3 -0
new file mode 100755
index 0000000000..2237506bf2
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+echo rm $@
t/t5300-pack-object.sh +4 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
Signature · 1 line
2.47.0.107.g34b6ce9b30
#9 lore raw
Quoted 4 lines · Abhijeet Sonar

In c8aed5e8da (repository: stop setting SHA1 as the default object hash), we got rid of the default hash algorithm for the_repository. Due to this change, it is now the responsibility of the callers to set thier own default when this is not present.

"their own default".

Quoted 2 lines

As stated in the docs, show-index should use SHA1 as the default hash algorithm when ran outsize of a repository. Make sure this promise is

"outside a repository".

Quoted 8 lines

met by falling back to SHA1 when the_hash_algo is not present (i.e. when the command is ran outside of a repository). Also add a test that verifies this behaviour.

---
 builtin/show-index.c   | 6 ++++++
 rm                     | 3 +++

Huh?

Quoted 9 lines
 t/t5300-pack-object.sh | 4 ++++
 3 files changed, 13 insertions(+)
 create mode 100755 rm
builtin/show-index.c +0 -0
index f164c01bbe..645c2548fb 100644

builtin/show-index.c int cmd_show_index(int argc, · line 43

Quoted 6 lines
builtin/show-index.c +3 -0
@@ -38,6 +38,12 @@ int cmd_show_index(int argc,
 		repo_set_hash_algo(the_repository, hash_algo);
 	}
 
+	// Fallback to SHA1 if we are running outside of a repository.
+	// TODO: Figure out and implement a way to detect the hash algorithm in use by the
+	//       the index file passed in and use that instead.

/*
* A multi-line comment in our codebase looks
* like this; slash-asterisk and asterisk-slash
* are placed on their own lines. We do not do
* double-slash comments.
*/

builtin/show-index.c int cmd_show_index(int argc, · line 45

Quoted 2 lines
builtin/show-index.c +2 -0
+	if (!the_hash_algo)
+		repo_set_hash_algo(the_repository, GIT_HASH_SHA1);

OK. This is in line with how the command is documented to behave.

Having said that, I am not sure if it was an omission by mistake when 8e42eb0e (doc: sha256 is no longer experimental, 2023-07-31) marked SHA-256 as non-experimental, or it was deliberate. It would have been an equally plausible, if not more sensible, position to take to say that, since SHA-1 and SHA-256 are now on equal footing, we won't "default" to SHA-1 anymore, when 8e42eb0e declared that SHA-256 is no longer a second-class citizen.

In any case, we can further remedy that, if we really wanted to, by tweaking the documentation to require the option outside a repository without any default, for example, and then change this to die().

Of course, we may want to use the hash that is used in the index file we are reading, if we can, as your comment said.

These incremental improvements can be left outside the scope of this change.

rm line 3

Quoted 9 lines
rm new file +3 -0
new file mode 100755
index 0000000000..2237506bf2
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+echo rm $@

Please don't.

t/t5300-pack-object.sh test_expect_success 'index-pack --strict <pack> works in non-repo' ' · line 528

Quoted 11 lines
t/t5300-pack-object.sh +3 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'

If we are not using a hash that is not SHA-1, we should then be able to do the same check with

    nongit git show-index --object-format=<hash> <foo.idx

i.e., with an explicit argument. I do not think we have any hits in the t/ directory from

    $ git grep -e 'show-index .*--object-format' t/

so such a test might be worth adding, either as a part of this change or as a separate patch.

t/t5300-pack-object.sh test_expect_success 'index-pack --strict <pack> works in non-repo' ' · line 532

Quoted 3 lines
t/t5300-pack-object.sh +0 -0
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&

Except for these minor nits, everything else looks great.

Thanks.

#10 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Abhijeet Sonar

In c8aed5e8da (repository: stop setting SHA1 as the default object hash), we got rid of the default hash algorithm for the_repository. Due to this change, it is now the responsibility of the callers to set thier own default when this is not present.

Quoted 7 lines

"their own default".

Quoted 2 lines

As stated in the docs, show-index should use SHA1 as the default hash algorithm when ran outsize of a repository. Make sure this promise is

"outside a repository".

I will address those in v5, thanks

Quoted 51 lines
Quoted 8 lines

met by falling back to SHA1 when the_hash_algo is not present (i.e. when the command is ran outside of a repository). Also add a test that verifies this behaviour.

---
 builtin/show-index.c   | 6 ++++++
 rm                     | 3 +++

Huh?

Quoted 9 lines
 t/t5300-pack-object.sh | 4 ++++
 3 files changed, 13 insertions(+)
 create mode 100755 rm
builtin/show-index.c +0 -0
index f164c01bbe..645c2548fb 100644

builtin/show-index.c int cmd_show_index(int argc, · line 43

Quoted 6 lines
builtin/show-index.c +3 -0
@@ -38,6 +38,12 @@ int cmd_show_index(int argc,
 		repo_set_hash_algo(the_repository, hash_algo);
 	}
 
+	// Fallback to SHA1 if we are running outside of a repository.
+	// TODO: Figure out and implement a way to detect the hash algorithm in use by the
+	//       the index file passed in and use that instead.

/*
* A multi-line comment in our codebase looks
* like this; slash-asterisk and asterisk-slash
* are placed on their own lines. We do not do
* double-slash comments.
*/

builtin/show-index.c int cmd_show_index(int argc, · line 45

Quoted 2 lines
builtin/show-index.c +2 -0
+	if (!the_hash_algo)
+		repo_set_hash_algo(the_repository, GIT_HASH_SHA1);

OK. This is in line with how the command is documented to behave.

Having said that, I am not sure if it was an omission by mistake when 8e42eb0e (doc: sha256 is no longer experimental, 2023-07-31) marked SHA-256 as non-experimental, or it was deliberate. It would have been an equally plausible, if not more sensible, position to take to say that, since SHA-1 and SHA-256 are now on equal footing, we won't "default" to SHA-1 anymore, when 8e42eb0e declared that SHA-256 is no longer a second-class citizen.>
In any case, we can further remedy that, if we really wanted to, by tweaking the documentation to require the option outside a repository without any default, for example, and then change this to die().

Quoted 6 lines

Of course, we may want to use the hash that is used in the index file we are reading, if we can, as your comment said.

These incremental improvements can be left outside the scope of this change.

I see. So while this behavior not completely ideal, we are at least able to resolve a segfault. I take it that it is OK to leave it like this in this patch and address it separately after.

Quoted 12 lines

rm line 3

Quoted 9 lines
rm new file +3 -0
new file mode 100755
index 0000000000..2237506bf2
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+echo rm $@

Please don't.

Oops, this is embarrassing, that probably slipped in from a different thing I was experimenting with which is unrelated to this patch. I will verify that my patches are free of such errors in future before sending them, apologies.

Quoted 15 lines

t/t5300-pack-object.sh test_expect_success 'index-pack --strict <pack> works in non-repo' ' · line 528

Quoted 11 lines
t/t5300-pack-object.sh +3 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'

If we are not using a hash that is not SHA-1, we should then be able to do the same check with

Quoted 7 lines
    nongit git show-index --object-format=<hash> <foo.idx

i.e., with an explicit argument. I do not think we have any hits in the t/ directory from

    $ git grep -e 'show-index .*--object-format' t/

Would that look something like this?

```
diff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh
index 51fed26cc4..78047604e4 100755
--- a/t/t5300-pack-object.sh
+++ b/t/t5300-pack-object.sh
@@ -527,6 +527,22 @@ test_expect_success SHA1 'show-index works OK
outside a repository' '
        nongit git show-index <foo.idx
 '

+for hash in sha1 sha256
+do
+       test_expect_success 'show-index works OK outside a repository
with hash algo passed in via --object-format' '
+               git init --object-format=$hash $hash-repo &&
+               echo foo >$hash-repo/foo &&
+               git -C $hash-repo add foo &&
+               git -C $hash-repo commit -m "commit foo" &&
+               oid=$(git -C $hash-repo rev-parse HEAD) &&
+               echo $oid | git -C $hash-repo pack-objects $hash &&
+               mv $hash-repo/$hash-*.idx $hash.idx &&
+               nongit git show-index --object-format=$hash <$hash.idx &&
+               wow &&
+               rm -fr $hash/ $hash.idx
+       '
+done
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
        'index-pack --threads=N or pack.threads=N warns when no pthreads' '
        test_must_fail git index-pack --threads=2 2>err &&
```
Quoted 11 lines

so such a test might be worth adding, either as a part of this change or as a separate patch.

t/t5300-pack-object.sh test_expect_success 'index-pack --strict <pack> works in non-repo' ' · line 532

Quoted 3 lines
t/t5300-pack-object.sh +0 -0
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&

Except for these minor nits, everything else looks great.

Thanks.

#11 lore raw

v5 cover show-index: fix uninitialized hash function

In reply to Junio C Hamano

In this iteration, I have fixed some typos along with a stylistisc issue that were noted in v4.

I have also added an additional patch that adds a test for --object-format option in show-index as it was noted we don't already have any.

Abhijeet Sonar (2):
show-index: fix uninitialized hash function
t5300: add test for 'show-index --object-format'

 builtin/show-index.c   |  9 +++++++++
 t/t5300-pack-object.sh | 26 ++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

Range-diff against v4:
1: c75175ad9b ! 1: 05ee1e2ea5 show-index: fix uninitialized hash function
@@ Commit message
In c8aed5e8da (repository: stop setting SHA1 as the default object hash), we got rid of the default hash algorithm for the_repository. Due to this change, it is now the responsibility of the callers to set
- thier own default when this is not present.
+ their own default when this is not present.

As stated in the docs, show-index should use SHA1 as the default hash
- algorithm when ran outsize of a repository. Make sure this promise is
+ algorithm when run outside a repository. Make sure this promise is
met by falling back to SHA1 when the_hash_algo is not present (i.e.
- when the command is ran outside of a repository). Also add a test that
- verifies this behaviour.
+ when the command is run outside a repository). Also add a test that
+ verifies this behavior.

@@ builtin/show-index.c: int cmd_show_index(int argc,
repo_set_hash_algo(the_repository, hash_algo);
}

-+ // Fallback to SHA1 if we are running outside of a repository.
-+ // TODO: Figure out and implement a way to detect the hash algorithm in use by the
-+ // the index file passed in and use that instead.
++ /*
++ * Fallback to SHA1 if we are running outside of a repository.
++ *
++ * TODO: Figure out and implement a way to detect the hash algorithm in use by the
++ * the index file passed in and use that instead.
++ */
+ if (!the_hash_algo)
+ repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+
@@ builtin/show-index.c: int cmd_show_index(int argc,

if (fread(top_index, 2 * 4, 1, stdin) != 1)

- ## rm (new) ##
-@@
-+#!/bin/sh
-+
-+echo rm $@
-
## t/t5300-pack-object.sh ##
@@ t/t5300-pack-object.sh: test_expect_success 'index-pack --strict <pack> works in non-repo' '
test_path_is_file foo.idx
-: ---------- > 2: c8a28aae55 t5300: add test for 'show-index --object-format'

Signature · 1 line
2.47.0.107.g34b6ce9b30
#12 lore raw

PATCH v5 1/2 show-index: fix uninitialized hash function

In c8aed5e8da (repository: stop setting SHA1 as the default object hash), we got rid of the default hash algorithm for the_repository. Due to this change, it is now the responsibility of the callers to set their own default when this is not present.

As stated in the docs, show-index should use SHA1 as the default hash algorithm when run outside a repository. Make sure this promise is met by falling back to SHA1 when the_hash_algo is not present (i.e. when the command is run outside a repository). Also add a test that verifies this behavior.

---
 builtin/show-index.c   | 9 +++++++++
 t/t5300-pack-object.sh | 4 ++++
 2 files changed, 13 insertions(+)
builtin/show-index.c +9 -0
index f164c01bbe..b5e337869d 100644
@@ -38,6 +38,15 @@ int cmd_show_index(int argc,
 		repo_set_hash_algo(the_repository, hash_algo);
 	}
 
+	/*
+	 * Fallback to SHA1 if we are running outside of a repository.
+	 *
+	 * TODO: Figure out and implement a way to detect the hash algorithm in use by the
+	 *       the index file passed in and use that instead.
+	 */
+	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)
t/t5300-pack-object.sh +4 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
Signature · 1 line
2.47.0.107.g34b6ce9b30
#13 lore raw

PATCH v5 2/2 t5300: add test for 'show-index --object-format'

In reply to Abhijeet Sonar

In 88a09a557c (builtin/show-index: provide options to determine hash algo), the flag --object-format was added to show-index builtin as a way to provide a hash algorithm explicitly. However, we do not have tests in place for that functionality. Add them.

---
 t/t5300-pack-object.sh | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
t/t5300-pack-object.sh +22 -0
index 51fed26cc4..301d5f1b61 100755
@@ -527,6 +527,28 @@ test_expect_success SHA1 'show-index works OK outside a repository' '
 	nongit git show-index <foo.idx
 '
 
+for hash in sha1 sha256
+do
+	test_expect_success 'setup: show-index works OK outside a repository with hash algo passed in via --object-format' '
+		git init explicit-hash-$hash --object-format=$hash &&
+		test_commit -C explicit-hash-$hash one &&
+
+		cat >in <<-EOF &&
+		$(git -C explicit-hash-$hash rev-parse one)
+		EOF
+
+		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in
+	'
+
+	test_expect_success 'show-index works OK outside a repository with hash algo passed in via --object-format' '
+		idx=$(echo explicit-hash-$hash/explicit-hash-$hash*.idx) &&
+		nongit git show-index --object-format=$hash <"$idx" >actual &&
+		test_line_count = 1 actual &&
+
+		rm -rf explicit-hash-$hash
+	'
+done
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
Signature · 1 line
2.47.0.107.g34b6ce9b30
#14 lore raw

Re: [PATCH v5 2/2] t5300: add test for 'show-index --object-format'

Quoted 9 lines · Abhijeet Sonar

In 88a09a557c (builtin/show-index: provide options to determine hash algo), the flag --object-format was added to show-index builtin as a way to provide a hash algorithm explicitly. However, we do not have tests in place for that functionality. Add them.

---
 t/t5300-pack-object.sh | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

Nicely described.

t/t5300-pack-object.sh test_expect_success SHA1 'show-index works OK outside a repository' ' · line 533

Quoted 12 lines
t/t5300-pack-object.sh +4 -0
index 51fed26cc4..301d5f1b61 100755
@@ -527,6 +527,28 @@ test_expect_success SHA1 'show-index works OK outside a repository' '
 	nongit git show-index <foo.idx
 '
 
+for hash in sha1 sha256
+do
+	test_expect_success 'setup: show-index works OK outside a repository with hash algo passed in via --object-format' '
+		git init explicit-hash-$hash --object-format=$hash &&

"git help cli"; dashed options first and then other arguments.

t/t5300-pack-object.sh test_expect_success SHA1 'show-index works OK outside a repository' ' · line 538

Quoted 5 lines
t/t5300-pack-object.sh +5 -0
+		test_commit -C explicit-hash-$hash one &&
+
+		cat >in <<-EOF &&
+		$(git -C explicit-hash-$hash rev-parse one)
+		EOF

Hmph, is the above a roundabout way to say

		git -C explicit-hash-$hash rev-parse one >in &&

or am I missing some subtlety?

t/t5300-pack-object.sh test_expect_success SHA1 'show-index works OK outside a repository' ' · line 548

Quoted 10 lines
t/t5300-pack-object.sh +9 -0
+		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in
 
+	'
+
+	test_expect_success 'show-index works OK outside a repository with hash algo passed in via --object-format' '
+		idx=$(echo explicit-hash-$hash/explicit-hash-$hash*.idx) &&
+		nongit git show-index --object-format=$hash <"$idx" >actual &&
+		test_line_count = 1 actual &&
+
+		rm -rf explicit-hash-$hash

When this test fails (e.g., the number of lines in the show-index output is not 1), explicit-hash-$hash is not removed, because &&- chain short-circuits.

Perhaps join thw two into one and use test_when_finished, like this?

	test_expect_success 'show-index with explicit --object-format=$hash outside repo' '
		test_when_finished "rm -fr explicit-hash-$hash" &&
		git init --object-format=$hash explicit-hash-$hash &&
		...
                nongit git show-index --object-format=$hash <"$idx" >actual &&
		test_line_count 1 actual
	'

Other than that, very nicely done.

Thanks.

author Applied
#15 lore raw

v6 cover show-index: fix uninitialized hash function

Quoted 1 line

Nicely described.

Thanks!

Quoted 1 line

"git help cli"; dashed options first and then other arguments.

Applied.

Quoted 2 lines

Hmph, is the above a roundabout way to say
git -C explicit-hash-$hash rev-parse one >in &&

Applied.

Quoted 1 line

or am I missing some subtlety?

No, I don't think you are. However, I would like to point out that the code which I used as the inspiration also does thing the same way:

	test_expect_success 'pack-object <stdin parsing: [|--revs] with --stdin' '
		cat >in <<-EOF &&
		$(git -C pack-object-stdin rev-parse one)
		$(git -C pack-object-stdin rev-parse two)
		EOF
Quoted 6 lines

When this test fails (e.g., the number of lines in the show-index output is not 1), explicit-hash-$hash is not removed, because &&- chain short-circuits.

Perhaps join thw two into one and use test_when_finished, like this?

Quoted 7 lines
	test_expect_success 'show-index with explicit --object-format=$hash outside repo' '
		test_when_finished "rm -fr explicit-hash-$hash" &&
		git init --object-format=$hash explicit-hash-$hash &&
		...
                nongit git show-index --object-format=$hash <"$idx" >actual &&
		test_line_count 1 actual
	'

That makes sense, applied.

Abhijeet Sonar (2):
show-index: fix uninitialized hash function
t5300: add test for 'show-index --object-format'

 builtin/show-index.c   |  9 +++++++++
 t/t5300-pack-object.sh | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)

Range-diff against v5:

1:  05ee1e2ea5 = 1:  05ee1e2ea5 show-index: fix uninitialized hash function
2:  c8a28aae55 ! 2:  778f3ca18e t5300: add test for 'show-index --object-format'
    @@ t/t5300-pack-object.sh: test_expect_success SHA1 'show-index works OK outside a
      
     +for hash in sha1 sha256
     +do
    -+	test_expect_success 'setup: show-index works OK outside a repository with hash algo passed in via --object-format' '
    -+		git init explicit-hash-$hash --object-format=$hash &&
    -+		test_commit -C explicit-hash-$hash one &&
    -+
    -+		cat >in <<-EOF &&
    -+		$(git -C explicit-hash-$hash rev-parse one)
    -+		EOF
    -+
    -+		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in
    -+	'
    -+
     +	test_expect_success 'show-index works OK outside a repository with hash algo passed in via --object-format' '
    ++		test_when_finished "rm -rf explicit-hash-$hash" &&
    ++		git init --object-format=$hash explicit-hash-$hash &&
    ++		test_commit -C explicit-hash-$hash one &&
    ++		git -C explicit-hash-$hash rev-parse one >in &&
    ++		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in &&
     +		idx=$(echo explicit-hash-$hash/explicit-hash-$hash*.idx) &&
     +		nongit git show-index --object-format=$hash <"$idx" >actual &&
    -+		test_line_count = 1 actual &&
    -+
    -+		rm -rf explicit-hash-$hash
    ++		test_line_count = 1 actual
     +	'
     +done
     +
Signature · 1 line
2.47.0.107.g34b6ce9b30
#16 lore raw

PATCH v6 1/2 show-index: fix uninitialized hash function

In c8aed5e8da (repository: stop setting SHA1 as the default object hash), we got rid of the default hash algorithm for the_repository. Due to this change, it is now the responsibility of the callers to set their own default when this is not present.

As stated in the docs, show-index should use SHA1 as the default hash algorithm when run outside a repository. Make sure this promise is met by falling back to SHA1 when the_hash_algo is not present (i.e. when the command is run outside a repository). Also add a test that verifies this behavior.

---
 builtin/show-index.c   | 9 +++++++++
 t/t5300-pack-object.sh | 4 ++++
 2 files changed, 13 insertions(+)
builtin/show-index.c +9 -0
index f164c01bbe..b5e337869d 100644
@@ -38,6 +38,15 @@ int cmd_show_index(int argc,
 		repo_set_hash_algo(the_repository, hash_algo);
 	}
 
+	/*
+	 * Fallback to SHA1 if we are running outside of a repository.
+	 *
+	 * TODO: Figure out and implement a way to detect the hash algorithm in use by the
+	 *       the index file passed in and use that instead.
+	 */
+	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)
t/t5300-pack-object.sh +4 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
Signature · 1 line
2.47.0.107.g34b6ce9b30
#17 lore raw

PATCH v6 2/2 t5300: add test for 'show-index --object-format'

In reply to Abhijeet Sonar

In 88a09a557c (builtin/show-index: provide options to determine hash algo), the flag --object-format was added to show-index builtin as a way to provide a hash algorithm explicitly. However, we do not have tests in place for that functionality. Add them.

---
 t/t5300-pack-object.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
t/t5300-pack-object.sh +14 -0
index 51fed26cc4..bb6a22b438 100755
@@ -527,6 +527,20 @@ test_expect_success SHA1 'show-index works OK outside a repository' '
 	nongit git show-index <foo.idx
 '
 
+for hash in sha1 sha256
+do
+	test_expect_success 'show-index works OK outside a repository with hash algo passed in via --object-format' '
+		test_when_finished "rm -rf explicit-hash-$hash" &&
+		git init --object-format=$hash explicit-hash-$hash &&
+		test_commit -C explicit-hash-$hash one &&
+		git -C explicit-hash-$hash rev-parse one >in &&
+		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in &&
+		idx=$(echo explicit-hash-$hash/explicit-hash-$hash*.idx) &&
+		nongit git show-index --object-format=$hash <"$idx" >actual &&
+		test_line_count = 1 actual
+	'
+done
+
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
Signature · 1 line
2.47.0.107.g34b6ce9b30
#18 lore raw
In reply to Abhijeet Sonar
Quoted 8 lines · Abhijeet Sonar

No, I don't think you are. However, I would like to point out that the code which I used as the inspiration also does thing the same way:

	test_expect_success 'pack-object <stdin parsing: [|--revs] with --stdin' '
		cat >in <<-EOF &&
		$(git -C pack-object-stdin rev-parse one)
		$(git -C pack-object-stdin rev-parse two)
		EOF

There is a huge difference between one and two, though ;-) If you expect that your new thing may later have to expect more than one line of output, the way you wrote may be easier to extend, but if you know it won't gain any more lines to its output, output from a single command is better written without cat around it.

#19 lore raw
In reply to Abhijeet Sonar
Quoted 35 lines · Abhijeet Sonar

That makes sense, applied.

Abhijeet Sonar (2):
show-index: fix uninitialized hash function
t5300: add test for 'show-index --object-format'

 builtin/show-index.c   |  9 +++++++++
 t/t5300-pack-object.sh | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)

Range-diff against v5:

1:  05ee1e2ea5 = 1:  05ee1e2ea5 show-index: fix uninitialized hash function
2:  c8a28aae55 ! 2:  778f3ca18e t5300: add test for 'show-index --object-format'
    @@ t/t5300-pack-object.sh: test_expect_success SHA1 'show-index works OK outside a
      
     +for hash in sha1 sha256
     +do
    -+	test_expect_success 'setup: show-index works OK outside a repository with hash algo passed in via --object-format' '
    -+		git init explicit-hash-$hash --object-format=$hash &&
    -+		test_commit -C explicit-hash-$hash one &&
    -+
    -+		cat >in <<-EOF &&
    -+		$(git -C explicit-hash-$hash rev-parse one)
    -+		EOF
    -+
    -+		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in
    -+	'
    -+
     +	test_expect_success 'show-index works OK outside a repository with hash algo passed in via --object-format' '
    ++		test_when_finished "rm -rf explicit-hash-$hash" &&
    ++		git init --object-format=$hash explicit-hash-$hash &&
    ++		test_commit -C explicit-hash-$hash one &&
    ++		git -C explicit-hash-$hash rev-parse one >in &&
    ++		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in &&
     +		idx=$(echo explicit-hash-$hash/explicit-hash-$hash*.idx) &&
Quoted 8 lines
     +		nongit git show-index --object-format=$hash <"$idx" >actual &&
    -+		test_line_count = 1 actual &&
    -+
    -+		rm -rf explicit-hash-$hash
    ++		test_line_count = 1 actual
     +	'
     +done
     +

Thanks, this version looks good to me.

Patrick

#20 lore raw
Quoted 45 lines · Patrick Steinhardt
Quoted 35 lines · Abhijeet Sonar

That makes sense, applied.

Abhijeet Sonar (2):
show-index: fix uninitialized hash function
t5300: add test for 'show-index --object-format'

 builtin/show-index.c   |  9 +++++++++
 t/t5300-pack-object.sh | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)

Range-diff against v5:

1:  05ee1e2ea5 = 1:  05ee1e2ea5 show-index: fix uninitialized hash function
2:  c8a28aae55 ! 2:  778f3ca18e t5300: add test for 'show-index --object-format'
    @@ t/t5300-pack-object.sh: test_expect_success SHA1 'show-index works OK outside a
      
     +for hash in sha1 sha256
     +do
    -+	test_expect_success 'setup: show-index works OK outside a repository with hash algo passed in via --object-format' '
    -+		git init explicit-hash-$hash --object-format=$hash &&
    -+		test_commit -C explicit-hash-$hash one &&
    -+
    -+		cat >in <<-EOF &&
    -+		$(git -C explicit-hash-$hash rev-parse one)
    -+		EOF
    -+
    -+		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in
    -+	'
    -+
     +	test_expect_success 'show-index works OK outside a repository with hash algo passed in via --object-format' '
    ++		test_when_finished "rm -rf explicit-hash-$hash" &&
    ++		git init --object-format=$hash explicit-hash-$hash &&
    ++		test_commit -C explicit-hash-$hash one &&
    ++		git -C explicit-hash-$hash rev-parse one >in &&
    ++		git -C explicit-hash-$hash pack-objects explicit-hash-$hash <in &&
     +		idx=$(echo explicit-hash-$hash/explicit-hash-$hash*.idx) &&
Quoted 8 lines
     +		nongit git show-index --object-format=$hash <"$idx" >actual &&
    -+		test_line_count = 1 actual &&
    -+
    -+		rm -rf explicit-hash-$hash
    ++		test_line_count = 1 actual
     +	'
     +done
     +
Quoted 1 line

Thanks, this version looks good to me.

Thanks, both. Let me mark it for 'next', then.

#21 lore raw
In reply to Patrick Steinhardt
Quoted 10 lines · Patrick Steinhardt
Quoted 9 lines · Taylor Blau
Quoted 8 lines · Abhijeet Sonar
builtin/show-index.c +0 -0
index f164c01bbe..978ae70470 100644
@@ -38,6 +38,9 @@ int cmd_show_index(int argc,
 		repo_set_hash_algo(the_repository, hash_algo);
 	}
 
Quoted 8 lines
Quoted 2 lines

builtin/show-index.c int cmd_show_index(int argc, · line 42

Quoted 2 lines
builtin/show-index.c +2 -0
+	if (!the_hash_algo)
+		repo_set_hash_algo(the_repository, GIT_HASH_SHA1);

Let's add a todo-comment here. The behaviour with this patch is somewhat broken as you cannot inspect indices that use any other object hash than SHA256 outside of a repository. This is fine from my point of view and nothing that you have to fix here, as you simply fix up the broken behaviour. But in the future, we should either:

I will add those comments, thanks.

Quoted 3 lines

- Add logic to detect the format of the passed-in index and set that up as the hash algorithm

I am very interested in hacking around and trying to implement this. I read about the index format here:
https://git-scm.com/docs/index-format and (assuming I am looking at the right thing) it does not seem like index files contain information about the hash algorithm used in their headers or anywhere else. Are there other leads I should follow?

Quoted 4 lines

- If that is impossible, add a command line option to pick the hash algo.

Actually there is already an option (--object-format) that allows changing the hash algo used, it's just that default format is SHA1 when one is not specified. (or at least will be, after this patch)

Quoted 18 lines
Quoted 11 lines
Quoted 11 lines
builtin/show-index.c +0 -0
 	hashsz = the_hash_algo->rawsz;
 
 	if (fread(top_index, 2 * 4, 1, stdin) != 1)
t/t5300-pack-object.sh +0 -0
index 3b9dae331a..51fed26cc4 100755
@@ -523,6 +523,10 @@ test_expect_success 'index-pack --strict <pack> works in non-repo' '
 	test_path_is_file foo.idx
 '
 
Quoted 3 lines

t/t5300-pack-object.sh test_expect_success 'index-pack --strict <pack> works in non-repo' ' · line 528

Quoted 3 lines
t/t5300-pack-object.sh +3 -0
+test_expect_success SHA1 'show-index works OK outside a repository' '
+	nongit git show-index <foo.idx
+'

So how does this behave with SHA256? Does it raise an error? Does it segfault?

Quoted 6 lines

I think it's okay to fail with SHA256 for now, but I'd like the failure behaviour to be cleanish. So I'd prefer to not skip the test completely, but adapt our expectations based on the hash algo. Or have two separate tests, one for each hash, that explicitly init the repo with git init --ref-format=$hash, and then exercise the behaviour for each of the>

Running show-index outside of a repository with a SHA256 based index file gives:

$ git show-index <foo.idx
fatal: inconsistent 64b offset index

At the very least, it does not crash.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
t/t5300-pack-object.sh +0 -1
 test_expect_success !PTHREADS,!FAIL_PREREQS \
 	'index-pack --threads=N or pack.threads=N warns when no pthreads' '
 	test_must_fail git index-pack --threads=2 2>err &&
--

2.47.0.107.g34b6ce9b30

Quoted 7 lines
Quoted 3 lines

These all look reasonable and as-expected to me. Patrick (CC'd) has been reviewing similar changes elsewhere, so I'd like him to chime in as well on whether or not this looks good to go.

Ah, thanks. I've missed this topic somehow.

Patrick

Thanks

#22 lore raw
In reply to Taylor Blau
Quoted 10 lines · Taylor Blau
Quoted 5 lines · Abhijeet Sonar

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.

This commit description is good, and would benefit further from a bisection showing where the regression began. I don't think that it is a prerequisite for us moving this patch forward, though.

On bisecting, the offending commit appears to be c8aed5e8da (repository:
stop setting SHA1 as the default object hash).

Another related commit is ab274909d4 (builtin/diff: explicitly set hash algo when there is no repo) which did something very similar to my patch.

Thanks

#23 lore raw

Re: Re* [PATCH v2] show-index: fix uninitialized hash function

In reply to Junio C Hamano

Please excuse the neco-bump.

Quoted 3 lines · Junio C Hamano

With that, your patch would become like so:

------------ >8 ----------------------- >8 ------------

I misunderstood this as "I have made these changes to your patch on your behalf". But looking at how this was never queued and the commit does not appear in any upstream branch, I realised that I was supposed to send another iteration. Apologies.

I have sent another iteration (v3) just before writing this.

Thanks

#24 lore raw
In reply to Abhijeet Sonar
Quoted 9 lines · Abhijeet Sonar
t/t8101-show-index-hash-function.sh new file +3 -0
new file mode 100755
index 0000000000..2e9308f73c
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+test_description='git show-index'

t/t8101-show-index-hash-function.sh line 13

Quoted 10 lines
t/t8101-show-index-hash-function.sh new file +10 -0
+
+. ./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.

Signature · 2 lines
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA
#25 lore raw
In reply to Junio C Hamano
Quoted 13 lines · Junio C Hamano
Quoted 10 lines · Abhijeet Sonar

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 funciton is specified, causing a crash. Fix it by falling back to SHA1 when it is found uninitialized.

---
 builtin/show-index.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 5 lines

Nicely described.

We'd probably want to protect this with a new test, so that regardless of the choice of GIT_TEST_DEFAULT_HASH, the command should behave as advertised.

I wrote a test which build an index file using a hash-object | pack-objects chain. I am not sure if its the best way to do this, I would appreciate some guidance on this.

Another way I can think of is having an index file sit along with the tests in the codebase which will be read by show-index instead of generating one on the fly. Thoughts?

Thanks.

Quoted 19 lines

Having said that, I am not sure if --object-format specified on the command line, or picked up from the repository, makes much sense in the context of the command, especially for the longer term [*]. The command is designed to read from its standard input a byte-stream, which is assumed to be an .idx file of _any_ origin, so ideally it should be able to tell what hash the incoming data uses and use that hash algorithm, without being told from the command line?

But that longer-term worry has nothing to do with the validity of this patch (but the lack of test does). Thanks.

[Footnote]

* Perhaps the file format does not make it obvious what hash algorithm it uses, so it may be hard to auto-detect without additional code. But if that is the case, it would be something we may want to eventually fix.

#26 lore raw
In reply to Abhijeet Sonar
Quoted 3 lines · Abhijeet Sonar

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 funciton is

s/funciton/function/

Quoted 4 lines

specified, causing a crash. Fix it by falling back to SHA1 when it is found uninitialized.