Skip to content

Commit 72e3c32

Browse files
committed
Merge branch 'el/blame-date'
* el/blame-date: Make git blame's date output format configurable, like git log
2 parents e785dad + 31653c1 commit 72e3c32

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

Documentation/blame-options.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ of lines before or after the line given by <start>.
7070
tree copy has the contents of the named file (specify
7171
`-` to make the command read from the standard input).
7272

73+
--date <format>::
74+
The value is one of the following alternatives:
75+
{relative,local,default,iso,rfc,short}. If --date is not
76+
provided, the value of the blame.date config variable is
77+
used. If the blame.date config variable is also not set, the
78+
iso format is used. For more information, See the discussion
79+
of the --date option at linkgit:git-log[1].
80+
7381
-M|<num>|::
7482
Detect moving lines in the file as well. When a commit
7583
moves a block of lines in a file (e.g. the original file

builtin-blame.c

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Pickaxe
2+
* Blame
33
*
44
* Copyright (c) 2006, Junio C Hamano
55
*/
@@ -40,6 +40,10 @@ static int reverse;
4040
static int blank_boundary;
4141
static int incremental;
4242
static int xdl_opts = XDF_NEED_MINIMAL;
43+
44+
static enum date_mode blame_date_mode = DATE_ISO8601;
45+
static size_t blame_date_width;
46+
4347
static struct string_list mailmap;
4448

4549
#ifndef DEBUG
@@ -1532,24 +1536,20 @@ static const char *format_time(unsigned long time, const char *tz_str,
15321536
int show_raw_time)
15331537
{
15341538
static char time_buf[128];
1535-
time_t t = time;
1536-
int minutes, tz;
1537-
struct tm *tm;
1539+
const char *time_str;
1540+
int time_len;
1541+
int tz;
15381542

15391543
if (show_raw_time) {
15401544
sprintf(time_buf, "%lu %s", time, tz_str);
1541-
return time_buf;
15421545
}
1543-
1544-
tz = atoi(tz_str);
1545-
minutes = tz < 0 ? -tz : tz;
1546-
minutes = (minutes / 100)*60 + (minutes % 100);
1547-
minutes = tz < 0 ? -minutes : minutes;
1548-
t = time + minutes * 60;
1549-
tm = gmtime(&t);
1550-
1551-
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1552-
strcat(time_buf, tz_str);
1546+
else {
1547+
tz = atoi(tz_str);
1548+
time_str = show_date(time, tz, blame_date_mode);
1549+
time_len = strlen(time_str);
1550+
memcpy(time_buf, time_str, time_len);
1551+
memset(time_buf + time_len, ' ', blame_date_width - time_len);
1552+
}
15531553
return time_buf;
15541554
}
15551555

@@ -1954,6 +1954,12 @@ static int git_blame_config(const char *var, const char *value, void *cb)
19541954
blank_boundary = git_config_bool(var, value);
19551955
return 0;
19561956
}
1957+
if (!strcmp(var, "blame.date")) {
1958+
if (!value)
1959+
return config_error_nonbool(var);
1960+
blame_date_mode = parse_date_format(value);
1961+
return 0;
1962+
}
19571963
return git_default_config(var, value, cb);
19581964
}
19591965

@@ -2218,6 +2224,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
22182224

22192225
git_config(git_blame_config, NULL);
22202226
init_revisions(&revs, NULL);
2227+
revs.date_mode = blame_date_mode;
2228+
22212229
save_commit_buffer = 0;
22222230
dashdash_pos = 0;
22232231

@@ -2242,8 +2250,35 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
22422250
parse_done:
22432251
argc = parse_options_end(&ctx);
22442252

2245-
if (cmd_is_annotate)
2253+
if (cmd_is_annotate) {
22462254
output_option |= OUTPUT_ANNOTATE_COMPAT;
2255+
blame_date_mode = DATE_ISO8601;
2256+
} else {
2257+
blame_date_mode = revs.date_mode;
2258+
}
2259+
2260+
/* The maximum width used to show the dates */
2261+
switch (blame_date_mode) {
2262+
case DATE_RFC2822:
2263+
blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2264+
break;
2265+
case DATE_ISO8601:
2266+
blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
2267+
break;
2268+
case DATE_RAW:
2269+
blame_date_width = sizeof("1161298804 -0700");
2270+
break;
2271+
case DATE_SHORT:
2272+
blame_date_width = sizeof("2006-10-19");
2273+
break;
2274+
case DATE_RELATIVE:
2275+
/* "normal" is used as the fallback for "relative" */
2276+
case DATE_LOCAL:
2277+
case DATE_NORMAL:
2278+
blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
2279+
break;
2280+
}
2281+
blame_date_width -= 1; /* strip the null */
22472282

22482283
if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
22492284
opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |

0 commit comments

Comments
 (0)