@matheusbsbs / matheus-bagliano / commits / 8028184eec
Fork 0

pretty: add aliases for pretty formats

previously the only ways to alias a --pretty format within git were either to set the format as your default format (via the format.pretty configuration variable), or by using a regular git alias. This left the definition of more complicated formats to the realm of "builtin or nothing", with user-defined formats usually being reserved for quick one-offs. Here we allow user-defined formats to enjoy more or less the same benefits of builtins. By defining pretty.myalias, "myalias" can be used in place of whatever would normally come after --pretty=. This can be a format:, tformat:, raw (ie, defaulting to tformat), or the name of another builtin or user-defined pretty format. Signed-off-by: Will Palmer <wmpalmer@gmail.com> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Will Palmer committed May 2, 2010 at 12:00 UTC 8028184eecf1051e4b39b308a3ad909eed1371c4
4 files changed +145 -3
Documentation/config.txt
+10
@@ -1466,6 +1466,16 @@ pager.<cmd>::
1466 it takes precedence over this option. To disable pagination for
1467 all commands, set `core.pager` or `GIT_PAGER` to `cat`.
1468
1469 +pretty.<name>::
1470 + Alias for a --pretty= format string, as specified in
1471 + linkgit:git-log[1]. Any aliases defined here can be used just
1472 + as the built-in pretty formats could. For example,
1473 + running `git config pretty.changelog "format:{asterisk} %H %s"`
1474 + would cause the invocation `git log --pretty=changelog`
1475 + to be equivalent to running `git log "--pretty=format:{asterisk} %H %s"`.
1476 + Note that an alias with the same name as a built-in format
1477 + will be silently ignored.
1478 +
1479 pull.octopus::
1480 The default merge strategy to use when pulling multiple branches
1481 at once.
Documentation/pretty-formats.txt
+6 -1
@@ -11,7 +11,12 @@ have limited your view of history: for example, if you are
11 only interested in changes related to a certain directory or
12 file.
13
14 -Here are some additional details for each format:
14 +There are several built-in formats, and you can define
15 +additional formats by setting a pretty.<name>
16 +config option to either another format name, or a
17 +'format:' string, as described below (see
18 +linkgit:git-config[1]). Here are the details of the
19 +built-in formats:
20
21 * 'oneline'
22
pretty.c
+55 -2
@@ -18,7 +18,9 @@ static struct cmt_fmt_map {
18 int is_alias;
19 const char *user_format;
20 } *commit_formats;
21 +static size_t builtin_formats_len;
22 static size_t commit_formats_len;
23 +static size_t commit_formats_alloc;
24 static struct cmt_fmt_map *find_commit_format(const char *sought);
25
26 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
@@ -30,6 +32,51 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
32 rev->commit_format = CMIT_FMT_USERFORMAT;
33 }
34
35 +static int git_pretty_formats_config(const char *var, const char *value, void *cb)
36 +{
37 + struct cmt_fmt_map *commit_format = NULL;
38 + const char *name;
39 + const char *fmt;
40 + int i;
41 +
42 + if (prefixcmp(var, "pretty."))
43 + return 0;
44 +
45 + name = var + strlen("pretty.");
46 + for (i = 0; i < builtin_formats_len; i++) {
47 + if (!strcmp(commit_formats[i].name, name))
48 + return 0;
49 + }
50 +
51 + for (i = builtin_formats_len; i < commit_formats_len; i++) {
52 + if (!strcmp(commit_formats[i].name, name)) {
53 + commit_format = &commit_formats[i];
54 + break;
55 + }
56 + }
57 +
58 + if (!commit_format) {
59 + ALLOC_GROW(commit_formats, commit_formats_len+1,
60 + commit_formats_alloc);
61 + commit_format = &commit_formats[commit_formats_len];
62 + commit_formats_len++;
63 + }
64 +
65 + commit_format->name = xstrdup(name);
66 + commit_format->format = CMIT_FMT_USERFORMAT;
67 + git_config_string(&fmt, var, value);
68 + if (!prefixcmp(fmt, "format:") || !prefixcmp(fmt, "tformat:")) {
69 + commit_format->is_tformat = fmt[0] == 't';
70 + fmt = strchr(fmt, ':') + 1;
71 + } else if (strchr(fmt, '%'))
72 + commit_format->is_tformat = 1;
73 + else
74 + commit_format->is_alias = 1;
75 + commit_format->user_format = fmt;
76 +
77 + return 0;
78 +}
79 +
80 static void setup_commit_formats(void)
81 {
82 struct cmt_fmt_map builtin_formats[] = {
@@ -42,10 +89,12 @@ static void setup_commit_formats(void)
89 { "oneline", CMIT_FMT_ONELINE, 1 }
90 };
91 commit_formats_len = ARRAY_SIZE(builtin_formats);
45 - commit_formats = xmalloc(commit_formats_len *
46 - sizeof(*builtin_formats));
92 + builtin_formats_len = commit_formats_len;
93 + ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
94 memcpy(commit_formats, builtin_formats,
95 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
96 +
97 + git_config(git_pretty_formats_config, NULL);
98 }
99
100 static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
@@ -116,6 +165,10 @@ void get_commit_format(const char *arg, struct rev_info *rev)
165
166 rev->commit_format = commit_format->format;
167 rev->use_terminator = commit_format->is_tformat;
168 + if (commit_format->format == CMIT_FMT_USERFORMAT) {
169 + save_user_format(rev, commit_format->user_format,
170 + commit_format->is_tformat);
171 + }
172 }
173
174 /*
t/t4205-log-pretty-formats.sh new
+74
@@ -0,0 +1,74 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2010, Will Palmer
4 +#
5 +
6 +test_description='Test pretty formats'
7 +. ./test-lib.sh
8 +
9 +test_expect_success 'set up basic repos' '
10 + >foo &&
11 + >bar &&
12 + git add foo &&
13 + test_tick &&
14 + git commit -m initial &&
15 + git add bar &&
16 + test_tick &&
17 + git commit -m "add bar"
18 +'
19 +
20 +test_expect_success 'alias builtin format' '
21 + git log --pretty=oneline >expected &&
22 + git config pretty.test-alias oneline &&
23 + git log --pretty=test-alias >actual &&
24 + test_cmp expected actual
25 +'
26 +
27 +test_expect_success 'alias masking builtin format' '
28 + git log --pretty=oneline >expected &&
29 + git config pretty.oneline "%H" &&
30 + git log --pretty=oneline >actual &&
31 + test_cmp expected actual
32 +'
33 +
34 +test_expect_success 'alias user-defined format' '
35 + git log --pretty="format:%h" >expected &&
36 + git config pretty.test-alias "format:%h" &&
37 + git log --pretty=test-alias >actual &&
38 + test_cmp expected actual
39 +'
40 +
41 +test_expect_success 'alias user-defined tformat' '
42 + git log --pretty="tformat:%h" >expected &&
43 + git config pretty.test-alias "tformat:%h" &&
44 + git log --pretty=test-alias >actual &&
45 + test_cmp expected actual
46 +'
47 +
48 +test_expect_success 'alias non-existant format' '
49 + git config pretty.test-alias format-that-will-never-exist &&
50 + test_must_fail git log --pretty=test-alias
51 +'
52 +
53 +test_expect_success 'alias of an alias' '
54 + git log --pretty="tformat:%h" >expected &&
55 + git config pretty.test-foo "tformat:%h" &&
56 + git config pretty.test-bar test-foo &&
57 + git log --pretty=test-bar >actual && test_cmp expected actual
58 +'
59 +
60 +test_expect_success 'alias masking an alias' '
61 + git log --pretty=format:"Two %H" >expected &&
62 + git config pretty.duplicate "format:One %H" &&
63 + git config --add pretty.duplicate "format:Two %H" &&
64 + git log --pretty=duplicate >actual &&
65 + test_cmp expected actual
66 +'
67 +
68 +test_expect_success 'alias loop' '
69 + git config pretty.test-foo test-bar &&
70 + git config pretty.test-bar test-foo &&
71 + test_must_fail git log --pretty=test-foo
72 +'
73 +
74 +test_done