]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/cat-file: introduce function to report object status
authorPatrick Steinhardt <ps@pks.im>
Wed, 2 Apr 2025 11:13:37 +0000 (13:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 7 Apr 2025 21:43:49 +0000 (14:43 -0700)
We have multiple callsites that report the status of an object, for
example when the objec tis missing or its name is ambiguous. We're about
to add a couple more such callsites to report on "excluded" objects.

Prepare for this by introducing a new function `report_object_status()`
that encapsulates the functionality.

Note that this function also flushes stdout, which is a requirement so
that request-response style batched modes can learn about the status
before proceeding to the next object. We already flush correctly at all
existing callsites, even though the flush in `batch_one_object()` only
comes after the switch statement. That flush is now redundant, and we
could in theory deduplicate it by moving it into all branches that don't
use `report_object_status()`. But that doesn't quite feel sensible:

  - The duplicate flush should ultimately just be a no-op for us and
    thus shouldn't impact performance significantly.

  - By keeping the flush in `report_object_status()` we ensure that all
    future callers get semantics correct.

So let's just be pragmatic and live with the duplicated flush.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/cat-file.c

index b158b3acef945346605a9976b0939e18d96e1640..1261a3ce352becf5ab0f9e65cb247157299815e4 100644 (file)
@@ -455,6 +455,16 @@ static void print_default_format(struct strbuf *scratch, struct expand_data *dat
                    (uintmax_t)data->size, opt->output_delim);
 }
 
+static void report_object_status(struct batch_options *opt,
+                                const char *obj_name,
+                                const struct object_id *oid,
+                                const char *status)
+{
+       printf("%s %s%c", obj_name ? obj_name : oid_to_hex(oid),
+              status, opt->output_delim);
+       fflush(stdout);
+}
+
 /*
  * If "pack" is non-NULL, then "offset" is the byte offset within the pack from
  * which the object may be accessed (though note that we may also rely on
@@ -481,9 +491,7 @@ static void batch_object_write(const char *obj_name,
                                                       &data->oid, &data->info,
                                                       OBJECT_INFO_LOOKUP_REPLACE);
                if (ret < 0) {
-                       printf("%s missing%c",
-                              obj_name ? obj_name : oid_to_hex(&data->oid), opt->output_delim);
-                       fflush(stdout);
+                       report_object_status(opt, obj_name, &data->oid, "missing");
                        return;
                }
 
@@ -535,10 +543,10 @@ static void batch_one_object(const char *obj_name,
        if (result != FOUND) {
                switch (result) {
                case MISSING_OBJECT:
-                       printf("%s missing%c", obj_name, opt->output_delim);
+                       report_object_status(opt, obj_name, &data->oid, "missing");
                        break;
                case SHORT_NAME_AMBIGUOUS:
-                       printf("%s ambiguous%c", obj_name, opt->output_delim);
+                       report_object_status(opt, obj_name, &data->oid, "ambiguous");
                        break;
                case DANGLING_SYMLINK:
                        printf("dangling %"PRIuMAX"%c%s%c",