]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/checkout.c
replace {pre,suf}fixcmp() with {starts,ends}_with()
[thirdparty/git.git] / builtin / checkout.c
index 0f573970372664a16024e83ed9733c94eeee3b50..7e939b05e348006d5eb81c2d3e8cbbc4be3804cb 100644 (file)
@@ -781,7 +781,7 @@ static int switch_branches(const struct checkout_opts *opts,
        if (!(flag & REF_ISSYMREF))
                old.path = NULL;
 
-       if (old.path && !prefixcmp(old.path, "refs/heads/"))
+       if (old.path && starts_with(old.path, "refs/heads/"))
                old.name = old.path + strlen("refs/heads/");
 
        if (!new->name) {
@@ -816,7 +816,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
                return 0;
        }
 
-       if (!prefixcmp(var, "submodule."))
+       if (starts_with(var, "submodule."))
                return parse_submodule_config_option(var, value);
 
        return git_xmerge_config(var, value, NULL);
@@ -873,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>]
@@ -885,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> [--]
         *
-        *   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.
+        *   (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.
         *
-        *   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.
+        *   (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.
         *
-        *   Otherwise <something> shall not be ambiguous.
+        *   (c) Otherwise, if "--" is present, treat it like case (1).
+        *
+        *   (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.
+        *
+        * 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.
@@ -907,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;
                }
        }
@@ -958,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
@@ -1108,9 +1151,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                const char *argv0 = argv[0];
                if (!argc || !strcmp(argv0, "--"))
                        die (_("--track needs a branch name"));
-               if (!prefixcmp(argv0, "refs/"))
+               if (starts_with(argv0, "refs/"))
                        argv0 += 5;
-               if (!prefixcmp(argv0, "remotes/"))
+               if (starts_with(argv0, "remotes/"))
                        argv0 += 8;
                argv0 = strchr(argv0, '/');
                if (!argv0 || !argv0[1])