Skip to content

Commit

Permalink
Merge branch 'jk/color-and-pager'
Browse files Browse the repository at this point in the history
* jk/color-and-pager:
  want_color: automatically fallback to color.ui
  diff: don't load color config in plumbing
  config: refactor get_colorbool function
  color: delay auto-color decision until point of use
  git_config_colorbool: refactor stdout_is_tty handling
  diff: refactor COLOR_DIFF from a flag into an int
  setup_pager: set GIT_PAGER_IN_USE
  t7006: use test_config helpers
  test-lib: add helper functions for config
  t7006: modernize calls to unset

Conflicts:
	builtin/commit.c
	parse-options.c
  • Loading branch information
gitster committed Aug 29, 2011
2 parents e5cfcb0 + c9bfb95 commit f946b46
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 147 deletions.
7 changes: 2 additions & 5 deletions builtin/branch.c
Expand Up @@ -71,7 +71,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
static int git_branch_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "color.branch")) {
branch_use_color = git_config_colorbool(var, value, -1);
branch_use_color = git_config_colorbool(var, value);
return 0;
}
if (!prefixcmp(var, "color.branch.")) {
Expand All @@ -88,7 +88,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)

static const char *branch_get_color(enum color_branch ix)
{
if (branch_use_color > 0)
if (want_color(branch_use_color))
return branch_colors[ix];
return "";
}
Expand Down Expand Up @@ -673,9 +673,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix)

git_config(git_branch_config, NULL);

if (branch_use_color == -1)
branch_use_color = git_use_color_default;

track = git_branch_track;

head = resolve_ref("HEAD", head_sha1, 0, NULL);
Expand Down
13 changes: 2 additions & 11 deletions builtin/commit.c
Expand Up @@ -1141,7 +1141,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
return 0;
}
if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
s->use_color = git_config_colorbool(k, v, -1);
s->use_color = git_config_colorbool(k, v);
return 0;
}
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
Expand Down Expand Up @@ -1234,10 +1234,6 @@ int cmd_status(int argc, const char **argv, const char *prefix)

if (s.relative_paths)
s.prefix = prefix;
if (s.use_color == -1)
s.use_color = git_use_color_default;
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;

switch (status_format) {
case STATUS_FORMAT_SHORT:
Expand Down Expand Up @@ -1393,8 +1389,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
git_config(git_commit_config, &s);
determine_whence(&s);

if (s.use_color == -1)
s.use_color = git_use_color_default;
if (get_sha1("HEAD", sha1))
current_head = NULL;
else {
Expand All @@ -1404,11 +1398,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
}
argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
prefix, current_head, &s);
if (dry_run) {
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
if (dry_run)
return dry_run_commit(argc, argv, prefix, current_head, &s);
}
index_file = prepare_index(argc, argv, prefix, current_head, 0);

/* Set up everything for writing the commit object. This includes
Expand Down
28 changes: 11 additions & 17 deletions builtin/config.c
Expand Up @@ -303,24 +303,18 @@ static void get_color(const char *def_color)
fputs(parsed_color, stdout);
}

static int stdout_is_tty;
static int get_colorbool_found;
static int get_diff_color_found;
static int get_color_ui_found;
static int git_get_colorbool_config(const char *var, const char *value,
void *cb)
{
if (!strcmp(var, get_colorbool_slot)) {
get_colorbool_found =
git_config_colorbool(var, value, stdout_is_tty);
}
if (!strcmp(var, "diff.color")) {
get_diff_color_found =
git_config_colorbool(var, value, stdout_is_tty);
}
if (!strcmp(var, "color.ui")) {
git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
return 0;
}
if (!strcmp(var, get_colorbool_slot))
get_colorbool_found = git_config_colorbool(var, value);
else if (!strcmp(var, "diff.color"))
get_diff_color_found = git_config_colorbool(var, value);
else if (!strcmp(var, "color.ui"))
get_color_ui_found = git_config_colorbool(var, value);
return 0;
}

Expand All @@ -334,9 +328,11 @@ static int get_colorbool(int print)
if (!strcmp(get_colorbool_slot, "color.diff"))
get_colorbool_found = get_diff_color_found;
if (get_colorbool_found < 0)
get_colorbool_found = git_use_color_default;
get_colorbool_found = get_color_ui_found;
}

get_colorbool_found = want_color(get_colorbool_found);

if (print) {
printf("%s\n", get_colorbool_found ? "true" : "false");
return 0;
Expand Down Expand Up @@ -510,9 +506,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
}
else if (actions == ACTION_GET_COLORBOOL) {
if (argc == 1)
stdout_is_tty = git_config_bool("command line", argv[0]);
else if (argc == 0)
stdout_is_tty = isatty(1);
color_stdout_is_tty = git_config_bool("command line", argv[0]);
return get_colorbool(argc != 0);
}

Expand Down
3 changes: 0 additions & 3 deletions builtin/diff.c
Expand Up @@ -277,9 +277,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
gitmodules_config();
git_config(git_diff_ui_config, NULL);

if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;

init_revisions(&rev, prefix);

/* If this is a no-index diff, just run it and exit there. */
Expand Down
4 changes: 1 addition & 3 deletions builtin/grep.c
Expand Up @@ -325,7 +325,7 @@ static int grep_config(const char *var, const char *value, void *cb)
}

