The eventual goal is for git-worktree to populate the new worktree via
"git reset --hard" rather than "git checkout". As a consequence,
git-worktree will no longer be able to delegate to git-branch the
setting of the new worktree's HEAD to the desired branch (or commit, if
detached). Therefore, make git-worktree responsible for setting up HEAD
as either a symbolic reference if associated with a branch, or detached
if not.

Signed-off-by: Eric Sunshine <sunsh...@sunshineco.com>
---

As with the patch which separates branch creation from worktree
population (by using git-branch instead of "git checkout -b"), this
patch invokes git-symbolic-ref and git-update-ref rather than using the
lower-level C API, due to the added value of those commands. If this
proves the wrong approach, then the series can be re-rolled or follow-on
patches can instead use the C API.

 builtin/worktree.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index babdef1..94c1701 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -274,12 +274,20 @@ static int add_worktree(const char *path, const char 
*refname,
        setenv(GIT_WORK_TREE_ENVIRONMENT, path, 1);
        memset(&cp, 0, sizeof(cp));
        cp.git_cmd = 1;
+
+       if (commit)
+               argv_array_pushl(&cp.args, "update-ref", "HEAD",
+                                sha1_to_hex(commit->object.sha1), NULL);
+       else
+               argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
+                                symref.buf, NULL);
+       ret = run_command(&cp);
+       if (ret)
+               goto done;
+
+       cp.argv = NULL;
+       argv_array_clear(&cp.args);
        argv_array_push(&cp.args, "checkout");
-       if (opts->force)
-               argv_array_push(&cp.args, "--ignore-other-worktrees");
-       if (opts->detach)
-               argv_array_push(&cp.args, "--detach");
-       argv_array_push(&cp.args, refname);
        ret = run_command(&cp);
        if (!ret) {
                is_junk = 0;
@@ -288,6 +296,7 @@ static int add_worktree(const char *path, const char 
*refname,
                junk_work_tree = NULL;
                junk_git_dir = NULL;
        }
+done:
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/locked", sb_repo.buf);
        unlink_or_warn(sb.buf);
-- 
2.5.0.rc1.201.ga12d9f8

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to