cat-file: introduce batch_mode enum to replace print_contents

A future patch introduces a new --batch-command flag. Including --batch and --batch-check, we will have a total of three batch modes. print_contents is the only boolean on the batch_options sturct used to distinguish between the different modes. This makes the code harder to read. To reduce potential confusion, replace print_contents with an enum to help readability and clarity. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: John Cai <johncai86@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Cai committed Feb 18, 2022 at 18:23 UTC ac4e58cab9a946d4c45f1db5ee7c79b6fb14bd67
1 file changed +16 -4
builtin/cat-file.c
+16 -4
@@ -17,10 +17,15 @@
17 #include "object-store.h"
18 #include "promisor-remote.h"
19
20 +enum batch_mode {
21 + BATCH_MODE_CONTENTS,
22 + BATCH_MODE_INFO,
23 +};
24 +
25 struct batch_options {
26 int enabled;
27 int follow_symlinks;
23 - int print_contents;
28 + enum batch_mode batch_mode;
29 int buffer_output;
30 int all_objects;
31 int unordered;
@@ -386,7 +391,7 @@ static void batch_object_write(const char *obj_name,
391 strbuf_addch(scratch, '\n');
392 batch_write(opt, scratch->buf, scratch->len);
393
389 - if (opt->print_contents) {
394 + if (opt->batch_mode == BATCH_MODE_CONTENTS) {
395 print_object_or_die(opt, data);
396 batch_write(opt, "\n", 1);
397 }
@@ -536,7 +541,7 @@ static int batch_objects(struct batch_options *opt)
541 * If we are printing out the object, then always fill in the type,
542 * since we will want to decide whether or not to stream.
543 */
539 - if (opt->print_contents)
544 + if (opt->batch_mode == BATCH_MODE_CONTENTS)
545 data.info.typep = &data.type;
546
547 if (opt->all_objects) {
@@ -635,7 +640,14 @@ static int batch_option_callback(const struct option *opt,
640 }
641
642 bo->enabled = 1;
638 - bo->print_contents = !strcmp(opt->long_name, "batch");
643 +
644 + if (!strcmp(opt->long_name, "batch"))
645 + bo->batch_mode = BATCH_MODE_CONTENTS;
646 + else if (!strcmp(opt->long_name, "batch-check"))
647 + bo->batch_mode = BATCH_MODE_INFO;
648 + else
649 + BUG("%s given to batch-option-callback", opt->long_name);
650 +
651 bo->format = arg;
652
653 return 0;