Skip to content

Commit 7f44e3d

Browse files
sunshinecogitster
authored andcommitted
worktree: make setup of new HEAD distinct from worktree population
git-worktree currently conflates setting of HEAD in the new worktree and initial worktree population into a single git-checkout invocation which requires git-checkout to have special knowledge that it is operating on a newly created worktree. The eventual goal is to rid git-checkout of that overly-intimate knowledge. Once these operations are separate, 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 itself responsible for setting up HEAD as either a symbolic reference, if associated with a branch, or detached, if not. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f7c9dac commit 7f44e3d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

builtin/worktree.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,21 @@ static int add_worktree(const char *path, const char *refname,
278278
argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
279279
memset(&cp, 0, sizeof(cp));
280280
cp.git_cmd = 1;
281+
282+
if (commit)
283+
argv_array_pushl(&cp.args, "update-ref", "HEAD",
284+
sha1_to_hex(commit->object.sha1), NULL);
285+
else
286+
argv_array_pushl(&cp.args, "symbolic-ref", "HEAD",
287+
symref.buf, NULL);
288+
cp.env = child_env.argv;
289+
ret = run_command(&cp);
290+
if (ret)
291+
goto done;
292+
293+
cp.argv = NULL;
294+
argv_array_clear(&cp.args);
281295
argv_array_push(&cp.args, "checkout");
282-
if (opts->force)
283-
argv_array_push(&cp.args, "--ignore-other-worktrees");
284-
if (opts->detach)
285-
argv_array_push(&cp.args, "--detach");
286-
argv_array_push(&cp.args, refname);
287296
cp.env = child_env.argv;
288297
ret = run_command(&cp);
289298
if (!ret) {
@@ -293,6 +302,7 @@ static int add_worktree(const char *path, const char *refname,
293302
junk_work_tree = NULL;
294303
junk_git_dir = NULL;
295304
}
305+
done:
296306
strbuf_reset(&sb);
297307
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
298308
unlink_or_warn(sb.buf);

0 commit comments

Comments
 (0)