Skip to content

Commit 3bcc1f0

Browse files
john-caiJohn Cai
authored andcommitted
Improve UX for oneline with decorations
The current oneline log format prints the decoration before the commit message, which breaks up the commit message alignment. Teach `log` to understand a new option `--newlinefor` where `decorations` can be passed in for a newline to be added after decorations. Signed-off-by: John Cai <johncai86@gmail.com>
1 parent e9e5ba3 commit 3bcc1f0

File tree

8 files changed

+62
-14
lines changed

8 files changed

+62
-14
lines changed

Documentation/pretty-options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ people using 80-column terminals.
3232
This is a shorthand for "--pretty=oneline --abbrev-commit"
3333
used together.
3434

35+
--newlinefor[=(decorations)]
36+
Used in conjunction with --pretty=oneline to add a line break after
37+
decorations
38+
3539
--encoding=<encoding>::
3640
Commit objects record the character encoding used for the log message
3741
in their encoding header; this option can be used to tell the

builtin/log.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ static void init_log_defaults(void)
141141

142142
static void cmd_log_init_defaults(struct rev_info *rev)
143143
{
144+
struct newlinefor newlineforopts = {0};
145+
144146
if (fmt_pretty)
145147
get_commit_format(fmt_pretty, rev);
146148
if (default_follow)
@@ -156,6 +158,7 @@ static void cmd_log_init_defaults(struct rev_info *rev)
156158
rev->show_signature = default_show_signature;
157159
rev->encode_email_headers = default_encode_email_headers;
158160
rev->diffopt.flags.allow_textconv = 1;
161+
rev->newlinefor = &newlineforopts;
159162

160163
if (default_date_mode)
161164
parse_date_format(default_date_mode, &rev->date_mode);
@@ -170,6 +173,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
170173
static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
171174
static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
172175
static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
176+
static struct string_list newlinefor = STRING_LIST_INIT_NODUP;
173177
struct decoration_filter decoration_filter = {&decorate_refs_include,
174178
&decorate_refs_exclude,
175179
&decorate_refs_exclude_config};
@@ -189,6 +193,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
189193
OPT_CALLBACK('L', NULL, &line_cb, "range:file",
190194
N_("trace the evolution of line range <start>,<end> or function :<funcname> in <file>"),
191195
log_line_range_callback),
196+
OPT_STRING_LIST(0, "newlinefor", &newlinefor, N_("newlinefor"), N_("new line for <commit>,<source>, or <deocrations>")),
192197
OPT_END()
193198
};
194199

@@ -229,6 +234,9 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
229234
rev->sources = &revision_sources;
230235
}
231236

