Skip to content

Commit 6ee53d5

Browse files
committed
Issue #7 is now Issue git#12
1 parent b0b47f2 commit 6ee53d5

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

builtin/commit.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,20 @@ int run_commit_hook(int editor_is_used, const char *index_file, const char *name
15751575
return ret;
15761576
}
15771577

1578+
int parse_opt_bool(const struct option *opt, const char *arg, int unset)
1579+
{
1580+
int value;
1581+
1582+
if (!arg)
1583+
arg = unset ? "never" : (const char *)opt->defval;
1584+
value = git_config_colorbool(NULL, arg);
1585+
if (value < 0)
1586+
return opterror(opt,
1587+
"expects \"always\", \"auto\", or \"never\"", 0);
1588+
*(int *)opt->value = value;
1589+
return 0;
1590+
}
1591+
15781592
int cmd_commit(int argc, const char **argv, const char *prefix)
15791593
{
15801594
static struct wt_status s;
@@ -1596,7 +1610,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15961610
OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
15971611
OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
15981612
OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")),
1599-
OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
1613+
OPT_BOOL_C(0, "status", &include_status, N_("include status in commit message template"),
1614+
"commit.status", parse_opt_bool),
16001615
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
16011616
N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
16021617
/* end commit message options */

parse-options.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,19 @@ static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *optio
235235
}
236236

237237
static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
238-
const struct option *options)
238+
struct option *options)
239239
{
240240
const struct option *all_opts = options;
241241
const char *arg_end = strchrnul(arg, '=');
242242
const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
243243
int abbrev_flags = 0, ambiguous_flags = 0;
244+
int ret;
244245

245246
for (; options->type != OPTION_END; options++) {
246247
const char *rest, *long_name = options->long_name;
247248
int flags = 0, opt_flags = 0;
248249

250+
fprintf(stderr, "golong name = %s\n", long_name);
249251
if (!long_name)
250252
continue;
251253

@@ -313,7 +315,11 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
313315
continue;
314316
p->opt = rest + 1;
315317
}
316-
return get_value(p, options, all_opts, flags ^ opt_flags);
318+
319+
if (!(ret = get_value(p, options, all_opts, flags ^ opt_flags))) {
320+
options->flags |= PARSE_OPT_VIA_CLI;
321+
return ret;
322+
}
317323
}
318324

319325
if (ambiguous_option)
@@ -429,7 +435,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *,
429435
const struct option *, int, int);
430436

431437
int parse_options_step(struct parse_opt_ctx_t *ctx,
432-
const struct option *options,
438+
struct option *options,
433439
const char * const usagestr[])
434440
{
435441
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
@@ -514,6 +520,27 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
514520
ctx->out[ctx->cpidx++] = ctx->argv[0];
515521
ctx->opt = NULL;
516522
}
523+
524+
/* The loop above is driven by the argument vector, so we need
525+
* to make a second pass and find those options that are
526+
* configurable, and haven't been set via the command-line */
527+
for (; options->type != OPTION_END; options++) {
528+
if (!(options->flags & PARSE_OPT_CONFIGURABLE))
529+
continue;
530+
531+
if (options->flags & PARSE_OPT_VIA_CLI)
532+
continue;
533+
534+
/* TODO: Factor the handling of OPTION_CALLBACK in
535+
* get_value() into a function, do we also need to
536+
* save away the state from the loop above to handle
537+
* unset? I think not, I think we're always unset here
538+
* by definition, right? */
539+
fprintf(stderr, "long name = %s (via cli?: %d)\n", options->long_name, (options->flags & PARSE_OPT_VIA_CLI ? 1 : 0));
540+
541+
return (*options->conf_callback)(opt, NULL, 1) ? (-1) : 0;
542+
}
543+
517544
return PARSE_OPT_DONE;
518545

519546
show_usage_error:
@@ -530,7 +557,7 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
530557
}
531558

532559
int parse_options(int argc, const char **argv, const char *prefix,
533-
const struct option *options, const char * const usagestr[],
560+
struct option *options, const char * const usagestr[],
534561
int flags)
535562
{
536563
struct parse_opt_ctx_t ctx;

parse-options.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ enum parse_opt_option_flags {
3838
PARSE_OPT_LASTARG_DEFAULT = 16,
3939
PARSE_OPT_NODASH = 32,
4040
PARSE_OPT_LITERAL_ARGHELP = 64,
41-
PARSE_OPT_SHELL_EVAL = 256
41+
PARSE_OPT_SHELL_EVAL = 256,
42+
PARSE_OPT_CONFIGURABLE = 512,
43+
PARSE_OPT_VIA_CLI = 1024
4244
};
4345

4446
struct option;
@@ -111,7 +113,6 @@ struct option {
111113
parse_opt_cb *callback;
112114
intptr_t defval;
113115

114-
int conf_enabled;
115116
const char *conf_key;
116117
parse_opt_cb *conf_callback;
117118
};
@@ -128,7 +129,11 @@ struct option {
128129
(h), PARSE_OPT_NOARG }
129130
#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
130131
(h), PARSE_OPT_NOARG, NULL, (i) }
132+
#define OPT_SET_INT_C(s, l, v, h, i, ck, cb) \
133+
{ OPTION_SET_INT, (s), (l), (v), NULL, \
134+
(h), PARSE_OPT_NOARG | PARSE_OPT_CONFIGURABLE, NULL, (i), ck, cb }
131135
#define OPT_BOOL(s, l, v, h) OPT_SET_INT(s, l, v, h, 1)
136+
#define OPT_BOOL_C(s, l, v, h, ck, cb) OPT_SET_INT_C(s, l, v, h, 1, ck, cb)
132137
#define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
133138
(h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
134139
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \
@@ -170,7 +175,7 @@ struct option {
170175
* Returns the number of arguments left in argv[].
171176
*/
172177
extern int parse_options(int argc, const char **argv, const char *prefix,
173-
const struct option *options,
178+
struct option *options,
174179
const char * const usagestr[], int flags);
175180

176181
extern NORETURN void usage_with_options(const char * const *usagestr,
@@ -214,7 +219,7 @@ extern void parse_options_start(struct parse_opt_ctx_t *ctx,
214219
const struct option *options, int flags);
215220

216221
extern int parse_options_step(struct parse_opt_ctx_t *ctx,
217-
const struct option *options,
222+
struct option *options,
218223
const char * const usagestr[]);
219224

220225
extern int parse_options_end(struct parse_opt_ctx_t *ctx);

0 commit comments

Comments
 (0)