Skip to content

Commit f89d085

Browse files
avargitster
authored andcommitted
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 5d7eeee (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 5726a6b (*.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%[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 055e57b commit f89d085

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

builtin/log.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,10 @@ static void show_setup_revisions_tweak(struct rev_info *rev,
668668
int cmd_show(int argc, const char **argv, const char *prefix)
669669
{
670670
struct rev_info rev;
671-
struct object_array_entry *objects;
671+
unsigned int i;
672672
struct setup_revision_opt opt;
673673
struct pathspec match_all;
674-
int i, count, ret = 0;
674+
int ret = 0;
675675

676676
init_log_defaults();
677677
git_config(git_log_config, NULL);
@@ -698,12 +698,10 @@ int cmd_show(int argc, const char **argv, const char *prefix)
698698
if (!rev.no_walk)
699699
return cmd_log_deinit(cmd_log_walk(&rev), &rev);
700700

701-
count = rev.pending.nr;
702-
objects = rev.pending.objects;
703701
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;
707705
switch (o->type) {
708706
case OBJ_BLOB:
709707
ret = show_blob_object(&o->oid, &rev, name);
@@ -726,7 +724,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
726724
if (!o)
727725
ret = error(_("could not read object %s"),
728726
oid_to_hex(oid));
729-
objects[i].item = o;
727+
rev.pending.objects[i].item = o;
730728
i--;
731729
break;
732730
}
@@ -745,12 +743,19 @@ int cmd_show(int argc, const char **argv, const char *prefix)
745743
case OBJ_COMMIT:
746744
{
747745
struct object_array old;
746+
struct object_array blank = OBJECT_ARRAY_INIT;
748747

749748
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+
752751
add_object_array(o, name, &rev.pending);
753752
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+
*/
754759
memcpy(&rev.pending, &old, sizeof(rev.pending));
755760
break;
756761
}

0 commit comments

Comments
 (0)