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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 17, 2015 at 19:00 UTC ed197a6ab983783550bd0b0c71cb97d734f4cfaa
1 file changed +5 -12
builtin/worktree.c
+5 -12
@@ -192,7 +192,6 @@ static int add_worktree(const char *path, const char *refname,
192 int counter = 0, len, ret;
193 struct strbuf symref = STRBUF_INIT;
194 struct commit *commit = NULL;
195 - unsigned char rev[20];
195
196 if (file_exists(path) && !is_empty_dir(path))
197 die(_("'%s' already exists"), path);
@@ -253,20 +252,14 @@ static int add_worktree(const char *path, const char *refname,
252 real_path(get_git_common_dir()), name);
253 /*
254 * 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.
259 */
265 - if (!resolve_ref_unsafe("HEAD", 0, rev, NULL))
266 - die(_("unable to resolve HEAD"));
260 strbuf_reset(&sb);
261 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");
263 strbuf_reset(&sb);
264 strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
265 write_file(sb.buf, 1, "../..\n");