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 <martinvonz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin von Zweigbergk committed
Jan 14, 2013 at 21:47 UTC
39ea722d829ddc5bb253fbe12333d768965f4fe7
1 file changed
+38
-32
builtin/reset.c
+38
-32
@@ -198,36 +198,11 @@ static void die_if_unmerged_cache(int reset_type)
198
199
}
200
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)
202
{
203
- int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
204
- int patch_mode = 0;
203
+ int i = 0;
204
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];
206
/*
207
* Possible arguments are:
208
*
@@ -250,7 +225,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
225
* Otherwise, argv[i] could be either <rev> or <paths> and
226
* has to be unambiguous.
227
*/
253
- else if (!get_sha1_committish(argv[i], sha1)) {
228
+ else if (!get_sha1_committish(argv[i], unused)) {
229
/*
230
* Ok, argv[i] looks like a rev; it should not
231
* be a filename.
@@ -262,6 +237,40 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
237
verify_filename(prefix, argv[i], 1);
238
}
239
}
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);
274
275
if (get_sha1_committish(rev, sha1))
276
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)
286
die(_("Could not parse object '%s'."), rev);
287
hashcpy(sha1, commit->object.sha1);
288
280
- if (i < argc)
281
- pathspec = get_pathspec(prefix, argv + i);
282
-
289
if (patch_mode) {
290
if (reset_type != NONE)
291
die(_("--patch is incompatible with --{hard,mixed,soft}"));