branch: move 'current' check down to the presentation layer
We check if given ref is the current branch in print_ref_list(). Move this check to print_ref_item() where it is checked right before printing. This enables a smooth transition to using ref-filter APIs, as we can later replace the current check while printing to just check for FILTER_REFS_DETACHED instead. 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
f65f13911afa8dd66d163049d91dbfe2ed73717e
1 file changed
+9
-11
builtin/branch.c
+9
-11
@@ -534,9 +534,10 @@ static char *get_head_description(void)
534
}
535
536
static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
537
- int abbrev, int current, const char *remote_prefix)
537
+ int abbrev, int detached, const char *remote_prefix)
538
{
539
char c;
540
+ int current = 0;
541
int color;
542
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
543
const char *prefix = "";
@@ -548,15 +549,18 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
549
550
switch (item->kind) {
551
case REF_LOCAL_BRANCH:
551
- color = BRANCH_COLOR_LOCAL;
552
+ if (!detached && !strcmp(item->name, head))
553
+ current = 1;
554
+ else
555
+ color = BRANCH_COLOR_LOCAL;
556
break;
557
case REF_REMOTE_BRANCH:
558
color = BRANCH_COLOR_REMOTE;
559
prefix = remote_prefix;
560
break;
561
case REF_DETACHED_HEAD:
558
- color = BRANCH_COLOR_CURRENT;
562
desc = to_free = get_head_description();
563
+ current = 1;
564
break;
565
default:
566
color = BRANCH_COLOR_PLAIN;
@@ -684,15 +688,9 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
688
689
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
690
687
- for (i = 0; i < ref_list.index; i++) {
688
- int current = !detached && (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
689
- !strcmp(ref_list.list[i].name, head);
690
- /* If detached the first ref_item is the current ref */
691
- if (detached && i == 0)
692
- current = 1;
691
+ for (i = 0; i < ref_list.index; i++)
692
print_ref_item(&ref_list.list[i], maxwidth, verbose,
694
- abbrev, current, remote_prefix);
695
- }
693
+ abbrev, detached, remote_prefix);
694
695
free_ref_list(&ref_list);
696