Skip to content

Commit 6bf8426

Browse files
committed
Merge branch 'jx/blame-align-relative-time' into maint
"git blame" miscounted number of columns needed to show localized timestamps, resulting in jaggy left-side-edge of the source code lines in its output. * jx/blame-align-relative-time: blame: dynamic blame_date_width for different locales blame: fix broken time_buf paddings in relative timestamp
2 parents c122c9a + dd75553 commit 6bf8426

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

builtin/blame.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,22 +1556,29 @@ static void assign_blame(struct scoreboard *sb, int opt)
15561556
static const char *format_time(unsigned long time, const char *tz_str,
15571557
int show_raw_time)
15581558
{
1559-
static char time_buf[128];
1559+
static struct strbuf time_buf = STRBUF_INIT;
15601560

1561+
strbuf_reset(&time_buf);
15611562
if (show_raw_time) {
1562-
snprintf(time_buf, sizeof(time_buf), "%lu %s", time, tz_str);
1563+
strbuf_addf(&time_buf, "%lu %s", time, tz_str);
15631564
}
15641565
else {
15651566
const char *time_str;
1566-
int time_len;
1567+
size_t time_width;
15671568
int tz;
15681569
tz = atoi(tz_str);
15691570
time_str = show_date(time, tz, blame_date_mode);
1570-
time_len = strlen(time_str);
1571-
memcpy(time_buf, time_str, time_len);
1572-
memset(time_buf + time_len, ' ', blame_date_width - time_len);
1571+
strbuf_addstr(&time_buf, time_str);
1572+
/*
1573+
* Add space paddings to time_buf to display a fixed width
1574+
* string, and use time_width for display width calibration.
1575+
*/
1576+
for (time_width = utf8_strwidth(time_str);
1577+
time_width < blame_date_width;
1578+
time_width++)
1579+
strbuf_addch(&time_buf, ' ');
15731580
}
1574-
return time_buf;
1581+
return time_buf.buf;
15751582
}
15761583

15771584
#define OUTPUT_ANNOTATE_COMPAT 001
@@ -2331,7 +2338,14 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
23312338
blame_date_width = sizeof("2006-10-19");
23322339
break;
23332340
case DATE_RELATIVE:
2334-
/* "normal" is used as the fallback for "relative" */
2341+
/* TRANSLATORS: This string is used to tell us the maximum
2342+
display width for a relative timestamp in "git blame"
2343+
output. For C locale, "4 years, 11 months ago", which
2344+
takes 22 places, is the longest among various forms of
2345+
relative timestamps, but your language may need more or
2346+
fewer display columns. */
2347+
blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
2348+
break;
23352349
case DATE_LOCAL:
23362350
case DATE_NORMAL:
23372351
blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");

0 commit comments

Comments
 (0)