From f89d085b3f6c4861265baed212c8ff6b7a8ec8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 2 Aug 2022 17:33:14 +0200 Subject: log: refactor "rev.pending" code in cmd_show() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Helped-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/builtin/log.c b/builtin/log.c index b4b1d97..9b937d5 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -668,10 +668,10 @@ static void show_setup_revisions_tweak(struct rev_info *rev, int cmd_show(int argc, const char **argv, const char *prefix) { struct rev_info rev; - struct object_array_entry *objects; + unsigned int i; struct setup_revision_opt opt; struct pathspec match_all; - int i, count, ret = 0; + int ret = 0; init_log_defaults(); git_config(git_log_config, NULL); @@ -698,12 +698,10 @@ int cmd_show(int argc, const char **argv, const char *prefix) if (!rev.no_walk) return cmd_log_deinit(cmd_log_walk(&rev), &rev); - count = rev.pending.nr; - objects = rev.pending.objects; rev.diffopt.no_free = 1; - for (i = 0; i < count && !ret; i++) { - struct object *o = objects[i].item; - const char *name = objects[i].name; + for (i = 0; i < rev.pending.nr && !ret; i++) { + struct object *o = rev.pending.objects[i].item; + const char *name = rev.pending.objects[i].name; switch (o->type) { case OBJ_BLOB: ret = show_blob_object(&o->oid, &rev, name); @@ -726,7 +724,7 @@ int cmd_show(int argc, const char **argv, const char *prefix) if (!o) ret = error(_("could not read object %s"), oid_to_hex(oid)); - objects[i].item = o; + rev.pending.objects[i].item = o; i--; break; } @@ -745,12 +743,19 @@ int cmd_show(int argc, const char **argv, const char *prefix) case OBJ_COMMIT: { struct object_array old; + struct object_array blank = OBJECT_ARRAY_INIT; memcpy(&old, &rev.pending, sizeof(old)); - rev.pending.nr = rev.pending.alloc = 0; - rev.pending.objects = NULL; + memcpy(&rev.pending, &blank, sizeof(rev.pending)); + add_object_array(o, name, &rev.pending); ret = cmd_log_walk_no_free(&rev); + + /* + * No need for + * object_array_clear(&pending). It was + * cleared already in prepare_revision_walk() + */ memcpy(&rev.pending, &old, sizeof(rev.pending)); break; } -- cgit v0.10.2-6-g49f6