Skip to content

Commit 6d30862

Browse files
pcloudsgitster
authored andcommitted
worktree: add "unlock" command
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 58142c0 commit 6d30862

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

Documentation/git-worktree.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SYNOPSIS
1313
'git worktree list' [--porcelain]
1414
'git worktree lock' [--reason <string>] <worktree>
1515
'git worktree prune' [-n] [-v] [--expire <expire>]
16+
'git worktree unlock' <worktree>
1617

1718
DESCRIPTION
1819
-----------
@@ -73,6 +74,10 @@ prune::
7374

7475
Prune working tree information in $GIT_DIR/worktrees.
7576

77+
unlock::
78+
79+
Unlock a working tree, allowing it to be pruned, moved or deleted.
80+
7681
OPTIONS
7782
-------
7883

builtin/worktree.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static const char * const worktree_usage[] = {
1616
N_("git worktree list [<options>]"),
1717
N_("git worktree lock [<options>] <path>"),
1818
N_("git worktree prune [<options>]"),
19+
N_("git worktree unlock <path>"),
1920
NULL
2021
};
2122

@@ -495,6 +496,31 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
495496
return 0;
496497
}
497498

499+
static int unlock_worktree(int ac, const char **av, const char *prefix)
500+
{
501+
struct option options[] = {
502+
OPT_END()
503+
};
504+
struct worktree **worktrees, *wt;
505+
int ret;
506+
507+
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
508+
if (ac != 1)
509+
usage_with_options(worktree_usage, options);
510+
511+
worktrees = get_worktrees();
512+
wt = find_worktree(worktrees, prefix, av[0]);
513+
if (!wt)
514+
die(_("'%s' is not a working tree"), av[0]);
515+
if (is_main_worktree(wt))
516+
die(_("The main working tree cannot be locked or unlocked"));
517+
if (!is_worktree_locked(wt))
518+
die(_("'%s' is not locked"), av[0]);
519+
ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
520+
free_worktrees(worktrees);
521+
return ret;
522+
}
523+
498524
int cmd_worktree(int ac, const char **av, const char *prefix)
499525
{
500526
struct option options[] = {
@@ -513,5 +539,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
513539
return list(ac - 1, av + 1, prefix);
514540
if (!strcmp(av[1], "lock"))
515541
return lock_worktree(ac - 1, av + 1, prefix);
542+
if (!strcmp(av[1], "unlock"))
543+
return unlock_worktree(ac - 1, av + 1, prefix);
516544
usage_with_options(worktree_usage, options);
517545
}

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2597,7 +2597,7 @@ _git_whatchanged ()
25972597

25982598
_git_worktree ()
25992599
{
2600-
local subcommands="add list lock prune"
2600+
local subcommands="add list lock prune unlock"
26012601
local subcommand="$(__git_find_on_cmdline "$subcommands")"
26022602
if [ -z "$subcommand" ]; then
26032603
__gitcomp "$subcommands"

t/t2028-worktree-move.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ test_expect_success 'lock worktree twice (from the locked worktree)' '
4545
test_cmp expected .git/worktrees/source/locked
4646
'
4747

48+
test_expect_success 'unlock main worktree' '
49+
test_must_fail git worktree unlock .
50+
'
51+
52+
test_expect_success 'unlock linked worktree' '
53+
git worktree unlock source &&
54+
test_path_is_missing .git/worktrees/source/locked
55+
'
56+
57+
test_expect_success 'unlock worktree twice' '
58+
test_must_fail git worktree unlock source &&
59+
test_path_is_missing .git/worktrees/source/locked
60+
'
61+
4862
test_done

0 commit comments

Comments
 (0)