Skip to content

Commit 8b8e862

Browse files
peffgitster
authored andcommitted
ignore unknown color configuration
When parsing the config file, if there is a value that is syntactically correct but unused, we generally ignore it. This lets non-core porcelains store arbitrary information in the config file, and it means that configuration files can be shared between new and old versions of git (the old versions might simply ignore certain configuration). The one exception to this is color configuration; if we encounter a color.{diff,branch,status}.$slot variable, we die if it is not one of the recognized slots (presumably as a safety valve for user misconfiguration). This behavior has existed since 801235c (diff --color: use $GIT_DIR/config, 2006-06-24), but hasn't yet caused a problem. No porcelain has wanted to store extra colors, and we once a color area (like color.diff) has been introduced, we've never changed the set of color slots. However, that changed recently with the addition of color.diff.func. Now a user with color.diff.func in their config can no longer freely switch between v1.6.6 and older versions; the old versions will complain about the existence of the variable. This patch loosens the check to match the rest of git-config; unknown color slots are simply ignored. This doesn't fix this particular problem, as the older version (without this patch) is the problem, but it at least prevents it from happening again in the future. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 06500a0 commit 8b8e862

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

builtin-branch.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
6565
return BRANCH_COLOR_LOCAL;
6666
if (!strcasecmp(var+ofs, "current"))
6767
return BRANCH_COLOR_CURRENT;
68-
die("bad config variable '%s'", var);
68+
return -1;
6969
}
7070

7171
static int git_branch_config(const char *var, const char *value, void *cb)
@@ -76,6 +76,8 @@ static int git_branch_config(const char *var, const char *value, void *cb)
7676
}
7777
if (!prefixcmp(var, "color.branch.")) {
7878
int slot = parse_branch_color_slot(var, 13);
79+
if (slot < 0)
80+
return 0;
7981
if (!value)
8082
return config_error_nonbool(var);
8183
color_parse(value, var, branch_colors[slot]);

builtin-commit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ static int parse_status_slot(const char *var, int offset)
841841
return WT_STATUS_NOBRANCH;
842842
if (!strcasecmp(var+offset, "unmerged"))
843843
return WT_STATUS_UNMERGED;
844-
die("bad config variable '%s'", var);
844+
return -1;
845845
}
846846

847847
static int git_status_config(const char *k, const char *v, void *cb)
@@ -861,6 +861,8 @@ static int git_status_config(const char *k, const char *v, void *cb)
861861
}
862862
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
863863
int slot = parse_status_slot(k, 13);
864+
if (slot < 0)
865+
return 0;
864866
if (!v)
865867
return config_error_nonbool(k);
866868
color_parse(v, k, s->color_palette[slot]);

diff.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int parse_diff_color_slot(const char *var, int ofs)
5959
return DIFF_COMMIT;
6060
if (!strcasecmp(var+ofs, "whitespace"))
6161
return DIFF_WHITESPACE;
62-
die("bad config variable '%s'", var);
62+
return -1;
6363
}
6464

6565
static int git_config_rename(const char *var, const char *value)
@@ -118,6 +118,8 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
118118

119119
if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
120120
int slot = parse_diff_color_slot(var, 11);
121+
if (slot < 0)
122+
return 0;
121123
if (!value)
122124
return config_error_nonbool(var);
123125
color_parse(value, var, diff_colors[slot]);

t/t4026-color.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,21 @@ test_expect_success 'extra character after attribute' '
6666
invalid_color "dimX"
6767
'
6868

69+
test_expect_success 'unknown color slots are ignored (diff)' '
70+
git config --unset diff.color.new
71+
git config color.diff.nosuchslotwilleverbedefined white &&
72+
git diff --color
73+
'
74+
75+
test_expect_success 'unknown color slots are ignored (branch)' '
76+
git config color.branch.nosuchslotwilleverbedefined white &&
77+
git branch -a
78+
'
79+
80+
test_expect_success 'unknown color slots are ignored (status)' '
81+
git config color.status.nosuchslotwilleverbedefined white || exit
82+
git status
83+
case $? in 0|1) : ok ;; *) false ;; esac
84+
'
85+
6986
test_done

0 commit comments

Comments
 (0)