git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: git@vger.kernel.org
Cc: "Duy Nguyen" <pclouds@gmail.com>,
	"Jonathan Müller" <jonathanmueller.dev@gmail.com>,
	"Shourya Shukla" <shouryashukla.oo@gmail.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>
Subject: [PATCH 7/8] worktree: generalize candidate worktree path validation
Date: Mon,  8 Jun 2020 02:23:55 -0400	[thread overview]
Message-ID: <20200608062356.40264-8-sunshine@sunshineco.com> (raw)
In-Reply-To: <20200608062356.40264-1-sunshine@sunshineco.com>

"git worktree add" checks that the specified path is a valid location
for a new worktree by ensuring that the path does not already exist and
is not already registered to another worktree (a path can be registered
but missing, for instance, if it resides on removable media). Since "git
worktree add" is not the only command which should perform such
validation ("git worktree move" ought to also), generalize the the
validation function for use by other callers, as well.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 builtin/worktree.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index eebd77c46d..7c0637234e 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -274,34 +274,33 @@ static const char *worktree_basename(const char *path, int *olen)
 	return name;
 }
 
-static void validate_worktree_add(const char *path, const struct add_opts *opts)
+/* check that path is viable location for worktree */
+static void check_candidate_path(const char *path,
+				 int force,
+				 struct worktree **worktrees,
+				 const char *cmd)
 {
-	struct worktree **worktrees;
 	struct worktree *wt;
 	int locked;
 
 	if (file_exists(path) && !is_empty_dir(path))
 		die(_("'%s' already exists"), path);
 
-	worktrees = get_worktrees(0);
 	wt = find_worktree_by_path(worktrees, path);
 	if (!wt)
-		goto done;
+		return;
 
 	locked = !!worktree_lock_reason(wt);
-	if ((!locked && opts->force) || (locked && opts->force > 1)) {
+	if ((!locked && force) || (locked && force > 1)) {
 		if (delete_git_dir(wt->id))
-		    die(_("unable to re-add worktree '%s'"), path);
-		goto done;
+		    die(_("unusable worktree destination '%s'"), path);
+		return;
 	}
 
 	if (locked)
-		die(_("'%s' is a missing but locked worktree;\nuse 'add -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path);
+		die(_("'%s' is a missing but locked worktree;\nuse '%s -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), cmd, path);
 	else
-		die(_("'%s' is a missing but already registered worktree;\nuse 'add -f' to override, or 'prune' or 'remove' to clear"), path);
-
-done:
-	free_worktrees(worktrees);
+		die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), cmd, path);
 }
 
 static int add_worktree(const char *path, const char *refname,
@@ -318,8 +317,12 @@ static int add_worktree(const char *path, const char *refname,
 	struct commit *commit = NULL;
 	int is_branch = 0;
 	struct strbuf sb_name = STRBUF_INIT;
+	struct worktree **worktrees;
 
-	validate_worktree_add(path, opts);
+	worktrees = get_worktrees(0);
+	check_candidate_path(path, opts->force, worktrees, "add");
+	free_worktrees(worktrees);
+	worktrees = NULL;
 
 	/* is 'refname' a branch or commit? */
 	if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
-- 
2.27.0.290.gba653c62da


  parent reply	other threads:[~2020-06-08  6:25 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-08  6:23 [PATCH 0/8] worktree: tighten duplicate path detection Eric Sunshine
2020-06-08  6:23 ` [PATCH 1/8] worktree: factor out repeated string literal Eric Sunshine
2020-06-08 19:38   ` Shourya Shukla
2020-06-08 21:41     ` Eric Sunshine
2020-06-08  6:23 ` [PATCH 2/8] worktree: prune corrupted worktree even if locked Eric Sunshine
2020-06-08 21:23   ` Junio C Hamano
2020-06-09 17:34     ` Eric Sunshine
2020-06-08  6:23 ` [PATCH 3/8] worktree: give "should be pruned?" function more meaningful name Eric Sunshine
2020-06-08 21:25   ` Junio C Hamano
2020-06-08  6:23 ` [PATCH 4/8] worktree: make high-level pruning re-usable Eric Sunshine
2020-06-08 21:29   ` Junio C Hamano
2020-06-08  6:23 ` [PATCH 5/8] worktree: prune duplicate entries referencing same worktree path Eric Sunshine
2020-06-08  6:23 ` [PATCH 6/8] worktree: prune linked worktree referencing main " Eric Sunshine
2020-06-08 21:59   ` Junio C Hamano
2020-06-09 17:38     ` Eric Sunshine
2020-06-08  6:23 ` Eric Sunshine [this message]
2020-06-08 22:02   ` [PATCH 7/8] worktree: generalize candidate worktree path validation Junio C Hamano
2020-06-08  6:23 ` [PATCH 8/8] worktree: make "move" refuse to move atop missing registered worktree Eric Sunshine
2020-06-08 15:19   ` Eric Sunshine
2020-06-08 22:06   ` Junio C Hamano

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20200608062356.40264-8-sunshine@sunshineco.com \
    --to=sunshine@sunshineco.com \
    --cc=git@vger.kernel.org \
    --cc=jonathanmueller.dev@gmail.com \
    --cc=pclouds@gmail.com \
    --cc=shouryashukla.oo@gmail.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).