worktree: simplify prefixing paths
[gitweb.git] / builtin / worktree.c
index d8e3795dc44a0dc047cac77af92282361227111a..f9dac376f748480c72023e9f4375edc90069a2ce 100644 (file)
@@ -13,8 +13,8 @@
 
 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
 };
 
@@ -95,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))
@@ -205,7 +205,7 @@ static int add_worktree(const char *path, const char *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)
@@ -262,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, "../..");
@@ -337,7 +337,7 @@ static int add(int ac, const char **av, const char *prefix)
        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;
@@ -349,7 +349,7 @@ static int add(int ac, const char **av, const char *prefix)
                if (!opts.force &&
                    !strbuf_check_branch_ref(&symref, opts.new_branch) &&
                    ref_exists(symref.buf))
-                       die_if_checked_out(symref.buf);
+                       die_if_checked_out(symref.buf, 0);
                strbuf_release(&symref);
        }
 
@@ -467,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"))