Merge branch 'jk/options-cleanup'

Various clean-ups to the command line option parsing.

* jk/options-cleanup:
  apply, ls-files: simplify "-z" parsing
  checkout-index: disallow "--no-stage" option
  checkout-index: handle "--no-index" option
  checkout-index: handle "--no-prefix" option
  checkout-index: simplify "-z" option parsing
  give "nbuf" strbuf a more meaningful name
maint
Junio C Hamano 2016-02-10 14:20:08 -08:00
commit 722c924445
7 changed files with 57 additions and 102 deletions

View File

@ -4464,16 +4464,6 @@ static int option_parse_p(const struct option *opt,
return 0; return 0;
} }


static int option_parse_z(const struct option *opt,
const char *arg, int unset)
{
if (unset)
line_termination = '\n';
else
line_termination = 0;
return 0;
}

static int option_parse_space_change(const struct option *opt, static int option_parse_space_change(const struct option *opt,
const char *arg, int unset) const char *arg, int unset)
{ {
@ -4546,9 +4536,9 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
N_( "attempt three-way merge if a patch does not apply")), N_( "attempt three-way merge if a patch does not apply")),
OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor, OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
N_("build a temporary index based on embedded index information")), N_("build a temporary index based on embedded index information")),
{ OPTION_CALLBACK, 'z', NULL, NULL, NULL, /* Think twice before adding "--nul" synonym to this */
N_("paths are separated with NUL character"), OPT_SET_INT('z', NULL, &line_termination,
PARSE_OPT_NOARG, option_parse_z }, N_("paths are separated with NUL character"), '\0'),
OPT_INTEGER('C', NULL, &p_context, OPT_INTEGER('C', NULL, &p_context,
N_("ensure at least <n> lines of context match")), N_("ensure at least <n> lines of context match")),
{ OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"), { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),

View File

@ -72,24 +72,23 @@ static void check_attr(const char *prefix, int cnt,
static void check_attr_stdin_paths(const char *prefix, int cnt, static void check_attr_stdin_paths(const char *prefix, int cnt,
struct git_attr_check *check) struct git_attr_check *check)
{ {
struct strbuf buf, nbuf; struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;
strbuf_getline_fn getline_fn; strbuf_getline_fn getline_fn;


getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
strbuf_init(&buf, 0);
strbuf_init(&nbuf, 0);
while (getline_fn(&buf, stdin) != EOF) { while (getline_fn(&buf, stdin) != EOF) {
if (!nul_term_line && buf.buf[0] == '"') { if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&nbuf); strbuf_reset(&unquoted);
if (unquote_c_style(&nbuf, buf.buf, NULL)) if (unquote_c_style(&unquoted, buf.buf, NULL))
die("line is badly quoted"); die("line is badly quoted");
strbuf_swap(&buf, &nbuf); strbuf_swap(&buf, &unquoted);
} }
check_attr(prefix, cnt, check, buf.buf); check_attr(prefix, cnt, check, buf.buf);
maybe_flush_or_die(stdout, "attribute to stdout"); maybe_flush_or_die(stdout, "attribute to stdout");
} }
strbuf_release(&buf); strbuf_release(&buf);
strbuf_release(&nbuf); strbuf_release(&unquoted);
} }


static NORETURN void error_with_usage(const char *msg) static NORETURN void error_with_usage(const char *msg)

View File

@ -115,20 +115,19 @@ static int check_ignore(struct dir_struct *dir,


static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix) static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
{ {
struct strbuf buf, nbuf; struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;
char *pathspec[2] = { NULL, NULL }; char *pathspec[2] = { NULL, NULL };
strbuf_getline_fn getline_fn; strbuf_getline_fn getline_fn;
int num_ignored = 0; int num_ignored = 0;


getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
strbuf_init(&buf, 0);
strbuf_init(&nbuf, 0);
while (getline_fn(&buf, stdin) != EOF) { while (getline_fn(&buf, stdin) != EOF) {
if (!nul_term_line && buf.buf[0] == '"') { if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&nbuf); strbuf_reset(&unquoted);
if (unquote_c_style(&nbuf, buf.buf, NULL)) if (unquote_c_style(&unquoted, buf.buf, NULL))
die("line is badly quoted"); die("line is badly quoted");
strbuf_swap(&buf, &nbuf); strbuf_swap(&buf, &unquoted);
} }
pathspec[0] = buf.buf; pathspec[0] = buf.buf;
num_ignored += check_ignore(dir, prefix, num_ignored += check_ignore(dir, prefix,
@ -136,7 +135,7 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
maybe_flush_or_die(stdout, "check-ignore to stdout"); maybe_flush_or_die(stdout, "check-ignore to stdout");
} }
strbuf_release(&buf); strbuf_release(&buf);
strbuf_release(&nbuf); strbuf_release(&unquoted);
return num_ignored; return num_ignored;
} }



