Skip to content

Commit 507e6e9

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf11a67 commit 507e6e9

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Documentation/git-worktree.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-worktree - Manage multiple working trees
99
SYNOPSIS
1010
--------
1111
[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>]
1313
'git worktree list' [--porcelain]
1414
'git worktree lock' [--reason <string>] <worktree>
1515
'git worktree prune' [-n] [-v] [--expire <expire>]
@@ -107,6 +107,11 @@ OPTIONS
107107
such as configuring sparse-checkout. See "Sparse checkout"
108108
in linkgit:git-read-tree[1].
109109

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+
110115
-n::
111116
--dry-run::
112117
With `prune`, do not remove anything; just report what it would

builtin/worktree.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct add_opts {
2424
int force;
2525
int detach;
2626
int checkout;
27+
int keep_locked;
2728
const char *new_branch;
2829
int force_new_branch;
2930
};
@@ -242,7 +243,10 @@ static int add_worktree(const char *path, const char *refname,
242243
* after the preparation is over.
243244
*/
244245
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");
246250

247251
strbuf_addf(&sb_git, "%s/.git", path);
248252
if (safe_create_leading_directories_const(sb_git.buf))
@@ -303,9 +307,11 @@ static int add_worktree(const char *path, const char *refname,
303307
junk_git_dir = NULL;
304308

305309
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+
}
309315
argv_array_clear(&child_env);
310316
strbuf_release(&sb);
311317
strbuf_release(&symref);
@@ -328,6 +334,7 @@ static int add(int ac, const char **av, const char *prefix)
328334
N_("create or reset a branch")),
329335
OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
330336
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")),
331338
OPT_END()
332339
};
333340

t/t2025-worktree-add.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ test_expect_success '"add" worktree' '
6363
)
6464
'
6565

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+
6672
test_expect_success '"add" worktree from a subdir' '
6773
(
6874
mkdir sub &&

0 commit comments

Comments
 (0)