status/commit: make sure --porcelain is not affected by user-facing config

The recent addition of status.branch started affecting what is shown when "git status --porcelain" is run by mistake. Identify the configuration items that should be ignored under "--porcelain" option, introduce a "deferred config" mechanism to keep the values read from the configuration, and decide what value to use only after we read both from configuration and command line. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 24, 2013 at 11:41 UTC 84b4202d804c7faec76f3eab22744b6288c63481
3 files changed +61 -22
builtin/commit.c
+55 -22
@@ -111,12 +111,14 @@ static int show_ignored_in_status;
111 static const char *only_include_assumed;
112 static struct strbuf message = STRBUF_INIT;
113
114 -static enum {
114 +static enum status_format {
115 STATUS_FORMAT_NONE = 0,
116 STATUS_FORMAT_LONG,
117 STATUS_FORMAT_SHORT,
118 - STATUS_FORMAT_PORCELAIN
119 -} status_format;
118 + STATUS_FORMAT_PORCELAIN,
119 +
120 + STATUS_FORMAT_UNSPECIFIED
121 +} status_format = STATUS_FORMAT_UNSPECIFIED;
122
123 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
124 {
@@ -457,6 +459,9 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
459 case STATUS_FORMAT_PORCELAIN:
460 wt_porcelain_print(s);
461 break;
462 + case STATUS_FORMAT_UNSPECIFIED:
463 + die("BUG: finalize_deferred_config() should have been called");
464 + break;
465 case STATUS_FORMAT_NONE:
466 case STATUS_FORMAT_LONG:
467 wt_status_print(s);
@@ -958,6 +963,42 @@ static const char *read_commit_message(const char *name)
963 return logmsg_reencode(commit, NULL, out_enc);
964 }
965
966 +/*
967 + * Enumerate what needs to be propagated when --porcelain
968 + * is not in effect here.
969 + */
970 +static struct status_deferred_config {
971 + enum status_format status_format;
972 + int show_branch;
973 +} status_deferred_config = {
974 + STATUS_FORMAT_UNSPECIFIED,
975 + -1 /* unspecified */
976 +};
977 +
978 +static void finalize_deferred_config(struct wt_status *s)
979 +{
980 + int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
981 + !s->null_termination);
982 +
983 + if (s->null_termination) {
984 + if (status_format == STATUS_FORMAT_NONE ||
985 + status_format == STATUS_FORMAT_UNSPECIFIED)
986 + status_format = STATUS_FORMAT_PORCELAIN;
987 + else if (status_format == STATUS_FORMAT_LONG)
988 + die(_("--long and -z are incompatible"));
989 + }
990 +
991 + if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
992 + status_format = status_deferred_config.status_format;
993 + if (status_format == STATUS_FORMAT_UNSPECIFIED)
994 + status_format = STATUS_FORMAT_NONE;
995 +
996 + if (use_deferred_config && s->show_branch < 0)
997 + s->show_branch = status_deferred_config.show_branch;
998 + if (s->show_branch < 0)
999 + s->show_branch = 0;
1000 +}
1001 +
1002 static int parse_and_validate_options(int argc, const char *argv[],
1003 const struct option *options,
1004 const char * const usage[],
@@ -968,6 +1009,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
1009 int f = 0;
1010
1011 argc = parse_options(argc, argv, prefix, options, usage, 0);
1012 + finalize_deferred_config(s);
1013
1014 if (force_author && !strchr(force_author, '>'))
1015 force_author = find_author_by_nickname(force_author);
@@ -1052,12 +1094,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
1094 if (all && argc > 0)
1095 die(_("Paths with -a does not make sense."));
1096
1055 - if (s->null_termination) {
1056 - if (status_format == STATUS_FORMAT_NONE)
1057 - status_format = STATUS_FORMAT_PORCELAIN;
1058 - else if (status_format == STATUS_FORMAT_LONG)
1059 - die(_("--long and -z are incompatible"));
1060 - }
1097 if (status_format != STATUS_FORMAT_NONE)
1098 dry_run = 1;
1099
@@ -1112,13 +1148,13 @@ static int git_status_config(const char *k, const char *v, void *cb)
1148 }
1149 if (!strcmp(k, "status.short")) {
1150 if (git_config_bool(k, v))
1115 - status_format = STATUS_FORMAT_SHORT;
1151 + status_deferred_config.status_format = STATUS_FORMAT_SHORT;
1152 else
1117 - status_format = STATUS_FORMAT_NONE;
1153 + status_deferred_config.status_format = STATUS_FORMAT_NONE;
1154 return 0;
1155 }
1156 if (!strcmp(k, "status.branch")) {
1121 - s->show_branch = git_config_bool(k, v);
1157 + status_deferred_config.show_branch = git_config_bool(k, v);
1158 return 0;
1159 }
1160 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
@@ -1163,8 +1199,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1199 OPT__VERBOSE(&verbose, N_("be verbose")),
1200 OPT_SET_INT('s', "short", &status_format,
1201 N_("show status concisely"), STATUS_FORMAT_SHORT),
1166 - OPT_BOOLEAN('b', "branch", &s.show_branch,
1167 - N_("show branch information")),
1202 + OPT_BOOL('b', "branch", &s.show_branch,
1203 + N_("show branch information")),
1204 OPT_SET_INT(0, "porcelain", &status_format,
1205 N_("machine-readable output"),
1206 STATUS_FORMAT_PORCELAIN),
@@ -1197,13 +1233,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1233 builtin_status_options,
1234 builtin_status_usage, 0);
1235 finalize_colopts(&s.colopts, -1);
1200 -
1201 - if (s.null_termination) {
1202 - if (status_format == STATUS_FORMAT_NONE)
1203 - status_format = STATUS_FORMAT_PORCELAIN;
1204 - else if (status_format == STATUS_FORMAT_LONG)
1205 - die(_("--long and -z are incompatible"));
1206 - }
1236 + finalize_deferred_config(&s);
1237
1238 handle_untracked_files_arg(&s);
1239 if (show_ignored_in_status)
@@ -1232,6 +1262,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1262 case STATUS_FORMAT_PORCELAIN:
1263 wt_porcelain_print(&s);
1264 break;
1265 + case STATUS_FORMAT_UNSPECIFIED:
1266 + die("BUG: finalize_deferred_config() should have been called");
1267 + break;
1268 case STATUS_FORMAT_NONE:
1269 case STATUS_FORMAT_LONG:
1270 s.verbose = verbose;
@@ -1400,7 +1433,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1433 OPT_BOOLEAN(0, "dry-run", &dry_run, N_("show what would be committed")),
1434 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
1435 STATUS_FORMAT_SHORT),
1403 - OPT_BOOLEAN(0, "branch", &s.show_branch, N_("show branch information")),
1436 + OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1437 OPT_SET_INT(0, "porcelain", &status_format,
1438 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
1439 OPT_SET_INT(0, "long", &status_format,
t/t7508-status.sh
+5
@@ -1378,6 +1378,11 @@ test_expect_success '"status.branch=true" weaker than "--no-branch"' '
1378 test_cmp expected_nobranch actual
1379 '
1380
1381 +test_expect_success '"status.branch=true" weaker than "--porcelain"' '
1382 + git -c status.branch=true status --porcelain >actual &&
1383 + test_cmp expected_nobranch actual
1384 +'
1385 +
1386 test_expect_success '"status.branch=false" same as "--no-branch"' '
1387 git -c status.branch=false status -s >actual &&
1388 test_cmp expected_nobranch actual
wt-status.c
+1
@@ -127,6 +127,7 @@ void wt_status_prepare(struct wt_status *s)
127 s->change.strdup_strings = 1;
128 s->untracked.strdup_strings = 1;
129 s->ignored.strdup_strings = 1;
130 + s->show_branch = -1; /* unspecified */
131 }
132
133 static void wt_status_print_unmerged_header(struct wt_status *s)