Merge branch 'mm/checkout-auto-track-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Thu, 7 Nov 2013 22:36:59 +0000 (14:36 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 7 Nov 2013 22:36:59 +0000 (14:36 -0800)
"git checkout topic", when there is not yet a local "topic" branch
but there is a unique remote-tracking branch for a remote "topic"
branch, pretended as if "git checkout -t -b topic remote/$r/topic"
(for that unique remote $r) was run. This hack however was not
implemented for "git checkout topic --".

* mm/checkout-auto-track-fix:
checkout: proper error message on 'git checkout foo bar --'
checkout: allow dwim for branch creation for "git checkout $branch --"

1  2 
builtin/checkout.c
t/t2024-checkout-dwim.sh
diff --combined builtin/checkout.c
index 7025938ae30f2ce3104b27f66bf6e1f294edb810,e5103b9c9558d227a0a21c973fdbef456744e2a5..22cc7fcb59a508c861dd2b55b4c591c8792f8fdc
@@@ -97,7 -97,7 +97,7 @@@ static int read_tree_some(struct tree *
        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))
        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)) {
                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;
@@@ -321,7 -321,7 +321,7 @@@ static int checkout_paths(const struct 
  
        /* 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;
@@@ -587,7 -587,7 +587,7 @@@ static void update_refs_for_switch(cons
                                   struct branch_info *new)
  {
        struct strbuf msg = STRBUF_INIT;
 -      const char *old_desc;
 +      const char *old_desc, *reflog_msg;
        if (opts->new_branch) {
                if (opts->new_orphan_branch) {
                        if (opts->new_branch_log && !log_all_ref_updates) {
        old_desc = old->name;
        if (!old_desc && old->commit)
                old_desc = sha1_to_hex(old->commit->object.sha1);
 -      strbuf_addf(&msg, "checkout: moving from %s to %s",
 -                  old_desc ? old_desc : "(invalid)", new->name);
 +
 +      reflog_msg = getenv("GIT_REFLOG_ACTION");
 +      if (!reflog_msg)
 +              strbuf_addf(&msg, "checkout: moving from %s to %s",
 +                      old_desc ? old_desc : "(invalid)", new->name);
 +      else
 +              strbuf_insert(&msg, 0, reflog_msg, strlen(reflog_msg));
  
        if (!strcmp(new->name, "HEAD") && !new->path && !opts->force_detach) {
                /* Nothing to do. */
@@@ -843,16 -838,13 +843,16 @@@ static int check_tracking_name(struct r
        memset(&query, 0, sizeof(struct refspec));
        query.src = cb->src_ref;
        if (remote_find_tracking(remote, &query) ||
 -          get_sha1(query.dst, cb->dst_sha1))
 +          get_sha1(query.dst, cb->dst_sha1)) {
 +              free(query.dst);
                return 0;
 +      }
        if (cb->dst_ref) {
 +              free(query.dst);
                cb->unique = 0;
                return 0;
        }
 -      cb->dst_ref = xstrdup(query.dst);
 +      cb->dst_ref = query.dst;
        return 0;
  }
  
@@@ -880,7 -872,9 +880,9 @@@ static int parse_branchname_arg(int arg
        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>]
         *
         *   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.
        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;
                }
        }
  
        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
diff --combined t/t2024-checkout-dwim.sh
index 094b92ef489156980dc1429136d5dad25d01ac7d,36be80fd41a7301fad657b33f028af844add60f8..6ecb559465b43e528a5cb1511b758cc48d940e92
@@@ -104,7 -104,7 +104,7 @@@ test_expect_success 'setup more remote
                cd repo_c &&
                test_commit c_master &&
                git checkout -b bar &&
 -              test_commit c_bar
 +              test_commit c_bar &&
                git checkout -b spam &&
                test_commit c_spam
        ) &&
                cd repo_d &&
                test_commit d_master &&
                git checkout -b baz &&
 -              test_commit f_baz
 +              test_commit d_baz &&
                git checkout -b eggs &&
 -              test_commit c_eggs
 +              test_commit d_eggs
        ) &&
        git remote add repo_c repo_c &&
        git config remote.repo_c.fetch \
@@@ -164,4 -164,25 +164,25 @@@ test_expect_success 'checkout of branc
        test_branch_upstream eggs repo_d eggs
  '
  
+ test_expect_success 'checkout of branch with a file having the same name fails' '
+       git checkout -B master &&
+       test_might_fail git branch -D spam &&
+       >spam &&
+       test_must_fail git checkout spam &&
+       test_must_fail git rev-parse --verify refs/heads/spam &&
+       test_branch master
+ '
+ test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
+       git checkout -B master &&
+       test_might_fail git branch -D spam &&
+       >spam &&
+       git checkout spam -- &&
+       test_branch spam &&
+       test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
+       test_branch_upstream spam repo_c spam
+ '
  test_done