worktree add: add --lock option

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>

Nguyễn Thái Ngọc Duy committed Apr 12, 2017 at 20:58 UTC 507e6e9eecce5e7a2cc204c844bbb2f9b17b31e3
3 files changed +23 -5
Documentation/git-worktree.txt
+6 -1
@@ -9,7 +9,7 @@ git-worktree - Manage multiple working trees
9 SYNOPSIS
10 --------
11 [verse]
12 -'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
12 +'git worktree add' [-f] [--detach] [--checkout] [--lock] [-b <new-branch>] <path> [<branch>]
13 'git worktree list' [--porcelain]
14 'git worktree lock' [--reason <string>] <worktree>
15 'git worktree prune' [-n] [-v] [--expire <expire>]
@@ -107,6 +107,11 @@ OPTIONS
107 such as configuring sparse-checkout. See "Sparse checkout"
108 in linkgit:git-read-tree[1].
109
110 +--lock::
111 + Keep the working tree locked after creation. This is the
112 + equivalent of `git worktree lock` after `git worktree add`,
113 + but without race condition.
114 +
115 -n::
116 --dry-run::
117 With `prune`, do not remove anything; just report what it would
builtin/worktree.c
+11 -4
@@ -24,6 +24,7 @@ struct add_opts {
24 int force;
25 int detach;
26 int checkout;
27 + int keep_locked;
28 const char *new_branch;
29 int force_new_branch;
30 };
@@ -242,7 +243,10 @@ static int add_worktree(const char *path, const char *refname,
243 * after the preparation is over.
244 */
245 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
245 - write_file(sb.buf, "initializing");
246 + if (!opts->keep_locked)
247 + write_file(sb.buf, "initializing");
248 + else
249 + write_file(sb.buf, "added with --lock");
250
251 strbuf_addf(&sb_git, "%s/.git", path);
252 if (safe_create_leading_directories_const(sb_git.buf))
@@ -303,9 +307,11 @@ static int add_worktree(const char *path, const char *refname,
307 junk_git_dir = NULL;
308
309 done:
306 - strbuf_reset(&sb);
307 - strbuf_addf(&sb, "%s/locked", sb_repo.buf);
308 - unlink_or_warn(sb.buf);
310 + if (ret || !opts->keep_locked) {
311 + strbuf_reset(&sb);
312 + strbuf_addf(&sb, "%s/locked", sb_repo.buf);
313 + unlink_or_warn(sb.buf);
314 + }
315 argv_array_clear(&child_env);
316 strbuf_release(&sb);
317 strbuf_release(&symref);
@@ -328,6 +334,7 @@ static int add(int ac, const char **av, const char *prefix)
334 N_("create or reset a branch")),
335 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
336 OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
337 + OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
338 OPT_END()
339 };
340
t/t2025-worktree-add.sh
+6
@@ -63,6 +63,12 @@ test_expect_success '"add" worktree' '
63 )
64 '
65
66 +test_expect_success '"add" worktree with lock' '
67 + git rev-parse HEAD >expect &&
68 + git worktree add --detach --lock here-with-lock master &&
69 + test -f .git/worktrees/here-with-lock/locked
70 +'
71 +
72 test_expect_success '"add" worktree from a subdir' '
73 (
74 mkdir sub &&