@matheusbsbs / matheus-bagliano / commits / 31653c1abc
Fork 0

Make git blame's date output format configurable, like git log

Add the following: - git config value blame.date that expects one of the git log date formats (e.g. relative,local,default,iso,...); - git blame command line option --date expects one of the git log date formats; - documentation in blame-options.txt; - git blame uses the appropriate date.c functions and enums to make sense of the date format and provide appropriate data; git blame continues to line up the output columns by padding the date column up to the max width of the chosen date format. The date format for git blame without both blame.date and --date continues to be ISO for backwards compatibility. git annotate ignores the date format specifiers and continues to uses the ISO format, as before. Signed-off-by: Eugene Letuchy <eugene@facebook.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eugene Letuchy committed Feb 20, 2009 at 14:51 UTC 31653c1abc1ac80206db9efca56ff1969150d8fe
2 files changed +59 -16
Documentation/blame-options.txt
+8
@@ -70,6 +70,14 @@ of lines before or after the line given by <start>.
70 tree copy has the contents of the named file (specify
71 `-` to make the command read from the standard input).
72
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 +
81 -M|<num>|::
82 Detect moving lines in the file as well. When a commit
83 moves a block of lines in a file (e.g. the original file
builtin-blame.c
+51 -16
@@ -1,5 +1,5 @@
1 /*
2 - * Pickaxe
2 + * Blame
3 *
4 * Copyright (c) 2006, Junio C Hamano
5 */
@@ -40,6 +40,10 @@ static int reverse;
40 static int blank_boundary;
41 static int incremental;
42 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 +
47 static struct string_list mailmap;
48
49 #ifndef DEBUG
@@ -1507,24 +1511,20 @@ static const char *format_time(unsigned long time, const char *tz_str,
1511 int show_raw_time)
1512 {
1513 static char time_buf[128];
1510 - time_t t = time;
1511 - int minutes, tz;
1512 - struct tm *tm;
1514 + const char *time_str;
1515 + int time_len;
1516 + int tz;
1517
1518 if (show_raw_time) {
1519 sprintf(time_buf, "%lu %s", time, tz_str);
1516 - return time_buf;
1520 }
1518 -
1519 - tz = atoi(tz_str);
1520 - minutes = tz < 0 ? -tz : tz;
1521 - minutes = (minutes / 100)*60 + (minutes % 100);
1522 - minutes = tz < 0 ? -minutes : minutes;
1523 - t = time + minutes * 60;
1524 - tm = gmtime(&t);
1525 -
1526 - strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
1527 - strcat(time_buf, tz_str);
1521 + else {
1522 + tz = atoi(tz_str);
1523 + time_str = show_date(time, tz, blame_date_mode);
1524 + time_len = strlen(time_str);
1525 + memcpy(time_buf, time_str, time_len);
1526 + memset(time_buf + time_len, ' ', blame_date_width - time_len);
1527 + }
1528 return time_buf;
1529 }
1530
@@ -1975,6 +1975,12 @@ static int git_blame_config(const char *var, const char *value, void *cb)
1975 blank_boundary = git_config_bool(var, value);
1976 return 0;
1977 }
1978 + if (!strcmp(var, "blame.date")) {
1979 + if (!value)
1980 + return config_error_nonbool(var);
1981 + blame_date_mode = parse_date_format(value);
1982 + return 0;
1983 + }
1984 return git_default_config(var, value, cb);
1985 }
1986
@@ -2239,6 +2245,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2245
2246 git_config(git_blame_config, NULL);
2247 init_revisions(&revs, NULL);
2248 + revs.date_mode = blame_date_mode;
2249 +
2250 save_commit_buffer = 0;
2251 dashdash_pos = 0;
2252
@@ -2263,8 +2271,35 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2271 parse_done:
2272 argc = parse_options_end(&ctx);
2273
2266 - if (cmd_is_annotate)
2274 + if (cmd_is_annotate) {
2275 output_option |= OUTPUT_ANNOTATE_COMPAT;
2276 + blame_date_mode = DATE_ISO8601;
2277 + } else {
2278 + blame_date_mode = revs.date_mode;
2279 + }
2280 +
2281 + /* The maximum width used to show the dates */
2282 + switch (blame_date_mode) {
2283 + case DATE_RFC2822:
2284 + blame_date_width = sizeof("Thu, 19 Oct 2006 16:00:04 -0700");
2285 + break;
2286 + case DATE_ISO8601:
2287 + blame_date_width = sizeof("2006-10-19 16:00:04 -0700");
2288 + break;
2289 + case DATE_RAW:
2290 + blame_date_width = sizeof("1161298804 -0700");
2291 + break;
2292 + case DATE_SHORT:
2293 + blame_date_width = sizeof("2006-10-19");
2294 + break;
2295 + case DATE_RELATIVE:
2296 + /* "normal" is used as the fallback for "relative" */
2297 + case DATE_LOCAL:
2298 + case DATE_NORMAL:
2299 + blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
2300 + break;
2301 + }
2302 + blame_date_width -= 1; /* strip the null */
2303
2304 if (DIFF_OPT_TST(&revs.diffopt, FIND_COPIES_HARDER))
2305 opt |= (PICKAXE_BLAME_COPY | PICKAXE_BLAME_MOVE |