Merge branch 'ta/docfix-index-format-tech'
[gitweb.git] / builtin / worktree.c
index cf35b2aa7e2b7d495dbed61a9581fbb7dac45c8d..430b51e7a74bb28ac1532ac836e9770121977096 100644 (file)
@@ -7,6 +7,7 @@
 #include "refs.h"
 #include "run-command.h"
 #include "sigchain.h"
+#include "refs.h"
 
 static const char * const worktree_usage[] = {
        N_("git worktree add [<options>] <path> <branch>"),
@@ -192,7 +193,6 @@ static int add_worktree(const char *path, const char *refname,
        int counter = 0, len, ret;
        struct strbuf symref = STRBUF_INIT;
        struct commit *commit = NULL;
-       unsigned char rev[20];
 
        if (file_exists(path) && !is_empty_dir(path))
                die(_("'%s' already exists"), path);
@@ -253,37 +253,39 @@ static int add_worktree(const char *path, const char *refname,
                   real_path(get_git_common_dir()), name);
        /*
         * This is to keep resolve_ref() happy. We need a valid HEAD
-        * or is_git_directory() will reject the directory. Moreover, HEAD
-        * in the new worktree must resolve to the same value as HEAD in
-        * the current tree since the command invoked to populate the new
-        * worktree will be handed the branch/ref specified by the user.
-        * For instance, if the user asks for the new worktree to be based
-        * at HEAD~5, then the resolved HEAD~5 in the new worktree must
-        * match the resolved HEAD~5 in the current tree in order to match
-        * the user's expectation.
+        * or is_git_directory() will reject the directory. Any value which
+        * looks like an object ID will do since it will be immediately
+        * replaced by the symbolic-ref or update-ref invocation in the new
+        * worktree.
         */
-       if (!resolve_ref_unsafe("HEAD", 0, rev, NULL))
-               die(_("unable to resolve HEAD"));
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
-       write_file(sb.buf, 1, "%s\n", sha1_to_hex(rev));
+       write_file(sb.buf, 1, "0000000000000000000000000000000000000000\n");
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
        write_file(sb.buf, 1, "../..\n");
 
        fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
 
-       setenv("GIT_CHECKOUT_NEW_WORKTREE", "1", 1);
        argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
        argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
        memset(&cp, 0, sizeof(cp));
        cp.git_cmd = 1;
-       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);
+
+       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_pushl(&cp.args, "reset", "--hard", NULL);
        cp.env = child_env.argv;
        ret = run_command(&cp);
        if (!ret) {
@@ -293,6 +295,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);