git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Kazuki Yamaguchi <k@rhe.jp>
To: git@vger.kernel.org
Cc: Eric Sunshine <sunshine@sunshineco.com>,
	Duy Nguyen <pclouds@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Michael Haggerty <mhagger@alum.mit.edu>,
	Kazuki Yamaguchi <k@rhe.jp>
Subject: [PATCH v3 2/2] branch -m: update all per-worktree HEADs
Date: Sun, 27 Mar 2016 23:37:14 +0900	[thread overview]
Message-ID: <572722fb3051b9383d393a46c2f6d22991ab7a93.1459087958.git.k@rhe.jp> (raw)
In-Reply-To: <cover.1459087958.git.k@rhe.jp>
In-Reply-To: <cover.1459087958.git.k@rhe.jp>

When renaming a branch, currently only the HEAD of current working tree
is updated, but it must update HEADs of all working trees which point at
the old branch.

This is the current behavior, /path/to/wt's HEAD is not updated:

  % git worktree list
  /path/to     2c3c5f2 [master]
  /path/to/wt  2c3c5f2 [oldname]
  % git branch -m master master2
  % git worktree list
  /path/to     2c3c5f2 [master2]
  /path/to/wt  2c3c5f2 [oldname]
  % git branch -m oldname newname
  % git worktree list
  /path/to     2c3c5f2 [master2]
  /path/to/wt  0000000 [oldname]

This patch fixes this issue by updating all relevant worktree HEADs
when renaming a branch.

Signed-off-by: Kazuki Yamaguchi <k@rhe.jp>
---
 branch.c          | 23 +++++++++++++++++++++++
 branch.h          |  7 +++++++
 builtin/branch.c  |  3 +--
 t/t3200-branch.sh | 23 ++++++++++++++++++++++-
 4 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/branch.c b/branch.c
index c50ea42172ce..416244370783 100644
--- a/branch.c
+++ b/branch.c
@@ -344,3 +344,26 @@ void die_if_checked_out(const char *branch)
 		die(_("'%s' is already checked out at '%s'"), branch, existing);
 	}
 }
+
+int replace_each_worktree_head_symref(const char *oldref, const char *newref)
+{
+	int ret = 0;
+	struct worktree **worktrees = get_worktrees();
+	int i;
+
+	for (i = 0; worktrees[i]; i++) {
+		if (worktrees[i]->is_detached)
+			continue;
+		if (strcmp(oldref, worktrees[i]->head_ref))
+			continue;
+
+		if (set_worktree_head_symref(worktrees[i]->git_dir, newref)) {
+			ret = -1;
+			error(_("HEAD of working tree %s is not updated"),
+			      worktrees[i]->path);
+		}
+	}
+
+	free_worktrees(worktrees);
+	return ret;
+}
diff --git a/branch.h b/branch.h
index 78ad4387cd32..d69163daf793 100644
--- a/branch.h
+++ b/branch.h
@@ -60,4 +60,11 @@ extern int read_branch_desc(struct strbuf *, const char *branch_name);
  */
 extern void die_if_checked_out(const char *branch);
 
+/*
+ * Update all per-worktree HEADs pointing at the old ref to point the new ref.
+ * This will be used when renaming a branch. Returns 0 if successful, non-zero
+ * otherwise.
+ */
+extern int replace_each_worktree_head_symref(const char *oldref, const char *newref);
+
 #endif
diff --git a/builtin/branch.c b/builtin/branch.c
index 7b45b6bd6b80..b1738313175c 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -552,8 +552,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 	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 --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index a89724849065..f7d438bd7d1d 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -126,7 +126,28 @@ test_expect_success 'git branch -M foo bar should fail when bar is checked out'
 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' '
-- 
2.8.0.rc4.21.g05df949

      parent reply	other threads:[~2016-03-27 14:37 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-21  9:50 [PATCH] branch: update all per-worktree HEADs when renaming a branch Kazuki Yamaguchi
2016-03-21 17:41 ` Eric Sunshine
2016-03-22  0:49   ` Duy Nguyen
2016-03-25 11:56     ` Kazuki Yamaguchi
2016-03-25 11:33   ` Kazuki Yamaguchi
2016-03-25 18:28 ` [PATCH v2 0/5] branch: fix branch operations with multiple working trees Kazuki Yamaguchi
2016-03-25 21:13   ` Junio C Hamano
2016-03-27  7:29     ` Kazuki Yamaguchi
2016-03-25 18:28 ` [PATCH v2 1/5] refs: add new flag RESOLVE_REF_COMMON_DIR to resolve_ref_unsafe Kazuki Yamaguchi
2016-03-25 18:28 ` [PATCH v2 2/5] refs: add REF_COMMON_DIR flag Kazuki Yamaguchi
2016-03-25 18:28 ` [PATCH v2 3/5] refs: add create_symref_common_dir as a variation of create_symref Kazuki Yamaguchi
2016-03-25 18:28 ` [PATCH v2 4/5] branch -m: update all per-worktree HEADs Kazuki Yamaguchi
2016-03-25 18:28 ` [PATCH v2 5/5] branch -d: refuse deleting a branch which is currently checked out Kazuki Yamaguchi
2016-03-25 21:00   ` Junio C Hamano
2016-03-27 17:52   ` Eric Sunshine
2016-03-28  7:16     ` Kazuki Yamaguchi
2016-03-28  7:22     ` [PATCH v2] " Kazuki Yamaguchi
2016-03-28 16:51       ` Eric Sunshine
2016-03-29  9:28         ` Kazuki Yamaguchi
2016-03-29 18:47           ` Eric Sunshine
2016-03-29  9:38   ` [PATCH v3] " Kazuki Yamaguchi
2016-03-29 18:57     ` Eric Sunshine
2016-03-27 14:37 ` [PATCH v3 0/2] update all per-worktree HEADs when renaming a branch Kazuki Yamaguchi
2016-03-27 14:37 ` [PATCH v3 1/2] refs: add a new function set_worktree_head_symref Kazuki Yamaguchi
2016-03-28 17:48   ` Junio C Hamano
2016-03-29  2:23     ` David Turner
2016-04-07 21:20   ` Eric Sunshine
2016-04-08  6:37     ` Kazuki Yamaguchi
2016-04-08  6:42       ` Eric Sunshine
2016-03-27 14:37 ` Kazuki Yamaguchi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=572722fb3051b9383d393a46c2f6d22991ab7a93.1459087958.git.k@rhe.jp \
    --to=k@rhe.jp \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mhagger@alum.mit.edu \
    --cc=pclouds@gmail.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).