if (!strcmp(var, "color.grep"))
opt->color = git_config_colorbool(var, value, -1);
opt->color = git_config_colorbool(var, value);
else if (!strcmp(var, "color.grep.context"))
color = opt->color_context;
else if (!strcmp(var, "color.grep.filename"))
Expand Down Expand Up @@ -898,8 +898,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
strcpy(opt.color_sep, GIT_COLOR_CYAN);
opt.color = -1;
git_config(grep_config, &opt);
if (opt.color == -1)
opt.color = git_use_color_default;

/*
* If there is no -- then the paths must exist in the working
Expand Down
12 changes: 0 additions & 12 deletions builtin/log.c
Expand Up @@ -359,9 +359,6 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)

git_config(git_log_config, NULL);

if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;

init_revisions(&rev, prefix);
rev.diff = 1;
rev.simplify_history = 0;
Expand Down Expand Up @@ -446,9 +443,6 @@ int cmd_show(int argc, const char **argv, const char *prefix)

git_config(git_log_config, NULL);

if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;

init_pathspec(&match_all, NULL);
init_revisions(&rev, prefix);
rev.diff = 1;
Expand Down Expand Up @@ -524,9 +518,6 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)

git_config(git_log_config, NULL);

if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;

init_revisions(&rev, prefix);
init_reflog_walk(&rev.reflog_info);
rev.verbose_header = 1;
Expand All @@ -549,9 +540,6 @@ int cmd_log(int argc, const char **argv, const char *prefix)

git_config(git_log_config, NULL);

if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;

init_revisions(&rev, prefix);
rev.always_show_header = 1;
memset(&opt, 0, sizeof(opt));
Expand Down
6 changes: 0 additions & 6 deletions builtin/merge.c
Expand Up @@ -390,8 +390,6 @@ static void finish(const unsigned char *new_head, const char *msg)
opts.output_format |=
DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
opts.detect_rename = DIFF_DETECT_RENAME;
if (diff_use_color_default > 0)
DIFF_OPT_SET(&opts, COLOR_DIFF);
if (diff_setup_done(&opts) < 0)
die(_("diff_setup_done failed"));
diff_tree_sha1(head, new_head, "", &opts);
Expand Down Expand Up @@ -1033,10 +1031,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)

git_config(git_merge_config, NULL);

/* for color.ui */
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;

if (branch_mergeoptions)
parse_branch_merge_options(branch_mergeoptions);
argc = parse_options(argc, argv, prefix, builtin_merge_options,
Expand Down
9 changes: 3 additions & 6 deletions builtin/show-branch.c
Expand Up @@ -26,14 +26,14 @@ static const char **default_arg;

static const char *get_color_code(int idx)
{
if (showbranch_use_color)
if (want_color(showbranch_use_color))
return column_colors_ansi[idx % column_colors_ansi_max];
return "";
}

static const char *get_color_reset_code(void)
{
if (showbranch_use_color)
if (want_color(showbranch_use_color))
return GIT_COLOR_RESET;
return "";
}
Expand Down Expand Up @@ -573,7 +573,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
}

if (!strcmp(var, "color.showbranch")) {
showbranch_use_color = git_config_colorbool(var, value, -1);
showbranch_use_color = git_config_colorbool(var, value);
return 0;
}

Expand Down Expand Up @@ -685,9 +685,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)

git_config(git_show_branch_config, NULL);

