17
17
#include "revision.h"
18
18
#include "string-list.h"
19
19
#include "column.h"
20
+ #include "utf8.h"
20
21
21
22
static const char * const builtin_branch_usage [] = {
22
23
N_ ("git branch [options] [-r | -a] [--merged | --no-merged]" ),
@@ -249,7 +250,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
249
250
struct ref_item {
250
251
char * name ;
251
252
char * dest ;
252
- unsigned int kind , len ;
253
+ unsigned int kind , width ;
253
254
struct commit * commit ;
254
255
};
255
256
@@ -354,14 +355,14 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
354
355
newitem -> name = xstrdup (refname );
355
356
newitem -> kind = kind ;
356
357
newitem -> commit = commit ;
357
- newitem -> len = strlen (refname );
358
+ newitem -> width = utf8_strwidth (refname );
358
359
newitem -> dest = resolve_symref (orig_refname , prefix );
359
360
/* adjust for "remotes/" */
360
361
if (newitem -> kind == REF_REMOTE_BRANCH &&
361
362
ref_list -> kinds != REF_REMOTE_BRANCH )
362
- newitem -> len += 8 ;
363
- if (newitem -> len > ref_list -> maxwidth )
364
- ref_list -> maxwidth = newitem -> len ;
363
+ newitem -> width += 8 ;
364
+ if (newitem -> width > ref_list -> maxwidth )
365
+ ref_list -> maxwidth = newitem -> width ;
365
366
366
367
return 0 ;
367
368
}
@@ -490,11 +491,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
490
491
}
491
492
492
493
strbuf_addf (& name , "%s%s" , prefix , item -> name );
493
- if (verbose )
494
+ if (verbose ) {
495
+ int utf8_compensation = strlen (name .buf ) - utf8_strwidth (name .buf );
494
496
strbuf_addf (& out , "%c %s%-*s%s" , c , branch_get_color (color ),
495
- maxwidth , name .buf ,
497
+ maxwidth + utf8_compensation , name .buf ,
496
498
branch_get_color (BRANCH_COLOR_RESET ));
497
- else
499
+ } else
498
500
strbuf_addf (& out , "%c %s%s%s" , c , branch_get_color (color ),
499
501
name .buf , branch_get_color (BRANCH_COLOR_RESET ));
500
502
@@ -519,8 +521,8 @@ static int calc_maxwidth(struct ref_list *refs)
519
521
for (i = 0 ; i < refs -> index ; i ++ ) {
520
522
if (!matches_merge_filter (refs -> list [i ].commit ))
521
523
continue ;
522
- if (refs -> list [i ].len > w )
523
- w = refs -> list [i ].len ;
524
+ if (refs -> list [i ].width > w )
525
+ w = refs -> list [i ].width ;
524
526
}
525
527
return w ;
526
528
}
@@ -533,12 +535,12 @@ static void show_detached(struct ref_list *ref_list)
533
535
if (head_commit && is_descendant_of (head_commit , ref_list -> with_commit )) {
534
536
struct ref_item item ;
535
537
item .name = xstrdup (_ ("(no branch)" ));
536
- item .len = strlen (item .name );
538
+ item .width = utf8_strwidth (item .name );
537
539
item .kind = REF_LOCAL_BRANCH ;
538
540
item .dest = NULL ;
539
541
item .commit = head_commit ;
540
- if (item .len > ref_list -> maxwidth )
541
- ref_list -> maxwidth = item .len ;
542
+ if (item .width > ref_list -> maxwidth )
543
+ ref_list -> maxwidth = item .width ;
542
544
print_ref_item (& item , ref_list -> maxwidth , ref_list -> verbose , ref_list -> abbrev , 1 , "" );
543
545
free (item .name );
544
546
}
0 commit comments