Skip to content

Commit 18648e8

Browse files
martinvonzgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d94c5e2 commit 18648e8

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

builtin/reset.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,26 +153,15 @@ static void update_index_from_diff(struct diff_queue_struct *q,
153153
}
154154
}
155155

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)
169158
{
170159
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
171160
int index_fd;
172161
struct diff_options opt;
173162

174163
memset(&opt, 0, sizeof(opt));
175-
diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
164+
diff_tree_setup_paths(pathspec, &opt);
176165
opt.output_format = DIFF_FORMAT_CALLBACK;
177166
opt.format_callback = update_index_from_diff;
178167

@@ -216,6 +205,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
216205
const char *rev = "HEAD";
217206
unsigned char sha1[20], *orig = NULL, sha1_orig[20],
218207
*old_orig = NULL, sha1_old_orig[20];
208+
const char **pathspec = NULL;
219209
struct commit *commit;
220210
struct strbuf msg = STRBUF_INIT;
221211
const struct option options[] = {
@@ -287,22 +277,25 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
287277
die(_("Could not parse object '%s'."), rev);
288278
hashcpy(sha1, commit->object.sha1);
289279

280+
if (i < argc)
281+
pathspec = get_pathspec(prefix, argv + i);
282+
290283
if (patch_mode) {
291284
if (reset_type != NONE)
292285
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);
294287
}
295288

296289
/* git reset tree [--] paths... can be used to
297290
* load chosen paths from the tree into the index without
298291
* affecting the working tree nor HEAD. */
299-
if (i < argc) {
292+
if (pathspec) {
300293
if (reset_type == MIXED)
301294
warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
302295
else if (reset_type != NONE)
303296
die(_("Cannot do %s reset with paths."),
304297
_(reset_type_names[reset_type]));
305-
return read_from_tree(prefix, argv + i, sha1,
298+
return read_from_tree(pathspec, sha1,
306299
quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
307300
}
308301
if (reset_type == NONE)

0 commit comments

Comments
 (0)