branch: refactor width computation

Remove unnecessary variables from ref_list and ref_item which were used for width computation. This is to make ref_item similar to ref-filter's ref_array_item. This will ensure a smooth port of branch.c to use ref-filter APIs in further patches. Previously the maxwidth was computed when inserting the refs into the ref_list. Now, we obtain the entire ref_list and then compute maxwidth. Based-on-patch-by: Jeff King <peff@peff.net> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Sep 23, 2015 at 23:41 UTC 1051e40dba16bd8e490c41ce926c8c36b913de72
1 file changed +34 -30
builtin/branch.c
+34 -30
@@ -282,14 +282,14 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
282 struct ref_item {
283 char *name;
284 char *dest;
285 - unsigned int kind, width;
285 + unsigned int kind;
286 struct commit *commit;
287 int ignore;
288 };
289
290 struct ref_list {
291 struct rev_info revs;
292 - int index, alloc, maxwidth, verbose, abbrev;
292 + int index, alloc, verbose, abbrev;
293 struct ref_item *list;
294 struct commit_list *with_commit;
295 int kinds;
@@ -386,15 +386,8 @@ static int append_ref(const char *refname, const struct object_id *oid, int flag
386 newitem->name = xstrdup(refname);
387 newitem->kind = kind;
388 newitem->commit = commit;
389 - newitem->width = utf8_strwidth(refname);
389 newitem->dest = resolve_symref(orig_refname, prefix);
390 newitem->ignore = 0;
392 - /* adjust for "remotes/" */
393 - if (newitem->kind == REF_REMOTE_BRANCH &&
394 - ref_list->kinds != REF_REMOTE_BRANCH)
395 - newitem->width += 8;
396 - if (newitem->width > ref_list->maxwidth)
397 - ref_list->maxwidth = newitem->width;
391
392 return 0;
393 }
@@ -505,11 +498,12 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
498 }
499
500 static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
508 - int abbrev, int current, char *prefix)
501 + int abbrev, int current, const char *remote_prefix)
502 {
503 char c;
504 int color;
505 struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
506 + const char *prefix = "";
507
508 if (item->ignore)
509 return;
@@ -520,6 +514,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
514 break;
515 case REF_REMOTE_BRANCH:
516 color = BRANCH_COLOR_REMOTE;
517 + prefix = remote_prefix;
518 break;
519 default:
520 color = BRANCH_COLOR_PLAIN;
@@ -557,16 +552,22 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
552 strbuf_release(&out);
553 }
554
560 -static int calc_maxwidth(struct ref_list *refs)
555 +static int calc_maxwidth(struct ref_list *refs, int remote_bonus)
556 {
562 - int i, w = 0;
557 + int i, max = 0;
558 for (i = 0; i < refs->index; i++) {
564 - if (refs->list[i].ignore)
559 + struct ref_item *it = &refs->list[i];
560 + int w;
561 +
562 + if (it->ignore)
563 continue;
566 - if (refs->list[i].width > w)
567 - w = refs->list[i].width;
564 + w = utf8_strwidth(it->name);
565 + if (it->kind == REF_REMOTE_BRANCH)
566 + w += remote_bonus;
567 + if (w > max)
568 + max = w;
569 }
569 - return w;
570 + return max;
571 }
572
573 static char *get_head_description(void)
@@ -600,21 +601,18 @@ static char *get_head_description(void)
601 return strbuf_detach(&desc, NULL);
602 }
603
603 -static void show_detached(struct ref_list *ref_list)
604 +static void show_detached(struct ref_list *ref_list, int maxwidth)
605 {
606 struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
607
608 if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
609 struct ref_item item;
610 item.name = get_head_description();
610 - item.width = utf8_strwidth(item.name);
611 item.kind = REF_LOCAL_BRANCH;
612 item.dest = NULL;
613 item.commit = head_commit;
614 item.ignore = 0;
615 - if (item.width > ref_list->maxwidth)
616 - ref_list->maxwidth = item.width;
617 - print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
615 + print_ref_item(&item, maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
616 free(item.name);
617 }
618 }
@@ -624,6 +622,16 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
622 int i;
623 struct append_ref_cb cb;
624 struct ref_list ref_list;
625 + int maxwidth = 0;
626 + const char *remote_prefix = "";
627 +
628 + /*
629 + * If we are listing more than just remote branches,
630 + * then remote branches will have a "remotes/" prefix.
631 + * We need to account for this in the width.
632 + */
633 + if (kinds != REF_REMOTE_BRANCH)
634 + remote_prefix = "remotes/";
635
636 memset(&ref_list, 0, sizeof(ref_list));
637 ref_list.kinds = kinds;
@@ -667,26 +675,22 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
675 clear_commit_marks(item->commit, ALL_REV_FLAGS);
676 }
677 clear_commit_marks(filter, ALL_REV_FLAGS);
670 -
671 - if (verbose)
672 - ref_list.maxwidth = calc_maxwidth(&ref_list);
678 }
679 + if (verbose)
680 + maxwidth = calc_maxwidth(&ref_list, strlen(remote_prefix));
681
682 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
683
684 detached = (detached && (kinds & REF_LOCAL_BRANCH));
685 if (detached && match_patterns(pattern, "HEAD"))
679 - show_detached(&ref_list);
686 + show_detached(&ref_list, maxwidth);
687
688 for (i = 0; i < ref_list.index; i++) {
689 int current = !detached &&
690 (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
691 !strcmp(ref_list.list[i].name, head);
685 - char *prefix = (kinds != REF_REMOTE_BRANCH &&
686 - ref_list.list[i].kind == REF_REMOTE_BRANCH)
687 - ? "remotes/" : "";
688 - print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
689 - abbrev, current, prefix);
692 + print_ref_item(&ref_list.list[i], maxwidth, verbose,
693 + abbrev, current, remote_prefix);
694 }
695
696 free_ref_list(&ref_list);