Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 6dada01

Browse files
committed
Merge branch 'jn/wt-status'
Unify the codepaths that format new/modified/changed sections and conflicted paths in the "git status" output and make it possible to properly internationalize their output. * jn/wt-status: wt-status: lift the artificual "at least 20 columns" floor wt-status: i18n of section labels wt-status: extract the code to compute width for labels wt-status: make full label string to be subject to l10n
2 parents 10bdb20 + c7cb333 commit 6dada01

File tree

3 files changed

+88
-55
lines changed

3 files changed

+88
-55
lines changed

t/t7060-wtstatus.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ You have unmerged paths.
3838
Unmerged paths:
3939
(use "git add/rm <file>..." as appropriate to mark resolution)
4040
41-
deleted by us: foo
41+
deleted by us: foo
4242
4343
no changes added to commit (use "git add" and/or "git commit -a")
4444
EOF
@@ -142,8 +142,8 @@ You have unmerged paths.
142142
Unmerged paths:
143143
(use "git add/rm <file>..." as appropriate to mark resolution)
144144
145-
both added: conflict.txt
146-
deleted by them: main.txt
145+
both added: conflict.txt
146+
deleted by them: main.txt
147147
148148
no changes added to commit (use "git add" and/or "git commit -a")
149149
EOF
@@ -175,9 +175,9 @@ You have unmerged paths.
175175
Unmerged paths:
176176
(use "git add/rm <file>..." as appropriate to mark resolution)
177177
178-
both deleted: main.txt
179-
added by them: sub_master.txt
180-
added by us: sub_second.txt
178+
both deleted: main.txt
179+
added by them: sub_master.txt
180+
added by us: sub_second.txt
181181
182182
no changes added to commit (use "git add" and/or "git commit -a")
183183
EOF
@@ -203,7 +203,7 @@ Changes to be committed:
203203
Unmerged paths:
204204
(use "git rm <file>..." to mark resolution)
205205
206-
both deleted: main.txt
206+
both deleted: main.txt
207207
208208
Untracked files not listed (use -u option to show untracked files)
209209
EOF

t/t7512-status-help.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ You have unmerged paths.
3333
Unmerged paths:
3434
(use "git add <file>..." to mark resolution)
3535
36-
both modified: main.txt
36+
both modified: main.txt
3737
3838
no changes added to commit (use "git add" and/or "git commit -a")
3939
EOF
@@ -87,7 +87,7 @@ Unmerged paths:
8787
(use "git reset HEAD <file>..." to unstage)
8888
(use "git add <file>..." to mark resolution)
8989
90-
both modified: main.txt
90+
both modified: main.txt
9191
9292
no changes added to commit (use "git add" and/or "git commit -a")
9393
EOF
@@ -146,7 +146,7 @@ Unmerged paths:
146146
(use "git reset HEAD <file>..." to unstage)
147147
(use "git add <file>..." to mark resolution)
148148
149-
both modified: main.txt
149+
both modified: main.txt
150150
151151
no changes added to commit (use "git add" and/or "git commit -a")
152152
EOF
@@ -602,7 +602,7 @@ rebase in progress; onto $ONTO
602602
You are currently rebasing branch '\''statushints_disabled'\'' on '\''$ONTO'\''.
603603
604604
Unmerged paths:
605-
both modified: main.txt
605+
both modified: main.txt
606606
607607
no changes added to commit
608608
EOF
@@ -636,7 +636,7 @@ You are currently cherry-picking commit $TO_CHERRY_PICK.
636636
Unmerged paths:
637637
(use "git add <file>..." to mark resolution)
638638
639-
both modified: main.txt
639+
both modified: main.txt
640640
641641
no changes added to commit (use "git add" and/or "git commit -a")
642642
EOF
@@ -707,7 +707,7 @@ Unmerged paths:
707707
(use "git reset HEAD <file>..." to unstage)
708708
(use "git add <file>..." to mark resolution)
709709
710-
both modified: to-revert.txt
710+
both modified: to-revert.txt
711711
712712
no changes added to commit (use "git add" and/or "git commit -a")
713713
EOF

wt-status.c

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -245,53 +245,92 @@ static void wt_status_print_trailer(struct wt_status *s)
245245

246246
#define quote_path quote_path_relative
247247

