@matheusbsbs / matheus-bagliano / commits / 73e9da0196
Fork 0

Add an optional argument for --color options

Make git-branch, git-show-branch, git-grep, and all the diff-based programs accept an optional argument <when> for --color. The argument is a colorbool: "always", "never", or "auto". If no argument is given, "always" is used; --no-color is an alias for --color=never. This makes the command-line interface consistent with other GNU tools, such as `ls' and `grep', and with the git-config color options. Note that, without an argument, --color and --no-color work exactly as before. To implement this, two internal changes were made: 1. Allow the first argument of git_config_colorbool() to be NULL, in which case it returns -1 if the argument isn't "always", "never", or "auto". 2. Add OPT_COLOR_FLAG(), OPT__COLOR(), and parse_opt_color_flag_cb() to the option parsing library. The callback uses git_config_colorbool(), so color.h is now a dependency of parse-options.c. Signed-off-by: Mark Lodato <lodatom@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mark Lodato committed Feb 16, 2010 at 23:55 UTC 73e9da019655261e456ed862340880de365111f0
12 files changed +66 -11
Documentation/diff-options.txt
+3 -1
@@ -117,12 +117,14 @@ any of those replacements occurred.
117 option and lists the commits in that commit range like the 'summary'
118 option of linkgit:git-submodule[1] does.
119
120 ---color::
120 +--color[=<when>]::
121 Show colored diff.
122 + The value must be always (the default), never, or auto.
123
124 --no-color::
125 Turn off colored diff, even when the configuration file
126 gives the default to color output.
127 + Same as `--color=never`.
128
129 --color-words[=<regex>]::
130 Show colored word diff, i.e., color words which have changed.
Documentation/git-branch.txt
+4 -2
@@ -8,7 +8,7 @@ git-branch - List, create, or delete branches
8 SYNOPSIS
9 --------
10 [verse]
11 -'git branch' [--color | --no-color] [-r | -a]
11 +'git branch' [--color[=<when>] | --no-color] [-r | -a]
12 [-v [--abbrev=<length> | --no-abbrev]]
13 [(--merged | --no-merged | --contains) [<commit>]]
14 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
@@ -84,12 +84,14 @@ OPTIONS
84 -M::
85 Move/rename a branch even if the new branch name already exists.
86
87 ---color::
87 +--color[=<when>]::
88 Color branches to highlight current, local, and remote branches.
89 + The value must be always (the default), never, or auto.
90
91 --no-color::
92 Turn off branch colors, even when the configuration file gives the
93 default to color output.
94 + Same as `--color=never`.
95
96 -r::
97 List or delete (if used with -d) the remote-tracking branches.
Documentation/git-grep.txt
+4 -2
@@ -18,7 +18,7 @@ SYNOPSIS
18 [-z | --null]
19 [-c | --count] [--all-match] [-q | --quiet]
20 [--max-depth <depth>]
21 - [--color | --no-color]
21 + [--color[=<when>] | --no-color]
22 [-A <post-context>] [-B <pre-context>] [-C <context>]
23 [-f <file>] [-e] <pattern>
24 [--and|--or|--not|(|)|-e <pattern>...] [<tree>...]
@@ -111,12 +111,14 @@ OPTIONS
111 Instead of showing every matched line, show the number of
112 lines that match.
113
114 ---color::
114 +--color[=<when>]::
115 Show colored matches.
116 + The value must be always (the default), never, or auto.
117
118 --no-color::
119 Turn off match highlighting, even when the configuration file
120 gives the default to color output.
121 + Same as `--color=never`.
122
123 -[ABC] <context>::
124 Show `context` trailing (`A` -- after), or leading (`B`
Documentation/git-show-branch.txt
+4 -2
@@ -9,7 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git show-branch' [-a|--all] [-r|--remotes] [--topo-order | --date-order]
12 - [--current] [--color | --no-color] [--sparse]
12 + [--current] [--color[=<when>] | --no-color] [--sparse]
13 [--more=<n> | --list | --independent | --merge-base]
14 [--no-name | --sha1-name] [--topics]
15 [<rev> | <glob>]...
@@ -117,13 +117,15 @@ OPTIONS
117 When no explicit <ref> parameter is given, it defaults to the
118 current branch (or `HEAD` if it is detached).
119
120 ---color::
120 +--color[=<when>]::
121 Color the status sign (one of these: `*` `!` `+` `-`) of each commit
122 corresponding to the branch it's in.
123 + The value must be always (the default), never, or auto.
124
125 --no-color::
126 Turn off colored output, even when the configuration file gives the
127 default to color output.
128 + Same as `--color=never`.
129
130 Note that --more, --list, --independent and --merge-base options
131 are mutually exclusive.
Documentation/technical/api-parse-options.txt
+12
@@ -115,6 +115,9 @@ There are some macros to easily define options:
115 `OPT__ABBREV(&int_var)`::
116 Add `\--abbrev[=<n>]`.
117
118 +`OPT__COLOR(&int_var, description)`::
119 + Add `\--color[=<when>]` and `--no-color`.
120 +
121 `OPT__DRY_RUN(&int_var)`::
122 Add `-n, \--dry-run`.
123
@@ -183,6 +186,15 @@ There are some macros to easily define options:
186 arguments. Short options that happen to be digits take
187 precedence over it.
188
189 +`OPT_COLOR_FLAG(short, long, &int_var, description)`::
190 + Introduce an option that takes an optional argument that can
191 + have one of three values: "always", "never", or "auto". If the
192 + argument is not given, it defaults to "always". The `--no-` form
193 + works like `--long=never`; it cannot take an argument. If
194 + "always", set `int_var` to 1; if "never", set `int_var` to 0; if
195 + "auto", set `int_var` to 1 if stdout is a tty or a pager,
196 + 0 otherwise.
197 +
198
199 The last element of the array must be `OPT_END()`.
200
builtin-branch.c
+1 -1
@@ -610,7 +610,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
610 BRANCH_TRACK_EXPLICIT),
611 OPT_SET_INT( 0, "set-upstream", &track, "change upstream info",
612 BRANCH_TRACK_OVERRIDE),
613 - OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"),
613 + OPT__COLOR(&branch_use_color, "use colored output"),
614 OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches",
615 REF_REMOTE_BRANCH),
616 {
builtin-grep.c
+1 -1
@@ -782,7 +782,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
782 "print NUL after filenames"),
783 OPT_BOOLEAN('c', "count", &opt.count,
784 "show the number of matches instead of matching lines"),
785 - OPT_SET_INT(0, "color", &opt.color, "highlight matches", 1),
785 + OPT__COLOR(&opt.color, "highlight matches"),
786 OPT_GROUP(""),
787 OPT_CALLBACK('C', NULL, &opt, "n",
788 "show <n> context lines before and after matches",
builtin-show-branch.c
+2 -2
@@ -6,7 +6,7 @@
6 #include "parse-options.h"
7
8 static const char* show_branch_usage[] = {
9 - "git show-branch [-a|--all] [-r|--remotes] [--topo-order | --date-order] [--current] [--color | --no-color] [--sparse] [--more=<n> | --list | --independent | --merge-base] [--no-name | --sha1-name] [--topics] [<rev> | <glob>]...",
9 + "git show-branch [-a|--all] [-r|--remotes] [--topo-order | --date-order] [--current] [--color[=<when>] | --no-color] [--sparse] [--more=<n> | --list | --independent | --merge-base] [--no-name | --sha1-name] [--topics] [<rev> | <glob>]...",
10 "git show-branch (-g|--reflog)[=<n>[,<base>]] [--list] [<ref>]",
11 NULL
12 };
@@ -661,7 +661,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
661 "show remote-tracking and local branches"),
662 OPT_BOOLEAN('r', "remotes", &all_remotes,
663 "show remote-tracking branches"),
664 - OPT_BOOLEAN(0, "color", &showbranch_use_color,
664 + OPT__COLOR(&showbranch_use_color,
665 "color '*!+-' corresponding to the branch"),
666 { OPTION_INTEGER, 0, "more", &extra, "n",
667 "show <n> more commits after the common ancestor",
color.c
+3
@@ -138,6 +138,9 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
138 goto auto_color;
139 }
140
141 + if (!var)
142 + return -1;
143 +
144 /* Missing or explicit false to turn off colorization */
145 if (!git_config_bool(var, value))
146 return 0;
diff.c
+9
@@ -2826,6 +2826,15 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2826 DIFF_OPT_SET(options, FOLLOW_RENAMES);
2827 else if (!strcmp(arg, "--color"))
2828 DIFF_OPT_SET(options, COLOR_DIFF);
2829 + else if (!prefixcmp(arg, "--color=")) {
2830 + int value = git_config_colorbool(NULL, arg+8, -1);
2831 + if (value == 0)
2832 + DIFF_OPT_CLR(options, COLOR_DIFF);
2833 + else if (value > 0)
2834 + DIFF_OPT_SET(options, COLOR_DIFF);
2835 + else
2836 + return error("option `color' expects \"always\", \"auto\", or \"never\"");
2837 + }
2838 else if (!strcmp(arg, "--no-color"))
2839 DIFF_OPT_CLR(options, COLOR_DIFF);
2840 else if (!strcmp(arg, "--color-words")) {
parse-options.c
+16
@@ -2,6 +2,7 @@
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
5 +#include "color.h"
6
7 static int parse_options_usage(const char * const *usagestr,
8 const struct option *opts);
@@ -599,6 +600,21 @@ int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
600 return 0;
601 }
602
603 +int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
604 + int unset)
605 +{
606 + int value;
607 +
608 + if (!arg)
609 + arg = unset ? "never" : (const char *)opt->defval;
610 + value = git_config_colorbool(NULL, arg, -1);
611 + if (value < 0)
612 + return opterror(opt,
613 + "expects \"always\", \"auto\", or \"never\"", 0);
614 + *(int *)opt->value = value;
615 + return 0;
616 +}
617 +
618 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
619 int unset)
620 {
parse-options.h
+7
@@ -135,6 +135,10 @@ struct option {
135 PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }
136 #define OPT_FILENAME(s, l, v, h) { OPTION_FILENAME, (s), (l), (v), \
137 "FILE", (h) }
138 +#define OPT_COLOR_FLAG(s, l, v, h) \
139 + { OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
140 + parse_opt_color_flag_cb, (intptr_t)"always" }
141 +
142
143 /* parse_options() will filter out the processed options and leave the
144 * non-option arguments in argv[].
@@ -187,6 +191,7 @@ extern int parse_options_end(struct parse_opt_ctx_t *ctx);
191 /*----- some often used options -----*/
192 extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
193 extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
194 +extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
195 extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
196 extern int parse_opt_with_commit(const struct option *, const char *, int);
197 extern int parse_opt_tertiary(const struct option *, const char *, int);
@@ -203,5 +208,7 @@ extern int parse_opt_tertiary(const struct option *, const char *, int);
208 { OPTION_CALLBACK, 0, "abbrev", (var), "n", \
209 "use <n> digits to display SHA-1s", \
210 PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
211 +#define OPT__COLOR(var, h) \
212 + OPT_COLOR_FLAG(0, "color", (var), (h))
213
214 #endif