[PATCH v3 00/22] rid git-checkout of too-intimate knowledge of new worktree
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 worktreebranch.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< ----