Merge branch 'ab/retire-option-argument'

An oddball OPTION_ARGUMENT feature has been removed from the
parse-options API.

* ab/retire-option-argument:
  parse-options API: remove OPTION_ARGUMENT feature
  difftool: use run_command() API in run_file_diff()
  difftool: prepare "diff" cmdline in cmd_difftool()
  difftool: prepare "struct child_process" in cmd_difftool()
maint
Junio C Hamano 2021-09-23 13:44:48 -07:00
commit 0e35107e7d
6 changed files with 26 additions and 52 deletions

View File

@ -198,11 +198,6 @@ There are some macros to easily define options:
The filename will be prefixed by passing the filename along with The filename will be prefixed by passing the filename along with
the prefix argument of `parse_options()` to `prefix_filename()`. the prefix argument of `parse_options()` to `prefix_filename()`.


`OPT_ARGUMENT(long, &int_var, description)`::
Introduce a long-option argument that will be kept in `argv[]`.
If this option was seen, `int_var` will be set to one (except
if a `NULL` pointer was passed).

`OPT_NUMBER_CALLBACK(&var, description, func_ptr)`:: `OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::
Recognize numerical options like -123 and feed the integer as Recognize numerical options like -123 and feed the integer as
if it was an argument to the function given by `func_ptr`. if it was an argument to the function given by `func_ptr`.

View File

@ -331,7 +331,7 @@ static int checkout_path(unsigned mode, struct object_id *oid,
} }


static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix, static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
int argc, const char **argv) struct child_process *child)
{ {
char tmpdir[PATH_MAX]; char tmpdir[PATH_MAX];
struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT; struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
@ -352,7 +352,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
struct index_state wtindex; struct index_state wtindex;
struct checkout lstate, rstate; struct checkout lstate, rstate;
int rc, flags = RUN_GIT_CMD, err = 0; int rc, flags = RUN_GIT_CMD, err = 0;
struct child_process child = CHILD_PROCESS_INIT;
const char *helper_argv[] = { "difftool--helper", NULL, NULL, NULL }; const char *helper_argv[] = { "difftool--helper", NULL, NULL, NULL };
struct hashmap wt_modified, tmp_modified; struct hashmap wt_modified, tmp_modified;
int indices_loaded = 0; int indices_loaded = 0;
@ -387,19 +386,15 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
rdir_len = rdir.len; rdir_len = rdir.len;
wtdir_len = wtdir.len; wtdir_len = wtdir.len;


child.no_stdin = 1; child->no_stdin = 1;
child.git_cmd = 1; child->git_cmd = 1;
child.use_shell = 0; child->use_shell = 0;
child.clean_on_exit = 1; child->clean_on_exit = 1;
child.dir = prefix; child->dir = prefix;
child.out = -1; child->out = -1;
strvec_pushl(&child.args, "diff", "--raw", "--no-abbrev", "-z", if (start_command(child))
NULL);
for (i = 0; i < argc; i++)
strvec_push(&child.args, argv[i]);
if (start_command(&child))
die("could not obtain raw diff"); die("could not obtain raw diff");
fp = xfdopen(child.out, "r"); fp = xfdopen(child->out, "r");


/* Build index info for left and right sides of the diff */ /* Build index info for left and right sides of the diff */
i = 0; i = 0;
@ -525,7 +520,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,


fclose(fp); fclose(fp);
fp = NULL; fp = NULL;
if (finish_command(&child)) { if (finish_command(child)) {
ret = error("error occurred running diff --raw"); ret = error("error occurred running diff --raw");
goto finish; goto finish;
} }
@ -668,25 +663,23 @@ finish:
} }


static int run_file_diff(int prompt, const char *prefix, static int run_file_diff(int prompt, const char *prefix,
int argc, const char **argv) struct child_process *child)
{ {
struct strvec args = STRVEC_INIT;
const char *env[] = { const char *env[] = {
"GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL, "GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
NULL NULL
}; };
int i;


if (prompt > 0) if (prompt > 0)
env[2] = "GIT_DIFFTOOL_PROMPT=true"; env[2] = "GIT_DIFFTOOL_PROMPT=true";
else if (!prompt) else if (!prompt)
env[2] = "GIT_DIFFTOOL_NO_PROMPT=true"; env[2] = "GIT_DIFFTOOL_NO_PROMPT=true";


child->git_cmd = 1;
child->dir = prefix;
strvec_pushv(&child->env_array, env);


strvec_push(&args, "diff"); return run_command(child);
for (i = 0; i < argc; i++)
strvec_push(&args, argv[i]);
return run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);
} }


int cmd_difftool(int argc, const char **argv, const char *prefix) int cmd_difftool(int argc, const char **argv, const char *prefix)
@ -716,9 +709,10 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
"tool returns a non - zero exit code")), "tool returns a non - zero exit code")),
OPT_STRING('x', "extcmd", &extcmd, N_("command"), OPT_STRING('x', "extcmd", &extcmd, N_("command"),
N_("specify a custom command for viewing diffs")), N_("specify a custom command for viewing diffs")),
OPT_ARGUMENT("no-index", &no_index, N_("passed to `diff`")), OPT_BOOL(0, "no-index", &no_index, N_("passed to `diff`")),
OPT_END() OPT_END()
}; };
struct child_process child = CHILD_PROCESS_INIT;


git_config(difftool_config, NULL); git_config(difftool_config, NULL);
symlinks = has_symlinks; symlinks = has_symlinks;
@ -768,7 +762,14 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
* will invoke a separate instance of 'git-difftool--helper' for * will invoke a separate instance of 'git-difftool--helper' for
* each file that changed. * each file that changed.
*/ */
strvec_push(&child.args, "diff");
if (no_index)
strvec_push(&child.args, "--no-index");
if (dir_diff) if (dir_diff)
return run_dir_diff(extcmd, symlinks, prefix, argc, argv); strvec_pushl(&child.args, "--raw", "--no-abbrev", "-z", NULL);
return run_file_diff(prompt, prefix, argc, argv); strvec_pushv(&child.args, argv);

if (dir_diff)
return run_dir_diff(extcmd, symlinks, prefix, &child);
return run_file_diff(prompt, prefix, &child);
} }

View File

@ -310,19 +310,6 @@ static enum parse_opt_result parse_long_opt(
again: again:
if (!skip_prefix(arg, long_name, &rest)) if (!skip_prefix(arg, long_name, &rest))
rest = NULL; rest = NULL;
if (options->type == OPTION_ARGUMENT) {
if (!rest)
continue;
if (*rest == '=')
return error(_("%s takes no value"),
optname(options, flags));
if (*rest)
continue;
if (options->value)
*(int *)options->value = options->defval;
p->out[p->cpidx++] = arg - 2;
return PARSE_OPT_DONE;
}
if (!rest) { if (!rest) {
/* abbreviated? */ /* abbreviated? */
if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) && if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&

View File

@ -8,7 +8,6 @@
enum parse_opt_type { enum parse_opt_type {
/* special types */ /* special types */
OPTION_END, OPTION_END,
OPTION_ARGUMENT,
OPTION_GROUP, OPTION_GROUP,
OPTION_NUMBER, OPTION_NUMBER,
OPTION_ALIAS, OPTION_ALIAS,
@ -155,8 +154,6 @@ struct option {
#define OPT_INTEGER_F(s, l, v, h, f) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h), (f) } #define OPT_INTEGER_F(s, l, v, h, f) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h), (f) }


#define OPT_END() { OPTION_END } #define OPT_END() { OPTION_END }
#define OPT_ARGUMENT(l, v, h) { OPTION_ARGUMENT, 0, (l), (v), NULL, \
(h), PARSE_OPT_NOARG, NULL, 1 }
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) } #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0) #define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
#define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \ #define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \

View File

@ -134,7 +134,6 @@ int cmd__parse_options(int argc, const char **argv)
OPT_NOOP_NOARG(0, "obsolete"), OPT_NOOP_NOARG(0, "obsolete"),
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"), OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
OPT_GROUP("Magic arguments"), OPT_GROUP("Magic arguments"),
OPT_ARGUMENT("quux", NULL, "means --quux"),
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM", OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
number_callback), number_callback),
{ OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b", { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",

View File

@ -37,7 +37,6 @@ String options
--list <str> add str to list --list <str> add str to list


Magic arguments Magic arguments
--quux means --quux
-NUM set integer to NUM -NUM set integer to NUM
+ same as -b + same as -b
--ambiguous positive ambiguity --ambiguous positive ambiguity
@ -263,10 +262,6 @@ test_expect_success 'detect possible typos' '
test_cmp typo.err output.err test_cmp typo.err output.err
' '


test_expect_success 'keep some options as arguments' '
test-tool parse-options --expect="arg 00: --quux" --quux
'

cat >expect <<\EOF cat >expect <<\EOF
Callback: "four", 0 Callback: "four", 0
boolean: 5 boolean: 5