Skip to content

Commit

Permalink
worktree: make setup of new HEAD distinct from worktree population
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
sunshineco authored and gitster committed Jul 20, 2015
1 parent f7c9dac commit 7f44e3d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions builtin/worktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,21 @@ static int add_worktree(const char *path, const char *refname,
argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
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);
cp.env = child_env.argv;
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);
cp.env = child_env.argv;
ret = run_command(&cp);
if (!ret) {
Expand All @@ -293,6 +302,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);
Expand Down

0 comments on commit 7f44e3d

Please sign in to comment.