if (showbranch_use_color == -1)
showbranch_use_color = git_use_color_default;

/* If nothing is specified, try the default first */
if (ac == 1 && default_num) {
ac = default_num;
Expand Down
46 changes: 37 additions & 9 deletions color.c
@@ -1,7 +1,8 @@
#include "cache.h"
#include "color.h"

int git_use_color_default = 0;
static int git_use_color_default = 0;
int color_stdout_is_tty = -1;

/*
* The list of available column colors.
Expand Down Expand Up @@ -157,15 +158,15 @@ void color_parse_mem(const char *value, int value_len, const char *var,
die("bad color value '%.*s' for variable '%s'", value_len, value, var);
}

int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
int git_config_colorbool(const char *var, const char *value)
{
if (value) {
if (!strcasecmp(value, "never"))
return 0;
if (!strcasecmp(value, "always"))
return 1;
if (!strcasecmp(value, "auto"))
goto auto_color;
return GIT_COLOR_AUTO;
}

if (!var)
Expand All @@ -176,24 +177,51 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
return 0;

/* any normal truth value defaults to 'auto' */
auto_color:
if (stdout_is_tty < 0)
stdout_is_tty = isatty(1);
if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
return GIT_COLOR_AUTO;
}

static int check_auto_color(void)
{
if (color_stdout_is_tty < 0)
color_stdout_is_tty = isatty(1);
if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
char *term = getenv("TERM");
if (term && strcmp(term, "dumb"))
return 1;
}
return 0;
}

int git_color_default_config(const char *var, const char *value, void *cb)
int want_color(int var)
{
static int want_auto = -1;

if (var < 0)
var = git_use_color_default;

if (var == GIT_COLOR_AUTO) {
if (want_auto < 0)
want_auto = check_auto_color();
return want_auto;
}
return var;
}

int git_color_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "color.ui")) {
git_use_color_default = git_config_colorbool(var, value, -1);
git_use_color_default = git_config_colorbool(var, value);
return 0;
}

return 0;
}

int git_color_default_config(const char *var, const char *value, void *cb)
{
if (git_color_config(var, value, cb) < 0)
return -1;

return git_default_config(var, value, cb);
}

Expand Down
22 changes: 18 additions & 4 deletions color.h
Expand Up @@ -49,20 +49,34 @@ struct strbuf;
#define GIT_COLOR_NIL "NIL"

/*
* This variable stores the value of color.ui
* The first three are chosen to match common usage in the code, and what is
* returned from git_config_colorbool. The "auto" value can be returned from
* config_colorbool, and will be converted by want_color() into either 0 or 1.
*/
extern int git_use_color_default;
#define GIT_COLOR_UNKNOWN -1
#define GIT_COLOR_NEVER 0
#define GIT_COLOR_ALWAYS 1
#define GIT_COLOR_AUTO 2

/* A default list of colors to use for commit graphs and show-branch output */
extern const char *column_colors_ansi[];
extern const int column_colors_ansi_max;

/*
* Use this instead of git_default_config if you need the value of color.ui.
* Generally the color code will lazily figure this out itself, but
* this provides a mechanism for callers to override autodetection.
*/
extern int color_stdout_is_tty;

/*
* Use the first one if you need only color config; the second is a convenience
* if you are just going to change to git_default_config, too.
*/
int git_color_config(const char *var, const char *value, void *cb);
int git_color_default_config(const char *var, const char *value, void *cb);

int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
int git_config_colorbool(const char *var, const char *value);
int want_color(int var);
void color_parse(const char *value, const char *var, char *dst);
void color_parse_mem(const char *value, int len, const char *var, char *dst);
__attribute__((format (printf, 3, 4)))
Expand Down
7 changes: 3 additions & 4 deletions combine-diff.c
Expand Up @@ -702,9 +702,8 @@ static void show_combined_header(struct combine_diff_path *elem,
int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
const char *c_reset = diff_get_color(use_color, DIFF_RESET);
const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
const char *abb;
int added = 0;
int deleted = 0;
Expand Down Expand Up @@ -964,7 +963,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
show_combined_header(elem, num_parent, dense, rev,
mode_differs, 1);
dump_sline(sline, cnt, num_parent,
DIFF_OPT_TST(opt, COLOR_DIFF), result_deleted);
opt->use_color, result_deleted);
}
free(result);

Expand Down

0 comments on commit f946b46

Please sign in to comment.