rev-parse: add an --output-object-format parameter

The new --output-object-format parameter returns the oid in the specified format. This is a generally useful plumbing facility. It is useful for writing test cases and for directly querying the translation maps. Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric W. Biederman committed Oct 1, 2023 at 21:40 UTC d7446c89b8ed57f1fa0a5b2ea5e4b7e5fcb7d9cd
2 files changed +35
Documentation/git-rev-parse.txt
+12
@@ -159,6 +159,18 @@ for another option.
159 unfortunately named tag "master"), and show them as full
160 refnames (e.g. "refs/heads/master").
161
162 +--output-object-format=(sha1|sha256|storage)::
163 +
164 + Allow oids to be input from any object format that the current
165 + repository supports.
166 +
167 + Specifying "sha1" translates if necessary and returns a sha1 oid.
168 +
169 + Specifying "sha256" translates if necessary and returns a sha256 oid.
170 +
171 + Specifying "storage" translates if necessary and returns an oid in
172 + encoded in the storage hash algorithm.
173 +
174 Options for Objects
175 ~~~~~~~~~~~~~~~~~~~
176
builtin/rev-parse.c
+23
@@ -25,6 +25,7 @@
25 #include "submodule.h"
26 #include "commit-reach.h"
27 #include "shallow.h"
28 +#include "object-file-convert.h"
29
30 #define DO_REVS 1
31 #define DO_NOREV 2
@@ -675,6 +676,8 @@ static void print_path(const char *path, const char *prefix, enum format_type fo
676 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
677 {
678 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
679 + const struct git_hash_algo *output_algo = NULL;
680 + const struct git_hash_algo *compat = NULL;
681 int did_repo_setup = 0;
682 int has_dashdash = 0;
683 int output_prefix = 0;
@@ -746,6 +749,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
749
750 prepare_repo_settings(the_repository);
751 the_repository->settings.command_requires_full_index = 0;
752 + compat = the_repository->compat_hash_algo;
753 }
754
755 if (!strcmp(arg, "--")) {
@@ -833,6 +837,22 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
837 flags |= GET_OID_QUIETLY;
838 continue;
839 }
840 + if (opt_with_value(arg, "--output-object-format", &arg)) {
841 + if (!arg)
842 + die(_("no object format specified"));
843 + if (!strcmp(arg, the_hash_algo->name) ||
844 + !strcmp(arg, "storage")) {
845 + flags |= GET_OID_HASH_ANY;
846 + output_algo = the_hash_algo;
847 + continue;
848 + }
849 + else if (compat && !strcmp(arg, compat->name)) {
850 + flags |= GET_OID_HASH_ANY;
851 + output_algo = compat;
852 + continue;
853 + }
854 + else die(_("unsupported object format: %s"), arg);
855 + }
856 if (opt_with_value(arg, "--short", &arg)) {
857 filter &= ~(DO_FLAGS|DO_NOREV);
858 verify = 1;
@@ -1083,6 +1103,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
1103 }
1104 if (!get_oid_with_context(the_repository, name,
1105 flags, &oid, &unused)) {
1106 + if (output_algo)
1107 + repo_oid_to_algop(the_repository, &oid,
1108 + output_algo, &oid);
1109 if (verify)
1110 revs_count++;
1111 else