log: add --clear-decorations option

The previous changes introduced a new default ref filter for decorations in the 'git log' command. This can be overridden using --decorate-refs=HEAD and --decorate-refs=refs/, but that is cumbersome for users. Instead, add a --clear-decorations option that resets all previous filters to a blank filter that accepts all refs. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Aug 5, 2022 at 17:58 UTC 748706d71365f419dad94cb7e54a31151c197218
8 files changed +371 -7
Documentation/git-log.txt
+5
@@ -56,6 +56,11 @@ If none of these options or config settings are given, then references are
56 used as decoration if they match `HEAD`, `refs/heads/`, `refs/remotes/`,
57 `refs/stash/`, or `refs/tags/`.
58
59 +--clear-decorations::
60 + When specified, this option clears all previous `--decorate-refs`
61 + or `--decorate-refs-exclude` options and relaxes the default
62 + decoration filter to include all references.
63 +
64 --source::
65 Print out the ref name given on the command line by which each
66 commit was reached.
builtin/log.c
+20 -4
@@ -101,6 +101,20 @@ static int parse_decoration_style(const char *value)
101 return -1;
102 }
103
104 +static int use_default_decoration_filter = 1;
105 +static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
106 +static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
107 +static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
108 +
109 +static int clear_decorations_callback(const struct option *opt,
110 + const char *arg, int unset)
111 +{
112 + string_list_clear(&decorate_refs_include, 0);
113 + string_list_clear(&decorate_refs_exclude, 0);
114 + use_default_decoration_filter = 0;
115 + return 0;
116 +}
117 +
118 static int decorate_callback(const struct option *opt, const char *arg, int unset)
119 {
120 if (unset)
@@ -176,7 +190,8 @@ static void set_default_decoration_filter(struct decoration_filter *decoration_f
190 item->string);
191 }
192
179 - if (decoration_filter->exclude_ref_pattern->nr ||
193 + if (!use_default_decoration_filter ||
194 + decoration_filter->exclude_ref_pattern->nr ||
195 decoration_filter->include_ref_pattern->nr ||
196 decoration_filter->exclude_ref_config_pattern->nr)
197 return;
@@ -199,9 +214,6 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
214 struct userformat_want w;
215 int quiet = 0, source = 0, mailmap;
216 static struct line_opt_callback_data line_cb = {NULL, NULL, STRING_LIST_INIT_DUP};
202 - static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
203 - static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
204 - static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
217 struct decoration_filter decoration_filter = {
218 .exclude_ref_pattern = &decorate_refs_exclude,
219 .include_ref_pattern = &decorate_refs_include,
@@ -214,6 +226,10 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
226 OPT_BOOL(0, "source", &source, N_("show source")),
227 OPT_BOOL(0, "use-mailmap", &mailmap, N_("use mail map file")),
228 OPT_ALIAS(0, "mailmap", "use-mailmap"),
229 + OPT_CALLBACK_F(0, "clear-decorations", NULL, NULL,
230 + N_("clear all previously-defined decoration filters"),
231 + PARSE_OPT_NOARG | PARSE_OPT_NONEG,
232 + clear_decorations_callback),
233 OPT_STRING_LIST(0, "decorate-refs", &decorate_refs_include,
234 N_("pattern"), N_("only decorate refs that match <pattern>")),
235 OPT_STRING_LIST(0, "decorate-refs-exclude", &decorate_refs_exclude,
t/t4013-diff-various.sh
+2
@@ -352,6 +352,8 @@ log -GF -p --pickaxe-all master
352 log -IA -IB -I1 -I2 -p master
353 log --decorate --all
354 log --decorate=full --all
355 +log --decorate --clear-decorations --all
356 +log --decorate=full --clear-decorations --all
357
358 rev-list --parents HEAD
359 rev-list --children HEAD
t/t4013/diff.log_--decorate=full_--clear-decorations_--all new
+61
@@ -0,0 +1,61 @@
1 +$ git log --decorate=full --clear-decorations --all
2 +commit b7e0bc69303b488b47deca799a7d723971dfa6cd (refs/heads/mode)
3 +Author: A U Thor <author@example.com>
4 +Date: Mon Jun 26 00:06:00 2006 +0000
5 +
6 + update mode
7 +
8 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (refs/heads/note)
9 +Author: A U Thor <author@example.com>
10 +Date: Mon Jun 26 00:06:00 2006 +0000
11 +
12 + update mode (file2)
13 +
14 +Notes:
15 + note
16 +
17 +commit cd4e72fd96faed3f0ba949dc42967430374e2290 (refs/heads/rearrange)
18 +Author: A U Thor <author@example.com>
19 +Date: Mon Jun 26 00:06:00 2006 +0000
20 +
21 + Rearranged lines in dir/sub
22 +
23 +commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
24 +Author: A U Thor <author@example.com>
25 +Date: Mon Jun 26 00:06:00 2006 +0000
26 +
27 + Notes added by 'git notes add'
28 +
29 +commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> refs/heads/master)
30 +Merge: 9a6d494 c7a2ab9
31 +Author: A U Thor <author@example.com>
32 +Date: Mon Jun 26 00:04:00 2006 +0000
33 +
34 + Merge branch 'side'
35 +
36 +commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (refs/heads/side)
37 +Author: A U Thor <author@example.com>
38 +Date: Mon Jun 26 00:03:00 2006 +0000
39 +
40 + Side
41 +
42 +commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
43 +Author: A U Thor <author@example.com>
44 +Date: Mon Jun 26 00:02:00 2006 +0000
45 +
46 + Third
47 +
48 +commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
49 +Author: A U Thor <author@example.com>
50 +Date: Mon Jun 26 00:01:00 2006 +0000
51 +
52 + Second
53 +
54 + This is the second commit.
55 +
56 +commit 444ac553ac7612cc88969031b02b3767fb8a353a (refs/heads/initial)
57 +Author: A U Thor <author@example.com>
58 +Date: Mon Jun 26 00:00:00 2006 +0000
59 +
60 + Initial
61 +$
t/t4013/diff.log_--decorate=full_--decorate-all_--all new
+61
@@ -0,0 +1,61 @@
1 +$ git log --decorate=full --decorate-all --all
2 +commit b7e0bc69303b488b47deca799a7d723971dfa6cd (refs/heads/mode)
3 +Author: A U Thor <author@example.com>
4 +Date: Mon Jun 26 00:06:00 2006 +0000
5 +
6 + update mode
7 +
8 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (refs/heads/note)
9 +Author: A U Thor <author@example.com>
10 +Date: Mon Jun 26 00:06:00 2006 +0000
11 +
12 + update mode (file2)
13 +
14 +Notes:
15 + note
16 +
17 +commit cd4e72fd96faed3f0ba949dc42967430374e2290 (refs/heads/rearrange)
18 +Author: A U Thor <author@example.com>
19 +Date: Mon Jun 26 00:06:00 2006 +0000
20 +
21 + Rearranged lines in dir/sub
22 +
23 +commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
24 +Author: A U Thor <author@example.com>
25 +Date: Mon Jun 26 00:06:00 2006 +0000
26 +
27 + Notes added by 'git notes add'
28 +
29 +commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> refs/heads/master)
30 +Merge: 9a6d494 c7a2ab9
31 +Author: A U Thor <author@example.com>
32 +Date: Mon Jun 26 00:04:00 2006 +0000
33 +
34 + Merge branch 'side'
35 +
36 +commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (refs/heads/side)
37 +Author: A U Thor <author@example.com>
38 +Date: Mon Jun 26 00:03:00 2006 +0000
39 +
40 + Side
41 +
42 +commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
43 +Author: A U Thor <author@example.com>
44 +Date: Mon Jun 26 00:02:00 2006 +0000
45 +
46 + Third
47 +
48 +commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
49 +Author: A U Thor <author@example.com>
50 +Date: Mon Jun 26 00:01:00 2006 +0000
51 +
52 + Second
53 +
54 + This is the second commit.
55 +
56 +commit 444ac553ac7612cc88969031b02b3767fb8a353a (refs/heads/initial)
57 +Author: A U Thor <author@example.com>
58 +Date: Mon Jun 26 00:00:00 2006 +0000
59 +
60 + Initial
61 +$
t/t4013/diff.log_--decorate_--clear-decorations_--all new
+61
@@ -0,0 +1,61 @@
1 +$ git log --decorate --clear-decorations --all
2 +commit b7e0bc69303b488b47deca799a7d723971dfa6cd (mode)
3 +Author: A U Thor <author@example.com>
4 +Date: Mon Jun 26 00:06:00 2006 +0000
5 +
6 + update mode
7 +
8 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (note)
9 +Author: A U Thor <author@example.com>
10 +Date: Mon Jun 26 00:06:00 2006 +0000
11 +
12 + update mode (file2)
13 +
14 +Notes:
15 + note
16 +
17 +commit cd4e72fd96faed3f0ba949dc42967430374e2290 (rearrange)
18 +Author: A U Thor <author@example.com>
19 +Date: Mon Jun 26 00:06:00 2006 +0000
20 +
21 + Rearranged lines in dir/sub
22 +
23 +commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
24 +Author: A U Thor <author@example.com>
25 +Date: Mon Jun 26 00:06:00 2006 +0000
26 +
27 + Notes added by 'git notes add'
28 +
29 +commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> master)
30 +Merge: 9a6d494 c7a2ab9
31 +Author: A U Thor <author@example.com>
32 +Date: Mon Jun 26 00:04:00 2006 +0000
33 +
34 + Merge branch 'side'
35 +
36 +commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (side)
37 +Author: A U Thor <author@example.com>
38 +Date: Mon Jun 26 00:03:00 2006 +0000
39 +
40 + Side
41 +
42 +commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
43 +Author: A U Thor <author@example.com>
44 +Date: Mon Jun 26 00:02:00 2006 +0000
45 +
46 + Third
47 +
48 +commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
49 +Author: A U Thor <author@example.com>
50 +Date: Mon Jun 26 00:01:00 2006 +0000
51 +
52 + Second
53 +
54 + This is the second commit.
55 +
56 +commit 444ac553ac7612cc88969031b02b3767fb8a353a (initial)
57 +Author: A U Thor <author@example.com>
58 +Date: Mon Jun 26 00:00:00 2006 +0000
59 +
60 + Initial
61 +$
t/t4013/diff.log_--decorate_--decorate-all_--all new
+61
@@ -0,0 +1,61 @@
1 +$ git log --decorate --decorate-all --all
2 +commit b7e0bc69303b488b47deca799a7d723971dfa6cd (mode)
3 +Author: A U Thor <author@example.com>
4 +Date: Mon Jun 26 00:06:00 2006 +0000
5 +
6 + update mode
7 +
8 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (note)
9 +Author: A U Thor <author@example.com>
10 +Date: Mon Jun 26 00:06:00 2006 +0000
11 +
12 + update mode (file2)
13 +
14 +Notes:
15 + note
16 +
17 +commit cd4e72fd96faed3f0ba949dc42967430374e2290 (rearrange)
18 +Author: A U Thor <author@example.com>
19 +Date: Mon Jun 26 00:06:00 2006 +0000
20 +
21 + Rearranged lines in dir/sub
22 +
23 +commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
24 +Author: A U Thor <author@example.com>
25 +Date: Mon Jun 26 00:06:00 2006 +0000
26 +
27 + Notes added by 'git notes add'
28 +
29 +commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> master)
30 +Merge: 9a6d494 c7a2ab9
31 +Author: A U Thor <author@example.com>
32 +Date: Mon Jun 26 00:04:00 2006 +0000
33 +
34 + Merge branch 'side'
35 +
36 +commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (side)
37 +Author: A U Thor <author@example.com>
38 +Date: Mon Jun 26 00:03:00 2006 +0000
39 +
40 + Side
41 +
42 +commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
43 +Author: A U Thor <author@example.com>
44 +Date: Mon Jun 26 00:02:00 2006 +0000
45 +
46 + Third
47 +
48 +commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
49 +Author: A U Thor <author@example.com>
50 +Date: Mon Jun 26 00:01:00 2006 +0000
51 +
52 + Second
53 +
54 + This is the second commit.
55 +
56 +commit 444ac553ac7612cc88969031b02b3767fb8a353a (initial)
57 +Author: A U Thor <author@example.com>
58 +Date: Mon Jun 26 00:00:00 2006 +0000
59 +
60 + Initial
61 +$
t/t4202-log.sh
+100 -3
@@ -704,9 +704,12 @@ test_expect_success 'set up more tangled history' '
704 git checkout -b tangle HEAD~6 &&
705 test_commit tangle-a tangle-a a &&
706 git merge main~3 &&
707 + git update-ref refs/prefetch/merge HEAD &&
708 git merge side~1 &&
709 + git update-ref refs/rewritten/merge HEAD &&
710 git checkout main &&
711 git merge tangle &&
712 + git update-ref refs/hidden/tangle HEAD &&
713 git checkout -b reach &&
714 test_commit reach &&
715 git checkout main &&
@@ -974,9 +977,9 @@ test_expect_success 'decorate-refs-exclude and simplify-by-decoration' '
977 Merge-tag-reach (HEAD -> main)
978 reach (tag: reach, reach)
979 seventh (tag: seventh)
977 - Merge-branch-tangle
978 - Merge-branch-side-early-part-into-tangle (tangle)
979 - tangle-a (tag: tangle-a)
980 + Merge-branch-tangle (refs/hidden/tangle)
981 + Merge-branch-side-early-part-into-tangle (refs/rewritten/merge, tangle)
982 + Merge-branch-main-early-part-into-tangle (refs/prefetch/merge)
983 EOF
984 git log -n6 --decorate=short --pretty="tformat:%f%d" \
985 --decorate-refs-exclude="*octopus*" \
@@ -1037,6 +1040,100 @@ test_expect_success 'decorate-refs focus from default' '
1040 ! grep HEAD actual
1041 '
1042
1043 +test_expect_success '--clear-decorations overrides defaults' '
1044 + cat >expect.default <<-\EOF &&
1045 + Merge-tag-reach (HEAD -> refs/heads/main)
1046 + Merge-tags-octopus-a-and-octopus-b
1047 + seventh (tag: refs/tags/seventh)
1048 + octopus-b (tag: refs/tags/octopus-b, refs/heads/octopus-b)
1049 + octopus-a (tag: refs/tags/octopus-a, refs/heads/octopus-a)
1050 + reach (tag: refs/tags/reach, refs/heads/reach)
1051 + Merge-branch-tangle
1052 + Merge-branch-side-early-part-into-tangle (refs/heads/tangle)
1053 + Merge-branch-main-early-part-into-tangle
1054 + tangle-a (tag: refs/tags/tangle-a)
1055 + Merge-branch-side
1056 + side-2 (tag: refs/tags/side-2, refs/heads/side)
1057 + side-1 (tag: refs/tags/side-1)
1058 + Second
1059 + sixth
1060 + fifth
1061 + fourth
1062 + third
1063 + second
1064 + initial
1065 + EOF
1066 + git log --decorate=full --pretty="tformat:%f%d" >actual &&
1067 + test_cmp expect.default actual &&
1068 +
1069 + cat >expect.all <<-\EOF &&
1070 + Merge-tag-reach (HEAD -> refs/heads/main)
1071 + Merge-tags-octopus-a-and-octopus-b
1072 + seventh (tag: refs/tags/seventh)
1073 + octopus-b (tag: refs/tags/octopus-b, refs/heads/octopus-b)
1074 + octopus-a (tag: refs/tags/octopus-a, refs/heads/octopus-a)
1075 + reach (tag: refs/tags/reach, refs/heads/reach)
1076 + Merge-branch-tangle (refs/hidden/tangle)
1077 + Merge-branch-side-early-part-into-tangle (refs/rewritten/merge, refs/heads/tangle)
1078 + Merge-branch-main-early-part-into-tangle (refs/prefetch/merge)
1079 + tangle-a (tag: refs/tags/tangle-a)
1080 + Merge-branch-side
1081 + side-2 (tag: refs/tags/side-2, refs/heads/side)
1082 + side-1 (tag: refs/tags/side-1)
1083 + Second
1084 + sixth
1085 + fifth
1086 + fourth
1087 + third
1088 + second
1089 + initial
1090 + EOF
1091 + git log --decorate=full --pretty="tformat:%f%d" \
1092 + --clear-decorations >actual &&
1093 + test_cmp expect.all actual
1094 +'
1095 +
1096 +test_expect_success '--clear-decorations clears previous exclusions' '
1097 + cat >expect.all <<-\EOF &&
1098 + Merge-tag-reach (HEAD -> refs/heads/main)
1099 + reach (tag: refs/tags/reach, refs/heads/reach)
1100 + Merge-tags-octopus-a-and-octopus-b
1101 + octopus-b (tag: refs/tags/octopus-b, refs/heads/octopus-b)
1102 + octopus-a (tag: refs/tags/octopus-a, refs/heads/octopus-a)
1103 + seventh (tag: refs/tags/seventh)
1104 + Merge-branch-tangle (refs/hidden/tangle)
1105 + Merge-branch-side-early-part-into-tangle (refs/rewritten/merge, refs/heads/tangle)
1106 + Merge-branch-main-early-part-into-tangle (refs/prefetch/merge)
1107 + tangle-a (tag: refs/tags/tangle-a)
1108 + side-2 (tag: refs/tags/side-2, refs/heads/side)
1109 + side-1 (tag: refs/tags/side-1)
1110 + initial
1111 + EOF
1112 +
1113 + git log --decorate=full --pretty="tformat:%f%d" \
1114 + --simplify-by-decoration \
1115 + --decorate-refs-exclude="heads/octopus*" \
1116 + --decorate-refs="heads" \
1117 + --clear-decorations >actual &&
1118 + test_cmp expect.all actual &&
1119 +
1120 + cat >expect.filtered <<-\EOF &&
1121 + Merge-tags-octopus-a-and-octopus-b
1122 + octopus-b (refs/heads/octopus-b)
1123 + octopus-a (refs/heads/octopus-a)
1124 + initial
1125 + EOF
1126 +
1127 + git log --decorate=full --pretty="tformat:%f%d" \
1128 + --simplify-by-decoration \
1129 + --decorate-refs-exclude="heads/octopus" \
1130 + --decorate-refs="heads" \
1131 + --clear-decorations \
1132 + --decorate-refs-exclude="tags/" \
1133 + --decorate-refs="heads/octopus*" >actual &&
1134 + test_cmp expect.filtered actual
1135 +'
1136 +
1137 test_expect_success 'log.decorate config parsing' '
1138 git log --oneline --decorate=full >expect.full &&
1139 git log --oneline --decorate=short >expect.short &&