Skip to content

Commit 1051e40

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Mentored-by: Christian Couder <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5242860 commit 1051e40

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

builtin/branch.c

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
282282
struct ref_item {
283283
char *name;
284284
char *dest;
285-
unsigned int kind, width;
285+
unsigned int kind;
286286
struct commit *commit;
287287
int ignore;
288288
};
289289

290290
struct ref_list {
291291
struct rev_info revs;
292-
int index, alloc, maxwidth, verbose, abbrev;
292+
int index, alloc, verbose, abbrev;
293293
struct ref_item *list;
294294
struct commit_list *with_commit;
295295
int kinds;
@@ -386,15 +386,8 @@ static int append_ref(const char *refname, const struct object_id *oid, int flag
386386
newitem->name = xstrdup(refname);
387387
newitem->kind = kind;
388388
newitem->commit = commit;
389-
newitem->width = utf8_strwidth(refname);
390389
newitem->dest = resolve_symref(orig_refname, prefix);
391390
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;
398391

399392
return 0;
400393
}
@@ -505,11 +498,12 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
505498
}
506499

507500
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)
509502
{
510503
char c;
511504
int color;
512505
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
506+
const char *prefix = "";
513507

514508
if (item->ignore)
515509
return;
@@ -520,6 +514,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
520514
break;
521515
case REF_REMOTE_BRANCH:
522516
color = BRANCH_COLOR_REMOTE;
517+
prefix = remote_prefix;
523518
break;
524519
default:
525520
color = BRANCH_COLOR_PLAIN;
@@ -557,16 +552,22 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
557552
strbuf_release(&out);
558553
}
559554

560-
static int calc_maxwidth(struct ref_list *refs)
555+
static int calc_maxwidth(struct ref_list *refs, int remote_bonus)
561556
{
562-
int i, w = 0;
557+
int i, max = 0;
563558
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)
565563
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;
568569
}
569-
return w;
570+
return max;
570571
}
571572

572573
static char *get_head_description(void)
@@ -600,21 +601,18 @@ static char *get_head_description(void)
600601
return strbuf_detach(&desc, NULL);
601602
}
602603

603-
static void show_detached(struct ref_list *ref_list)
604+
static void show_detached(struct ref_list *ref_list, int maxwidth)
604605
{
605606
struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
606607

607608
if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
608609
struct ref_item item;
609610
item.name = get_head_description();
610-
item.width = utf8_strwidth(item.name);
611611
item.kind = REF_LOCAL_BRANCH;
612612
item.dest = NULL;
613613
item.commit = head_commit;
614614
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, "");
618616
free(item.name);
619617
}
620618
}
@@ -624,6 +622,16 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
624622
int i;
625623
struct append_ref_cb cb;
626624
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/";
627635

628636
memset(&ref_list, 0, sizeof(ref_list));
629637
ref_list.kinds = kinds;
@@ -667,26 +675,22 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
667675
clear_commit_marks(item->commit, ALL_REV_FLAGS);
668676
}
669677
clear_commit_marks(filter, ALL_REV_FLAGS);
670-
671-
if (verbose)
672-
ref_list.maxwidth = calc_maxwidth(&ref_list);
673678
}
679+
if (verbose)
680+
maxwidth = calc_maxwidth(&ref_list, strlen(remote_prefix));
674681

675682
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
676683

677684
detached = (detached && (kinds & REF_LOCAL_BRANCH));
678685
if (detached && match_patterns(pattern, "HEAD"))
679-
show_detached(&ref_list);
686+
show_detached(&ref_list, maxwidth);
680687

681688
for (i = 0; i < ref_list.index; i++) {
682689
int current = !detached &&
683690
(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
684691
!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);
690694
}
691695

692696
free_ref_list(&ref_list);

0 commit comments

Comments
 (0)