Skip to content

Commit 8023a42

Browse files
committed
Merge branch 'nd/branch-v-alignment'
Output from "git branch -v" contains "(no branch)" that could be localized, but the code to align it along with the names of branches were counting in bytes, not in display columns. * nd/branch-v-alignment: branch -v: align even when branch names are in UTF-8
2 parents e6dd70e + 1452bd6 commit 8023a42

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Documentation/revisions.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ when you run `git cherry-pick`.
5555
+
5656
Note that any of the 'refs/*' cases above may come either from
5757
the '$GIT_DIR/refs' directory or from the '$GIT_DIR/packed-refs' file.
58+
While the ref name encoding is unspecified, UTF-8 is prefered as
59+
some output processing may assume ref names in UTF-8.
5860

5961
'<refname>@\{<date>\}', e.g. 'master@\{yesterday\}', 'HEAD@\{5 minutes ago\}'::
6062
A ref followed by the suffix '@' with a date specification

builtin/branch.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "revision.h"
1818
#include "string-list.h"
1919
#include "column.h"
20+
#include "utf8.h"
2021

2122
static const char * const builtin_branch_usage[] = {
2223
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,
249250
struct ref_item {
250251
char *name;
251252
char *dest;
252-
unsigned int kind, len;
253+
unsigned int kind, width;
253254
struct commit *commit;
254255
};
255256

@@ -354,14 +355,14 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
354355
newitem->name = xstrdup(refname);
355356
newitem->kind = kind;
356357
newitem->commit = commit;
357-
newitem->len = strlen(refname);
358+
newitem->width = utf8_strwidth(refname);
358359
newitem->dest = resolve_symref(orig_refname, prefix);
359360
/* adjust for "remotes/" */
360361
if (newitem->kind == REF_REMOTE_BRANCH &&
361362
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;
365366

366367
return 0;
367368
}
@@ -490,11 +491,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
490491
}
491492

492493
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);
494496
strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
495-
maxwidth, name.buf,
497+
maxwidth + utf8_compensation, name.buf,
496498
branch_get_color(BRANCH_COLOR_RESET));
497-
else
499+
} else
498500
strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color),
499501
name.buf, branch_get_color(BRANCH_COLOR_RESET));
500502

@@ -519,8 +521,8 @@ static int calc_maxwidth(struct ref_list *refs)
519521
for (i = 0; i < refs->index; i++) {
520522
if (!matches_merge_filter(refs->list[i].commit))
521523
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;
524526
}
525527
return w;
526528
}
@@ -533,12 +535,12 @@ static void show_detached(struct ref_list *ref_list)
533535
if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
534536
struct ref_item item;
535537
item.name = xstrdup(_("(no branch)"));
536-
item.len = strlen(item.name);
538+
item.width = utf8_strwidth(item.name);
537539
item.kind = REF_LOCAL_BRANCH;
538540
item.dest = NULL;
539541
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;
542544
print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
543545
free(item.name);
544546
}

0 commit comments

Comments
 (0)