Skip to content

Commit e52e6f7

Browse files
committed
Merge branch 'nd/pretty-formats'
pretty-printing body of the commit that is stored in non UTF-8 encoding did not work well. The early part of this series fixes it. And then it adds %C(auto) specifier that turns the coloring on when we are emitting to the terminal, and adds column-aligning format directives. * nd/pretty-formats: pretty: support %>> that steal trailing spaces pretty: support truncating in %>, %< and %>< pretty: support padding placeholders, %< %> and %>< pretty: add %C(auto) for auto-coloring pretty: split color parsing into a separate function pretty: two phase conversion for non utf-8 commits utf8.c: add reencode_string_len() that can handle NULs in string utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences utf8.c: move display_mode_esc_sequence_len() for use by other functions pretty: share code between format_decoration and show_decorations pretty-formats.txt: wrap long lines pretty: get the correct encoding for --pretty:format=%e pretty: save commit encoding from logmsg_reencode if the caller needs it
2 parents 7093d2c + 1640632 commit e52e6f7

14 files changed

Lines changed: 648 additions & 125 deletions

Documentation/pretty-formats.txt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,22 @@ The placeholders are:
106106
- '%P': parent hashes
107107
- '%p': abbreviated parent hashes
108108
- '%an': author name
109-
- '%aN': author name (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
109+
- '%aN': author name (respecting .mailmap, see linkgit:git-shortlog[1]
110+
or linkgit:git-blame[1])
110111
- '%ae': author email
111-
- '%aE': author email (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
112+
- '%aE': author email (respecting .mailmap, see
113+
linkgit:git-shortlog[1] or linkgit:git-blame[1])
112114
- '%ad': author date (format respects --date= option)
113115
- '%aD': author date, RFC2822 style
114116
- '%ar': author date, relative
115117
- '%at': author date, UNIX timestamp
116118
- '%ai': author date, ISO 8601 format
117119
- '%cn': committer name
118-
- '%cN': committer name (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
120+
- '%cN': committer name (respecting .mailmap, see
121+
linkgit:git-shortlog[1] or linkgit:git-blame[1])
119122
- '%ce': committer email
120-
- '%cE': committer email (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
123+
- '%cE': committer email (respecting .mailmap, see
124+
linkgit:git-shortlog[1] or linkgit:git-blame[1])
121125
- '%cd': committer date
122126
- '%cD': committer date, RFC2822 style
123127
- '%cr': committer date, relative
@@ -138,9 +142,11 @@ The placeholders are:
138142
- '%gD': reflog selector, e.g., `refs/stash@{1}`
139143
- '%gd': shortened reflog selector, e.g., `stash@{1}`
140144
- '%gn': reflog identity name
141-
- '%gN': reflog identity name (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
145+
- '%gN': reflog identity name (respecting .mailmap, see
146+
linkgit:git-shortlog[1] or linkgit:git-blame[1])
142147
- '%ge': reflog identity email
143-
- '%gE': reflog identity email (respecting .mailmap, see linkgit:git-shortlog[1] or linkgit:git-blame[1])
148+
- '%gE': reflog identity email (respecting .mailmap, see
149+
linkgit:git-shortlog[1] or linkgit:git-blame[1])
144150
- '%gs': reflog subject
145151
- '%Cred': switch color to red
146152
- '%Cgreen': switch color to green
@@ -150,13 +156,28 @@ The placeholders are:
150156
adding `auto,` at the beginning will emit color only when colors are
151157
enabled for log output (by `color.diff`, `color.ui`, or `--color`, and
152158
respecting the `auto` settings of the former if we are going to a
153-
terminal)
159+
terminal). `auto` alone (i.e. `%C(auto)`) will turn on auto coloring
160+
on the next placeholders until the color is switched again.
154161
- '%m': left, right or boundary mark
155162
- '%n': newline
156163
- '%%': a raw '%'
157164
- '%x00': print a byte from a hex code
158165
- '%w([<w>[,<i1>[,<i2>]]])': switch line wrapping, like the -w option of
159166
linkgit:git-shortlog[1].
167+
- '%<(<N>[,trunc|ltrunc|mtrunc])': make the next placeholder take at
168+
least N columns, padding spaces on the right if necessary.
169+
Optionally truncate at the beginning (ltrunc), the middle (mtrunc)
170+
or the end (trunc) if the output is longer than N columns.
171+
Note that truncating only works correctly with N >= 2.
172+
- '%<|(<N>)': make the next placeholder take at least until Nth
173+
columns, padding spaces on the right if necessary
174+
- '%>(<N>)', '%>|(<N>)': similar to '%<(<N>)', '%<|(<N>)'
175+
respectively, but padding spaces on the left
176+
- '%>>(<N>)', '%>>|(<N>)': similar to '%>(<N>)', '%>|(<N>)'
177+
respectively, except that if the next placeholder takes more spaces
178+
than given and there are spaces on its left, use those spaces
179+
- '%><(<N>)', '%><|(<N>)': similar to '% <(<N>)', '%<|(<N>)'
180+
respectively, but padding both sides (i.e. the text is centered)
160181

161182
NOTE: Some placeholders may depend on other options given to the
162183
revision traversal engine. For example, the `%g*` reflog options will

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ static void get_commit_info(struct commit *commit,
14301430
commit_info_init(ret);
14311431

14321432
encoding = get_log_output_encoding();
1433-
message = logmsg_reencode(commit, encoding);
1433+
message = logmsg_reencode(commit, NULL, encoding);
14341434
get_ac_line(message, "\nauthor ",
14351435
&ret->author, &ret->author_mail,
14361436
&ret->author_time, &ret->author_tz);

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ static const char *read_commit_message(const char *name)
955955
if (!commit)
956956
die(_("could not lookup commit %s"), name);
957957
out_enc = get_commit_output_encoding();
958-
return logmsg_reencode(commit, out_enc);
958+
return logmsg_reencode(commit, NULL, out_enc);
959959
}
960960

961961
static int parse_and_validate_options(int argc, const char *argv[],

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct userformat_want {
101101
extern int has_non_ascii(const char *text);
102102
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
103103
extern char *logmsg_reencode(const struct commit *commit,
104+
char **commit_encoding,
104105
const char *output_encoding);
105106
extern void logmsg_free(char *msg, const struct commit *commit);
106107
extern void get_commit_format(const char *arg, struct rev_info *);

compat/precompose_utf8.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void precompose_argv(int argc, const char **argv)
7878
size_t namelen;
7979
oldarg = argv[i];
8080
if (has_non_ascii(oldarg, (size_t)-1, &namelen)) {
81-
newarg = reencode_string_iconv(oldarg, namelen, ic_precompose);
81+
newarg = reencode_string_iconv(oldarg, namelen, ic_precompose, NULL);
8282
if (newarg)
8383
argv[i] = newarg;
8484
}

log-tree.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,52 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre
175175
}
176176
}
177177

178-
void show_decorations(struct rev_info *opt, struct commit *commit)
178+
/*
179+
* The caller makes sure there is no funny color before
180+
* calling. format_decorations makes sure the same after return.
181+
*/
182+
void format_decorations(struct strbuf *sb,
183+
const struct commit *commit,
184+
int use_color)
179185
{
180186
const char *prefix;
181187
struct name_decoration *decoration;
182188
const char *color_commit =
183-
diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
189+
diff_get_color(use_color, DIFF_COMMIT);
184190
const char *color_reset =
185-
decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
191+
decorate_get_color(use_color, DECORATION_NONE);
186192

187-
if (opt->show_source && commit->util)
188-
printf("\t%s", (char *) commit->util);
189-
if (!opt->show_decorations)
190-
return;
191193
decoration = lookup_decoration(&name_decoration, &commit->object);
192194
if (!decoration)
193195
return;
194196
prefix = " (";
195197
while (decoration) {
196-
printf("%s", prefix);
197-
fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
198-
stdout);
198+
strbuf_addstr(sb, color_commit);
199+
strbuf_addstr(sb, prefix);
200+
strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
199201
if (decoration->type == DECORATION_REF_TAG)
200-
fputs("tag: ", stdout);
201-
printf("%s", decoration->name);
202-
fputs(color_reset, stdout);
203-
fputs(color_commit, stdout);
202+
strbuf_addstr(sb, "tag: ");
203+
strbuf_addstr(sb, decoration->name);
204+
strbuf_addstr(sb, color_reset);
204205
prefix = ", ";
205206
decoration = decoration->next;
206207
}
207-
putchar(')');
208+
strbuf_addstr(sb, color_commit);
209+
strbuf_addch(sb, ')');
210+
strbuf_addstr(sb, color_reset);
211+
}
212+
213+
void show_decorations(struct rev_info *opt, struct commit *commit)
214+
{
215+
struct strbuf sb = STRBUF_INIT;
216+
217+
if (opt->show_source && commit->util)
218+
printf("\t%s", (char *) commit->util);
219+
if (!opt->show_decorations)
220+
return;
221+
format_decorations(&sb, commit, opt->diffopt.use_color);
222+
fputs(sb.buf, stdout);
223+
strbuf_release(&sb);
208224
}
209225

210226
static unsigned int digits_in_number(unsigned int number)
@@ -540,8 +556,8 @@ void show_log(struct rev_info *opt)
540556
printf(" (from %s)",
541557
find_unique_abbrev(parent->object.sha1,
542558
abbrev_commit));
559+
fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
543560
show_decorations(opt, commit);
544-
printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
545561
if (opt->commit_format == CMIT_FMT_ONELINE) {
546562
putchar(' ');
547563
} else {

log-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int log_tree_diff_flush(struct rev_info *);
1313
int log_tree_commit(struct rev_info *, struct commit *);
1414
int log_tree_opt_parse(struct rev_info *, const char **, int);
1515
void show_log(struct rev_info *opt);
16+
void format_decorations(struct strbuf *sb, const struct commit *commit, int use_color);
1617
void show_decorations(struct rev_info *opt, struct commit *commit);
1718
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
1819
const char **subject_p,

0 commit comments

Comments
 (0)