Merge branch 'ky/branch-m-worktree' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 2 May 2016 21:24:05 +0000 (14:24 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 2 May 2016 21:24:05 +0000 (14:24 -0700)
When "git worktree" feature is in use, "git branch -m" renamed a
branch that is checked out in another worktree without adjusting
the HEAD symbolic ref for the worktree.

* ky/branch-m-worktree:
set_worktree_head_symref(): fix error message
branch -m: update all per-worktree HEADs
refs: add a new function set_worktree_head_symref

1  2 
builtin/branch.c
t/t3200-branch.sh
diff --combined builtin/branch.c
index 8885d9f8e2cdbd6b7c3e1ff753147f57ffd37243,b1738313175ca8566e9a06b2826a0425c9422a46..de6df09ed30df11ea853b5d815f22d9859ac24cc
@@@ -20,7 -20,6 +20,7 @@@
  #include "utf8.h"
  #include "wt-status.h"
  #include "ref-filter.h"
 +#include "worktree.h"
  
  static const char * const builtin_branch_usage[] = {
        N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
@@@ -216,21 -215,16 +216,21 @@@ static int delete_branches(int argc, co
                int flags = 0;
  
                strbuf_branchname(&bname, argv[i]);
 -              if (kinds == FILTER_REFS_BRANCHES && !strcmp(head, bname.buf)) {
 -                      error(_("Cannot delete the branch '%s' "
 -                            "which you are currently on."), bname.buf);
 -                      ret = 1;
 -                      continue;
 -              }
 -
                free(name);
 -
                name = mkpathdup(fmt, bname.buf);
 +
 +              if (kinds == FILTER_REFS_BRANCHES) {
 +                      char *worktree = find_shared_symref("HEAD", name);
 +                      if (worktree) {
 +                              error(_("Cannot delete branch '%s' "
 +                                      "checked out at '%s'"),
 +                                    bname.buf, worktree);
 +                              free(worktree);
 +                              ret = 1;
 +                              continue;
 +                      }
 +              }
 +
                target = resolve_ref_unsafe(name,
                                            RESOLVE_REF_READING
                                            | RESOLVE_REF_NO_RECURSE
@@@ -558,8 -552,7 +558,7 @@@ static void rename_branch(const char *o
        if (recovery)
                warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
  
-       /* no need to pass logmsg here as HEAD didn't really move */
-       if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
+       if (replace_each_worktree_head_symref(oldref.buf, newref.buf))
                die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
  
        strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
diff --combined t/t3200-branch.sh
index 508007fd377260fd46e385d6a47b8a9732ef1dd0,f7d438bd7d1db0f55e5730480adfee42fd15addf..f3e3b6cf2eabf0d57f64d794dba9a6195e8da246
@@@ -126,7 -126,28 +126,28 @@@ test_expect_success 'git branch -M foo 
  test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
        git checkout -b baz &&
        git branch bam &&
-       git branch -M baz bam
+       git branch -M baz bam &&
+       test $(git rev-parse --abbrev-ref HEAD) = bam
+ '
+ test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
+       git checkout master &&
+       git worktree add -b baz bazdir &&
+       git worktree add -f bazdir2 baz &&
+       git branch -M baz bam &&
+       test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
+       test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
+ '
+ test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
+       git checkout -b baz &&
+       git worktree add -f bazdir3 baz &&
+       (
+               cd bazdir3 &&
+               git branch -M baz bam &&
+               test $(git rev-parse --abbrev-ref HEAD) = bam
+       ) &&
+       test $(git rev-parse --abbrev-ref HEAD) = bam
  '
  
  test_expect_success 'git branch -M master should work when master is checked out' '
@@@ -403,12 -424,6 +424,12 @@@ test_expect_success 'test deleting bran
        test_i18ncmp expect actual
  '
  
 +test_expect_success 'deleting currently checked out branch fails' '
 +      git worktree add -b my7 my7 &&
 +      test_must_fail git -C my7 branch -d my7 &&
 +      test_must_fail git branch -d my7
 +'
 +
  test_expect_success 'test --track without .fetch entries' '
        git branch --track my8 &&
        test "$(git config branch.my8.remote)" &&