worktree: simplify prefixing paths
[gitweb.git] / builtin / worktree.c
index 0a45710be8561e51d1b2e29586a63b0aa8518e56..f9dac376f748480c72023e9f4375edc90069a2ce 100644 (file)
 
 static const char * const worktree_usage[] = {
        N_("git worktree add [<options>] <path> [<branch>]"),
-       N_("git worktree prune [<options>]"),
        N_("git worktree list [<options>]"),
+       N_("git worktree prune [<options>]"),
        NULL
 };
 
 struct add_opts {
        int force;
        int detach;
+       int checkout;
        const char *new_branch;
        int force_new_branch;
 };
@@ -94,7 +95,7 @@ static void prune_worktrees(void)
        if (!dir)
                return;
        while ((d = readdir(dir)) != NULL) {
-               if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+               if (is_dot_or_dotdot(d->d_name))
                        continue;
                strbuf_reset(&reason);
                if (!prune_worktree(d->d_name, &reason))
@@ -201,12 +202,10 @@ static int add_worktree(const char *path, const char *refname,
                die(_("'%s' already exists"), path);
 
        /* is 'refname' a branch or commit? */
-       if (opts->force_new_branch) /* definitely a branch */
-               ;
-       else if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
+       if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
                 ref_exists(symref.buf)) { /* it's a branch */
                if (!opts->force)
-                       die_if_checked_out(symref.buf);
+                       die_if_checked_out(symref.buf, 0);
        } else { /* must be a commit */
                commit = lookup_commit_reference_by_name(refname);
                if (!commit)
@@ -263,7 +262,7 @@ static int add_worktree(const char *path, const char *refname,
         */
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
-       write_file(sb.buf, "0000000000000000000000000000000000000000");
+       write_file(sb.buf, sha1_to_hex(null_sha1));
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
        write_file(sb.buf, "../..");
@@ -286,18 +285,22 @@ static int add_worktree(const char *path, const char *refname,
        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) {
-               is_junk = 0;
-               free(junk_work_tree);
-               free(junk_git_dir);
-               junk_work_tree = NULL;
-               junk_git_dir = NULL;
+       if (opts->checkout) {
+               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)
+                       goto done;
        }
+
+       is_junk = 0;
+       free(junk_work_tree);
+       free(junk_git_dir);
+       junk_work_tree = NULL;
+       junk_git_dir = NULL;
+
 done:
        strbuf_reset(&sb);
        strbuf_addf(&sb, "%s/locked", sb_repo.buf);
@@ -322,23 +325,34 @@ static int add(int ac, const char **av, const char *prefix)
                OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
                           N_("create or reset a branch")),
                OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
+               OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
                OPT_END()
        };
 
        memset(&opts, 0, sizeof(opts));
+       opts.checkout = 1;
        ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
        if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
                die(_("-b, -B, and --detach are mutually exclusive"));
        if (ac < 1 || ac > 2)
                usage_with_options(worktree_usage, options);
 
-       path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
+       path = prefix_filename(prefix, strlen(prefix), av[0]);
        branch = ac < 2 ? "HEAD" : av[1];
 
        opts.force_new_branch = !!new_branch_force;
-       if (opts.force_new_branch)
+       if (opts.force_new_branch) {
+               struct strbuf symref = STRBUF_INIT;
+
                opts.new_branch = new_branch_force;
 
+               if (!opts.force &&
+                   !strbuf_check_branch_ref(&symref, opts.new_branch) &&
+                   ref_exists(symref.buf))
+                       die_if_checked_out(symref.buf, 0);
+               strbuf_release(&symref);
+       }
+
        if (ac < 2 && !opts.new_branch && !opts.detach) {
                int n;
                const char *s = worktree_basename(path, &n);
@@ -453,6 +467,8 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
 
        if (ac < 2)
                usage_with_options(worktree_usage, options);
+       if (!prefix)
+               prefix = "";
        if (!strcmp(av[1], "add"))
                return add(ac - 1, av + 1, prefix);
        if (!strcmp(av[1], "prune"))