Skip to content

Commit 722c924

Browse files
committed
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
2 parents 24abb31 + 1f3c79a commit 722c924

File tree

7 files changed

+57
-102
lines changed

7 files changed

+57
-102
lines changed

builtin/apply.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4464,16 +4464,6 @@ static int option_parse_p(const struct option *opt,
44644464
return 0;
44654465
}
44664466

4467-
static int option_parse_z(const struct option *opt,
4468-
const char *arg, int unset)
4469-
{
4470-
if (unset)
4471-
line_termination = '\n';
4472-
else
4473-
line_termination = 0;
4474-
return 0;
4475-
}
4476-
44774467
static int option_parse_space_change(const struct option *opt,
44784468
const char *arg, int unset)
44794469
{
@@ -4546,9 +4536,9 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
45464536
N_( "attempt three-way merge if a patch does not apply")),
45474537
OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
45484538
N_("build a temporary index based on embedded index information")),
4549-
{ OPTION_CALLBACK, 'z', NULL, NULL, NULL,
4550-
N_("paths are separated with NUL character"),
4551-
PARSE_OPT_NOARG, option_parse_z },
4539+
/* Think twice before adding "--nul" synonym to this */
4540+
OPT_SET_INT('z', NULL, &line_termination,
4541+
N_("paths are separated with NUL character"), '\0'),
45524542
OPT_INTEGER('C', NULL, &p_context,
45534543
N_("ensure at least <n> lines of context match")),
45544544
{ OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),

