Skip to content

Commit 39ea722

Browse files
martinvonzgitster
authored andcommitted
reset.c: extract function for parsing arguments
Declutter cmd_reset() a bit by moving out the argument parsing to its own function. Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4f4ad3d commit 39ea722

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

builtin/reset.c

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -198,36 +198,11 @@ static void die_if_unmerged_cache(int reset_type)
198198

199199
}
200200

201-
int cmd_reset(int argc, const char **argv, const char *prefix)
201+
static const char **parse_args(int argc, const char **argv, const char *prefix, const char **rev_ret)
202202
{
203-
int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
204-
int patch_mode = 0;
203+
int i = 0;
205204
const char *rev = "HEAD";
206-
unsigned char sha1[20], *orig = NULL, sha1_orig[20],
207-
*old_orig = NULL, sha1_old_orig[20];
208-
const char **pathspec = NULL;
209-
struct commit *commit;
210-
struct strbuf msg = STRBUF_INIT;
211-
const struct option options[] = {
212-
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
213-
OPT_SET_INT(0, "mixed", &reset_type,
214-
N_("reset HEAD and index"), MIXED),
215-
OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
216-
OPT_SET_INT(0, "hard", &reset_type,
217-
N_("reset HEAD, index and working tree"), HARD),
218-
OPT_SET_INT(0, "merge", &reset_type,
219-
N_("reset HEAD, index and working tree"), MERGE),
220-
OPT_SET_INT(0, "keep", &reset_type,
221-
N_("reset HEAD but keep local changes"), KEEP),
222-
OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")),
223-
OPT_END()
224-
};
225-
226-
git_config(git_default_config, NULL);
227-
228-
argc = parse_options(argc, argv, prefix, options, git_reset_usage,
229-
PARSE_OPT_KEEP_DASHDASH);
230-
205+
unsigned char unused[20];
231206
/*
232207
* Possible arguments are:
233208
*
@@ -250,7 +225,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
250225
* Otherwise, argv[i] could be either <rev> or <paths> and
251226
* has to be unambiguous.
252227
*/
253-
else if (!get_sha1_committish(argv[i], sha1)) {
228+
else if (!get_sha1_committish(argv[i], unused)) {
254229
/*
255230
* Ok, argv[i] looks like a rev; it should not
256231
* be a filename.
@@ -262,6 +237,40 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
262237
verify_filename(prefix, argv[i], 1);
263238
}
264239
}
240+
*rev_ret = rev;
241+
return i < argc ? get_pathspec(prefix, argv + i) : NULL;
242+
}
243+
244+
int cmd_reset(int argc, const char **argv, const char *prefix)
245+
{
246+
int reset_type = NONE, update_ref_status = 0, quiet = 0;
247+
int patch_mode = 0;
248+
const char *rev;
249+
unsigned char sha1[20], *orig = NULL, sha1_orig[20],
250+
*old_orig = NULL, sha1_old_orig[20];
251+
const char **pathspec = NULL;
252+
struct commit *commit;
253+
struct strbuf msg = STRBUF_INIT;
254+
const struct option options[] = {
255+
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
256+
OPT_SET_INT(0, "mixed", &reset_type,
257+
N_("reset HEAD and index"), MIXED),
258+
OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
259+
OPT_SET_INT(0, "hard", &reset_type,
260+
N_("reset HEAD, index and working tree"), HARD),
261+
OPT_SET_INT(0, "merge", &reset_type,
262+
N_("reset HEAD, index and working tree"), MERGE),
263+
OPT_SET_INT(0, "keep", &reset_type,
264+
N_("reset HEAD but keep local changes"), KEEP),
265+
OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")),
266+
OPT_END()
267+
};
268+
269+
git_config(git_default_config, NULL);
270+
271+
argc = parse_options(argc, argv, prefix, options, git_reset_usage,
272+
PARSE_OPT_KEEP_DASHDASH);
273+
pathspec = parse_args(argc, argv, prefix, &rev);
265274

266275
if (get_sha1_committish(rev, sha1))
267276
die(_("Failed to resolve '%s' as a valid ref."), rev);
@@ -277,9 +286,6 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
277286
die(_("Could not parse object '%s'."), rev);
278287
hashcpy(sha1, commit->object.sha1);
279288

280-
if (i < argc)
281-
pathspec = get_pathspec(prefix, argv + i);
282-
283289
if (patch_mode) {
284290
if (reset_type != NONE)
285291
die(_("--patch is incompatible with --{hard,mixed,soft}"));

0 commit comments

Comments
 (0)