@matheusbsbs / matheus-bagliano / commits / 4da45bef56
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Apr 7, 2008 at 17:11 UTC 4da45bef56e1547eb6525015ada0fdfc01d8295b
7 files changed +43 -31
builtin-commit.c
+1 -1
@@ -810,7 +810,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
810
811 rev.verbose_header = 1;
812 rev.show_root_diff = 1;
813 - rev.commit_format = get_commit_format("format:%h: %s");
813 + get_commit_format("format:%h: %s", &rev);
814 rev.always_show_header = 0;
815 rev.diffopt.detect_rename = 1;
816 rev.diffopt.rename_limit = 100;
builtin-log.c
+2 -1
@@ -56,7 +56,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
56 rev->abbrev = DEFAULT_ABBREV;
57 rev->commit_format = CMIT_FMT_DEFAULT;
58 if (fmt_pretty)
59 - rev->commit_format = get_commit_format(fmt_pretty);
59 + get_commit_format(fmt_pretty, rev);
60 rev->verbose_header = 1;
61 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
62 rev->show_root_diff = default_show_root;
@@ -400,6 +400,7 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
400 * allow us to set a different default.
401 */
402 rev.commit_format = CMIT_FMT_ONELINE;
403 + rev.use_terminator = 1;
404 rev.always_show_header = 1;
405
406 /*
commit.h
+2 -1
@@ -63,7 +63,8 @@ enum cmit_fmt {
63 };
64
65 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 *);
68 extern void format_commit_message(const struct commit *commit,
69 const void *format, struct strbuf *sb);
70 extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*,
log-tree.c
+2 -2
@@ -249,9 +249,9 @@ void show_log(struct rev_info *opt, const char *sep)
249 * not have an empty line between entries.
250 */
251 extra = "";
252 - if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
252 + if (*sep != '\n' && opt->use_terminator)
253 extra = "\n";
254 - if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
254 + if (opt->shown_one && !opt->use_terminator)
255 putchar(opt->diffopt.line_termination);
256 opt->shown_one = 1;
257
pretty.c
+33 -24
@@ -4,40 +4,49 @@
4 #include "diff.h"
5 #include "revision.h"
6
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 -
7 static char *user_format;
8
24 -enum cmit_fmt get_commit_format(const char *arg)
9 +void get_commit_format(const char *arg, struct rev_info *rev)
10 {
11 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 + }
31 if (*arg == '=')
32 arg++;
32 - if (!prefixcmp(arg, "format:")) {
33 + if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
34 + const char *cp = strchr(arg, ':') + 1;
35 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;
41 }
42 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
43 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 + }
50 }
51
52 die("invalid --pretty format: %s", arg);
revision.c
+1 -1
@@ -1198,7 +1198,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
1198 }
1199 if (!prefixcmp(arg, "--pretty")) {
1200 revs->verbose_header = 1;
1201 - revs->commit_format = get_commit_format(arg+8);
1201 + get_commit_format(arg+8, revs);
1202 continue;
1203 }
1204 if (!strcmp(arg, "--root")) {
revision.h
+2 -1
@@ -64,7 +64,8 @@ struct rev_info {
64
65 /* Format info */
66 unsigned int shown_one:1,
67 - abbrev_commit:1;
67 + abbrev_commit:1,
68 + use_terminator:1;
69 enum date_mode date_mode;
70
71 const char **ignore_packed; /* pretend objects in these are unpacked */