worktree: add "unlock" command

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jun 13, 2016 at 19:18 UTC 6d308627cae4d34c058591de73ce14a52b79cf4e
4 files changed +48 -1
Documentation/git-worktree.txt
+5
@@ -13,6 +13,7 @@ SYNOPSIS
13 'git worktree list' [--porcelain]
14 'git worktree lock' [--reason <string>] <worktree>
15 'git worktree prune' [-n] [-v] [--expire <expire>]
16 +'git worktree unlock' <worktree>
17
18 DESCRIPTION
19 -----------
@@ -73,6 +74,10 @@ prune::
74
75 Prune working tree information in $GIT_DIR/worktrees.
76
77 +unlock::
78 +
79 +Unlock a working tree, allowing it to be pruned, moved or deleted.
80 +
81 OPTIONS
82 -------
83
builtin/worktree.c
+28
@@ -16,6 +16,7 @@ static const char * const worktree_usage[] = {
16 N_("git worktree list [<options>]"),
17 N_("git worktree lock [<options>] <path>"),
18 N_("git worktree prune [<options>]"),
19 + N_("git worktree unlock <path>"),
20 NULL
21 };
22
@@ -495,6 +496,31 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
496 return 0;
497 }
498
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 +
524 int cmd_worktree(int ac, const char **av, const char *prefix)
525 {
526 struct option options[] = {
@@ -513,5 +539,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
539 return list(ac - 1, av + 1, prefix);
540 if (!strcmp(av[1], "lock"))
541 return lock_worktree(ac - 1, av + 1, prefix);
542 + if (!strcmp(av[1], "unlock"))
543 + return unlock_worktree(ac - 1, av + 1, prefix);
544 usage_with_options(worktree_usage, options);
545 }
contrib/completion/git-completion.bash
+1 -1
@@ -2597,7 +2597,7 @@ _git_whatchanged ()
2597
2598 _git_worktree ()
2599 {
2600 - local subcommands="add list lock prune"
2600 + local subcommands="add list lock prune unlock"
2601 local subcommand="$(__git_find_on_cmdline "$subcommands")"
2602 if [ -z "$subcommand" ]; then
2603 __gitcomp "$subcommands"
t/t2028-worktree-move.sh
+14
@@ -45,4 +45,18 @@ test_expect_success 'lock worktree twice (from the locked worktree)' '
45 test_cmp expected .git/worktrees/source/locked
46 '
47
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 +
62 test_done