git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 00/22] rid git-checkout of too-intimate knowledge of new worktree

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jul 17, 2015, 22:59 UTC
Message-ID
<1437174017-81687-1-git-send-email-sunshine@sunshineco.com>

This is v3 of [1] which rids git-checkout of specialized knowledge that it's operating in a newly created linked worktree. Thanks to Junio for his review, and Duy and Michael J Gruber for additional observations.

A v2 to v3 interdiff is included below.
Changes since v2:
* patch 06/22: strip only trailing ".git" rather than "/.git" from
  "'branch' is already checked out at '/some/patch/.git'" message[2];
  also reword commit message slightly to better emphasize that the
  trailing ".git" is more than a mere cosmetic issue[2]
* patch 10/22 (new): change worktree setup message from "Enter
  <path>..." to "Preparing <path>..." to avoid possibly confusing the
  user into thinking that 'cd <path>' occurred[3]
* patch 13/22: add tests of -b/-B/--detach exclusivity
* patch 14/22 (new): suppress accidental triggering of branch
  auto-vivication with "git worktree add --detach <path>"; instead treat
  it as shorthand for "git worktree add --detach <path> HEAD"[3]

[1]: http://thread.gmane.org/gmane.comp.version-control.git/274024 [2]: http://article.gmane.org/gmane.comp.version-control.git/274035 [3]: http://article.gmane.org/gmane.comp.version-control.git/274083

Eric Sunshine (22):
  checkout: avoid resolving HEAD unnecessarily
  checkout: name check_linked_checkouts() more meaningfully
  checkout: improve die_if_checked_out() robustness
  checkout: die_if_checked_out: simplify strbuf management
  checkout: generalize die_if_checked_out() branch name argument
  checkout: check_linked_checkout: improve "already checked out"
    aesthetic
  checkout: check_linked_checkout: simplify symref parsing
  checkout: teach check_linked_checkout() about symbolic link HEAD
  branch: publish die_if_checked_out()
  worktree: improve worktree setup message
  worktree: simplify new branch (-b/-B) option checking
  worktree: introduce options container
  worktree: make --detach mutually exclusive with -b/-B
  worktree: add: suppress auto-vivication with --detach and no <branch>
  worktree: make branch creation distinct from worktree population
  worktree: elucidate environment variables intended for child processes
  worktree: add_worktree: construct worktree-population command locally
  worktree: detect branch-name/detached and error conditions locally
  worktree: make setup of new HEAD distinct from worktree population
  worktree: avoid resolving HEAD unnecessarily
  worktree: populate via "git reset --hard" rather than "git checkout"
  checkout: drop intimate knowledge of newly created worktree
 branch.c                |  67 ++++++++++++++++++++++++++
 branch.h                |   7 +++
 builtin/checkout.c      |  82 +++-----------------------------
 builtin/worktree.c      | 123 ++++++++++++++++++++++++++++++++----------------
 t/t2025-worktree-add.sh |  34 +++++++++++++
 5 files changed, 197 insertions(+), 116 deletions(-)
-- 
2.5.0.rc2.378.g0af52e8

---- 8< ----
diff --git a/branch.c b/branch.c
index dfd7698..af9480f 100644
--- a/branch.c
+++ b/branch.c
@@ -347,7 +347,7 @@ static void check_linked_checkout(const char *branch, const char *id)
 	} else
 		strbuf_addstr(&gitdir, get_git_common_dir());
 	skip_prefix(branch, "refs/heads/", &branch);