View File

@ -130,33 +130,6 @@ static const char * const builtin_checkout_index_usage[] = {


static struct lock_file lock_file; static struct lock_file lock_file;


static int option_parse_u(const struct option *opt,
const char *arg, int unset)
{
int *newfd = opt->value;

state.refresh_cache = 1;
state.istate = &the_index;
if (*newfd < 0)
*newfd = hold_locked_index(&lock_file, 1);
return 0;
}

static int option_parse_z(const struct option *opt,
const char *arg, int unset)
{
nul_term_line = !unset;
return 0;
}

static int option_parse_prefix(const struct option *opt,
const char *arg, int unset)
{
state.base_dir = arg;
state.base_dir_len = strlen(arg);
return 0;
}

static int option_parse_stage(const struct option *opt, static int option_parse_stage(const struct option *opt,
const char *arg, int unset) const char *arg, int unset)
{ {
@ -168,7 +141,7 @@ static int option_parse_stage(const struct option *opt,
if ('1' <= ch && ch <= '3') if ('1' <= ch && ch <= '3')
checkout_stage = arg[0] - '0'; checkout_stage = arg[0] - '0';
else else
die("stage should be between 1 and 3 or all"); die(_("stage should be between 1 and 3 or all"));
} }
return 0; return 0;
} }
@ -181,6 +154,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
int read_from_stdin = 0; int read_from_stdin = 0;
int prefix_length; int prefix_length;
int force = 0, quiet = 0, not_new = 0; int force = 0, quiet = 0, not_new = 0;
int index_opt = 0;
struct option builtin_checkout_index_options[] = { struct option builtin_checkout_index_options[] = {
OPT_BOOL('a', "all", &all, OPT_BOOL('a', "all", &all,
N_("check out all files in the index")), N_("check out all files in the index")),
@ -189,22 +163,19 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
N_("no warning for existing files and files not in index")), N_("no warning for existing files and files not in index")),
OPT_BOOL('n', "no-create", &not_new, OPT_BOOL('n', "no-create", &not_new,
N_("don't checkout new files")), N_("don't checkout new files")),
{ OPTION_CALLBACK, 'u', "index", &newfd, NULL, OPT_BOOL('u', "index", &index_opt,
N_("update stat information in the index file"), N_("update stat information in the index file")),
PARSE_OPT_NOARG, option_parse_u }, OPT_BOOL('z', NULL, &nul_term_line,
{ OPTION_CALLBACK, 'z', NULL, NULL, NULL, N_("paths are separated with NUL character")),
N_("paths are separated with NUL character"),
PARSE_OPT_NOARG, option_parse_z },
OPT_BOOL(0, "stdin", &read_from_stdin, OPT_BOOL(0, "stdin", &read_from_stdin,
N_("read list of paths from the standard input")), N_("read list of paths from the standard input")),
OPT_BOOL(0, "temp", &to_tempfile, OPT_BOOL(0, "temp", &to_tempfile,
N_("write the content to temporary files")), N_("write the content to temporary files")),
OPT_CALLBACK(0, "prefix", NULL, N_("string"), OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
N_("when creating files, prepend <string>"), N_("when creating files, prepend <string>")),
option_parse_prefix), { OPTION_CALLBACK, 0, "stage", NULL, "1-3|all",
OPT_CALLBACK(0, "stage", NULL, NULL,
N_("copy out the files from named stage"), N_("copy out the files from named stage"),
option_parse_stage), PARSE_OPT_NONEG, option_parse_stage },
OPT_END() OPT_END()
}; };


@ -212,7 +183,6 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
usage_with_options(builtin_checkout_index_usage, usage_with_options(builtin_checkout_index_usage,
builtin_checkout_index_options); builtin_checkout_index_options);
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
state.base_dir = "";
prefix_length = prefix ? strlen(prefix) : 0; prefix_length = prefix ? strlen(prefix) : 0;


if (read_cache() < 0) { if (read_cache() < 0) {
@ -225,15 +195,17 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
state.quiet = quiet; state.quiet = quiet;
state.not_new = not_new; state.not_new = not_new;


if (state.base_dir_len || to_tempfile) { if (!state.base_dir)
/* when --prefix is specified we do not state.base_dir = "";
* want to update cache. state.base_dir_len = strlen(state.base_dir);
*/
if (state.refresh_cache) { /*
rollback_lock_file(&lock_file); * when --prefix is specified we do not want to update cache.
newfd = -1; */
} if (index_opt && !state.base_dir_len && !to_tempfile) {
state.refresh_cache = 0; state.refresh_cache = 1;
state.istate = &the_index;
newfd = hold_locked_index(&lock_file, 1);
} }


/* Check out named files first */ /* Check out named files first */
@ -251,7 +223,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
} }


