* [PATCH 1/3] show-index: implement automatic hash detection
2026-01-20 14:05 [RFC][PATCH 0/3] show-index: modernize and implement auto-detection of hash algorithm Shreyansh Paliwal
@ 2026-01-20 14:05 ` Shreyansh Paliwal
2026-01-20 18:07 ` Junio C Hamano
2026-01-20 14:05 ` [PATCH 2/3] show-index: use gettext wrapping in error messages Shreyansh Paliwal
` (2 subsequent siblings)
3 siblings, 1 reply; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-20 14:05 UTC (permalink / raw)
To: git; +Cc: gitster, Shreyansh Paliwal
When git show-index is run outside a repository,
it currently falls back to SHA-1 unless the hash algorithm
is explicitly specified via --object-format.
This can lead to failures when reading SHA-256 pack index files.
To prevent this add an automatic hash algorithm detection,
as suggested by an existing TODO comment in the code.
For v2 index files, the fixed size can be computed and then,
the overall file size combined with the number of objects,
can be used to compute the hash size of the objects.
Since SHA-1 and SHA-256 use fixed hash sizes (20 and 32 bytes,
respectively), the hash algorithm can be determined.
This detection is limited in scope. It only applies when the
index file does not contain any 64-bit offset entries, which introduce
additional variable-sized data into the file layout. When such offsets are
present, automatic detection becomes irrelevant, and the user is instead
required to specify the hash algorithm explicitly using --object-format.
Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
---
builtin/show-index.c | 45 +++++++++++++++++++++++++++++++++-----------
1 file changed, 34 insertions(+), 11 deletions(-)
diff --git a/builtin/show-index.c b/builtin/show-index.c
index 2c3e2940ce..be62edc57b 100644
--- a/builtin/show-index.c
+++ b/builtin/show-index.c
@@ -40,17 +40,6 @@ 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_DEFAULT);
-
- hashsz = the_hash_algo->rawsz;
-
if (fread(top_index, 2 * 4, 1, stdin) != 1)
die("unable to read header");
if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
@@ -71,6 +60,40 @@ int cmd_show_index(int argc,
die("corrupt index file");
nr = n;
}
+
+ /* detection of hash algorithm
+ Only works for small files, i.e without large offsets */
+ if(!the_hash_algo && version == 2) {
+ struct stat st;
+ size_t file_base_size;
+ size_t table_size;
+ size_t size_rem;
+ size_t hash_size;
+
+ if(fstat(0, &st) || !S_ISREG(st.st_mode))
+ die(_("unable to detect hash from non-regular file"));
+
+ file_base_size = 8 + (256 * 4);
+ table_size = file_base_size + (nr * 4 * 4);
+ size_rem = st.st_size - table_size;
+ hash_size = size_rem / (nr + 2);
+
+ if(hash_size == GIT_SHA1_RAWSZ) {
+ repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+ } else if(hash_size == GIT_SHA256_RAWSZ) {
+ repo_set_hash_algo(the_repository, GIT_HASH_SHA256);
+ } else {
+ die(_("unable to detect hash algorithm, "
+ "use --object-format option"));
+ }
+ }
+
+ /* Final fallback to SHA1 */
+ if(!the_hash_algo)
+ repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+
+ hashsz = the_hash_algo->rawsz;
+
if (version == 1) {
for (i = 0; i < nr; i++) {
unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
--
2.52.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* Re: [PATCH 1/3] show-index: implement automatic hash detection
2026-01-20 14:05 ` [PATCH 1/3] show-index: implement automatic hash detection Shreyansh Paliwal
@ 2026-01-20 18:07 ` Junio C Hamano
2026-01-21 8:09 ` Patrick Steinhardt
2026-01-21 10:28 ` Shreyansh Paliwal
0 siblings, 2 replies; 25+ messages in thread
From: Junio C Hamano @ 2026-01-20 18:07 UTC (permalink / raw)
To: Shreyansh Paliwal; +Cc: git
Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
> @@ -71,6 +60,40 @@ int cmd_show_index(int argc,
> die("corrupt index file");
> nr = n;
> }
> +
> + /* detection of hash algorithm
> + Only works for small files, i.e without large offsets */
> + if(!the_hash_algo && version == 2) {
We have one SP between "if" (and other syntactic elements like
"while") and the open parenthesis "(". End-user controlled function
names lack this SP between <word> and "(".
If we turn what is inide of this block into a separate helper
function, it would allow us to structure the logic better.
/* Returns GIT_HASH_* constants, or GIT_HASH_UNKNOWN */
static int auto_detect_hash_function(int fd)
For example, ...
> + struct stat st;
> + size_t file_base_size;
> + size_t table_size;
> + size_t size_rem;
> + size_t hash_size;
> +
> + if(fstat(0, &st) || !S_ISREG(st.st_mode))
> + die(_("unable to detect hash from non-regular file"));
... this "die()" does not have to be here. We can just return
GIT_HASH_UNKNOWN and let the caller fallback. Does the existing
code correctly complain when the filestream is opened for a
non-regular file, or it just gets totally confused?
> + file_base_size = 8 + (256 * 4);
> + table_size = file_base_size + (nr * 4 * 4);
> + size_rem = st.st_size - table_size;
> + hash_size = size_rem / (nr + 2);
> +
> + if(hash_size == GIT_SHA1_RAWSZ) {
> + repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
> + } else if(hash_size == GIT_SHA256_RAWSZ) {
> + repo_set_hash_algo(the_repository, GIT_HASH_SHA256);
And instead of calling repo_set_hash_algo(), just return the
constants so that the caller can handle it. And
> + } else {
> + die(_("unable to detect hash algorithm, "
> + "use --object-format option"));
... this also can return GIT_HASH_UNKNOWN, without complaining
anything.
> + }
> + }
So, instead of inserting all of the above lines in cmd_show_index(),
we'd have something like the following ...
hash_func = auto_detect_hash_function(0);
if (hash_func == GIT_HASH_UNKNOWN) {
warning(_("assuming SHA-1; use --object-format to override"));
hash_func = GIT_HASH_SHA1;
}
repo_set_hash_algo(the_repository, hash_func);
hashsz = the_hash_algo->rawsz;
... there.
By the way, what happens if we find SHA-256 also broken and end up
choosing another hash function that is 256-bit wide in the next hash
revamp?
Thanks.
> +
> + /* Final fallback to SHA1 */
> + if(!the_hash_algo)
> + repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
> +
> + hashsz = the_hash_algo->rawsz;
> +
> if (version == 1) {
> for (i = 0; i < nr; i++) {
> unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH 1/3] show-index: implement automatic hash detection
2026-01-20 18:07 ` Junio C Hamano
@ 2026-01-21 8:09 ` Patrick Steinhardt
2026-01-21 10:31 ` Shreyansh Paliwal
2026-01-23 20:29 ` brian m. carlson
2026-01-21 10:28 ` Shreyansh Paliwal
1 sibling, 2 replies; 25+ messages in thread
From: Patrick Steinhardt @ 2026-01-21 8:09 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Shreyansh Paliwal, git
On Tue, Jan 20, 2026 at 10:07:42AM -0800, Junio C Hamano wrote:
> Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
> > @@ -71,6 +60,40 @@ int cmd_show_index(int argc,
[snip]
> By the way, what happens if we find SHA-256 also broken and end up
> choosing another hash function that is 256-bit wide in the next hash
> revamp?
Yeah, agreed. The index unfortunately does not carry sufficient info to
clearly identify the hash function that is in use, and second-guessing
via the hash length doesn't really seem like a sensible solution to me.
If we cannot tell for sure what the hash is, then we should rather ask
the user to specify the object format. And in fact we already do that,
as we have the `--object-format=` option for git-show-index(1).
I think if we wanted to fix properly this we should rather introduce
index v5 with a header that encodes the hash used by it. Like that we
wouldn't have to guess anymore. Whether the hassle is worth it might be
a different question though.
Patrick
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/3] show-index: implement automatic hash detection
2026-01-21 8:09 ` Patrick Steinhardt
@ 2026-01-21 10:31 ` Shreyansh Paliwal
2026-01-23 7:22 ` Patrick Steinhardt
2026-01-23 20:29 ` brian m. carlson
1 sibling, 1 reply; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-21 10:31 UTC (permalink / raw)
To: git; +Cc: gitster, ps
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 1887 bytes --]
> On Tue, Jan 20, 2026 at 10:07:42AM -0800, Junio C Hamano wrote:
> > Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
> > > @@ -71,6 +60,40 @@ int cmd_show_index(int argc,
> [snip]
> > By the way, what happens if we find SHA-256 also broken and end up
> > choosing another hash function that is 256-bit wide in the next hash
> > revamp?
>
> Yeah, agreed. The index unfortunately does not carry sufficient info to
> clearly identify the hash function that is in use, and second-guessing
> via the hash length doesn't really seem like a sensible solution to me.
> If we cannot tell for sure what the hash is, then we should rather ask
> the user to specify the object format. And in fact we already do that,
> as we have the `--object-format=` option for git-show-index(1).
Yes this is exactly why I was peculiar about this patch and the
TODO comment, also why I sent it out as an RFC.
I initially assumed that in the near future we’re unlikely to move away
from SHA-256 to another hash, but I agree that relying
on hash length is still a heuristic that won't be a good approach
in the long term as well as it creates ambiguity in the large files
containing 64-bit offsets.
So should we drop this thought entirely and just make sure
that if git show-index is run outside a repo,
it should throw an error asking the the user
to use --object-format option rather than silently
falling back to SHA-1 which is the current approach.
> I think if we wanted to fix properly this we should rather introduce
> index v5 with a header that encodes the hash used by it. Like that we
> wouldn't have to guess anymore. Whether the hassle is worth it might be
> a different question though.
Yes, I agree that the best fix for long term would be an index
that contains header encoded with hash, but I guess
it would require many changes in the whole pack index flow.
Best,
Shreyansh
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/3] show-index: implement automatic hash detection
2026-01-21 10:31 ` Shreyansh Paliwal
@ 2026-01-23 7:22 ` Patrick Steinhardt
2026-01-23 16:08 ` Shreyansh Paliwal
0 siblings, 1 reply; 25+ messages in thread
From: Patrick Steinhardt @ 2026-01-23 7:22 UTC (permalink / raw)
To: Shreyansh Paliwal; +Cc: git, gitster
On Wed, Jan 21, 2026 at 04:01:47PM +0530, Shreyansh Paliwal wrote:
> > On Tue, Jan 20, 2026 at 10:07:42AM -0800, Junio C Hamano wrote:
> > > Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
> > > > @@ -71,6 +60,40 @@ int cmd_show_index(int argc,
> > [snip]
> > > By the way, what happens if we find SHA-256 also broken and end up
> > > choosing another hash function that is 256-bit wide in the next hash
> > > revamp?
> >
> > Yeah, agreed. The index unfortunately does not carry sufficient info to
> > clearly identify the hash function that is in use, and second-guessing
> > via the hash length doesn't really seem like a sensible solution to me.
> > If we cannot tell for sure what the hash is, then we should rather ask
> > the user to specify the object format. And in fact we already do that,
> > as we have the `--object-format=` option for git-show-index(1).
>
> Yes this is exactly why I was peculiar about this patch and the
> TODO comment, also why I sent it out as an RFC.
>
> I initially assumed that in the near future we’re unlikely to move away
> from SHA-256 to another hash, but I agree that relying
> on hash length is still a heuristic that won't be a good approach
> in the long term as well as it creates ambiguity in the large files
> containing 64-bit offsets.
>
> So should we drop this thought entirely and just make sure
> that if git show-index is run outside a repo,
> it should throw an error asking the the user
> to use --object-format option rather than silently
> falling back to SHA-1 which is the current approach.
That would be a regression for users that currently _can_ run
git-show-index(1) outside of a repository with a SHA-1 based index. It's
not going to be a common use case, but I wouldn't be surprised if there
was at least one user out there that we'd break with such a change.
Patrick
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/3] show-index: implement automatic hash detection
2026-01-23 7:22 ` Patrick Steinhardt
@ 2026-01-23 16:08 ` Shreyansh Paliwal
0 siblings, 0 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-23 16:08 UTC (permalink / raw)
To: git; +Cc: gitster, ps
> That would be a regression for users that currently _can_ run
> git-show-index(1) outside of a repository with a SHA-1 based index. It's
> not going to be a common use case, but I wouldn't be surprised if there
> was at least one user out there that we'd break with such a change.
That makes sense, but relying on a silent SHA-1 fallback purely because we
are outside of a repository still feels a bit ambiguous to me. It works today
mostly because SHA-1 has historically been the default, but if in future
git introduces additional hash functions or if SHA-256 indexes become
more relevant to this particular usage.
Though, I think based on the discussion so far,
we can only show a warning like this along with the SHA-1 fallback like this,
warning(_("assuming SHA-1; use --object-format to override"));
Do you think if this would be appropriate, or if anything
better can be done to handle this or
is it just better to leave the behavior as-is.
Let me know :)
Best,
Shreyansh
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/3] show-index: implement automatic hash detection
2026-01-21 8:09 ` Patrick Steinhardt
2026-01-21 10:31 ` Shreyansh Paliwal
@ 2026-01-23 20:29 ` brian m. carlson
1 sibling, 0 replies; 25+ messages in thread
From: brian m. carlson @ 2026-01-23 20:29 UTC (permalink / raw)
To: Patrick Steinhardt; +Cc: Junio C Hamano, Shreyansh Paliwal, git
[-- Attachment #1: Type: text/plain, Size: 708 bytes --]
On 2026-01-21 at 08:09:11, Patrick Steinhardt wrote:
> I think if we wanted to fix properly this we should rather introduce
> index v5 with a header that encodes the hash used by it. Like that we
> wouldn't have to guess anymore. Whether the hassle is worth it might be
> a different question though.
Index v3, which will be used during interoperability, will encode the
hash algorithm and can be used for single-hash implementations as well
as multi-hash implementations.
There's an implementation in my `sha256-interop` branch at
https://github.com/bk2204/git.git and we could simply enable it by
default at some point in the future.
--
brian m. carlson (they/them)
Toronto, Ontario, CA
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 262 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/3] show-index: implement automatic hash detection
2026-01-20 18:07 ` Junio C Hamano
2026-01-21 8:09 ` Patrick Steinhardt
@ 2026-01-21 10:28 ` Shreyansh Paliwal
1 sibling, 0 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-21 10:28 UTC (permalink / raw)
To: git; +Cc: gitster
> Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
>
> > @@ -71,6 +60,40 @@ int cmd_show_index(int argc,
> > die("corrupt index file");
> > nr = n;
> > }
> > +
> > + /* detection of hash algorithm
> > + Only works for small files, i.e without large offsets */
> > + if(!the_hash_algo && version == 2) {
>
> We have one SP between "if" (and other syntactic elements like
> "while") and the open parenthesis "(". End-user controlled function
> names lack this SP between <word> and "(".
>
Got it, I will keep this in mind.
> For example, ...
>
> > + struct stat st;
> > + size_t file_base_size;
> > + size_t table_size;
> > + size_t size_rem;
> > + size_t hash_size;
> > +
> > + if(fstat(0, &st) || !S_ISREG(st.st_mode))
> > + die(_("unable to detect hash from non-regular file"));
>
> ... this "die()" does not have to be here. We can just return
> GIT_HASH_UNKNOWN and let the caller fallback. Does the existing
> code correctly complain when the filestream is opened for a
> non-regular file, or it just gets totally confused?
>
I believe there is no explicit check for
irregular files in the current implementation.
> > + file_base_size = 8 + (256 * 4);
> > + table_size = file_base_size + (nr * 4 * 4);
> > + size_rem = st.st_size - table_size;
> > + hash_size = size_rem / (nr + 2);
> > +
> > + if(hash_size == GIT_SHA1_RAWSZ) {
> > + repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
> > + } else if(hash_size == GIT_SHA256_RAWSZ) {
> > + repo_set_hash_algo(the_repository, GIT_HASH_SHA256);
>
> And instead of calling repo_set_hash_algo(), just return the
> constants so that the caller can handle it. And
>
> > + } else {
> > + die(_("unable to detect hash algorithm, "
> > + "use --object-format option"));
>
> ... this also can return GIT_HASH_UNKNOWN, without complaining
> anything.
>
> > + }
> > + }
>
> So, instead of inserting all of the above lines in cmd_show_index(),
> we'd have something like the following ...
>
> hash_func = auto_detect_hash_function(0);
> if (hash_func == GIT_HASH_UNKNOWN) {
> warning(_("assuming SHA-1; use --object-format to override"));
> hash_func = GIT_HASH_SHA1;
> }
> repo_set_hash_algo(the_repository, hash_func);
> hashsz = the_hash_algo->rawsz;
>
> ... there.
>
Yes this is surely a much better approach than before,
I will implement this as a seperate helper function in v2
if we go ahead with the detection logic.
^ permalink raw reply [flat|nested] 25+ messages in thread
* [PATCH 2/3] show-index: use gettext wrapping in error messages
2026-01-20 14:05 [RFC][PATCH 0/3] show-index: modernize and implement auto-detection of hash algorithm Shreyansh Paliwal
2026-01-20 14:05 ` [PATCH 1/3] show-index: implement automatic hash detection Shreyansh Paliwal
@ 2026-01-20 14:05 ` Shreyansh Paliwal
2026-01-20 14:05 ` [PATCH 3/3] show-index: remove global state variables Shreyansh Paliwal
2026-01-29 15:36 ` [PATCH] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
3 siblings, 0 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-20 14:05 UTC (permalink / raw)
To: git; +Cc: gitster, Shreyansh Paliwal
Previously, error messages were passed directly to die().
As suggested by the Git coding guidelines, wrap user-visible strings
in the _() macro so they can be translated.
Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
---
builtin/show-index.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/builtin/show-index.c b/builtin/show-index.c
index be62edc57b..a9c2f18b73 100644
--- a/builtin/show-index.c
+++ b/builtin/show-index.c
@@ -41,23 +41,23 @@ int cmd_show_index(int argc,
}
if (fread(top_index, 2 * 4, 1, stdin) != 1)
- die("unable to read header");
+ die(_("unable to read header"));
if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
version = ntohl(top_index[1]);
if (version < 2 || version > 2)
- die("unknown index version");
+ die(_("unknown index version"));
if (fread(top_index, 256 * 4, 1, stdin) != 1)
- die("unable to read index");
+ die(_("unable to read index"));
} else {
version = 1;
if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
- die("unable to read index");
+ die(_("unable to read index"));
}
nr = 0;
for (i = 0; i < 256; i++) {
unsigned n = ntohl(top_index[i]);
if (n < nr)
- die("corrupt index file");
+ die(_("corrupt index file"));
nr = n;
}
@@ -99,7 +99,7 @@ int cmd_show_index(int argc,
unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
if (fread(entry, 4 + hashsz, 1, stdin) != 1)
- die("unable to read entry %u/%u", i, nr);
+ die(_("unable to read entry %u/%u"), i, nr);
offset = ntohl(entry[0]);
printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
}
@@ -113,15 +113,15 @@ int cmd_show_index(int argc,
ALLOC_ARRAY(entries, nr);
for (i = 0; i < nr; i++) {
if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
- die("unable to read sha1 %u/%u", i, nr);
+ die(_("unable to read sha1 %u/%u"), i, nr);
entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
}
for (i = 0; i < nr; i++)
if (fread(&entries[i].crc, 4, 1, stdin) != 1)
- die("unable to read crc %u/%u", i, nr);
+ die(_("unable to read crc %u/%u"), i, nr);
for (i = 0; i < nr; i++)
if (fread(&entries[i].off, 4, 1, stdin) != 1)
- die("unable to read 32b offset %u/%u", i, nr);
+ die(_("unable to read 32b offset %u/%u"), i, nr);
for (i = 0; i < nr; i++) {
uint64_t offset;
uint32_t off = ntohl(entries[i].off);
@@ -130,9 +130,9 @@ int cmd_show_index(int argc,
} else {
uint32_t off64[2];
if ((off & 0x7fffffff) != off64_nr)
- die("inconsistent 64b offset index");
+ die(_("inconsistent 64b offset index"));
if (fread(off64, 8, 1, stdin) != 1)
- die("unable to read 64b offset %u", off64_nr);
+ die(_("unable to read 64b offset %u"), off64_nr);
offset = (((uint64_t)ntohl(off64[0])) << 32) |
ntohl(off64[1]);
off64_nr++;
--
2.52.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH 3/3] show-index: remove global state variables
2026-01-20 14:05 [RFC][PATCH 0/3] show-index: modernize and implement auto-detection of hash algorithm Shreyansh Paliwal
2026-01-20 14:05 ` [PATCH 1/3] show-index: implement automatic hash detection Shreyansh Paliwal
2026-01-20 14:05 ` [PATCH 2/3] show-index: use gettext wrapping in error messages Shreyansh Paliwal
@ 2026-01-20 14:05 ` Shreyansh Paliwal
2026-01-21 10:39 ` Phillip Wood
2026-01-29 15:36 ` [PATCH] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
3 siblings, 1 reply; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-20 14:05 UTC (permalink / raw)
To: git; +Cc: gitster, Shreyansh Paliwal
As Git is in the process of removing global state,
this function still relies on the global variables,
the_repository and the_hash_algo.
Remove the associated macro and the UNUSED attribute from
the repo parameter, and replace all uses of the_repository and
the_hash_algo with repo and repo->hash_algo, respectively.
This modernizes git show-index and makes it more compatible.
Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
---
builtin/show-index.c | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/builtin/show-index.c b/builtin/show-index.c
index a9c2f18b73..96adae14c0 100644
--- a/builtin/show-index.c
+++ b/builtin/show-index.c
@@ -1,4 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS
#include "builtin.h"
@@ -16,7 +15,7 @@ static const char *const show_index_usage[] = {
int cmd_show_index(int argc,
const char **argv,
const char *prefix,
- struct repository *repo UNUSED)
+ struct repository *repo)
{
int i;
unsigned nr;
@@ -37,7 +36,7 @@ int cmd_show_index(int argc,
hash_algo = hash_algo_by_name(hash_name);
if (hash_algo == GIT_HASH_UNKNOWN)
die(_("Unknown hash algorithm"));
- repo_set_hash_algo(the_repository, hash_algo);
+ repo_set_hash_algo(repo, hash_algo);
}
if (fread(top_index, 2 * 4, 1, stdin) != 1)
@@ -63,7 +62,7 @@ int cmd_show_index(int argc,
/* detection of hash algorithm
Only works for small files, i.e without large offsets */
- if(!the_hash_algo && version == 2) {
+ if(!repo->hash_algo && version == 2) {
struct stat st;
size_t file_base_size;
size_t table_size;
@@ -79,9 +78,9 @@ int cmd_show_index(int argc,
hash_size = size_rem / (nr + 2);
if(hash_size == GIT_SHA1_RAWSZ) {
- repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+ repo_set_hash_algo(repo, GIT_HASH_SHA1);
} else if(hash_size == GIT_SHA256_RAWSZ) {
- repo_set_hash_algo(the_repository, GIT_HASH_SHA256);
+ repo_set_hash_algo(repo, GIT_HASH_SHA256);
} else {
die(_("unable to detect hash algorithm, "
"use --object-format option"));
@@ -89,10 +88,10 @@ int cmd_show_index(int argc,
}
/* Final fallback to SHA1 */
- if(!the_hash_algo)
- repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+ if(!repo->hash_algo)
+ repo_set_hash_algo(repo, GIT_HASH_SHA1);
- hashsz = the_hash_algo->rawsz;
+ hashsz = repo->hash_algo->rawsz;
if (version == 1) {
for (i = 0; i < nr; i++) {
@@ -114,7 +113,7 @@ int cmd_show_index(int argc,
for (i = 0; i < nr; i++) {
if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
die(_("unable to read sha1 %u/%u"), i, nr);
- entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
+ entries[i].oid.algo = hash_algo_by_ptr(repo->hash_algo);
}
for (i = 0; i < nr; i++)
if (fread(&entries[i].crc, 4, 1, stdin) != 1)
--
2.52.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* Re: [PATCH 3/3] show-index: remove global state variables
2026-01-20 14:05 ` [PATCH 3/3] show-index: remove global state variables Shreyansh Paliwal
@ 2026-01-21 10:39 ` Phillip Wood
2026-01-21 12:47 ` Shreyansh Paliwal
2026-01-21 17:23 ` Junio C Hamano
0 siblings, 2 replies; 25+ messages in thread
From: Phillip Wood @ 2026-01-21 10:39 UTC (permalink / raw)
To: Shreyansh Paliwal, git; +Cc: gitster
On 20/01/2026 14:05, Shreyansh Paliwal wrote:
> As Git is in the process of removing global state,
> this function still relies on the global variables,
> the_repository and the_hash_algo.
>
> Remove the associated macro and the UNUSED attribute from
> the repo parameter, and replace all uses of the_repository and
> the_hash_algo with repo and repo->hash_algo, respectively.
I don't think that is a good idea because repo will be NULL outside of a
repository. For a lot of commands that does not matter because they
require a repository to run but judging from the first patch in this
series this command is supposed to be able to run outside a repository.
I'm increasingly of the opinion that adding a repository argument to the
builtin commands was a mistake as they all just use a single repository
so using "the_repository" seems perfectly reasonable. It leads to
problems like the segfault in this patch and takes attention away from
the much more useful task of moving our library code away from using
"the_repository". If you're interested in contributing to that effort
then there are a number of instances of "the_repository" in wt-status.c
that can be trivially replaced by the repository instance in "struct
wt_status" or the repository passed to the function. I'm not sure how
easy it is to remove them all - you might need to change the code to
pass a repository instance down the call chain in a few cases but there
are certainly quite a few that can be easily and usefully cleaned up.
Thanks
Phillip
> This modernizes git show-index and makes it more compatible.
>
> Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
> ---
> builtin/show-index.c | 19 +++++++++----------
> 1 file changed, 9 insertions(+), 10 deletions(-)
>
> diff --git a/builtin/show-index.c b/builtin/show-index.c
> index a9c2f18b73..96adae14c0 100644
> --- a/builtin/show-index.c
> +++ b/builtin/show-index.c
> @@ -1,4 +1,3 @@
> -#define USE_THE_REPOSITORY_VARIABLE
> #define DISABLE_SIGN_COMPARE_WARNINGS
>
> #include "builtin.h"
> @@ -16,7 +15,7 @@ static const char *const show_index_usage[] = {
> int cmd_show_index(int argc,
> const char **argv,
> const char *prefix,
> - struct repository *repo UNUSED)
> + struct repository *repo)
> {
> int i;
> unsigned nr;
> @@ -37,7 +36,7 @@ int cmd_show_index(int argc,
> hash_algo = hash_algo_by_name(hash_name);
> if (hash_algo == GIT_HASH_UNKNOWN)
> die(_("Unknown hash algorithm"));
> - repo_set_hash_algo(the_repository, hash_algo);
> + repo_set_hash_algo(repo, hash_algo);
> }
>
> if (fread(top_index, 2 * 4, 1, stdin) != 1)
> @@ -63,7 +62,7 @@ int cmd_show_index(int argc,
>
> /* detection of hash algorithm
> Only works for small files, i.e without large offsets */
> - if(!the_hash_algo && version == 2) {
> + if(!repo->hash_algo && version == 2) {
> struct stat st;
> size_t file_base_size;
> size_t table_size;
> @@ -79,9 +78,9 @@ int cmd_show_index(int argc,
> hash_size = size_rem / (nr + 2);
>
> if(hash_size == GIT_SHA1_RAWSZ) {
> - repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
> + repo_set_hash_algo(repo, GIT_HASH_SHA1);
> } else if(hash_size == GIT_SHA256_RAWSZ) {
> - repo_set_hash_algo(the_repository, GIT_HASH_SHA256);
> + repo_set_hash_algo(repo, GIT_HASH_SHA256);
> } else {
> die(_("unable to detect hash algorithm, "
> "use --object-format option"));
> @@ -89,10 +88,10 @@ int cmd_show_index(int argc,
> }
>
> /* Final fallback to SHA1 */
> - if(!the_hash_algo)
> - repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
> + if(!repo->hash_algo)
> + repo_set_hash_algo(repo, GIT_HASH_SHA1);
>
> - hashsz = the_hash_algo->rawsz;
> + hashsz = repo->hash_algo->rawsz;
>
> if (version == 1) {
> for (i = 0; i < nr; i++) {
> @@ -114,7 +113,7 @@ int cmd_show_index(int argc,
> for (i = 0; i < nr; i++) {
> if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
> die(_("unable to read sha1 %u/%u"), i, nr);
> - entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
> + entries[i].oid.algo = hash_algo_by_ptr(repo->hash_algo);
> }
> for (i = 0; i < nr; i++)
> if (fread(&entries[i].crc, 4, 1, stdin) != 1)
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH 3/3] show-index: remove global state variables
2026-01-21 10:39 ` Phillip Wood
@ 2026-01-21 12:47 ` Shreyansh Paliwal
2026-01-21 17:23 ` Junio C Hamano
1 sibling, 0 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-21 12:47 UTC (permalink / raw)
To: git; +Cc: gitster, phillip.wood123
> On 20/01/2026 14:05, Shreyansh Paliwal wrote:
> > As Git is in the process of removing global state,
> > this function still relies on the global variables,
> > the_repository and the_hash_algo.
> >
> > Remove the associated macro and the UNUSED attribute from
> > the repo parameter, and replace all uses of the_repository and
> > the_hash_algo with repo and repo->hash_algo, respectively.
>
> I don't think that is a good idea because repo will be NULL outside of a
> repository. For a lot of commands that does not matter because they
> require a repository to run but judging from the first patch in this
> series this command is supposed to be able to run outside a repository.
>
> I'm increasingly of the opinion that adding a repository argument to the
> builtin commands was a mistake as they all just use a single repository
> so using "the_repository" seems perfectly reasonable. It leads to
> problems like the segfault in this patch and takes attention away from
> the much more useful task of moving our library code away from using
That makes a lot of sense, especially for the commands
which are meant to run outside the repo as well.
In hindsight, the NULL repo issue and the segfault risk
should have been obvious to me, particularly given that I started
by creating the hash detection for no-repo cases :)
Anyways I will drop this patch in the next version.
> "the_repository". If you're interested in contributing to that effort
> then there are a number of instances of "the_repository" in wt-status.c
> that can be trivially replaced by the repository instance in "struct
> wt_status" or the repository passed to the function. I'm not sure how
> easy it is to remove them all - you might need to change the code to
> pass a repository instance down the call chain in a few cases but there
> are certainly quite a few that can be easily and usefully cleaned up.
Yes sure, I will take a look and see where I can contribute in wt-status.c,
towards reducing global-state usage.
Best,
Shreyansh
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 3/3] show-index: remove global state variables
2026-01-21 10:39 ` Phillip Wood
2026-01-21 12:47 ` Shreyansh Paliwal
@ 2026-01-21 17:23 ` Junio C Hamano
1 sibling, 0 replies; 25+ messages in thread
From: Junio C Hamano @ 2026-01-21 17:23 UTC (permalink / raw)
To: Phillip Wood; +Cc: Shreyansh Paliwal, git
Phillip Wood <phillip.wood123@gmail.com> writes:
> I'm increasingly of the opinion that adding a repository argument to the
> builtin commands was a mistake as they all just use a single repository
> so using "the_repository" seems perfectly reasonable. It leads to
> problems like the segfault in this patch and takes attention away from
> the much more useful task of moving our library code away from using
> "the_repository".
Very well said ;-)
^ permalink raw reply [flat|nested] 25+ messages in thread
* [PATCH] show-index: warn when falling back to SHA-1 outside a repository
2026-01-20 14:05 [RFC][PATCH 0/3] show-index: modernize and implement auto-detection of hash algorithm Shreyansh Paliwal
` (2 preceding siblings ...)
2026-01-20 14:05 ` [PATCH 3/3] show-index: remove global state variables Shreyansh Paliwal
@ 2026-01-29 15:36 ` Shreyansh Paliwal
2026-01-29 23:03 ` Junio C Hamano
` (2 more replies)
3 siblings, 3 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-29 15:36 UTC (permalink / raw)
To: git; +Cc: gitster, ps, Shreyansh Paliwal
When 'git show-index' is run outside of a
repository and no hashing algorithm is
specified via --object-format, it silently
falls back to SHA-1, relying on the
historical default.
This works for existing SHA-1 based
index files, but the behavior can be ambiguous
and confusing when the input index file uses a
different hash algorithm, such as SHA-256.
Add a warning when this fallback happens
to make the assumption explicit and to
guide users toward using --object-format
when needed.
Additionally, wrap user-facing die() messages
with _() so they can be translated via gettext.
Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
---
builtin/show-index.c | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/builtin/show-index.c b/builtin/show-index.c
index 2c3e2940ce..14147c2ff2 100644
--- a/builtin/show-index.c
+++ b/builtin/show-index.c
@@ -36,39 +36,42 @@ int cmd_show_index(int argc,
if (hash_name) {
hash_algo = hash_algo_by_name(hash_name);
if (hash_algo == GIT_HASH_UNKNOWN)
- die(_("Unknown hash algorithm"));
+ die(_("unknown hash algorithm"));
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.
+ * TODO: If a future implementation of index file version encodes the hash
+ * algorithm in its header, enable show-index to infer it from the
+ * header rather than relying on repository context or a default fallback.
*/
- if (!the_hash_algo)
+ if (!the_hash_algo) {
+ warning(_("assuming SHA-1; use --object-format to override"));
repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
+ }
hashsz = the_hash_algo->rawsz;
if (fread(top_index, 2 * 4, 1, stdin) != 1)
- die("unable to read header");
+ die(_("unable to read header"));
if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
version = ntohl(top_index[1]);
if (version < 2 || version > 2)
- die("unknown index version");
+ die(_("unknown index version"));
if (fread(top_index, 256 * 4, 1, stdin) != 1)
- die("unable to read index");
+ die(_("unable to read index"));
} else {
version = 1;
if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
- die("unable to read index");
+ die(_("unable to read index"));
}
nr = 0;
for (i = 0; i < 256; i++) {
unsigned n = ntohl(top_index[i]);
if (n < nr)
- die("corrupt index file");
+ die(_("corrupt index file"));
nr = n;
}
if (version == 1) {
@@ -76,7 +79,7 @@ int cmd_show_index(int argc,
unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
if (fread(entry, 4 + hashsz, 1, stdin) != 1)
- die("unable to read entry %u/%u", i, nr);
+ die(_("unable to read entry %u/%u"), i, nr);
offset = ntohl(entry[0]);
printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
}
@@ -90,15 +93,15 @@ int cmd_show_index(int argc,
ALLOC_ARRAY(entries, nr);
for (i = 0; i < nr; i++) {
if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
- die("unable to read sha1 %u/%u", i, nr);
+ die(_("unable to read sha1 %u/%u"), i, nr);
entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
}
for (i = 0; i < nr; i++)
if (fread(&entries[i].crc, 4, 1, stdin) != 1)
- die("unable to read crc %u/%u", i, nr);
+ die(_("unable to read crc %u/%u"), i, nr);
for (i = 0; i < nr; i++)
if (fread(&entries[i].off, 4, 1, stdin) != 1)
- die("unable to read 32b offset %u/%u", i, nr);
+ die(_("unable to read 32b offset %u/%u"), i, nr);
for (i = 0; i < nr; i++) {
uint64_t offset;
uint32_t off = ntohl(entries[i].off);
@@ -107,9 +110,9 @@ int cmd_show_index(int argc,
} else {
uint32_t off64[2];
if ((off & 0x7fffffff) != off64_nr)
- die("inconsistent 64b offset index");
+ die(_("inconsistent 64b offset index"));
if (fread(off64, 8, 1, stdin) != 1)
- die("unable to read 64b offset %u", off64_nr);
+ die(_("unable to read 64b offset %u"), off64_nr);
offset = (((uint64_t)ntohl(off64[0])) << 32) |
ntohl(off64[1]);
off64_nr++;
--
2.52.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* Re: [PATCH] show-index: warn when falling back to SHA-1 outside a repository
2026-01-29 15:36 ` [PATCH] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
@ 2026-01-29 23:03 ` Junio C Hamano
2026-01-30 8:59 ` Shreyansh Paliwal
2026-01-29 23:12 ` brian m. carlson
2026-01-30 15:31 ` [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext Shreyansh Paliwal
2 siblings, 1 reply; 25+ messages in thread
From: Junio C Hamano @ 2026-01-29 23:03 UTC (permalink / raw)
To: Shreyansh Paliwal; +Cc: git, ps
Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
> When 'git show-index' is run outside of a
> repository and no hashing algorithm is
> specified via --object-format, it silently
> falls back to SHA-1, relying on the
> historical default.
>
> This works for existing SHA-1 based
> index files, but the behavior can be ambiguous
> and confusing when the input index file uses a
> different hash algorithm, such as SHA-256.
>
> Add a warning when this fallback happens
> to make the assumption explicit and to
> guide users toward using --object-format
> when needed.
Line wrapping at 50 columns certainly makes the lines narrower than
80 column limit, but let's not go to the extreme. We recommend that
the lines are still less than 80-columns after being quoted a few
times in e-mail exchange (as you can see, I lost 2 columns by
quoting once in the above), which means that around ~70 columns is
the practical fill-column.
> Additionally, wrap user-facing die() messages
> with _() so they can be translated via gettext.
It is somewhat distracting that such "while at it" changes dominate
this ~100-line patch, whose "primary change" is a mere three lines
we can see here:
> - if (!the_hash_algo)
> + if (!the_hash_algo) {
> + warning(_("assuming SHA-1; use --object-format to override"));
> repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
> + }
Can we push the "while at it" message changes to a separate patch, a
preparatory clean-up, on top of which another primary patch adds the
above warning? Alternatively, have the primary patch that adds the
above warning and does nothing else, followed by a post clean-up patch
to tweak the existing error messages?
Thanks.
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH] show-index: warn when falling back to SHA-1 outside a repository
2026-01-29 23:03 ` Junio C Hamano
@ 2026-01-30 8:59 ` Shreyansh Paliwal
0 siblings, 0 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-30 8:59 UTC (permalink / raw)
To: git; +Cc: gitster, ps
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 1917 bytes --]
> > When 'git show-index' is run outside of a
> > repository and no hashing algorithm is
> > specified via --object-format, it silently
> > falls back to SHA-1, relying on the
> > historical default.
> >
> > This works for existing SHA-1 based
> > index files, but the behavior can be ambiguous
> > and confusing when the input index file uses a
> > different hash algorithm, such as SHA-256.
> >
> > Add a warning when this fallback happens
> > to make the assumption explicit and to
> > guide users toward using --object-format
> > when needed.
>
> Line wrapping at 50 columns certainly makes the lines narrower than
> 80 column limit, but let's not go to the extreme. We recommend that
> the lines are still less than 80-columns after being quoted a few
> times in e-mail exchange (as you can see, I lost 2 columns by
> quoting once in the above), which means that around ~70 columns is
> the practical fill-column.
Understood. I Will make sure to keep message wrapping around ~70 columns.
> > Additionally, wrap user-facing die() messages
> > with _() so they can be translated via gettext.
>
> It is somewhat distracting that such "while at it" changes dominate
> this ~100-line patch, whose "primary change" is a mere three lines
> we can see here:
>
> > - if (!the_hash_algo)
> > + if (!the_hash_algo) {
> > + warning(_("assuming SHA-1; use --object-format to override"));
> > repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
> > + }
>
>
> Can we push the "while at it" message changes to a separate patch, a
> preparatory clean-up, on top of which another primary patch adds the
> above warning? Alternatively, have the primary patch that adds the
> above warning and does nothing else, followed by a post clean-up patch
> to tweak the existing error messages?
Yes, agreed. I’ll split the changes into separate patches again, as in
the original RFC series, and send a v2.
Best,
Shreyansh
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH] show-index: warn when falling back to SHA-1 outside a repository
2026-01-29 15:36 ` [PATCH] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
2026-01-29 23:03 ` Junio C Hamano
@ 2026-01-29 23:12 ` brian m. carlson
2026-01-30 9:04 ` Shreyansh Paliwal
2026-01-30 15:31 ` [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext Shreyansh Paliwal
2 siblings, 1 reply; 25+ messages in thread
From: brian m. carlson @ 2026-01-29 23:12 UTC (permalink / raw)
To: Shreyansh Paliwal; +Cc: git, gitster, ps
[-- Attachment #1: Type: text/plain, Size: 878 bytes --]
On 2026-01-29 at 15:36:55, Shreyansh Paliwal wrote:
> /*
> * 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.
> + * TODO: If a future implementation of index file version encodes the hash
> + * algorithm in its header, enable show-index to infer it from the
> + * header rather than relying on repository context or a default fallback.
> */
> - if (!the_hash_algo)
> + if (!the_hash_algo) {
> + warning(_("assuming SHA-1; use --object-format to override"));
> repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
If we're going to start producing a warning, can we also learn a
`--quiet` option to silence it?
--
brian m. carlson (they/them)
Toronto, Ontario, CA
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 262 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH] show-index: warn when falling back to SHA-1 outside a repository
2026-01-29 23:12 ` brian m. carlson
@ 2026-01-30 9:04 ` Shreyansh Paliwal
2026-01-30 13:40 ` Patrick Steinhardt
0 siblings, 1 reply; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-30 9:04 UTC (permalink / raw)
To: git; +Cc: gitster, ps, sandals
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 1183 bytes --]
> On 2026-01-29 at 15:36:55, Shreyansh Paliwal wrote:
> > /*
> > * 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.
> > + * TODO: If a future implementation of index file version encodes the hash
> > + * algorithm in its header, enable show-index to infer it from the
> > + * header rather than relying on repository context or a default fallback.
> > */
> > - if (!the_hash_algo)
> > + if (!the_hash_algo) {
> > + warning(_("assuming SHA-1; use --object-format to override"));
> > repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
>
> If we're going to start producing a warning, can we also learn a
> `--quiet` option to silence it?
Hi,
That is a good point.
However, the warning is only shown for a particular usecase: when
'git show-index' is run outside of a repository and --object-format
is not specified. Given that narrow scope, I’m wondering whether
adding a dedicated --quiet option for only this warning would be worthwhile.
Let me know what you think :)
Best,
Shreyansh
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH] show-index: warn when falling back to SHA-1 outside a repository
2026-01-30 9:04 ` Shreyansh Paliwal
@ 2026-01-30 13:40 ` Patrick Steinhardt
2026-01-30 17:01 ` Junio C Hamano
0 siblings, 1 reply; 25+ messages in thread
From: Patrick Steinhardt @ 2026-01-30 13:40 UTC (permalink / raw)
To: Shreyansh Paliwal; +Cc: git, gitster, sandals
On Fri, Jan 30, 2026 at 02:34:53PM +0530, Shreyansh Paliwal wrote:
> > On 2026-01-29 at 15:36:55, Shreyansh Paliwal wrote:
> > > /*
> > > * 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.
> > > + * TODO: If a future implementation of index file version encodes the hash
> > > + * algorithm in its header, enable show-index to infer it from the
> > > + * header rather than relying on repository context or a default fallback.
> > > */
> > > - if (!the_hash_algo)
> > > + if (!the_hash_algo) {
> > > + warning(_("assuming SHA-1; use --object-format to override"));
> > > repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
> >
> > If we're going to start producing a warning, can we also learn a
> > `--quiet` option to silence it?
>
> Hi,
>
> That is a good point.
> However, the warning is only shown for a particular usecase: when
> 'git show-index' is run outside of a repository and --object-format
> is not specified. Given that narrow scope, I’m wondering whether
> adding a dedicated --quiet option for only this warning would be worthwhile.
>
> Let me know what you think :)
I also wonder whether "--quiet" might be a bit _too_ generic in this
context. I would rather want to use this flag for something that you
actually have a good reason to silence, instead of only for a warning.
In theory, the user already has the ability to silence the warning: they
can simply pass "--object-format=sha256". If you think that's not enough
I'd buid on top of our `advice_if_enabled()` infra, so that the warning
can be globally disabled by setting a config option.
Thanks!
Patrick
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH] show-index: warn when falling back to SHA-1 outside a repository
2026-01-30 13:40 ` Patrick Steinhardt
@ 2026-01-30 17:01 ` Junio C Hamano
0 siblings, 0 replies; 25+ messages in thread
From: Junio C Hamano @ 2026-01-30 17:01 UTC (permalink / raw)
To: Patrick Steinhardt; +Cc: Shreyansh Paliwal, git, sandals
Patrick Steinhardt <ps@pks.im> writes:
> I also wonder whether "--quiet" might be a bit _too_ generic in this
> context. I would rather want to use this flag for something that you
> actually have a good reason to silence, instead of only for a warning.
>
> In theory, the user already has the ability to silence the warning: they
> can simply pass "--object-format=sha256". If you think that's not enough
> I'd buid on top of our `advice_if_enabled()` infra, so that the warning
> can be globally disabled by setting a config option.
I like the "use --object-format then you would never see this
warning" very much. It is possible that we may want to squelch
any and all warning and error messages, not limited to this one,
in which case it is an option to add "--quiet" to the command, but I
somehow feel that it is outside the topic of this change.
Thanks.
^ permalink raw reply [flat|nested] 25+ messages in thread
* [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext
2026-01-29 15:36 ` [PATCH] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
2026-01-29 23:03 ` Junio C Hamano
2026-01-29 23:12 ` brian m. carlson
@ 2026-01-30 15:31 ` Shreyansh Paliwal
2026-01-30 15:31 ` [PATCH V2 1/2] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
` (2 more replies)
2 siblings, 3 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-30 15:31 UTC (permalink / raw)
To: git; +Cc: ps, sandals, gitster, Shreyansh Paliwal
'git show-index' relies on implicit assumption in the absence
of repository context to fall back to SHA-1 as the hashing
algorithm.
patch 1/2: add a warning when this fallback happens.
patch 2/2: cleanup by adding gettext wrapping to all
user-facing error messages in show-index.c.
Shreyansh Paliwal (2):
show-index: warn when falling back to SHA-1 outside a repository
show-index: use gettext wrapping in user facing error messages
builtin/show-index.c | 31 +++++++++++++++++--------------
1 file changed, 17 insertions(+), 14 deletions(-)
---
Changes in V2:
- Split the original patch into two separate patches for better
clarity and distinction.
- Improved line wrapping to around ~70 columns.
--
2.52.0
^ permalink raw reply [flat|nested] 25+ messages in thread* [PATCH V2 1/2] show-index: warn when falling back to SHA-1 outside a repository
2026-01-30 15:31 ` [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext Shreyansh Paliwal
@ 2026-01-30 15:31 ` Shreyansh Paliwal
2026-01-30 15:31 ` [PATCH V2 2/2] show-index: use gettext wrapping in user facing error messages Shreyansh Paliwal
2026-01-30 17:07 ` [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext Junio C Hamano
2 siblings, 0 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-30 15:31 UTC (permalink / raw)
To: git; +Cc: ps, sandals, gitster, Shreyansh Paliwal
When 'git show-index' is run outside of a repository and no hashing
algorithm is specified via --object-format, it silently falls back
to SHA-1, relying on the historical default.
This works for existing SHA-1 based index files, but the behavior can
be ambiguous and confusing when the input index file uses a different
hash algorithm, such as SHA-256.
Add a warning when this fallback happens to make the assumption
explicit and to guide users toward using --object-format when needed.
Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
---
builtin/show-index.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/builtin/show-index.c b/builtin/show-index.c
index 2c3e2940ce..45795da2da 100644
--- a/builtin/show-index.c
+++ b/builtin/show-index.c
@@ -43,11 +43,14 @@ int cmd_show_index(int argc,
/*
* 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.
+ * TODO: If a future implementation of index file version encodes the hash
+ * algorithm in its header, enable show-index to infer it from the
+ * header rather than relying on repository context or a default fallback.
*/
- if (!the_hash_algo)
+ if (!the_hash_algo) {
+ warning(_("assuming SHA-1; use --object-format to override"));
repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
+ }
hashsz = the_hash_algo->rawsz;
--
2.52.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH V2 2/2] show-index: use gettext wrapping in user facing error messages
2026-01-30 15:31 ` [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext Shreyansh Paliwal
2026-01-30 15:31 ` [PATCH V2 1/2] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
@ 2026-01-30 15:31 ` Shreyansh Paliwal
2026-01-30 17:07 ` [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext Junio C Hamano
2 siblings, 0 replies; 25+ messages in thread
From: Shreyansh Paliwal @ 2026-01-30 15:31 UTC (permalink / raw)
To: git; +Cc: ps, sandals, gitster, Shreyansh Paliwal
Multiple 'die()' calls in show-index.c use literal strings directly.
Wrap all user-facing 'die()' messages with '_()' so they can be translated
via gettext, this ensures better support for users.
Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
---
builtin/show-index.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/builtin/show-index.c b/builtin/show-index.c
index 45795da2da..24f0230967 100644
--- a/builtin/show-index.c
+++ b/builtin/show-index.c
@@ -55,23 +55,23 @@ int cmd_show_index(int argc,
hashsz = the_hash_algo->rawsz;
if (fread(top_index, 2 * 4, 1, stdin) != 1)
- die("unable to read header");
+ die(_("unable to read header"));
if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
version = ntohl(top_index[1]);
if (version < 2 || version > 2)
- die("unknown index version");
+ die(_("unknown index version"));
if (fread(top_index, 256 * 4, 1, stdin) != 1)
- die("unable to read index");
+ die(_("unable to read index"));
} else {
version = 1;
if (fread(&top_index[2], 254 * 4, 1, stdin) != 1)
- die("unable to read index");
+ die(_("unable to read index"));
}
nr = 0;
for (i = 0; i < 256; i++) {
unsigned n = ntohl(top_index[i]);
if (n < nr)
- die("corrupt index file");
+ die(_("corrupt index file"));
nr = n;
}
if (version == 1) {
@@ -79,7 +79,7 @@ int cmd_show_index(int argc,
unsigned int offset, entry[(GIT_MAX_RAWSZ + 4) / sizeof(unsigned int)];
if (fread(entry, 4 + hashsz, 1, stdin) != 1)
- die("unable to read entry %u/%u", i, nr);
+ die(_("unable to read entry %u/%u"), i, nr);
offset = ntohl(entry[0]);
printf("%u %s\n", offset, hash_to_hex((void *)(entry+1)));
}
@@ -93,15 +93,15 @@ int cmd_show_index(int argc,
ALLOC_ARRAY(entries, nr);
for (i = 0; i < nr; i++) {
if (fread(entries[i].oid.hash, hashsz, 1, stdin) != 1)
- die("unable to read sha1 %u/%u", i, nr);
+ die(_("unable to read sha1 %u/%u"), i, nr);
entries[i].oid.algo = hash_algo_by_ptr(the_hash_algo);
}
for (i = 0; i < nr; i++)
if (fread(&entries[i].crc, 4, 1, stdin) != 1)
- die("unable to read crc %u/%u", i, nr);
+ die(_("unable to read crc %u/%u"), i, nr);
for (i = 0; i < nr; i++)
if (fread(&entries[i].off, 4, 1, stdin) != 1)
- die("unable to read 32b offset %u/%u", i, nr);
+ die(_("unable to read 32b offset %u/%u"), i, nr);
for (i = 0; i < nr; i++) {
uint64_t offset;
uint32_t off = ntohl(entries[i].off);
@@ -110,9 +110,9 @@ int cmd_show_index(int argc,
} else {
uint32_t off64[2];
if ((off & 0x7fffffff) != off64_nr)
- die("inconsistent 64b offset index");
+ die(_("inconsistent 64b offset index"));
if (fread(off64, 8, 1, stdin) != 1)
- die("unable to read 64b offset %u", off64_nr);
+ die(_("unable to read 64b offset %u"), off64_nr);
offset = (((uint64_t)ntohl(off64[0])) << 32) |
ntohl(off64[1]);
off64_nr++;
--
2.52.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* Re: [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext
2026-01-30 15:31 ` [PATCH V2 0/2] show-index: add warning and wrap error messages with gettext Shreyansh Paliwal
2026-01-30 15:31 ` [PATCH V2 1/2] show-index: warn when falling back to SHA-1 outside a repository Shreyansh Paliwal
2026-01-30 15:31 ` [PATCH V2 2/2] show-index: use gettext wrapping in user facing error messages Shreyansh Paliwal
@ 2026-01-30 17:07 ` Junio C Hamano
2 siblings, 0 replies; 25+ messages in thread
From: Junio C Hamano @ 2026-01-30 17:07 UTC (permalink / raw)
To: Shreyansh Paliwal; +Cc: git, ps, sandals
Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
> Changes in V2:
> - Split the original patch into two separate patches for better
> clarity and distinction.
> - Improved line wrapping to around ~70 columns.
Queued. Thanks.
^ permalink raw reply [flat|nested] 25+ messages in thread