git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Stephen Manz via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Eric Sunshine <sunshine@sunshineco.com>,
	Stephen Manz <smanz@alum.mit.edu>,
	Stephen Manz <smanz@alum.mit.edu>
Subject: [PATCH v4 3/3] worktree: teach `add` to accept --reason <string> with --lock
Date: Thu, 15 Jul 2021 02:32:30 +0000	[thread overview]
Message-ID: <4b6bb50d3d66ce8b033c475fbb9d3bda672e2612.1626316350.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.992.v4.git.1626316350.gitgitgadget@gmail.com>

From: Stephen Manz <smanz@alum.mit.edu>

The default reason stored in the lock file, "added with --lock",
is unlikely to be what the user would have given in a separate
`git worktree lock` command. Allowing `--reason` to be specified
along with `--lock` when adding a working tree gives the user control
over the reason for locking without needing a second command.

Signed-off-by: Stephen Manz <smanz@alum.mit.edu>
---
 Documentation/git-worktree.txt |  4 ++--
 builtin/worktree.c             | 21 ++++++++++++++++-----
 t/t2400-worktree-add.sh        | 14 ++++++++++++++
 3 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 66e67e6cbfa..8a7cbdd19c1 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -9,7 +9,7 @@ git-worktree - Manage multiple working trees
 SYNOPSIS
 --------
 [verse]
-'git worktree add' [-f] [--detach] [--checkout] [--lock] [-b <new-branch>] <path> [<commit-ish>]
+'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]] [-b <new-branch>] <path> [<commit-ish>]
 'git worktree list' [--porcelain]
 'git worktree lock' [--reason <string>] <worktree>
 'git worktree move' <worktree> <new-path>
@@ -242,7 +242,7 @@ With `list`, annotate missing working trees as prunable if they are
 older than `<time>`.
 
 --reason <string>::
-	With `lock`, an explanation why the working tree is locked.
+	With `lock` or with `add --lock`, an explanation why the working tree is locked.
 
 <worktree>::
 	Working trees can be identified by path, either relative or
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 4829b9507ff..0d0a80da61f 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -30,7 +30,7 @@ struct add_opts {
 	int detach;
 	int quiet;
 	int checkout;
-	int keep_locked;
+	const char *keep_locked;
 };
 
 static int show_only;
@@ -302,10 +302,10 @@ static int add_worktree(const char *path, const char *refname,
 	 * after the preparation is over.
 	 */
 	strbuf_addf(&sb, "%s/locked", sb_repo.buf);
-	if (!opts->keep_locked)
-		write_file(sb.buf, _("initializing"));
+	if (opts->keep_locked)
+		write_file(sb.buf, "%s", opts->keep_locked);
 	else
-		write_file(sb.buf, _("added with --lock"));
+		write_file(sb.buf, _("initializing"));
 
 	strbuf_addf(&sb_git, "%s/.git", path);
 	if (safe_create_leading_directories_const(sb_git.buf))
@@ -475,6 +475,8 @@ static int add(int ac, const char **av, const char *prefix)
 	const char *branch;
 	const char *new_branch = NULL;
 	const char *opt_track = NULL;
+	const char *lock_reason = NULL;
+	int keep_locked = 0;
 	struct option options[] = {
 		OPT__FORCE(&opts.force,
 			   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)
 			   N_("create or reset a branch")),
 		OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")),
 		OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
-		OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
+		OPT_BOOL(0, "lock", &keep_locked, N_("keep the new working tree locked")),
+		OPT_STRING(0, "reason", &lock_reason, N_("string"),
+			   N_("reason for locking")),
 		OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
 		OPT_PASSTHRU(0, "track", &opt_track, NULL,
 			     N_("set up tracking mode (see git-branch(1))"),
@@ -500,6 +504,13 @@ static int add(int ac, const char **av, const char *prefix)
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
 	if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
 		die(_("-b, -B, and --detach are mutually exclusive"));
+	if (lock_reason && !keep_locked)
+		die(_("--reason requires --lock"));
+	if (lock_reason)
+		opts.keep_locked = lock_reason;
+	else if (keep_locked)
+		opts.keep_locked = _("added with --lock");
+
 	if (ac < 1 || ac > 2)
 		usage_with_options(worktree_usage, options);
 
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index 93d3795cab9..37ad79470fb 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -72,6 +72,20 @@ test_expect_success '"add" worktree with lock' '
 	test -f .git/worktrees/here-with-lock/locked
 '
 
+test_expect_success '"add" worktree with lock and reason' '
+	lock_reason="why not" &&
+	git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
+	test_when_finished "git worktree unlock here-with-lock-reason || :" &&
+	test -f .git/worktrees/here-with-lock-reason/locked &&
+	echo "$lock_reason" >expect &&
+	test_cmp expect .git/worktrees/here-with-lock-reason/locked
+'
+
+test_expect_success '"add" worktree with reason but no lock' '
+	test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
+	test_path_is_missing .git/worktrees/here-with-reason-only/locked
+'
+
 test_expect_success '"add" worktree from a subdir' '
 	(
 		mkdir sub &&
-- 
gitgitgadget

  parent reply	other threads:[~2021-07-15  2:32 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-06  5:47 [PATCH] worktree: teach `add` to accept --reason <string> with --lock Stephen Manz via GitGitGadget
2021-07-06  6:19 ` Eric Sunshine
2021-07-06 19:42   ` Junio C Hamano
2021-07-09  6:11     ` Eric Sunshine
2021-07-09 15:23       ` Junio C Hamano
2021-07-10  7:11         ` Eric Sunshine
2021-07-08 15:50 ` [PATCH v2 0/3] worktree: teach add to accept --reason " Stephen Manz via GitGitGadget
2021-07-08 15:50   ` [PATCH v2 1/3] t2400: remove unneeded `git rev-parse` from '"add" worktree with lock' test Stephen Manz via GitGitGadget
2021-07-08 15:50   ` [PATCH v2 2/3] worktree: default lock string should be marked with `_()` for translation Stephen Manz via GitGitGadget
2021-07-09  6:19     ` Eric Sunshine
2021-07-09 15:58       ` Junio C Hamano
2021-07-08 15:50   ` [PATCH v2 3/3] worktree: teach `add` to accept --reason <string> with --lock Stephen Manz via GitGitGadget
2021-07-09  7:45   ` [PATCH v2 0/3] worktree: teach add to accept --reason " Eric Sunshine
2021-07-09 16:03     ` Junio C Hamano
2021-07-10  7:15       ` Eric Sunshine
2021-07-11  0:27   ` [PATCH v3 " Stephen Manz via GitGitGadget
2021-07-11  0:27     ` [PATCH v3 1/3] t2400: clean up '"add" worktree with lock' test Stephen Manz via GitGitGadget
2021-07-11  0:27     ` [PATCH v3 2/3] worktree: mark lock strings with `_()` for translation Stephen Manz via GitGitGadget
2021-07-11  0:27     ` [PATCH v3 3/3] worktree: teach `add` to accept --reason <string> with --lock Stephen Manz via GitGitGadget
2021-07-13  3:37       ` Eric Sunshine
2021-07-13  3:47     ` [PATCH v3 0/3] worktree: teach add to accept --reason " Eric Sunshine
2021-07-15  2:32     ` [PATCH v4 " Stephen Manz via GitGitGadget
2021-07-15  2:32       ` [PATCH v4 1/3] t2400: clean up '"add" worktree with lock' test Stephen Manz via GitGitGadget
2021-07-15  2:32       ` [PATCH v4 2/3] worktree: mark lock strings with `_()` for translation Stephen Manz via GitGitGadget
2021-07-15  2:32       ` Stephen Manz via GitGitGadget [this message]
2021-07-15 17:17       ` [PATCH v4 0/3] worktree: teach add to accept --reason with --lock Eric Sunshine
2021-07-17  0:14         ` Eric Sunshine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4b6bb50d3d66ce8b033c475fbb9d3bda672e2612.1626316350.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=smanz@alum.mit.edu \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).