248-
static void wt_status_print_unmerged_data(struct wt_status *s,
249-
struct string_list_item *it)
248+
static const char *wt_status_unmerged_status_string(int stagemask)
250249
{
251-
const char *c = color(WT_STATUS_UNMERGED, s);
252-
struct wt_status_change_data *d = it->util;
253-
struct strbuf onebuf = STRBUF_INIT;
254-
const char *one, *how = _("bug");
255-
256-
one = quote_path(it->string, s->prefix, &onebuf);
257-
status_printf(s, color(WT_STATUS_HEADER, s), "\t");
258-
switch (d->stagemask) {
259-
case 1: how = _("both deleted:"); break;
260-
case 2: how = _("added by us:"); break;
261-
case 3: how = _("deleted by them:"); break;
262-
case 4: how = _("added by them:"); break;
263-
case 5: how = _("deleted by us:"); break;
264-
case 6: how = _("both added:"); break;
265-
case 7: how = _("both modified:"); break;
250+
switch (stagemask) {
251+
case 1:
252+
return _("both deleted:");
253+
case 2:
254+
return _("added by us:");
255+
case 3:
256+
return _("deleted by them:");
257+
case 4:
258+
return _("added by them:");
259+
case 5:
260+
return _("deleted by us:");
261+
case 6:
262+
return _("both added:");
263+
case 7:
264+
return _("both modified:");
265+
default:
266+
die(_("bug: unhandled unmerged status %x"), stagemask);
266267
}
267-
status_printf_more(s, c, "%-20s%s\n", how, one);
268-
strbuf_release(&onebuf);
269268
}
270269

271270
static const char *wt_status_diff_status_string(int status)
272271
{
273272
switch (status) {
274273
case DIFF_STATUS_ADDED:
275-
return _("new file");
274+
return _("new file:");
276275
case DIFF_STATUS_COPIED:
277-
return _("copied");
276+
return _("copied:");
278277
case DIFF_STATUS_DELETED:
279-
return _("deleted");
278+
return _("deleted:");
280279
case DIFF_STATUS_MODIFIED:
281-
return _("modified");
280+
return _("modified:");
282281
case DIFF_STATUS_RENAMED:
283-
return _("renamed");
282+
return _("renamed:");
284283
case DIFF_STATUS_TYPE_CHANGED:
285-
return _("typechange");
284+
return _("typechange:");
286285
case DIFF_STATUS_UNKNOWN:
287-
return _("unknown");
286+
return _("unknown:");
288287
case DIFF_STATUS_UNMERGED:
289-
return _("unmerged");
288+
return _("unmerged:");
290289
default:
291290
return NULL;
292291
}
293292
}
294293

294+
static int maxwidth(const char *(*label)(int), int minval, int maxval)
295+
{
296+
int result = 0, i;
297+
298+
for (i = minval; i <= maxval; i++) {
299+
const char *s = label(i);
300+
int len = s ? utf8_strwidth(s) : 0;
301+
if (len > result)
302+
result = len;
303+
}
304+
return result;
305+
}
306+
307+
static void wt_status_print_unmerged_data(struct wt_status *s,
308+
struct string_list_item *it)
309+
{
310+
const char *c = color(WT_STATUS_UNMERGED, s);
311+
struct wt_status_change_data *d = it->util;
312+
struct strbuf onebuf = STRBUF_INIT;
313+
static char *padding;
314+
static int label_width;
315+
const char *one, *how;
316+
int len;
317+
318+
if (!padding) {
319+
label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
320+
label_width += strlen(" ");
321+
padding = xmallocz(label_width);
322+
memset(padding, ' ', label_width);
323+
}
324+
325+
one = quote_path(it->string, s->prefix, &onebuf);
326+
status_printf(s, color(WT_STATUS_HEADER, s), "\t");
327+
328+
how = wt_status_unmerged_status_string(d->stagemask);
329+
len = label_width - utf8_strwidth(how);
330+
status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
331+
strbuf_release(&onebuf);
332+
}
333+
295334
static void wt_status_print_change_data(struct wt_status *s,
296335
int change_type,
297336
struct string_list_item *it)
@@ -305,21 +344,16 @@ static void wt_status_print_change_data(struct wt_status *s,
305344
struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
306345
struct strbuf extra = STRBUF_INIT;
307346
static char *padding;
347+
static int label_width;
308348
const char *what;
309349
int len;
310350

311351
if (!padding) {
312-
int width = 0;
313-
/* If DIFF_STATUS_* uses outside this range, we're in trouble */
314-
for (status = 'A'; status <= 'Z'; status++) {
315-
what = wt_status_diff_status_string(status);
316-
len = what ? strlen(what) : 0;
317-
if (len > width)
318-
width = len;
319-
}
320-
width += 2; /* colon and a space */
321-
padding = xmallocz(width);
322-
memset(padding, ' ', width);
352+
/* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
353+
label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
354+
label_width += strlen(" ");
355+
padding = xmallocz(label_width);
356+
memset(padding, ' ', label_width);
323357
}
324358

325359
one_name = two_name = it->string;
@@ -355,14 +389,13 @@ static void wt_status_print_change_data(struct wt_status *s,
355389
what = wt_status_diff_status_string(status);
356390
if (!what)
357391
die(_("bug: unhandled diff status %c"), status);
358-
/* 1 for colon, which is not part of "what" */
359-
len = strlen(padding) - (utf8_strwidth(what) + 1);
392+
len = label_width - utf8_strwidth(what);
360393
assert(len >= 0);
361394
if (status == DIFF_STATUS_COPIED || status == DIFF_STATUS_RENAMED)
362-
status_printf_more(s, c, "%s:%.*s%s -> %s",
395+
status_printf_more(s, c, "%s%.*s%s -> %s",
363396
what, len, padding, one, two);
364397
else
365-
status_printf_more(s, c, "%s:%.*s%s",
398+
status_printf_more(s, c, "%s%.*s%s",
366399
what, len, padding, one);
367400
if (extra.len) {
368401
status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);

0 commit comments

Comments
 (0)