@@ -282,14 +282,14 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
282
282
struct ref_item {
283
283
char * name ;
284
284
char * dest ;
285
- unsigned int kind , width ;
285
+ unsigned int kind ;
286
286
struct commit * commit ;
287
287
int ignore ;
288
288
};
289
289
290
290
struct ref_list {
291
291
struct rev_info revs ;
292
- int index , alloc , maxwidth , verbose , abbrev ;
292
+ int index , alloc , verbose , abbrev ;
293
293
struct ref_item * list ;
294
294
struct commit_list * with_commit ;
295
295
int kinds ;
@@ -386,15 +386,8 @@ static int append_ref(const char *refname, const struct object_id *oid, int flag
386
386
newitem -> name = xstrdup (refname );
387
387
newitem -> kind = kind ;
388
388
newitem -> commit = commit ;
389
- newitem -> width = utf8_strwidth (refname );
390
389
newitem -> dest = resolve_symref (orig_refname , prefix );
391
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 ;
398
391
399
392
return 0 ;
400
393
}
@@ -505,11 +498,12 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
505
498
}
506
499
507
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 )
509
502
{
510
503
char c ;
511
504
int color ;
512
505
struct strbuf out = STRBUF_INIT , name = STRBUF_INIT ;
506
+ const char * prefix = "" ;
513
507
514
508
if (item -> ignore )
515
509
return ;
@@ -520,6 +514,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
520
514
break ;
521
515
case REF_REMOTE_BRANCH :
522
516
color = BRANCH_COLOR_REMOTE ;
517
+ prefix = remote_prefix ;
523
518
break ;
524
519
default :
525
520
color = BRANCH_COLOR_PLAIN ;
@@ -557,16 +552,22 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
557
552
strbuf_release (& out );
558
553
}
559
554
560
- static int calc_maxwidth (struct ref_list * refs )
555
+ static int calc_maxwidth (struct ref_list * refs , int remote_bonus )
561
556
{
562
- int i , w = 0 ;
557
+ int i , max = 0 ;
563
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 )
565
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 ;
568
569
}
569
- return w ;
570
+ return max ;
570
571
}
571
572
572
573
static char * get_head_description (void )
@@ -600,21 +601,18 @@ static char *get_head_description(void)
600
601
return strbuf_detach (& desc , NULL );
601
602
}
602
603
603
- static void show_detached (struct ref_list * ref_list )
604
+ static void show_detached (struct ref_list * ref_list , int maxwidth )
604
605
{
605
606
struct commit * head_commit = lookup_commit_reference_gently (head_sha1 , 1 );
606
607
607
608
if (head_commit && is_descendant_of (head_commit , ref_list -> with_commit )) {
608
609
struct ref_item item ;
609
610
item .name = get_head_description ();
610
- item .width = utf8_strwidth (item .name );
611
611
item .kind = REF_LOCAL_BRANCH ;
612
612
item .dest = NULL ;
613
613
item .commit = head_commit ;
614
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 , "" );
618
616
free (item .name );
619
617
}
620
618
}
@@ -624,6 +622,16 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
624
622
int i ;
625
623
struct append_ref_cb cb ;
626
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/" ;
627
635
628
636
memset (& ref_list , 0 , sizeof (ref_list ));
629
637
ref_list .kinds = kinds ;
@@ -667,26 +675,22 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
667
675
clear_commit_marks (item -> commit , ALL_REV_FLAGS );
668
676
}
669
677
clear_commit_marks (filter , ALL_REV_FLAGS );
670
-
671
- if (verbose )
672
- ref_list .maxwidth = calc_maxwidth (& ref_list );
673
678
}
679
+ if (verbose )
680
+ maxwidth = calc_maxwidth (& ref_list , strlen (remote_prefix ));
674
681
675
682
qsort (ref_list .list , ref_list .index , sizeof (struct ref_item ), ref_cmp );
676
683
677
684
detached = (detached && (kinds & REF_LOCAL_BRANCH ));
678
685
if (detached && match_patterns (pattern , "HEAD" ))
679
- show_detached (& ref_list );
686
+ show_detached (& ref_list , maxwidth );
680
687
681
688
for (i = 0 ; i < ref_list .index ; i ++ ) {
682
689
int current = !detached &&
683
690
(ref_list .list [i ].kind == REF_LOCAL_BRANCH ) &&
684
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 );
690
694
}
691
695
692
696
free_ref_list (& ref_list );
0 commit comments