worktree: add "unlock" command
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Mon, 13 Jun 2016 12:18:25 +0000 (19:18 +0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 8 Jul 2016 22:31:04 +0000 (15:31 -0700)
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-worktree.txt
builtin/worktree.c
contrib/completion/git-completion.bash
t/t2028-worktree-move.sh
index b49b25bf52b37d5fd96251cc3647e1ca04c60b2f..27330c5a39fcdefe80551cab3ec2d1295d115434 100644 (file)
@@ -13,6 +13,7 @@ SYNOPSIS
 'git worktree list' [--porcelain]
 'git worktree lock' [--reason <string>] <worktree>
 'git worktree prune' [-n] [-v] [--expire <expire>]
+'git worktree unlock' <worktree>
 
 DESCRIPTION
 -----------
@@ -73,6 +74,10 @@ prune::
 
 Prune working tree information in $GIT_DIR/worktrees.
 
+unlock::
+
+Unlock a working tree, allowing it to be pruned, moved or deleted.
+
 OPTIONS
 -------
 
index 3b9220f7a6c5c64dd765be6b45740d921871f09f..48774211189b939591428e2d28d9e3d59ed3c1a5 100644 (file)
@@ -16,6 +16,7 @@ static const char * const worktree_usage[] = {
        N_("git worktree list [<options>]"),
        N_("git worktree lock [<options>] <path>"),
        N_("git worktree prune [<options>]"),
+       N_("git worktree unlock <path>"),
        NULL
 };
 
@@ -495,6 +496,31 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
        return 0;
 }
 
+static int unlock_worktree(int ac, const char **av, const char *prefix)
+{
+       struct option options[] = {
+               OPT_END()
+       };
+       struct worktree **worktrees, *wt;
+       int ret;
+
+       ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
+       if (ac != 1)
+               usage_with_options(worktree_usage, options);
+
+       worktrees = get_worktrees();
+       wt = find_worktree(worktrees, prefix, av[0]);
+       if (!wt)
+               die(_("'%s' is not a working tree"), av[0]);
+       if (is_main_worktree(wt))
+               die(_("The main working tree cannot be locked or unlocked"));
+       if (!is_worktree_locked(wt))
+               die(_("'%s' is not locked"), av[0]);
+       ret = unlink_or_warn(git_common_path("worktrees/%s/locked", wt->id));
+       free_worktrees(worktrees);
+       return ret;
+}
+
 int cmd_worktree(int ac, const char **av, const char *prefix)
 {
        struct option options[] = {
@@ -513,5 +539,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
                return list(ac - 1, av + 1, prefix);
        if (!strcmp(av[1], "lock"))
                return lock_worktree(ac - 1, av + 1, prefix);
+       if (!strcmp(av[1], "unlock"))
+               return unlock_worktree(ac - 1, av + 1, prefix);
        usage_with_options(worktree_usage, options);
 }
index f88727dfe771614de04dcfacb24f4bf2c37e8d1e..0e3841d83547e93de386979b1dac471e82fa5e9f 100644 (file)
@@ -2597,7 +2597,7 @@ _git_whatchanged ()
 
 _git_worktree ()
 {
-       local subcommands="add list lock prune"
+       local subcommands="add list lock prune unlock"
        local subcommand="$(__git_find_on_cmdline "$subcommands")"
        if [ -z "$subcommand" ]; then
                __gitcomp "$subcommands"
index 3a8512c03004dfc9c8c63f9388af6904765e6027..8298aaf97f706ea796a12614acafd19636aeaa70 100755 (executable)
@@ -45,4 +45,18 @@ test_expect_success 'lock worktree twice (from the locked worktree)' '
        test_cmp expected .git/worktrees/source/locked
 '
 
+test_expect_success 'unlock main worktree' '
+       test_must_fail git worktree unlock .
+'
+
+test_expect_success 'unlock linked worktree' '
+       git worktree unlock source &&
+       test_path_is_missing .git/worktrees/source/locked
+'
+
+test_expect_success 'unlock worktree twice' '
+       test_must_fail git worktree unlock source &&
+       test_path_is_missing .git/worktrees/source/locked
+'
+
 test_done