Skip to content

Commit ed197a6

Browse files
sunshinecogitster
authored andcommitted
worktree: avoid resolving HEAD unnecessarily
Now that git-worktree sets HEAD explicitly to its final value via either git-symbolic-ref or git-update-ref, rather than relying upon git-checkout to do so, the "hack" for pacifying is_git_directory() with a temporary HEAD, though still necessary, can be simplified. Since the real HEAD is now populated with its proper final value, the value of the temporary HEAD truly no longer matters, and any value which looks like an object ID is good enough to satisfy is_git_directory(). Therefore, just set the temporary HEAD to a literal value rather than going through the effort of resolving the current branch's HEAD. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7f44e3d commit ed197a6

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

builtin/worktree.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ static int add_worktree(const char *path, const char *refname,
192192
int counter = 0, len, ret;
193193
struct strbuf symref = STRBUF_INIT;
194194
struct commit *commit = NULL;
195-
unsigned char rev[20];
196195

197196
if (file_exists(path) && !is_empty_dir(path))
198197
die(_("'%s' already exists"), path);
@@ -253,20 +252,14 @@ static int add_worktree(const char *path, const char *refname,
253252
real_path(get_git_common_dir()), name);
254253
/*
255254
* This is to keep resolve_ref() happy. We need a valid HEAD
256-
* or is_git_directory() will reject the directory. Moreover, HEAD
257-
* in the new worktree must resolve to the same value as HEAD in
258-
* the current tree since the command invoked to populate the new
259-
* worktree will be handed the branch/ref specified by the user.
260-
* For instance, if the user asks for the new worktree to be based
261-
* at HEAD~5, then the resolved HEAD~5 in the new worktree must
262-
* match the resolved HEAD~5 in the current tree in order to match
263-
* the user's expectation.
255+
* or is_git_directory() will reject the directory. Any value which
256+
* looks like an object ID will do since it will be immediately
257+
* replaced by the symbolic-ref or update-ref invocation in the new
258+
* worktree.
264259
*/
265-
if (!resolve_ref_unsafe("HEAD", 0, rev, NULL))
266-
die(_("unable to resolve HEAD"));
267260
strbuf_reset(&sb);
268261
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
269-
write_file(sb.buf, 1, "%s\n", sha1_to_hex(rev));
262+
write_file(sb.buf, 1, "0000000000000000000000000000000000000000\n");
270263
strbuf_reset(&sb);
271264
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
272265
write_file(sb.buf, 1, "../..\n");

0 commit comments

Comments
 (0)