]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/show-index: provide options to determine hash algo
authorbrian m. carlson <sandals@crustytoothpaste.net>
Mon, 25 May 2020 19:59:12 +0000 (19:59 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 May 2020 17:07:07 +0000 (10:07 -0700)
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 <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-show-index.txt
builtin/show-index.c
git.c

index 424e4ba84cf9b0444a41f0a163de0a58a6ac378a..39b1d8eaa1456f60af07de2e30f0653f9d5efd64 100644 (file)
@@ -9,7 +9,7 @@ git-show-index - Show packed archive index
 SYNOPSIS
 --------
 [verse]
-'git show-index'
+'git show-index' [--object-format=<hash-algorithm>]
 
 
 DESCRIPTION
@@ -36,6 +36,15 @@ Note that you can get more information on a packfile by calling
 linkgit:git-verify-pack[1]. However, as this command considers only the
 index file itself, it's both faster and more flexible.
 
+OPTIONS
+-------
+
+--object-format=<hash-algorithm>::
+       Specify the given object format (hash algorithm) for the index file.  The
+       valid values are 'sha1' and (if enabled) 'sha256'.  The default is the
+       algorithm for the current repository (set by `extensions.objectFormat`), or
+       'sha1' if no value is set or outside a repository..
+
 GIT
 ---
 Part of the linkgit:git[1] suite
index 0826f6a5a2c2207d1a17070f0f246cc999f31104..8106b03a6b3243c4d8c5e86e9b668727598696ed 100644 (file)
@@ -1,9 +1,12 @@
 #include "builtin.h"
 #include "cache.h"
 #include "pack.h"
+#include "parse-options.h"
 
-static const char show_index_usage[] =
-"git show-index";
+static const char *const show_index_usage[] = {
+       "git show-index [--object-format=<hash-algorithm>]",
+       NULL
+};
 
 int cmd_show_index(int argc, const char **argv, const char *prefix)
 {
@@ -11,10 +14,26 @@ int cmd_show_index(int argc, const char **argv, const char *prefix)
        unsigned nr;
        unsigned int version;
        static unsigned int top_index[256];
-       const unsigned hashsz = the_hash_algo->rawsz;
+       unsigned hashsz;
+       const char *hash_name = NULL;
+       int hash_algo;
+       const struct option show_index_options[] = {
+               OPT_STRING(0, "object-format", &hash_name, N_("hash-algorithm"),
+                          N_("specify the hash algorithm to use")),
+               OPT_END()
+       };
+
+       argc = parse_options(argc, argv, prefix, show_index_options, show_index_usage, 0);
+
+       if (hash_name) {
+               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);
+       }
+
+       hashsz = the_hash_algo->rawsz;
 
-       if (argc != 1)
-               usage(show_index_usage);
        if (fread(top_index, 2 * 4, 1, stdin) != 1)
                die("unable to read header");
        if (top_index[0] == htonl(PACK_IDX_SIGNATURE)) {
diff --git a/git.c b/git.c
index 2e4efb4ff08f16cd6040350bf2720e0375adab96..e53e8159a2a63f04f1a5836c996d4d62c4588797 100644 (file)
--- a/git.c
+++ b/git.c
@@ -573,7 +573,7 @@ static struct cmd_struct commands[] = {
        { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
        { "show", cmd_show, RUN_SETUP },
        { "show-branch", cmd_show_branch, RUN_SETUP },
-       { "show-index", cmd_show_index },
+       { "show-index", cmd_show_index, RUN_SETUP_GENTLY },
        { "show-ref", cmd_show_ref, RUN_SETUP },
        { "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
        { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },