diff options
author | Junio C Hamano <gitster@pobox.com> | 2016-07-28 10:34:41 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-07-28 10:34:42 -0700 |
commit | 2c608e0f7c1f0fb9fb3e42eb737d7b8543e11400 (patch) | |
tree | 2cec172b7ded4719fa38dc4df8ab5183ecea12af /builtin/worktree.c | |
parent | d0b6966e3dc0a1aca8b7aa89dd0f95cda50d21f2 (diff) | |
parent | 080739ba1db7897dde53427937acc011ea98a006 (diff) | |
download | git-2c608e0f7c1f0fb9fb3e42eb737d7b8543e11400.tar.gz git-2c608e0f7c1f0fb9fb3e42eb737d7b8543e11400.tar.xz |
Merge branch 'nd/worktree-lock'
"git worktree prune" protected worktrees that are marked as
"locked" by creating a file in a known location. "git worktree"
command learned a dedicated command pair to create and remove such
a file, so that the users do not have to do this with editor.
* nd/worktree-lock:
worktree.c: find_worktree() search by path suffix
worktree: add "unlock" command
worktree: add "lock" command
worktree.c: add is_worktree_locked()
worktree.c: add is_main_worktree()
worktree.c: add find_worktree()
Diffstat (limited to 'builtin/worktree.c')
-rw-r--r-- | builtin/worktree.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c index cce555cbb..5a41788ed 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -14,7 +14,9 @@ static const char * const worktree_usage[] = { N_("git worktree add [<options>] <path> [<branch>]"), N_("git worktree list [<options>]"), + N_("git worktree lock [<options>] <path>"), N_("git worktree prune [<options>]"), + N_("git worktree unlock <path>"), NULL }; @@ -462,6 +464,66 @@ static int list(int ac, const char **av, const char *prefix) return 0; } +static int lock_worktree(int ac, const char **av, const char *prefix) +{ + const char *reason = "", *old_reason; + struct option options[] = { + OPT_STRING(0, "reason", &reason, N_("string"), + N_("reason for locking")), + OPT_END() + }; + struct worktree **worktrees, *wt; + + 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")); + + old_reason = is_worktree_locked(wt); + if (old_reason) { + if (*old_reason) + die(_("'%s' is already locked, reason: %s"), + av[0], old_reason); + die(_("'%s' is already locked"), av[0]); + } + + write_file(git_common_path("worktrees/%s/locked", wt->id), + "%s", reason); + free_worktrees(worktrees); + 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[] = { @@ -478,5 +540,9 @@ int cmd_worktree(int ac, const char **av, const char *prefix) return prune(ac - 1, av + 1, prefix); if (!strcmp(av[1], "list")) 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); } |