Skip to content

Commit 88a09a5

Browse files
bk2204gitster
authored andcommitted
builtin/show-index: provide options to determine hash algo
show-index is capable of reading any possible index file whether or not the index is inside a repository. However, because our index files lack metadata about the hash algorithm in use, it's not possible to autodetect the algorithm that a particular index file is using. In order to allow us to read index files of any algorithm, let's set up the .git directory gently so that we default to the algorithm for the current repository, and add an --object-format option to allow users to override this setting and continue to run show-index outside of a repository altogether. Let's also document this new option so that people can find it and use it. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1610dda commit 88a09a5

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

Documentation/git-show-index.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-show-index - Show packed archive index
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git show-index'
12+
'git show-index' [--object-format=<hash-algorithm>]
1313

1414

1515
DESCRIPTION
@@ -36,6 +36,15 @@ Note that you can get more information on a packfile by calling
3636
linkgit:git-verify-pack[1]. However, as this command considers only the
3737
index file itself, it's both faster and more flexible.
3838

39+
OPTIONS
40+
-------
41+
42+
--object-format=<hash-algorithm>::
43+
Specify the given object format (hash algorithm) for the index file. The
44+
valid values are 'sha1' and (if enabled) 'sha256'. The default is the
45+
algorithm for the current repository (set by `extensions.objectFormat`), or
46+
'sha1' if no value is set or outside a repository..
47+
3948
GIT
4049
---
4150
Part of the linkgit:git[1] suite

builtin/show-index.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
#include "builtin.h"
22
#include "cache.h"
33
#include "pack.h"
4+
#include "parse-options.h"
45

5-
static const char show_index_usage[] =
6-
"git show-index";
6+
static const char *const show_index_usage[] = {
7+
"git show-index [--object-format=<hash-algorithm>]",
8+
NULL
9+
};
710

811
int cmd_show_index(int argc, const char **argv, const char *prefix)
912
{
1013
int i;
1114
unsigned nr;
1215
unsigned int version;
1316
static unsigned int top_index[256];
14-
const unsigned hashsz = the_hash_algo->rawsz;
17+
unsigned hashsz;
18+
const char *hash_name = NULL;
19+
int hash_algo;
20+
const struct option show_index_options[] = {
21+
OPT_STRING(0, "object-format", &hash_name, N_("hash-algorithm"),
22+
N_("specify the hash algorithm to use")),
23+
OPT_END()
24+
};
25+
26+
argc = parse_options(argc, argv, prefix, show_index_options, show_index_usage, 0);
27+
28+
if (hash_name) {
29+
hash_algo = hash_algo_by_name(hash_name);
30+
if (hash_algo == GIT_HASH_UNKNOWN)
31+
die(_("Unknown hash algorithm"));
32+
repo_set_hash_algo(the_repository, hash_algo);
33+
}
34+
35+
hashsz = the_hash_algo->rawsz;
1536

16-
if (argc != 1)
17-
usage(show_index_usage);
1837
if (fread(top_index, 2 * 4, 1, stdin) != 1)
1938
die("unable to read header");
2039
if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ static struct cmd_struct commands[] = {
573573
{ "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
574574
{ "show", cmd_show, RUN_SETUP },
575575
{ "show-branch", cmd_show_branch, RUN_SETUP },
576-
{ "show-index", cmd_show_index },
576+
{ "show-index", cmd_show_index, RUN_SETUP_GENTLY },
577577
{ "show-ref", cmd_show_ref, RUN_SETUP },
578578
{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
579579
{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },

0 commit comments

Comments
 (0)