Skip to content

Commit 4da45be

Browse files
committed
log: teach "terminator" vs "separator" mode to "--pretty=format"
This attached patch introduces a single bit "use_terminator" in "struct rev_info", which is normally false (i.e. most formats use separator semantics) but by flipping it to true, you can ask for terminator semantics just like oneline format does. The function get_commit_format(), which is what parses "--pretty=" option, now takes a pointer to "struct rev_info" and updates its commit_format and use_terminator fields. It used to return the value of type "enum cmit_fmt", but all the callers assigned it to rev->commit_format. There are only two cases the code turns use_terminator on. Obviously, the traditional oneline format (--pretty=oneline) is one of them, and the new case is --pretty=tformat:... that acts like --pretty=format:... but flips the bit on. With this, "--pretty=tformat:%H %s" acts like --pretty=oneline. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7134973 commit 4da45be

File tree

7 files changed

+43
-31
lines changed

7 files changed

+43
-31
lines changed

builtin-commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
810810

811811
rev.verbose_header = 1;
812812
rev.show_root_diff = 1;
813-
rev.commit_format = get_commit_format("format:%h: %s");
813+
get_commit_format("format:%h: %s", &rev);
814814
rev.always_show_header = 0;
815815
rev.diffopt.detect_rename = 1;
816816
rev.diffopt.rename_limit = 100;

builtin-log.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
5656
rev->abbrev = DEFAULT_ABBREV;
5757
rev->commit_format = CMIT_FMT_DEFAULT;
5858
if (fmt_pretty)
59-
rev->commit_format = get_commit_format(fmt_pretty);
59+
get_commit_format(fmt_pretty, rev);
6060
rev->verbose_header = 1;
6161
DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
6262
rev->show_root_diff = default_show_root;
@@ -400,6 +400,7 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
400400
* allow us to set a different default.
401401
*/
402402
rev.commit_format = CMIT_FMT_ONELINE;
403+
rev.use_terminator = 1;
403404
rev.always_show_header = 1;
404405

405406
/*

commit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ enum cmit_fmt {
6363
};
6464

6565
extern int non_ascii(int);
66-
extern enum cmit_fmt get_commit_format(const char *arg);
66+
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
67+
extern void get_commit_format(const char *arg, struct rev_info *);
6768
extern void format_commit_message(const struct commit *commit,
6869
const void *format, struct strbuf *sb);
6970
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*,

log-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ void show_log(struct rev_info *opt, const char *sep)
249249
* not have an empty line between entries.
250250
*/
251251
extra = "";
252-
if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
252+
if (*sep != '\n' && opt->use_terminator)
253253
extra = "\n";
254-
if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
254+
if (opt->shown_one && !opt->use_terminator)
255255
putchar(opt->diffopt.line_termination);
256256
opt->shown_one = 1;
257257

pretty.c

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,49 @@
44
#include "diff.h"
55
#include "revision.h"
66

7-
static struct cmt_fmt_map {
8-
const char *n;
9-
size_t cmp_len;
10-
enum cmit_fmt v;
11-
} cmt_fmts[] = {
12-
{ "raw", 1, CMIT_FMT_RAW },
13-
{ "medium", 1, CMIT_FMT_MEDIUM },
14-
{ "short", 1, CMIT_FMT_SHORT },
15-
{ "email", 1, CMIT_FMT_EMAIL },
16-
{ "full", 5, CMIT_FMT_FULL },
17-
{ "fuller", 5, CMIT_FMT_FULLER },
18-
{ "oneline", 1, CMIT_FMT_ONELINE },
19-
{ "format:", 7, CMIT_FMT_USERFORMAT},
20-
};
21-
227
static char *user_format;
238

24-
enum cmit_fmt get_commit_format(const char *arg)
9+
void get_commit_format(const char *arg, struct rev_info *rev)
2510
{
2611
int i;
27-
28-
if (!arg || !*arg)
29-
return CMIT_FMT_DEFAULT;
12+
static struct cmt_fmt_map {
13+
const char *n;
14+
size_t cmp_len;
15+
enum cmit_fmt v;
16+
} cmt_fmts[] = {
17+
{ "raw", 1, CMIT_FMT_RAW },
18+
{ "medium", 1, CMIT_FMT_MEDIUM },
19+
{ "short", 1, CMIT_FMT_SHORT },
20+
{ "email", 1, CMIT_FMT_EMAIL },
21+
{ "full", 5, CMIT_FMT_FULL },
22+
{ "fuller", 5, CMIT_FMT_FULLER },
23+
{ "oneline", 1, CMIT_FMT_ONELINE },
24+
};
25+
26+
rev->use_terminator = 0;
27+
if (!arg || !*arg) {
28+
rev->commit_format = CMIT_FMT_DEFAULT;
29+
return;
30+
}
3031
if (*arg == '=')
3132
arg++;
32-
if (!prefixcmp(arg, "format:")) {
33+
if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
34+
const char *cp = strchr(arg, ':') + 1;
3335
free(user_format);
34-
user_format = xstrdup(arg + 7);
35-
return CMIT_FMT_USERFORMAT;
36+
user_format = xstrdup(cp);
37+
if (arg[0] == 't')
38+
rev->use_terminator = 1;
39+
rev->commit_format = CMIT_FMT_USERFORMAT;
40+
return;
3641
}
3742
for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
3843
if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
39-
!strncmp(arg, cmt_fmts[i].n, strlen(arg)))
40-
return cmt_fmts[i].v;
44+
!strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
45+
if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
46+
rev->use_terminator = 1;
47+
rev->commit_format = cmt_fmts[i].v;
48+
return;
49+
}
4150
}
4251

4352
die("invalid --pretty format: %s", arg);

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
11981198
}
11991199
if (!prefixcmp(arg, "--pretty")) {
12001200
revs->verbose_header = 1;
1201-
revs->commit_format = get_commit_format(arg+8);
1201+
get_commit_format(arg+8, revs);
12021202
continue;
12031203
}
12041204
if (!strcmp(arg, "--root")) {

revision.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ struct rev_info {
6464

6565
/* Format info */
6666
unsigned int shown_one:1,
67-
abbrev_commit:1;
67+
abbrev_commit:1,
68+
use_terminator:1;
6869
enum date_mode date_mode;
6970

7071
const char **ignore_packed; /* pretend objects in these are unpacked */

0 commit comments

Comments
 (0)