builtin/check-attr.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,23 @@ static void check_attr(const char *prefix, int cnt,
7272
static void check_attr_stdin_paths(const char *prefix, int cnt,
7373
struct git_attr_check *check)
7474
{
75-
struct strbuf buf, nbuf;
75+
struct strbuf buf = STRBUF_INIT;
76+
struct strbuf unquoted = STRBUF_INIT;
7677
strbuf_getline_fn getline_fn;
7778

7879
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
79-
strbuf_init(&buf, 0);
80-
strbuf_init(&nbuf, 0);
8180
while (getline_fn(&buf, stdin) != EOF) {
8281
if (!nul_term_line && buf.buf[0] == '"') {
83-
strbuf_reset(&nbuf);
84-
if (unquote_c_style(&nbuf, buf.buf, NULL))
82+
strbuf_reset(&unquoted);
83+
if (unquote_c_style(&unquoted, buf.buf, NULL))
8584
die("line is badly quoted");
86-
strbuf_swap(&buf, &nbuf);
85+
strbuf_swap(&buf, &unquoted);
8786
}
8887
check_attr(prefix, cnt, check, buf.buf);
8988
maybe_flush_or_die(stdout, "attribute to stdout");
9089
}
9190
strbuf_release(&buf);
92-
strbuf_release(&nbuf);
91+
strbuf_release(&unquoted);
9392
}
9493

9594
static NORETURN void error_with_usage(const char *msg)

builtin/check-ignore.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,27 @@ static int check_ignore(struct dir_struct *dir,
115115

116116
static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
117117
{
118-
struct strbuf buf, nbuf;
118+
struct strbuf buf = STRBUF_INIT;
119+
struct strbuf unquoted = STRBUF_INIT;
119120
char *pathspec[2] = { NULL, NULL };
120121
strbuf_getline_fn getline_fn;
121122
int num_ignored = 0;
122123

123124
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
124-
strbuf_init(&buf, 0);
125-
strbuf_init(&nbuf, 0);
126125
while (getline_fn(&buf, stdin) != EOF) {
127126
if (!nul_term_line && buf.buf[0] == '"') {
128-
strbuf_reset(&nbuf);
129-
if (unquote_c_style(&nbuf, buf.buf, NULL))
127+
strbuf_reset(&unquoted);
128+
if (unquote_c_style(&unquoted, buf.buf, NULL))
130129
die("line is badly quoted");
131-
strbuf_swap(&buf, &nbuf);
130+
strbuf_swap(&buf, &unquoted);
132131
}
133132
pathspec[0] = buf.buf;
134133
num_ignored += check_ignore(dir, prefix,
135134
1, (const char **)pathspec);
136135
maybe_flush_or_die(stdout, "check-ignore to stdout");
137136
}
138137
strbuf_release(&buf);
139-
strbuf_release(&nbuf);
138+
strbuf_release(&unquoted);
140139
return num_ignored;
141140
}
142141

builtin/checkout-index.c

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,6 @@ static const char * const builtin_checkout_index_usage[] = {
130130

131131
static struct lock_file lock_file;
132132

133-
static int option_parse_u(const struct option *opt,
134-
const char *arg, int unset)
135-
{
136-
int *newfd = opt->value;
137-
138-
state.refresh_cache = 1;
139-
state.istate = &the_index;
140-
if (*newfd < 0)
141-
*newfd = hold_locked_index(&lock_file, 1);
142-
return 0;
143-
}
144-
145-
static int option_parse_z(const struct option *opt,
146-
const char *arg, int unset)
147-
{
148-
nul_term_line = !unset;
149-
return 0;
150-
}
151-
152-
static int option_parse_prefix(const struct option *opt,
153-
const char *arg, int unset)
154-
{
155-
state.base_dir = arg;
156-
state.base_dir_len = strlen(arg);
157-
return 0;
158-
}
159-
160133
static int option_parse_stage(const struct option *opt,
161134
const char *arg, int unset)
162135
{
@@ -168,7 +141,7 @@ static int option_parse_stage(const struct option *opt,
168141
if ('1' <= ch && ch <= '3')
169142
checkout_stage = arg[0] - '0';
170143
else
171-
die("stage should be between 1 and 3 or all");
144+
die(_("stage should be between 1 and 3 or all"));
172145
}
173146
return 0;
174147
}
@@ -181,6 +154,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
181154
int read_from_stdin = 0;
182155
int prefix_length;
183156
int force = 0, quiet = 0, not_new = 0;
157+
int index_opt = 0;
184158
struct option builtin_checkout_index_options[] = {
185159
OPT_BOOL('a', "all", &all,
186160
N_("check out all files in the index")),
@@ -189,30 +163,26 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
189163
N_("no warning for existing files and files not in index")),
190164
OPT_BOOL('n', "no-create", &not_new,
191165
N_("don't checkout new files")),
192-
{ OPTION_CALLBACK, 'u', "index", &newfd, NULL,
193-
N_("update stat information in the index file"),
194-
PARSE_OPT_NOARG, option_parse_u },
195-
{ OPTION_CALLBACK, 'z', NULL, NULL, NULL,
196-
N_("paths are separated with NUL character"),
197-
PARSE_OPT_NOARG, option_parse_z },
166+
OPT_BOOL('u', "index", &index_opt,
167+
N_("update stat information in the index file")),
168+
OPT_BOOL('z', NULL, &nul_term_line,
169+
N_("paths are separated with NUL character")),
198170
OPT_BOOL(0, "stdin", &read_from_stdin,
199171
N_("read list of paths from the standard input")),
200172
OPT_BOOL(0, "temp", &to_tempfile,
201173
N_("write the content to temporary files")),
202-
OPT_CALLBACK(0, "prefix", NULL, N_("string"),
203-
N_("when creating files, prepend <string>"),
204-
option_parse_prefix),
205-
OPT_CALLBACK(0, "stage", NULL, NULL,
174+
OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
175+
N_("when creating files, prepend <string>")),
176+
{ OPTION_CALLBACK, 0, "stage", NULL, "1-3|all",
206177
N_("copy out the files from named stage"),
207-
option_parse_stage),
178+
PARSE_OPT_NONEG, option_parse_stage },
208179
OPT_END()
209180
};
210181

211182
if (argc == 2 && !strcmp(argv[1], "-h"))
212183
usage_with_options(builtin_checkout_index_usage,
213184
builtin_checkout_index_options);
214185
git_config(git_default_config, NULL);
215-
state.base_dir = "";
216186
prefix_length = prefix ? strlen(prefix) : 0;
217187

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

228-
if (state.base_dir_len || to_tempfile) {
229-
/* when --prefix is specified we do not
230-
* want to update cache.
231-
*/
232-
if (state.refresh_cache) {
233-
rollback_lock_file(&lock_file);
234-
newfd = -1;
235-
}
236-
state.refresh_cache = 0;
198+
if (!state.base_dir)
199+
state.base_dir = "";
200+
state.base_dir_len = strlen(state.base_dir);
201+
202+
/*
203+
* when --prefix is specified we do not want to update cache.
204+
*/
205+
if (index_opt && !state.base_dir_len && !to_tempfile) {
206+
state.refresh_cache = 1;
207+
state.istate = &the_index;
208+
newfd = hold_locked_index(&lock_file, 1);
237209
}
238210

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

253225
if (read_from_stdin) {
254-
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
226+
struct strbuf buf = STRBUF_INIT;
227+
struct strbuf unquoted = STRBUF_INIT;
255228
strbuf_getline_fn getline_fn;
256229

257230
if (all)
@@ -261,16 +234,16 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
261234
while (getline_fn(&buf, stdin) != EOF) {
262235
char *p;
263236
if (!nul_term_line && buf.buf[0] == '"') {
264-
strbuf_reset(&nbuf);
265-
if (unquote_c_style(&nbuf, buf.buf, NULL))
237+
strbuf_reset(&unquoted);
238+
if (unquote_c_style(&unquoted, buf.buf, NULL))
266239
die("line is badly quoted");
267-
strbuf_swap(&buf, &nbuf);
240+
strbuf_swap(&buf, &unquoted);
268241
}
269242
p = prefix_path(prefix, prefix_length, buf.buf);
270243
checkout_file(p, prefix);
271244
free(p);
272245
}
273-
strbuf_release(&nbuf);
246+
strbuf_release(&unquoted);
274247
strbuf_release(&buf);
275248
}
276249

builtin/hash-object.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ static void hash_object(const char *path, const char *type, const char *vpath,
5858
static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
5959
int literally)
6060
{
61-
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
61+
struct strbuf buf = STRBUF_INIT;
62+
struct strbuf unquoted = STRBUF_INIT;
6263

6364
while (strbuf_getline(&buf, stdin) != EOF) {
6465
if (buf.buf[0] == '"') {
65-
strbuf_reset(&nbuf);
66-
if (unquote_c_style(&nbuf, buf.buf, NULL))
66+
strbuf_reset(&unquoted);
67+
if (unquote_c_style(&unquoted, buf.buf, NULL))
6768
die("line is badly quoted");
68-
strbuf_swap(&buf, &nbuf);
69+
strbuf_swap(&buf, &unquoted);
6970
}
7071
hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
7172
literally);
7273
}
7374
strbuf_release(&buf);
74-
strbuf_release(&nbuf);
75+
strbuf_release(&unquoted);
7576
}
7677

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

builtin/ls-files.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,6 @@ static const char * const ls_files_usage[] = {
379379
NULL
380380
};
381381

382-
static int option_parse_z(const struct option *opt,
383-
const char *arg, int unset)
384-
{
385-
line_terminator = unset ? '\n' : '\0';
386-
387-
return 0;
388-
}
389-
390382
static int option_parse_exclude(const struct option *opt,
391383
const char *arg, int unset)
392384
{
@@ -428,9 +420,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
428420
struct exclude_list *el;
429421
struct string_list exclude_list = STRING_LIST_INIT_NODUP;
430422
struct option builtin_ls_files_options[] = {
431-
{ OPTION_CALLBACK, 'z', NULL, NULL, NULL,
432-
N_("paths are separated with NUL character"),
433-
PARSE_OPT_NOARG, option_parse_z },
423+
/* Think twice before adding "--nul" synonym to this */
424+
OPT_SET_INT('z', NULL, &line_terminator,
425+
N_("paths are separated with NUL character"), '\0'),
434426
OPT_BOOL('t', NULL, &show_tag,
435427
N_("identify the file status with tags")),
436428
OPT_BOOL('v', NULL, &show_valid_bit,

builtin/update-index.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,24 +1086,25 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10861086
}
10871087

10881088
if (read_from_stdin) {
1089-
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
1089+
struct strbuf buf = STRBUF_INIT;
1090+
struct strbuf unquoted = STRBUF_INIT;
10901091

10911092
setup_work_tree();
10921093
while (getline_fn(&buf, stdin) != EOF) {
10931094
char *p;
10941095
if (!nul_term_line && buf.buf[0] == '"') {
1095-
strbuf_reset(&nbuf);
1096-
if (unquote_c_style(&nbuf, buf.buf, NULL))
1096+
strbuf_reset(&unquoted);
1097+
if (unquote_c_style(&unquoted, buf.buf, NULL))
10971098
die("line is badly quoted");
1098-
strbuf_swap(&buf, &nbuf);
1099+
strbuf_swap(&buf, &unquoted);
10991100
}
11001101
p = prefix_path(prefix, prefix_length, buf.buf);
11011102
update_one(p);
11021103
if (set_executable_bit)
11031104
chmod_path(set_executable_bit, p);
11041105
free(p);
11051106
}
1106-
strbuf_release(&nbuf);
1107+
strbuf_release(&unquoted);
11071108
strbuf_release(&buf);
11081109
}
11091110

0 commit comments

Comments
 (0)