Skip to content

Commit d7446c8

Browse files
ebiedermgitster
authored andcommitted
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" <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9ae702f commit d7446c8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Documentation/git-rev-parse.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ for another option.
159159
unfortunately named tag "master"), and show them as full
160160
refnames (e.g. "refs/heads/master").
161161

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+
162174
Options for Objects
163175
~~~~~~~~~~~~~~~~~~~
164176

builtin/rev-parse.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "submodule.h"
2626
#include "commit-reach.h"
2727
#include "shallow.h"
28+
#include "object-file-convert.h"
2829

2930
#define DO_REVS 1
3031
#define DO_NOREV 2
@@ -675,6 +676,8 @@ static void print_path(const char *path, const char *prefix, enum format_type fo
675676
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
676677
{
677678
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;
678681
int did_repo_setup = 0;
679682
int has_dashdash = 0;
680683
int output_prefix = 0;
@@ -746,6 +749,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
746749

747750
prepare_repo_settings(the_repository);
748751
the_repository->settings.command_requires_full_index = 0;
752+
compat = the_repository->compat_hash_algo;
749753
}
750754

751755
if (!strcmp(arg, "--")) {
@@ -833,6 +837,22 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
833837
flags |= GET_OID_QUIETLY;
834838
continue;
835839
}
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+
}
836856
if (opt_with_value(arg, "--short", &arg)) {
837857
filter &= ~(DO_FLAGS|DO_NOREV);
838858
verify = 1;
@@ -1083,6 +1103,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
10831103
}
10841104
if (!get_oid_with_context(the_repository, name,
10851105
flags, &oid, &unused)) {
1106+
if (output_algo)
1107+
repo_oid_to_algop(the_repository, &oid,
1108+
output_algo, &oid);
10861109
if (verify)
10871110
revs_count++;
10881111
else

0 commit comments

Comments
 (0)