Skip to content

Commit e311597

Browse files
committed
Merge branch 'nd/worktree-add-lock'
Allow to lock a worktree immediately after it's created. This helps prevent a race between "git worktree add; git worktree lock" and "git worktree prune". * nd/worktree-add-lock: worktree add: add --lock option
2 parents 7ba7bff + 507e6e9 commit e311597

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
};
@@ -240,7 +241,10 @@ static int add_worktree(const char *path, const char *refname,
240241
* after the preparation is over.
241242
*/
242243
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
243-
write_file(sb.buf, "initializing");
244+
if (!opts->keep_locked)
245+
write_file(sb.buf, "initializing");
246+
else
247+
write_file(sb.buf, "added with --lock");
244248

245249
strbuf_addf(&sb_git, "%s/.git", path);
246250
if (safe_create_leading_directories_const(sb_git.buf))
@@ -301,9 +305,11 @@ static int add_worktree(const char *path, const char *refname,
301305
junk_git_dir = NULL;
302306

303307
done:
304-
strbuf_reset(&sb);
305-
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
306-
unlink_or_warn(sb.buf);
308+
if (ret || !opts->keep_locked) {
309+
strbuf_reset(&sb);
310+
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
311+
unlink_or_warn(sb.buf);
312+
}
307313
argv_array_clear(&child_env);
308314
strbuf_release(&sb);
309315
strbuf_release(&symref);
@@ -326,6 +332,7 @@ static int add(int ac, const char **av, const char *prefix)
326332
N_("create or reset a branch")),
327333
OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
328334
OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
335+
OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
329336
OPT_END()
330337
};
331338

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)