builtin/cat-file: let the oid determine the output algorithm

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" <ebiederm@xmission.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric W. Biederman committed Oct 1, 2023 at 21:40 UTC d6222a2d05c93b22d5ef19b7361f7b1ef2964eb4
1 file changed +9 -3
builtin/cat-file.c
+9 -3
@@ -107,7 +107,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
107 struct object_info oi = OBJECT_INFO_INIT;
108 struct strbuf sb = STRBUF_INIT;
109 unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
110 - unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
110 + unsigned get_oid_flags =
111 + GET_OID_RECORD_PATH |
112 + GET_OID_ONLY_TO_DIE |
113 + GET_OID_HASH_ANY;
114 const char *path = force_path;
115 const int opt_cw = (opt == 'c' || opt == 'w');
116 if (!path && opt_cw)
@@ -223,7 +226,8 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
226 &size);
227 const char *target;
228 if (!skip_prefix(buffer, "object ", &target) ||
226 - get_oid_hex(target, &blob_oid))
229 + get_oid_hex_algop(target, &blob_oid,
230 + &hash_algos[oid.algo]))
231 die("%s not a valid tag", oid_to_hex(&oid));
232 free(buffer);
233 } else
@@ -512,7 +516,9 @@ static void batch_one_object(const char *obj_name,
516 struct expand_data *data)
517 {
518 struct object_context ctx;
515 - int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
519 + int flags =
520 + GET_OID_HASH_ANY |
521 + (opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0);
522 enum get_oid_result result;
523
524 result = get_oid_with_context(the_repository, obj_name,