git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: randall.becker@nexbridge.ca
To: git@vger.kernel.org
Cc: "Randall S. Becker" <rsbecker@nexbridge.com>
Subject: [Patch 1/3] config.c: add conditional include based on worktree.
Date: Mon, 12 Jul 2021 18:31:37 -0400	[thread overview]
Message-ID: <20210712223139.24409-2-randall.becker@nexbridge.ca> (raw)
In-Reply-To: <20210712223139.24409-1-randall.becker@nexbridge.ca>

From: "Randall S. Becker" <rsbecker@nexbridge.com>

This enhancement extends the [includeIf] semantics to include conditional
inclusion based on whether the conditional is within a specific worktree
or case-insensitive worktree. The [includeIf "worktree:path"] and
[includeIf "worktree/i:path"] and analogous to the gitdir: and gitdir/i:
conditions, respectively.

Signed-off-by: Randall S. Becker <rsbecker@nexbridge.com>
---
 config.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/config.c b/config.c
index f9c400ad30..e2b2364579 100644
--- a/config.c
+++ b/config.c
@@ -272,6 +272,64 @@ static int include_by_gitdir(const struct config_options *opts,
 	return ret;
 }
 
+static int include_by_worktree(const struct config_options *opts,
+			       const char *cond, size_t cond_len, int icase)
+{
+	struct strbuf text = STRBUF_INIT;
+	struct strbuf pattern = STRBUF_INIT;
+	int ret = 0, prefix;
+	const char *worktree;
+	int already_tried_absolute = 0;
+
+	if (the_repository->worktree)
+		worktree = the_repository->worktree;
+	else
+		goto done;
+
+	strbuf_realpath(&text, worktree, 1);
+	strbuf_add(&pattern, cond, cond_len);
+	prefix = prepare_include_condition_pattern(&pattern);
+
+again:
+	if (prefix < 0)
+		goto done;
+
+	if (prefix > 0) {
+		/*
+		 * perform literal matching on the prefix part so that
+		 * any wildcard character in it can't create side effects.
+		 */
+		if (text.len < prefix)
+			goto done;
+		if (!icase && strncmp(pattern.buf, text.buf, prefix))
+			goto done;
+		if (icase && strncasecmp(pattern.buf, text.buf, prefix))
+			goto done;
+	}
+
+	ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
+			 WM_PATHNAME | (icase ? WM_CASEFOLD : 0));
+
+	if (!ret && !already_tried_absolute) {
+		/*
+		 * We've tried e.g. matching worktree:~/work, but if
+		 * ~/work is a symlink to /mnt/storage/work
+		 * strbuf_realpath() will expand it, so the rule won't
+		 * match. Let's match against a
+		 * strbuf_add_absolute_path() version of the path,
+		 * which'll do the right thing
+		 */
+		strbuf_reset(&text);
+		strbuf_add_absolute_path(&text, worktree);
+		already_tried_absolute = 1;
+		goto again;
+	}
+done:
+	strbuf_release(&pattern);
+	strbuf_release(&text);
+	return ret;
+}
+
 static int include_by_branch(const char *cond, size_t cond_len)
 {
 	int flags;
@@ -300,6 +358,11 @@ static int include_condition_is_true(const struct config_options *opts,
 		return include_by_gitdir(opts, cond, cond_len, 0);
 	else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
 		return include_by_gitdir(opts, cond, cond_len, 1);
+	else if (skip_prefix_mem(cond, cond_len, "worktree:", &cond, &cond_len))
+		return include_by_worktree(opts, cond, cond_len, 0);
+	else if (skip_prefix_mem(cond, cond_len, "worktree/i:", &cond,
+				 &cond_len))
+		return include_by_worktree(opts, cond, cond_len, 1);
 	else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
 		return include_by_branch(cond, cond_len);
 
-- 
2.32.0


  reply	other threads:[~2021-07-12 22:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-12 22:31 [Patch 0/3] includeIf series for worktrees randall.becker
2021-07-12 22:31 ` randall.becker [this message]
2021-07-13 13:03   ` [Patch 1/3] config.c: add conditional include based on worktree Johannes Schindelin
2021-07-12 22:31 ` [Patch 2/3] Documentation/config.txt: add worktree includeIf conditionals randall.becker
2021-07-14  1:04   ` Junio C Hamano
2021-07-14 13:46     ` Randall S. Becker
2021-07-14 17:10       ` Junio C Hamano
2021-07-14 17:30         ` Randall S. Becker
2021-07-12 22:31 ` [Patch 3/3] t1305: add tests for includeIf:worktree randall.becker

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=20210712223139.24409-2-randall.becker@nexbridge.ca \
    --to=randall.becker@nexbridge.ca \
    --cc=git@vger.kernel.org \
    --cc=rsbecker@nexbridge.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).