rev-list: disable object/refname ambiguity check with --stdin
[gitweb.git] / builtin / checkout.c
index 6721de2ac867fe8b566ef0c6815fc870efb6ec98..54f80bd38a7413390ed2093a626b129a1a3e8e7f 100644 (file)
@@ -83,12 +83,9 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
        return 0;
 }
 
-static int read_tree_some(struct tree *tree, const char **pathspec)
+static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)
 {
-       struct pathspec ps;
-       init_pathspec(&ps, pathspec);
-       read_tree_recursive(tree, "", 0, 0, &ps, update_some, NULL);
-       free_pathspec(&ps);
+       read_tree_recursive(tree, "", 0, 0, pathspec, update_some, NULL);
 
        /* update the index with the given tree's info
         * for all args, expanding wildcards, and exit
@@ -97,7 +94,7 @@ static int read_tree_some(struct tree *tree, const char **pathspec)
        return 0;
 }
 
-static int skip_same_name(struct cache_entry *ce, int pos)
+static int skip_same_name(const struct cache_entry *ce, int pos)
 {
        while (++pos < active_nr &&
               !strcmp(active_cache[pos]->name, ce->name))
@@ -105,7 +102,7 @@ static int skip_same_name(struct cache_entry *ce, int pos)
        return pos;
 }
 
-static int check_stage(int stage, struct cache_entry *ce, int pos)
+static int check_stage(int stage, const struct cache_entry *ce, int pos)
 {
        while (pos < active_nr &&
               !strcmp(active_cache[pos]->name, ce->name)) {
@@ -119,7 +116,7 @@ static int check_stage(int stage, struct cache_entry *ce, int pos)
                return error(_("path '%s' does not have their version"), ce->name);
 }
 
-static int check_stages(unsigned stages, struct cache_entry *ce, int pos)
+static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
 {
        unsigned seen = 0;
        const char *name = ce->name;
@@ -228,8 +225,6 @@ static int checkout_paths(const struct checkout_opts *opts,
        int flag;
        struct commit *head;
        int errs = 0;
-       int stage = opts->writeout_stage;
-       int merge = opts->merge;
        int newfd;
        struct lock_file *lock_file;
 
@@ -257,16 +252,16 @@ static int checkout_paths(const struct checkout_opts *opts,
 
        if (opts->patch_mode)
                return run_add_interactive(revision, "--patch=checkout",
-                                          opts->pathspec.raw);
+                                          &opts->pathspec);
 
        lock_file = xcalloc(1, sizeof(struct lock_file));
 
        newfd = hold_locked_index(lock_file, 1);
-       if (read_cache_preload(opts->pathspec.raw) < 0)
+       if (read_cache_preload(&opts->pathspec) < 0)
                return error(_("corrupt index file"));
 
        if (opts->source_tree)
-               read_tree_some(opts->source_tree, opts->pathspec.raw);
+               read_tree_some(opts->source_tree, &opts->pathspec);
 
        ps_matched = xcalloc(1, opts->pathspec.nr);
 
@@ -307,7 +302,7 @@ static int checkout_paths(const struct checkout_opts *opts,
                        ce->ce_flags |= CE_MATCHED;
        }
 
-       if (report_path_error(ps_matched, opts->pathspec.raw, opts->prefix)) {
+       if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {
                free(ps_matched);
                return 1;
        }
@@ -319,14 +314,14 @@ static int checkout_paths(const struct checkout_opts *opts,
 
        /* Any unmerged paths? */
        for (pos = 0; pos < active_nr; pos++) {
-               struct cache_entry *ce = active_cache[pos];
+               const struct cache_entry *ce = active_cache[pos];
                if (ce->ce_flags & CE_MATCHED) {
                        if (!ce_stage(ce))
                                continue;
                        if (opts->force) {
                                warning(_("path '%s' is unmerged"), ce->name);
-                       } else if (stage) {
-                               errs |= check_stage(stage, ce, pos);
+                       } else if (opts->writeout_stage) {
+                               errs |= check_stage(opts->writeout_stage, ce, pos);
                        } else if (opts->merge) {
                                errs |= check_stages((1<<2) | (1<<3), ce, pos);
                        } else {
@@ -350,9 +345,9 @@ static int checkout_paths(const struct checkout_opts *opts,
                                errs |= checkout_entry(ce, &state, NULL);
                                continue;
                        }
-                       if (stage)
-                               errs |= checkout_stage(stage, ce, pos, &state);
-                       else if (merge)
+                       if (opts->writeout_stage)
+                               errs |= checkout_stage(opts->writeout_stage, ce, pos, &state);
+                       else if (opts->merge)
                                errs |= checkout_merged(pos, &state);
                        pos = skip_same_name(ce, pos) - 1;
                }
@@ -878,7 +873,9 @@ static int parse_branchname_arg(int argc, const char **argv,
        int argcount = 0;
        unsigned char branch_rev[20];
        const char *arg;
-       int has_dash_dash;
+       int dash_dash_pos;
+       int has_dash_dash = 0;
+       int i;
 
        /*
         * case 1: git checkout <ref> -- [<paths>]
@@ -890,20 +887,30 @@ static int parse_branchname_arg(int argc, const char **argv,
         *
         *   everything after the '--' must be paths.
         *
-        * case 3: git checkout <something> [<paths>]
+        * case 3: git checkout <something> [--]
+        *
+        *   (a) If <something> is a commit, that is to
+        *       switch to the branch or detach HEAD at it.  As a special case,
+        *       if <something> is A...B (missing A or B means HEAD but you can
+        *       omit at most one side), and if there is a unique merge base
+        *       between A and B, A...B names that merge base.
+        *
+        *   (b) If <something> is _not_ a commit, either "--" is present
+        *       or <something> is not a path, no -t nor -b was given, and
+        *       and there is a tracking branch whose name is <something>
+        *       in one and only one remote, then this is a short-hand to
+        *       fork local <something> from that remote-tracking branch.
         *
-        *   With no paths, if <something> is a commit, that is to
-        *   switch to the branch or detach HEAD at it.  As a special case,
-        *   if <something> is A...B (missing A or B means HEAD but you can
-        *   omit at most one side), and if there is a unique merge base
-        *   between A and B, A...B names that merge base.
+        *   (c) Otherwise, if "--" is present, treat it like case (1).
         *
-        *   With no paths, if <something> is _not_ a commit, no -t nor -b
-        *   was given, and there is a tracking branch whose name is
-        *   <something> in one and only one remote, then this is a short-hand
-        *   to fork local <something> from that remote-tracking branch.
+        *   (d) Otherwise :
+        *       - if it's a reference, treat it like case (1)
+        *       - else if it's a path, treat it like case (2)
+        *       - else: fail.
         *
-        *   Otherwise <something> shall not be ambiguous.
+        * case 4: git checkout <something> <paths>
+        *
+        *   The first argument must not be ambiguous.
         *   - If it's *only* a reference, treat it like case (1).
         *   - If it's only a path, treat it like case (2).
         *   - else: fail.
@@ -912,28 +919,59 @@ static int parse_branchname_arg(int argc, const char **argv,
        if (!argc)
                return 0;
 
-       if (!strcmp(argv[0], "--"))     /* case (2) */
-               return 1;
-
        arg = argv[0];
-       has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");
+       dash_dash_pos = -1;
+       for (i = 0; i < argc; i++) {
+               if (!strcmp(argv[i], "--")) {
+                       dash_dash_pos = i;
+                       break;
+               }
+       }
+       if (dash_dash_pos == 0)
+               return 1; /* case (2) */
+       else if (dash_dash_pos == 1)
+               has_dash_dash = 1; /* case (3) or (1) */
+       else if (dash_dash_pos >= 2)
+               die(_("only one reference expected, %d given."), dash_dash_pos);
 
        if (!strcmp(arg, "-"))
                arg = "@{-1}";
 
        if (get_sha1_mb(arg, rev)) {
-               if (has_dash_dash)          /* case (1) */
-                       die(_("invalid reference: %s"), arg);
-               if (dwim_new_local_branch_ok &&
-                   !check_filename(NULL, arg) &&
-                   argc == 1) {
+               /*
+                * Either case (3) or (4), with <something> not being
+                * a commit, or an attempt to use case (1) with an
+                * invalid ref.
+                *
+                * It's likely an error, but we need to find out if
+                * we should auto-create the branch, case (3).(b).
+                */
+               int recover_with_dwim = dwim_new_local_branch_ok;
+
+               if (check_filename(NULL, arg) && !has_dash_dash)
+                       recover_with_dwim = 0;
+               /*
+                * Accept "git checkout foo" and "git checkout foo --"
+                * as candidates for dwim.
+                */
+               if (!(argc == 1 && !has_dash_dash) &&
+                   !(argc == 2 && has_dash_dash))
+                       recover_with_dwim = 0;
+
+               if (recover_with_dwim) {
                        const char *remote = unique_tracking_name(arg, rev);
-                       if (!remote)
-                               return argcount;
-                       *new_branch = arg;
-                       arg = remote;
-                       /* DWIMmed to create local branch */
-               } else {
+                       if (remote) {
+                               *new_branch = arg;
+                               arg = remote;
+                               /* DWIMmed to create local branch, case (3).(b) */
+                       } else {
+                               recover_with_dwim = 0;
+                       }
+               }
+
+               if (!recover_with_dwim) {
+                       if (has_dash_dash)
+                               die(_("invalid reference: %s"), arg);
                        return argcount;
                }
        }
@@ -963,7 +1001,7 @@ static int parse_branchname_arg(int argc, const char **argv,
 
        if (!*source_tree)                   /* case (1): want a tree */
                die(_("reference is not a tree: %s"), arg);
-       if (!has_dash_dash) {/* case (3 -> 1) */
+       if (!has_dash_dash) {/* case (3).(d) -> (1) */
                /*
                 * Do not complain the most common case
                 *      git checkout branch
@@ -1054,8 +1092,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                           N_("create and checkout a new branch")),
                OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
                           N_("create/reset and checkout a branch")),
-               OPT_BOOLEAN('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
-               OPT_BOOLEAN(0, "detach", &opts.force_detach, N_("detach the HEAD at named commit")),
+               OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
+               OPT_BOOL(0, "detach", &opts.force_detach, N_("detach the HEAD at named commit")),
                OPT_SET_INT('t', "track",  &opts.track, N_("set upstream info for new branch"),
                        BRANCH_TRACK_EXPLICIT),
                OPT_STRING(0, "orphan", &opts.new_orphan_branch, N_("new branch"), N_("new unparented branch")),
@@ -1064,16 +1102,15 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                OPT_SET_INT('3', "theirs", &opts.writeout_stage, N_("checkout their version for unmerged files"),
                            3),
                OPT__FORCE(&opts.force, N_("force checkout (throw away local modifications)")),
-               OPT_BOOLEAN('m', "merge", &opts.merge, N_("perform a 3-way merge with the new branch")),
-               OPT_BOOLEAN(0, "overwrite-ignore", &opts.overwrite_ignore, N_("update ignored files (default)")),
+               OPT_BOOL('m', "merge", &opts.merge, N_("perform a 3-way merge with the new branch")),
+               OPT_BOOL(0, "overwrite-ignore", &opts.overwrite_ignore, N_("update ignored files (default)")),
                OPT_STRING(0, "conflict", &conflict_style, N_("style"),
                           N_("conflict style (merge or diff3)")),
-               OPT_BOOLEAN('p', "patch", &opts.patch_mode, N_("select hunks interactively")),
+               OPT_BOOL('p', "patch", &opts.patch_mode, N_("select hunks interactively")),
                OPT_BOOL(0, "ignore-skip-worktree-bits", &opts.ignore_skipworktree,
                         N_("do not limit pathspecs to sparse entries only")),
-               { OPTION_BOOLEAN, 0, "guess", &dwim_new_local_branch, NULL,
-                 N_("second guess 'git checkout no-such-branch'"),
-                 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
+               OPT_HIDDEN_BOOL(0, "guess", &dwim_new_local_branch,
+                               N_("second guess 'git checkout no-such-branch'")),
                OPT_END(),
        };
 
@@ -1152,17 +1189,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        }
 
        if (argc) {
-               /*
-                * In patch mode (opts.patch_mode != 0), we pass the
-                * pathspec to an external program, git-add--interactive.
-                * Do not accept any kind of magic that that program
-                * cannot handle. Magic mask is pretty safe to be
-                * lifted for new magic when opts.patch_mode == 0.
-                */
-               parse_pathspec(&opts.pathspec,
-                              opts.patch_mode == 0 ? 0 :
-                              (PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP),
-                              0, prefix, argv);
+               parse_pathspec(&opts.pathspec, 0,
+                              opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
+                              prefix, argv);
 
                if (!opts.pathspec.nr)
                        die(_("invalid path specification"));