237+
if (string_list_has_string(&newlinefor, "decorations"))
238+
rev->newlinefor->decorations = 1;
239+
232240
if (mailmap) {
233241
rev->mailmap = xcalloc(1, sizeof(struct string_list));
234242
read_mailmap(rev->mailmap);

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ const char *repo_find_unique_abbrev(struct repository *r, const struct object_id
11471147
#define find_unique_abbrev(oid, len) repo_find_unique_abbrev(the_repository, oid, len)
11481148
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
11491149
#define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len)
1150+
int find_unique_abbrev_len(struct repository *r, int len);
11501151

11511152
/* set default permissions by passing mode arguments to open(2) */
11521153
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);

log-tree.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,24 @@ static void show_name(struct strbuf *sb, const struct name_decoration *decoratio
278278
strbuf_addstr(sb, decoration->name);
279279
}
280280

281+
static void indent(struct strbuf *sb, int len)
282+
{
283+
strbuf_addchars(sb, ' ', len);
284+
}
285+
281286
/*
282287
* The caller makes sure there is no funny color before calling.
283288
* format_decorations_extended makes sure the same after return.
284289
*/
285290
void format_decorations_extended(struct strbuf *sb,
286291
const struct commit *commit,
287292
int use_color,
293+
int format,
288294
const char *prefix,
289295
const char *separator,
290-
const char *suffix)
296+
const char *suffix,
297+
int abbrev_len,
298+
int newline)
291299
{
292300
const struct name_decoration *decoration;
293301
const struct name_decoration *current_and_HEAD;
@@ -333,11 +341,16 @@ void format_decorations_extended(struct strbuf *sb,
333341
strbuf_addstr(sb, color_commit);
334342
strbuf_addstr(sb, suffix);
335343
strbuf_addstr(sb, color_reset);
344+
if ((format == CMIT_FMT_ONELINE) && newline == 1) {
345+
strbuf_addstr(sb, "\n");
346+
indent(sb, abbrev_len);
347+
}
336348
}
337349

338350
void show_decorations(struct rev_info *opt, struct commit *commit)
339351
{
340352
struct strbuf sb = STRBUF_INIT;
353+
int newline = 0;
341354

342355
if (opt->sources) {
343356
char **slot = revision_sources_peek(opt->sources, commit);
@@ -347,7 +360,12 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
347360
}
348361
if (!opt->show_decorations)
349362
return;
350-
format_decorations(&sb, commit, opt->diffopt.use_color);
363+
364+
if (opt->newlinefor != NULL){
365+
newline = opt->newlinefor->decorations;
366+
}
367+
368+
format_decorations(&sb, commit, opt->diffopt.use_color, opt->commit_format, opt->abbrev, newline);
351369
fputs(sb.buf, opt->diffopt.file);
352370
strbuf_release(&sb);
353371
}
@@ -624,6 +642,8 @@ void show_log(struct rev_info *opt)
624642
const char *extra_headers = opt->extra_headers;
625643
struct pretty_print_context ctx = {0};
626644

645+
opt->abbrev = find_unique_abbrev_len(the_repository, abbrev_commit);
646+
627647
opt->loginfo = NULL;
628648
if (!opt->verbose_header) {
629649
graph_show_commit(opt->graph);

log-tree.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ int log_tree_commit(struct rev_info *, struct commit *);
1919
void show_log(struct rev_info *opt);
2020
void format_decorations_extended(struct strbuf *sb, const struct commit *commit,
2121
int use_color,
22+
int format,
2223
const char *prefix,
2324
const char *separator,
24-
const char *suffix);
25-
#define format_decorations(strbuf, commit, color) \
26-
format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")
25+
const char *suffix,
26+
int abbrev_len,
27+
int newline);
28+
#define format_decorations(strbuf, commit, color, format, abbrev_len, newline) \
29+
format_decorations_extended((strbuf), (commit), (color), (format), " (", ", ", ")", (abbrev_len), (newline))
2730
void show_decorations(struct rev_info *opt, struct commit *commit);
2831
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
2932
const char **extra_headers_p,

object-name.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,8 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
660660
find_abbrev_len_for_pack(p, mad);
661661
}
662662

663-
int repo_find_unique_abbrev_r(struct repository *r, char *hex,
664-
const struct object_id *oid, int len)
663+
int find_unique_abbrev_len(struct repository *r, int len)
665664
{
666-
struct disambiguate_state ds;
667-
struct min_abbrev_data mad;
668-
struct object_id oid_ret;
669-
const unsigned hexsz = r->hash_algo->hexsz;
670-
671665
if (len < 0) {
672666
unsigned long count = repo_approximate_object_count(r);
673667
/*
@@ -690,6 +684,19 @@ int repo_find_unique_abbrev_r(struct repository *r, char *hex,
690684
len = FALLBACK_DEFAULT_ABBREV;
691685
}
692686

687+
return len;
688+
}
689+
690+
int repo_find_unique_abbrev_r(struct repository *r, char *hex,
691+
const struct object_id *oid, int len)
692+
{
693+
struct disambiguate_state ds;
694+
struct min_abbrev_data mad;
695+
struct object_id oid_ret;
696+
const unsigned hexsz = r->hash_algo->hexsz;
697+
698+
len = find_unique_abbrev_len(r, len);
699+
693700
oid_to_hex_r(hex, oid);
694701
if (len == hexsz || !len)
695702
return hexsz;

pretty.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,10 +1385,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
13851385
strbuf_addstr(sb, get_revision_mark(NULL, commit));
13861386
return 1;
13871387
case 'd':
1388-
format_decorations(sb, commit, c->auto_color);
1388+
format_decorations(sb, commit, c->auto_color, c->pretty_ctx->fmt, 0, 0);
13891389
return 1;
13901390
case 'D':
1391-
format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1391+
format_decorations_extended(sb, commit, c->auto_color, c->pretty_ctx->fmt, "", ", ", "", 0, 0);
13921392
return 1;
13931393
case 'S':/* tag/branch like --source */
13941394
if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))

revision.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ struct rev_cmdline_info {
8181

8282
struct oidset;
8383
struct topo_walk_info;
84+
struct newlinefor {
85+
unsigned int decorations;
86+
};
87+
8488

8589
struct rev_info {
8690
/* Starting list */
@@ -243,6 +247,7 @@ struct rev_info {
243247
intno_inline;
244248
intshow_log_size;
245249
struct string_list *mailmap;
250+
struct newlinefor *newlinefor;
246251

247252
/* Filter by commit log message */
248253
struct grep_optgrep_filter;

0 commit comments

Comments
 (0)