]> git.ipfire.org Git - thirdparty/git.git/commitdiff
worktree add: add --lock option
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Wed, 12 Apr 2017 13:58:05 +0000 (20:58 +0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Apr 2017 00:59:02 +0000 (17:59 -0700)
As explained in the document. This option has an advantage over the
command sequence "git worktree add && git worktree lock": there will be
no gap that somebody can accidentally "prune" the new worktree (or soon,
explicitly "worktree remove" it).

"worktree add" does keep a lock on while it's preparing the worktree.
If --lock is specified, this lock remains after the worktree is created.

Suggested-by: David Taylor <David.Taylor@dell.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-worktree.txt
builtin/worktree.c
t/t2025-worktree-add.sh

index 553cf8413f1792b2eec4c8827996e32f143b817a..b472acc3567f2cb0ed574a48459adb1c9a6f0334 100644 (file)
@@ -9,7 +9,7 @@ git-worktree - Manage multiple working trees
 SYNOPSIS
 --------
 [verse]
-'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
+'git worktree add' [-f] [--detach] [--checkout] [--lock] [-b <new-branch>] <path> [<branch>]
 'git worktree list' [--porcelain]
 'git worktree lock' [--reason <string>] <worktree>
 'git worktree prune' [-n] [-v] [--expire <expire>]
@@ -107,6 +107,11 @@ OPTIONS
        such as configuring sparse-checkout. See "Sparse checkout"
        in linkgit:git-read-tree[1].
 
+--lock::
+       Keep the working tree locked after creation. This is the
+       equivalent of `git worktree lock` after `git worktree add`,
+       but without race condition.
+
 -n::
 --dry-run::
        With `prune`, do not remove anything; just report what it would
index 9993ded41aaaaa9a9291bf51b9949a97418a5e76..05ade547cada4c7ede7f76fc8e15c26956a216e5 100644 (file)
@@ -24,6 +24,7 @@ struct add_opts {
        int force;
        int detach;
        int checkout;
+       int keep_locked;
        const char *new_branch;
        int force_new_branch;
 };
@@ -242,7 +243,10 @@ static int add_worktree(const char *path, const char *refname,
         * after the preparation is over.
         */
        strbuf_addf(&sb, "%s/locked", sb_repo.buf);
-       write_file(sb.buf, "initializing");
+       if (!opts->keep_locked)
+               write_file(sb.buf, "initializing");
+       else
+               write_file(sb.buf, "added with --lock");
 
        strbuf_addf(&sb_git, "%s/.git", path);
        if (safe_create_leading_directories_const(sb_git.buf))
@@ -303,9 +307,11 @@ static int add_worktree(const char *path, const char *refname,
        junk_git_dir = NULL;
 
 done:
-       strbuf_reset(&sb);
-       strbuf_addf(&sb, "%s/locked", sb_repo.buf);
-       unlink_or_warn(sb.buf);
+       if (ret || !opts->keep_locked) {
+               strbuf_reset(&sb);
+               strbuf_addf(&sb, "%s/locked", sb_repo.buf);
+               unlink_or_warn(sb.buf);
+       }
        argv_array_clear(&child_env);
        strbuf_release(&sb);
        strbuf_release(&symref);
@@ -328,6 +334,7 @@ static int add(int ac, const char **av, const char *prefix)
                           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_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
                OPT_END()
        };
 
index b618d6be21ff8926cb0d9dff1881c6e5e9eeb35a..b5c47ac602d65ebfb0f4baebc098f26d5d1a464c 100755 (executable)
@@ -63,6 +63,12 @@ test_expect_success '"add" worktree' '
        )
 '
 
+test_expect_success '"add" worktree with lock' '
+       git rev-parse HEAD >expect &&
+       git worktree add --detach --lock here-with-lock master &&
+       test -f .git/worktrees/here-with-lock/locked
+'
+
 test_expect_success '"add" worktree from a subdir' '
        (
                mkdir sub &&