pretty: allow tweaking tabwidth in --expand-tabs

When the local convention of the project is to use tab width that is not 8, it may make sense to allow "git log --expand-tabs=<n>" to tweak the output to match it. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 29, 2016 at 16:05 UTC fe37a9c586a65943e1bca327a1bbe1ca4a3d3023
4 files changed +22 -13
Documentation/pretty-options.txt
+6 -3
@@ -42,16 +42,19 @@ people using 80-column terminals.
42 verbatim; this means that invalid sequences in the original
43 commit may be copied to the output.
44
45 +--expand-tabs=<n>::
46 --expand-tabs::
47 --no-expand-tabs::
48 Perform a tab expansion (replace each tab with enough spaces
48 - to fill to the next display column that is multiple of 8)
49 + to fill to the next display column that is multiple of '<n>')
50 in the log message before showing it in the output.
51 + `--expand-tabs` is a short-hand for `--expand-tabs=8`, and
52 + `--no-expand-tabs` is a short-hand for `--expand-tabs=0`,
53 + which disables tab expansion.
54 +
55 By default, tabs are expanded in pretty formats that indent the log
56 message by 4 spaces (i.e. 'medium', which is the default, 'full',
53 -and 'fuller'). `--no-expand-tabs` option can be used to disable
54 -this.
57 +and 'fuller').
58
59 ifndef::git-rev-list[]
60 --notes[=<ref>]::
commit.h
+1 -1
@@ -147,7 +147,7 @@ struct pretty_print_context {
147 int preserve_subject;
148 struct date_mode date_mode;
149 unsigned date_mode_explicit:1;
150 - unsigned expand_tabs_in_log:1;
150 + int expand_tabs_in_log;
151 int need_8bit_cte;
152 char *notes_message;
153 struct reflog_walk_info *reflog_info;
pretty.c
+8 -7
@@ -89,11 +89,11 @@ static void setup_commit_formats(void)
89 {
90 struct cmt_fmt_map builtin_formats[] = {
91 { "raw", CMIT_FMT_RAW, 0, 0 },
92 - { "medium", CMIT_FMT_MEDIUM, 0, 1 },
92 + { "medium", CMIT_FMT_MEDIUM, 0, 8 },
93 { "short", CMIT_FMT_SHORT, 0, 0 },
94 { "email", CMIT_FMT_EMAIL, 0, 0 },
95 - { "fuller", CMIT_FMT_FULLER, 0, 1 },
96 - { "full", CMIT_FMT_FULL, 0, 1 },
95 + { "fuller", CMIT_FMT_FULLER, 0, 8 },
96 + { "full", CMIT_FMT_FULL, 0, 8 },
97 { "oneline", CMIT_FMT_ONELINE, 1, 0 }
98 };
99 commit_formats_len = ARRAY_SIZE(builtin_formats);
@@ -1645,7 +1645,7 @@ static int pp_utf8_width(const char *start, const char *end)
1645 return width;
1646 }
1647
1648 -static void strbuf_add_tabexpand(struct strbuf *sb,
1648 +static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
1649 const char *line, int linelen)
1650 {
1651 const char *tab;
@@ -1666,7 +1666,7 @@ static void strbuf_add_tabexpand(struct strbuf *sb,
1666 strbuf_add(sb, line, tab - line);
1667
1668 /* .. and the de-tabified tab */
1669 - strbuf_addchars(sb, ' ', 8 - (width % 8));
1669 + strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
1670
1671 /* Skip over the printed part .. */
1672 linelen -= tab + 1 - line;
@@ -1692,7 +1692,7 @@ static void pp_handle_indent(struct pretty_print_context *pp,
1692 {
1693 strbuf_addchars(sb, ' ', indent);
1694 if (pp->expand_tabs_in_log)
1695 - strbuf_add_tabexpand(sb, line, linelen);
1695 + strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
1696 else
1697 strbuf_add(sb, line, linelen);
1698 }
@@ -1723,7 +1723,8 @@ void pp_remainder(struct pretty_print_context *pp,
1723 if (indent)
1724 pp_handle_indent(pp, sb, indent, line, linelen);
1725 else if (pp->expand_tabs_in_log)
1726 - strbuf_add_tabexpand(sb, line, linelen);
1726 + strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
1727 + line, linelen);
1728 else
1729 strbuf_add(sb, line, linelen);
1730 strbuf_addch(sb, '\n');
revision.c
+7 -2
@@ -1415,7 +1415,7 @@ void init_revisions(struct rev_info *revs, const char *prefix)
1415 revs->expand_tabs_in_log = -1;
1416
1417 revs->commit_format = CMIT_FMT_DEFAULT;
1418 - revs->expand_tabs_in_log_default = 1;
1418 + revs->expand_tabs_in_log_default = 8;
1419
1420 init_grep_defaults();
1421 grep_init(&revs->grep_filter, prefix);
@@ -1918,9 +1918,14 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1918 revs->pretty_given = 1;
1919 get_commit_format(arg+9, revs);
1920 } else if (!strcmp(arg, "--expand-tabs")) {
1921 - revs->expand_tabs_in_log = 1;
1921 + revs->expand_tabs_in_log = 8;
1922 } else if (!strcmp(arg, "--no-expand-tabs")) {
1923 revs->expand_tabs_in_log = 0;
1924 + } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1925 + int val;
1926 + if (strtol_i(arg, 10, &val) < 0 || val < 0)
1927 + die("'%s': not a non-negative integer", arg);
1928 + revs->expand_tabs_in_log = val;
1929 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1930 revs->show_notes = 1;
1931 revs->show_notes_given = 1;