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>
This commit is contained in:
Eric W. Biederman
2023-10-01 21:40:26 -05:00
committed by Junio C Hamano
parent 9ae702faf1
commit d7446c89b8
2 changed files with 35 additions and 0 deletions

View File

@ -159,6 +159,18 @@ for another option.
unfortunately named tag "master"), and show them as full unfortunately named tag "master"), and show them as full
refnames (e.g. "refs/heads/master"). refnames (e.g. "refs/heads/master").
--output-object-format=(sha1|sha256|storage)::
Allow oids to be input from any object format that the current
repository supports.
Specifying "sha1" translates if necessary and returns a sha1 oid.
Specifying "sha256" translates if necessary and returns a sha256 oid.
Specifying "storage" translates if necessary and returns an oid in
encoded in the storage hash algorithm.
Options for Objects Options for Objects
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

View File

@ -25,6 +25,7 @@
#include "submodule.h" #include "submodule.h"
#include "commit-reach.h" #include "commit-reach.h"
#include "shallow.h" #include "shallow.h"
#include "object-file-convert.h"
#define DO_REVS 1 #define DO_REVS 1
#define DO_NOREV 2 #define DO_NOREV 2
@ -675,6 +676,8 @@ static void print_path(const char *path, const char *prefix, enum format_type fo
int cmd_rev_parse(int argc, const char **argv, const char *prefix) int cmd_rev_parse(int argc, const char **argv, const char *prefix)
{ {
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
const struct git_hash_algo *output_algo = NULL;
const struct git_hash_algo *compat = NULL;
int did_repo_setup = 0; int did_repo_setup = 0;
int has_dashdash = 0; int has_dashdash = 0;
int output_prefix = 0; int output_prefix = 0;
@ -746,6 +749,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
prepare_repo_settings(the_repository); prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0; the_repository->settings.command_requires_full_index = 0;
compat = the_repository->compat_hash_algo;
} }
if (!strcmp(arg, "--")) { if (!strcmp(arg, "--")) {
@ -833,6 +837,22 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
flags |= GET_OID_QUIETLY; flags |= GET_OID_QUIETLY;
continue; continue;
} }
if (opt_with_value(arg, "--output-object-format", &arg)) {
if (!arg)
die(_("no object format specified"));
if (!strcmp(arg, the_hash_algo->name) ||
!strcmp(arg, "storage")) {
flags |= GET_OID_HASH_ANY;
output_algo = the_hash_algo;
continue;
}
else if (compat && !strcmp(arg, compat->name)) {
flags |= GET_OID_HASH_ANY;
output_algo = compat;
continue;
}
else die(_("unsupported object format: %s"), arg);
}
if (opt_with_value(arg, "--short", &arg)) { if (opt_with_value(arg, "--short", &arg)) {
filter &= ~(DO_FLAGS|DO_NOREV); filter &= ~(DO_FLAGS|DO_NOREV);
verify = 1; verify = 1;
@ -1083,6 +1103,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
} }
if (!get_oid_with_context(the_repository, name, if (!get_oid_with_context(the_repository, name,
flags, &oid, &unused)) { flags, &oid, &unused)) {
if (output_algo)
repo_oid_to_algop(the_repository, &oid,
output_algo, &oid);
if (verify) if (verify)
revs_count++; revs_count++;
else else