Skip to content

Commit 01369fd

Browse files
committed
Merge branch 'sm/worktree-add-lock'
"git worktree add --lock" learned to record why the worktree is locked with a custom message. * sm/worktree-add-lock: worktree: teach `add` to accept --reason <string> with --lock worktree: mark lock strings with `_()` for translation t2400: clean up '"add" worktree with lock' test
2 parents e5cc59c + 0db4961 commit 01369fd

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

Documentation/git-worktree.txt

Lines changed: 2 additions & 2 deletions
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] [--lock] [-b <new-branch>] <path> [<commit-ish>]
12+
'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]] [-b <new-branch>] <path> [<commit-ish>]
1313
'git worktree list' [--porcelain]
1414
'git worktree lock' [--reason <string>] <worktree>
1515
'git worktree move' <worktree> <new-path>
@@ -242,7 +242,7 @@ With `list`, annotate missing working trees as prunable if they are
242242
older than `<time>`.
243243

244244
--reason <string>::
245-
With `lock`, an explanation why the working tree is locked.
245+
With `lock` or with `add --lock`, an explanation why the working tree is locked.
246246

247247
<worktree>::
248248
Working trees can be identified by path, either relative or

builtin/worktree.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct add_opts {
3030
int detach;
3131
int quiet;
3232
int checkout;
33-
int keep_locked;
33+
const char *keep_locked;
3434
};
3535

3636
static int show_only;
@@ -302,10 +302,10 @@ static int add_worktree(const char *path, const char *refname,
302302
* after the preparation is over.
303303
*/
304304
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
305-
if (!opts->keep_locked)
306-
write_file(sb.buf, "initializing");
305+
if (opts->keep_locked)
306+
write_file(sb.buf, "%s", opts->keep_locked);
307307
else
308-
write_file(sb.buf, "added with --lock");
308+
write_file(sb.buf, _("initializing"));
309309

310310
strbuf_addf(&sb_git, "%s/.git", path);
311311
if (safe_create_leading_directories_const(sb_git.buf))
@@ -475,6 +475,8 @@ static int add(int ac, const char **av, const char *prefix)
475475
const char *branch;
476476
const char *new_branch = NULL;
477477
const char *opt_track = NULL;
478+
const char *lock_reason = NULL;
479+
int keep_locked = 0;
478480
struct option options[] = {
479481
OPT__FORCE(&opts.force,
480482
N_("checkout <branch> even if already checked out in other worktree"),
@@ -485,7 +487,9 @@ static int add(int ac, const char **av, const char *prefix)
485487
N_("create or reset a branch")),
486488
OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")),
487489
OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
488-
OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
490+
OPT_BOOL(0, "lock", &keep_locked, N_("keep the new working tree locked")),
491+
OPT_STRING(0, "reason", &lock_reason, N_("string"),
492+
N_("reason for locking")),
489493
OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
490494
OPT_PASSTHRU(0, "track", &opt_track, NULL,
491495
N_("set up tracking mode (see git-branch(1))"),
@@ -500,6 +504,13 @@ static int add(int ac, const char **av, const char *prefix)
500504
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
501505
if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
502506
die(_("-b, -B, and --detach are mutually exclusive"));
507+
if (lock_reason && !keep_locked)
508+
die(_("--reason requires --lock"));
509+
if (lock_reason)
510+
opts.keep_locked = lock_reason;
511+
else if (keep_locked)
512+
opts.keep_locked = _("added with --lock");
513+
503514
if (ac < 1 || ac > 2)
504515
usage_with_options(worktree_usage, options);
505516

t/t2400-worktree-add.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,25 @@ test_expect_success '"add" worktree' '
6767
'
6868

6969
test_expect_success '"add" worktree with lock' '
70-
git rev-parse HEAD >expect &&
7170
git worktree add --detach --lock here-with-lock main &&
71+
test_when_finished "git worktree unlock here-with-lock || :" &&
7272
test -f .git/worktrees/here-with-lock/locked
7373
'
7474

75+
test_expect_success '"add" worktree with lock and reason' '
76+
lock_reason="why not" &&
77+
git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
78+
test_when_finished "git worktree unlock here-with-lock-reason || :" &&
79+
test -f .git/worktrees/here-with-lock-reason/locked &&
80+
echo "$lock_reason" >expect &&
81+
test_cmp expect .git/worktrees/here-with-lock-reason/locked
82+
'
83+
84+
test_expect_success '"add" worktree with reason but no lock' '
85+
test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
86+
test_path_is_missing .git/worktrees/here-with-reason-only/locked
87+
'
88+
7589
test_expect_success '"add" worktree from a subdir' '
7690
(
7791
mkdir sub &&

0 commit comments

Comments
 (0)