if (read_from_stdin) { if (read_from_stdin) {
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;
strbuf_getline_fn getline_fn; strbuf_getline_fn getline_fn;


if (all) if (all)
@ -261,16 +234,16 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
while (getline_fn(&buf, stdin) != EOF) { while (getline_fn(&buf, stdin) != EOF) {
char *p; char *p;
if (!nul_term_line && buf.buf[0] == '"') { if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&nbuf); strbuf_reset(&unquoted);
if (unquote_c_style(&nbuf, buf.buf, NULL)) if (unquote_c_style(&unquoted, buf.buf, NULL))
die("line is badly quoted"); die("line is badly quoted");
strbuf_swap(&buf, &nbuf); strbuf_swap(&buf, &unquoted);
} }
p = prefix_path(prefix, prefix_length, buf.buf); p = prefix_path(prefix, prefix_length, buf.buf);
checkout_file(p, prefix); checkout_file(p, prefix);
free(p); free(p);
} }
strbuf_release(&nbuf); strbuf_release(&unquoted);
strbuf_release(&buf); strbuf_release(&buf);
} }



View File

@ -58,20 +58,21 @@ static void hash_object(const char *path, const char *type, const char *vpath,
static void hash_stdin_paths(const char *type, int no_filters, unsigned flags, static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
int literally) int literally)
{ {
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;


while (strbuf_getline(&buf, stdin) != EOF) { while (strbuf_getline(&buf, stdin) != EOF) {
if (buf.buf[0] == '"') { if (buf.buf[0] == '"') {
strbuf_reset(&nbuf); strbuf_reset(&unquoted);
if (unquote_c_style(&nbuf, buf.buf, NULL)) if (unquote_c_style(&unquoted, buf.buf, NULL))
die("line is badly quoted"); die("line is badly quoted");
strbuf_swap(&buf, &nbuf); strbuf_swap(&buf, &unquoted);
} }
hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags, hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
literally); literally);
} }
strbuf_release(&buf); strbuf_release(&buf);
strbuf_release(&nbuf); strbuf_release(&unquoted);
} }


int cmd_hash_object(int argc, const char **argv, const char *prefix) int cmd_hash_object(int argc, const char **argv, const char *prefix)

View File

@ -379,14 +379,6 @@ static const char * const ls_files_usage[] = {
NULL NULL
}; };


static int option_parse_z(const struct option *opt,
const char *arg, int unset)
{
line_terminator = unset ? '\n' : '\0';

return 0;
}

static int option_parse_exclude(const struct option *opt, static int option_parse_exclude(const struct option *opt,
const char *arg, int unset) const char *arg, int unset)
{ {
@ -428,9 +420,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
struct exclude_list *el; struct exclude_list *el;
struct string_list exclude_list = STRING_LIST_INIT_NODUP; struct string_list exclude_list = STRING_LIST_INIT_NODUP;
struct option builtin_ls_files_options[] = { struct option builtin_ls_files_options[] = {
{ OPTION_CALLBACK, 'z', NULL, NULL, NULL, /* Think twice before adding "--nul" synonym to this */
N_("paths are separated with NUL character"), OPT_SET_INT('z', NULL, &line_terminator,
PARSE_OPT_NOARG, option_parse_z }, N_("paths are separated with NUL character"), '\0'),
OPT_BOOL('t', NULL, &show_tag, OPT_BOOL('t', NULL, &show_tag,
N_("identify the file status with tags")), N_("identify the file status with tags")),
OPT_BOOL('v', NULL, &show_valid_bit, OPT_BOOL('v', NULL, &show_valid_bit,

View File

@ -1086,16 +1086,17 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
} }


if (read_from_stdin) { if (read_from_stdin) {
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;


setup_work_tree(); setup_work_tree();
while (getline_fn(&buf, stdin) != EOF) { while (getline_fn(&buf, stdin) != EOF) {
char *p; char *p;
if (!nul_term_line && buf.buf[0] == '"') { if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&nbuf); strbuf_reset(&unquoted);
if (unquote_c_style(&nbuf, buf.buf, NULL)) if (unquote_c_style(&unquoted, buf.buf, NULL))
die("line is badly quoted"); die("line is badly quoted");
strbuf_swap(&buf, &nbuf); strbuf_swap(&buf, &unquoted);
} }
p = prefix_path(prefix, prefix_length, buf.buf); p = prefix_path(prefix, prefix_length, buf.buf);
update_one(p); update_one(p);
@ -1103,7 +1104,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
chmod_path(set_executable_bit, p); chmod_path(set_executable_bit, p);
free(p); free(p);
} }
strbuf_release(&nbuf); strbuf_release(&unquoted);
strbuf_release(&buf); strbuf_release(&buf);
} }