-	strbuf_strip_suffix(&gitdir, "/.git");
+	strbuf_strip_suffix(&gitdir, ".git");
 	die(_("'%s' is already checked out at '%s'"), branch, gitdir.buf);
 done:
 	strbuf_release(&path);
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 2873064..5d9371c 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -264,7 +264,7 @@ static int add_worktree(const char *path, const char *refname,
 	strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
 	write_file(sb.buf, 1, "../..\n");
 
-	fprintf_ln(stderr, _("Enter %s (identifier %s)"), path, name);
+	fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
 
 	argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
 	argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
@@ -335,7 +335,7 @@ static int add(int ac, const char **av, const char *prefix)
 	if (opts.force_new_branch)
 		opts.new_branch = new_branch_force;
 
-	if (ac < 2 && !opts.new_branch) {
+	if (ac < 2 && !opts.new_branch && !opts.detach) {
 		int n;
 		const char *s = worktree_basename(path, &n);
 		opts.new_branch = xstrndup(s, n);
diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
index 9e30690..8267411 100755
--- a/t/t2025-worktree-add.sh
+++ b/t/t2025-worktree-add.sh
@@ -153,6 +153,14 @@ test_expect_success '"add -b" with <branch> omitted' '
 	test_cmp_rev HEAD burble
 '
 
+test_expect_success '"add --detach" with <branch> omitted' '
+	git worktree add --detach fishhook &&
+	git rev-parse HEAD >expected &&
+	git -C fishhook rev-parse HEAD >actual &&
+	test_cmp expected actual &&
+	test_must_fail git -C fishhook symbolic-ref HEAD
+'
+
 test_expect_success '"add" with <branch> omitted' '
 	git worktree add wiffle/bat &&
 	test_cmp_rev HEAD bat
@@ -167,4 +175,22 @@ test_expect_success '"add" auto-vivify does not clobber existing branch' '
 	test_path_is_missing precious
 '
 
+test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
+	git worktree add --detach mish/mash &&
+	test_must_fail git rev-parse mash -- &&
+	test_must_fail git -C mish/mash symbolic-ref HEAD
+'
+
+test_expect_success '"add" -b/-B mutually exclusive' '
+	test_must_fail git worktree add -b poodle -B poodle bamboo master
+'
+
+test_expect_success '"add" -b/--detach mutually exclusive' '
+	test_must_fail git worktree add -b poodle --detach bamboo master
+'
+
+test_expect_success '"add" -B/--detach mutually exclusive' '
+	test_must_fail git worktree add -B poodle --detach bamboo master
+'
+
 test_done
---- 8< ----
Next: Eric Sunshine
Message 1 of 24 in “rid git-checkout of too-intimate knowledge of new worktree”
  1. 00/22 rid git-checkout of too-intimate knowledge of new worktreeEric Sunshine, Jul 17, 2015
  2. 01/22 checkout: avoid resolving HEAD unnecessarilyEric Sunshine, Jul 17, 2015
  3. 02/22 checkout: name check_linked_checkouts() more meaningfullyEric Sunshine, Jul 17, 2015
  4. 03/22 checkout: improve die_if_checked_out() robustnessEric Sunshine, Jul 17, 2015
  5. 04/22 checkout: die_if_checked_out: simplify strbuf managementEric Sunshine, Jul 17, 2015
  6. 05/22 checkout: generalize die_if_checked_out() branch name argumentEric Sunshine, Jul 17, 2015
  7. 06/22 checkout: check_linked_checkout: improve "already checked out" aestheticEric Sunshine, Jul 17, 2015
  8. 07/22 checkout: check_linked_checkout: simplify symref parsingEric Sunshine, Jul 17, 2015
  9. 08/22 checkout: teach check_linked_checkout() about symbolic link HEADEric Sunshine, Jul 17, 2015
  10. 09/22 branch: publish die_if_checked_out()Eric Sunshine, Jul 17, 2015
  11. 10/22 worktree: improve worktree setup messageEric Sunshine, Jul 17, 2015
  12. 11/22 worktree: simplify new branch (-b/-B) option checkingEric Sunshine, Jul 17, 2015
  13. 12/22 worktree: introduce options containerEric Sunshine, Jul 17, 2015
  14. 13/22 worktree: make --detach mutually exclusive with -b/-BEric Sunshine, Jul 17, 2015
  15. 14/22 worktree: add: suppress auto-vivication with --detach and no <branch>Eric Sunshine, Jul 17, 2015
  16. Eric SunshineJul 17, 2015
  17. 15/22 worktree: make branch creation distinct from worktree populationEric Sunshine, Jul 17, 2015
  18. 16/22 worktree: elucidate environment variables intended for child processesEric Sunshine, Jul 17, 2015
  19. 17/22 worktree: add_worktree: construct worktree-population command locallyEric Sunshine, Jul 17, 2015
  20. 18/22 worktree: detect branch-name/detached and error conditions locallyEric Sunshine, Jul 17, 2015
  21. 19/22 worktree: make setup of new HEAD distinct from worktree populationEric Sunshine, Jul 17, 2015
  22. 20/22 worktree: avoid resolving HEAD unnecessarilyEric Sunshine, Jul 17, 2015
  23. 21/22 worktree: populate via "git reset --hard" rather than "git checkout"Eric Sunshine, Jul 17, 2015
  24. 22/22 checkout: drop intimate knowledge of newly created worktreeEric Sunshine, Jul 17, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.