Skip to content

Commit

Permalink
builtin/cat-file: let the oid determine the output algorithm
Browse files Browse the repository at this point in the history
Use GET_OID_HASH_ANY when calling get_oid_with_context.  This
implements the semi-obvious behaviour that specifying a sha1 oid shows
the output for a sha1 encoded object, and specifying a sha256 oid
shows the output for a sha256 encoded object.

This is useful for testing the the conversion of an object to an
equivalent object encoded with a different hash function.

Signed-off-by: "Eric W. Biederman" <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
ebiederm authored and gitster committed Oct 2, 2023
1 parent d7446c8 commit d6222a2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions builtin/cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
struct object_info oi = OBJECT_INFO_INIT;
struct strbuf sb = STRBUF_INIT;
unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
unsigned get_oid_flags =
GET_OID_RECORD_PATH |
GET_OID_ONLY_TO_DIE |
GET_OID_HASH_ANY;
const char *path = force_path;
const int opt_cw = (opt == 'c' || opt == 'w');
if (!path && opt_cw)
Expand Down Expand Up @@ -223,7 +226,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
&size);
const char *target;
if (!skip_prefix(buffer, "object ", &target) ||
get_oid_hex(target, &blob_oid))
get_oid_hex_algop(target, &blob_oid,
&hash_algos[oid.algo]))
die("%s not a valid tag", oid_to_hex(&oid));
free(buffer);
} else
Expand Down Expand Up @@ -512,7 +516,9 @@ static void batch_one_object(const char *obj_name,
struct expand_data *data)
{
struct object_context ctx;
int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
int flags =
GET_OID_HASH_ANY |
(opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0);
enum get_oid_result result;

result = get_oid_with_context(the_repository, obj_name,
Expand Down

0 comments on commit d6222a2

Please sign in to comment.