Skip to content

Commit fe37a9c

Browse files
committed
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 <[email protected]>
1 parent 0893eec commit fe37a9c

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

Documentation/pretty-options.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ people using 80-column terminals.
4242
verbatim; this means that invalid sequences in the original
4343
commit may be copied to the output.
4444

45+
--expand-tabs=<n>::
4546
--expand-tabs::
4647
--no-expand-tabs::
4748
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>')
4950
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.
5054
+
5155
By default, tabs are expanded in pretty formats that indent the log
5256
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').
5558

5659
ifndef::git-rev-list[]
5760
--notes[=<ref>]::

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct pretty_print_context {
147147
int preserve_subject;
148148
struct date_mode date_mode;
149149
unsigned date_mode_explicit:1;
150-
unsigned expand_tabs_in_log:1;
150+
int expand_tabs_in_log;
151151
int need_8bit_cte;
152152
char *notes_message;
153153
struct reflog_walk_info *reflog_info;

pretty.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ static void setup_commit_formats(void)
8989
{
9090
struct cmt_fmt_map builtin_formats[] = {
9191
{ "raw", CMIT_FMT_RAW, 0, 0 },
92-
{ "medium", CMIT_FMT_MEDIUM, 0, 1 },
92+
{ "medium", CMIT_FMT_MEDIUM, 0, 8 },
9393
{ "short", CMIT_FMT_SHORT, 0, 0 },
9494
{ "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 },
9797
{ "oneline", CMIT_FMT_ONELINE, 1, 0 }
9898
};
9999
commit_formats_len = ARRAY_SIZE(builtin_formats);
@@ -1645,7 +1645,7 @@ static int pp_utf8_width(const char *start, const char *end)
16451645
return width;
16461646
}
16471647

1648-
static void strbuf_add_tabexpand(struct strbuf *sb,
1648+
static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
16491649
const char *line, int linelen)
16501650
{
16511651
const char *tab;
@@ -1666,7 +1666,7 @@ static void strbuf_add_tabexpand(struct strbuf *sb,
16661666
strbuf_add(sb, line, tab - line);
16671667

16681668
/* .. and the de-tabified tab */
1669-
strbuf_addchars(sb, ' ', 8 - (width % 8));
1669+
strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
16701670

16711671
/* Skip over the printed part .. */
16721672
linelen -= tab + 1 - line;
@@ -1692,7 +1692,7 @@ static void pp_handle_indent(struct pretty_print_context *pp,
16921692
{
16931693
strbuf_addchars(sb, ' ', indent);
16941694
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);
16961696
else
16971697
strbuf_add(sb, line, linelen);
16981698
}
@@ -1723,7 +1723,8 @@ void pp_remainder(struct pretty_print_context *pp,
17231723
if (indent)
17241724
pp_handle_indent(pp, sb, indent, line, linelen);
17251725
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);
17271728
else
17281729
strbuf_add(sb, line, linelen);
17291730
strbuf_addch(sb, '\n');

revision.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ void init_revisions(struct rev_info *revs, const char *prefix)
14151415
revs->expand_tabs_in_log = -1;
14161416

14171417
revs->commit_format = CMIT_FMT_DEFAULT;
1418-
revs->expand_tabs_in_log_default = 1;
1418+
revs->expand_tabs_in_log_default = 8;
14191419

14201420
init_grep_defaults();
14211421
grep_init(&revs->grep_filter, prefix);
@@ -1918,9 +1918,14 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
19181918
revs->pretty_given = 1;
19191919
get_commit_format(arg+9, revs);
19201920
} else if (!strcmp(arg, "--expand-tabs")) {
1921-
revs->expand_tabs_in_log = 1;
1921+
revs->expand_tabs_in_log = 8;
19221922
} else if (!strcmp(arg, "--no-expand-tabs")) {
19231923
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;
19241929
} else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
19251930
revs->show_notes = 1;
19261931
revs->show_notes_given = 1;

0 commit comments

Comments
 (0)