reset: convert to use parse_pathspec

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2013-07-14 15:35:47 +07:00 коммит произвёл Junio C Hamano
Родитель 5a76aff1a6
Коммит f8144c9fcf
1 изменённых файлов: 16 добавлений и 10 удалений

Просмотреть файл

@ -174,7 +174,10 @@ static void die_if_unmerged_cache(int reset_type)
} }
static const char **parse_args(const char **argv, const char *prefix, const char **rev_ret) static void parse_args(struct pathspec *pathspec,
const char **argv, const char *prefix,
int patch_mode,
const char **rev_ret)
{ {
const char *rev = "HEAD"; const char *rev = "HEAD";
unsigned char unused[20]; unsigned char unused[20];
@ -216,7 +219,10 @@ static const char **parse_args(const char **argv, const char *prefix, const char
} }
} }
*rev_ret = rev; *rev_ret = rev;
return argv[0] ? get_pathspec(prefix, argv) : NULL; parse_pathspec(pathspec,
patch_mode ? PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP : 0,
PATHSPEC_PREFER_FULL,
prefix, argv);
} }
static int update_refs(const char *rev, const unsigned char *sha1) static int update_refs(const char *rev, const unsigned char *sha1)
@ -246,7 +252,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
int patch_mode = 0, unborn; int patch_mode = 0, unborn;
const char *rev; const char *rev;
unsigned char sha1[20]; unsigned char sha1[20];
const char **pathspec = NULL; struct pathspec pathspec;
const struct option options[] = { const struct option options[] = {
OPT__QUIET(&quiet, N_("be quiet, only report errors")), OPT__QUIET(&quiet, N_("be quiet, only report errors")),
OPT_SET_INT(0, "mixed", &reset_type, OPT_SET_INT(0, "mixed", &reset_type,
@ -266,13 +272,13 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options, git_reset_usage, argc = parse_options(argc, argv, prefix, options, git_reset_usage,
PARSE_OPT_KEEP_DASHDASH); PARSE_OPT_KEEP_DASHDASH);
pathspec = parse_args(argv, prefix, &rev); parse_args(&pathspec, argv, prefix, patch_mode, &rev);
unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1); unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1);
if (unborn) { if (unborn) {
/* reset on unborn branch: treat as reset to empty tree */ /* reset on unborn branch: treat as reset to empty tree */
hashcpy(sha1, EMPTY_TREE_SHA1_BIN); hashcpy(sha1, EMPTY_TREE_SHA1_BIN);
} else if (!pathspec) { } else if (!pathspec.nr) {
struct commit *commit; struct commit *commit;
if (get_sha1_committish(rev, sha1)) if (get_sha1_committish(rev, sha1))
die(_("Failed to resolve '%s' as a valid revision."), rev); die(_("Failed to resolve '%s' as a valid revision."), rev);
@ -293,13 +299,13 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
if (patch_mode) { if (patch_mode) {
if (reset_type != NONE) if (reset_type != NONE)
die(_("--patch is incompatible with --{hard,mixed,soft}")); die(_("--patch is incompatible with --{hard,mixed,soft}"));
return run_add_interactive(sha1_to_hex(sha1), "--patch=reset", pathspec); return run_add_interactive(sha1_to_hex(sha1), "--patch=reset", pathspec.raw);
} }
/* git reset tree [--] paths... can be used to /* git reset tree [--] paths... can be used to
* load chosen paths from the tree into the index without * load chosen paths from the tree into the index without
* affecting the working tree nor HEAD. */ * affecting the working tree nor HEAD. */
if (pathspec) { if (pathspec.nr) {
if (reset_type == MIXED) if (reset_type == MIXED)
warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead.")); warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
else if (reset_type != NONE) else if (reset_type != NONE)
@ -326,7 +332,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
int newfd = hold_locked_index(lock, 1); int newfd = hold_locked_index(lock, 1);
if (reset_type == MIXED) { if (reset_type == MIXED) {
if (read_from_tree(pathspec, sha1)) if (read_from_tree(pathspec.raw, sha1))
return 1; return 1;
} else { } else {
int err = reset_index(sha1, reset_type, quiet); int err = reset_index(sha1, reset_type, quiet);
@ -347,7 +353,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
die(_("Could not write new index file.")); die(_("Could not write new index file."));
} }
if (!pathspec && !unborn) { if (!pathspec.nr && !unborn) {
/* Any resets without paths update HEAD to the head being /* Any resets without paths update HEAD to the head being
* switched to, saving the previous head in ORIG_HEAD before. */ * switched to, saving the previous head in ORIG_HEAD before. */
update_ref_status = update_refs(rev, sha1); update_ref_status = update_refs(rev, sha1);
@ -355,7 +361,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
if (reset_type == HARD && !update_ref_status && !quiet) if (reset_type == HARD && !update_ref_status && !quiet)
print_new_head_line(lookup_commit_reference(sha1)); print_new_head_line(lookup_commit_reference(sha1));
} }
if (!pathspec) if (!pathspec.nr)
remove_branch_state(); remove_branch_state();
return update_ref_status; return update_ref_status;