log: refactor "rev.pending" code in cmd_show()

Refactor the juggling of "rev.pending" and our replacement for it amended in the preceding commit so that: * We use an "unsigned int" instead of an "int" for "i", this matches the types of "struct rev_info" itself. * We don't need the "count" and "objects" variables introduced in 5d7eeee2ac6 (git-show: grok blobs, trees and tags, too, 2006-12-14). They were originally added since we'd clobber rev.pending in the loop without restoring it. Since the preceding commit we are restoring it when we handle OBJ_COMMIT, so the main for-loop can refer to "rev.pending" didrectly. * We use the "memcpy a &blank" idiom introduced in 5726a6b4012 (*.c *_init(): define in terms of corresponding *_INIT macro, 2021-07-01). This is more obvious than relying on us enumerating all of the relevant members of the "struct object_array" that we need to clear. * We comment on why we don't need an object_array_clear() here, see the analysis in [1]. 1. https://lore.kernel.org/git/YuQtJ2DxNKX%2Fy70N@coredump.intra.peff.net/ Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Aug 2, 2022 at 17:33 UTC f89d085b3f6c4861265baed212c8ff6b7a8ec8e6
1 file changed +15 -10
builtin/log.c
+15 -10
@@ -668,10 +668,10 @@ static void show_setup_revisions_tweak(struct rev_info *rev,
668 int cmd_show(int argc, const char **argv, const char *prefix)
669 {
670 struct rev_info rev;
671 - struct object_array_entry *objects;
671 + unsigned int i;
672 struct setup_revision_opt opt;
673 struct pathspec match_all;
674 - int i, count, ret = 0;
674 + int ret = 0;
675
676 init_log_defaults();
677 git_config(git_log_config, NULL);
@@ -698,12 +698,10 @@ int cmd_show(int argc, const char **argv, const char *prefix)
698 if (!rev.no_walk)
699 return cmd_log_deinit(cmd_log_walk(&rev), &rev);
700
701 - count = rev.pending.nr;
702 - objects = rev.pending.objects;
701 rev.diffopt.no_free = 1;
704 - for (i = 0; i < count && !ret; i++) {
705 - struct object *o = objects[i].item;
706 - const char *name = objects[i].name;
702 + for (i = 0; i < rev.pending.nr && !ret; i++) {
703 + struct object *o = rev.pending.objects[i].item;
704 + const char *name = rev.pending.objects[i].name;
705 switch (o->type) {
706 case OBJ_BLOB:
707 ret = show_blob_object(&o->oid, &rev, name);
@@ -726,7 +724,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
724 if (!o)
725 ret = error(_("could not read object %s"),
726 oid_to_hex(oid));
729 - objects[i].item = o;
727 + rev.pending.objects[i].item = o;
728 i--;
729 break;
730 }
@@ -745,12 +743,19 @@ int cmd_show(int argc, const char **argv, const char *prefix)
743 case OBJ_COMMIT:
744 {
745 struct object_array old;
746 + struct object_array blank = OBJECT_ARRAY_INIT;
747
748 memcpy(&old, &rev.pending, sizeof(old));
750 - rev.pending.nr = rev.pending.alloc = 0;
751 - rev.pending.objects = NULL;
749 + memcpy(&rev.pending, &blank, sizeof(rev.pending));
750 +
751 add_object_array(o, name, &rev.pending);
752 ret = cmd_log_walk_no_free(&rev);
753 +
754 + /*
755 + * No need for
756 + * object_array_clear(&pending). It was
757 + * cleared already in prepare_revision_walk()
758 + */
759 memcpy(&rev.pending, &old, sizeof(rev.pending));
760 break;
761 }