reset.c: pass pathspec around instead of (prefix, argv) pair

We use the path arguments in two places in reset.c: in interactive_reset() and read_from_tree(). Both of these call get_pathspec(), so we pass the (prefix, argv) pair to both functions. Move the call to get_pathspec() out of these methods, for two reasons: 1) One argument is simpler than two. 2) It lets us use the (arguably clearer) "if (pathspec)" in place of "if (i < argc)". 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 18648e89e78662e3a6a9a38adfca3e6001cfaf85
1 file changed +10 -17
builtin/reset.c
+10 -17
@@ -153,26 +153,15 @@ static void update_index_from_diff(struct diff_queue_struct *q,
153 }
154 }
155
156 -static int interactive_reset(const char *revision, const char **argv,
157 - const char *prefix)
158 -{
159 - const char **pathspec = NULL;
160 -
161 - if (*argv)
162 - pathspec = get_pathspec(prefix, argv);
163 -
164 - return run_add_interactive(revision, "--patch=reset", pathspec);
165 -}
166 -
167 -static int read_from_tree(const char *prefix, const char **argv,
168 - unsigned char *tree_sha1, int refresh_flags)
156 +static int read_from_tree(const char **pathspec, unsigned char *tree_sha1,
157 + int refresh_flags)
158 {
159 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
160 int index_fd;
161 struct diff_options opt;
162
163 memset(&opt, 0, sizeof(opt));
175 - diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
164 + diff_tree_setup_paths(pathspec, &opt);
165 opt.output_format = DIFF_FORMAT_CALLBACK;
166 opt.format_callback = update_index_from_diff;
167
@@ -216,6 +205,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
205 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[] = {
@@ -287,22 +277,25 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
277 die(_("Could not parse object '%s'."), rev);
278 hashcpy(sha1, commit->object.sha1);
279
280 + if (i < argc)
281 + pathspec = get_pathspec(prefix, argv + i);
282 +
283 if (patch_mode) {
284 if (reset_type != NONE)
285 die(_("--patch is incompatible with --{hard,mixed,soft}"));
293 - return interactive_reset(rev, argv + i, prefix);
286 + return run_add_interactive(rev, "--patch=reset", pathspec);
287 }
288
289 /* git reset tree [--] paths... can be used to
290 * load chosen paths from the tree into the index without
291 * affecting the working tree nor HEAD. */
299 - if (i < argc) {
292 + if (pathspec) {
293 if (reset_type == MIXED)
294 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
295 else if (reset_type != NONE)
296 die(_("Cannot do %s reset with paths."),
297 _(reset_type_names[reset_type]));
305 - return read_from_tree(prefix, argv + i, sha1,
298 + return read_from_tree(pathspec, sha1,
299 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
300 }
301 if (reset_type == NONE)