git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[Patch 1/3] config.c: add conditional include based on worktree.

From
randall.becker@nexbridge.ca <randall.becker@nexbridge.ca>
Date
Jul 12, 2021, 22:31 UTC
Message-ID
<20210712223139.24409-2-randall.becker@nexbridge.ca>
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
Previous: randall.becker@nexbridge.caNext: Johannes Schindelin
Message 2 of 9 in “includeIf series for worktrees”
  1. 0/3 includeIf series for worktreesrandall.becker@nexbridge.ca, Jul 12, 2021
  2. 1/3 config.c: add conditional include based on worktree.randall.becker@nexbridge.ca, Jul 12, 2021
  3. Johannes SchindelinJul 13, 2021
  4. 2/3 Documentation/config.txt: add worktree includeIf conditionals.randall.becker@nexbridge.ca, Jul 12, 2021
  5. Junio C HamanoJul 14, 2021
  6. Randall S. BeckerJul 14, 2021
  7. Junio C HamanoJul 14, 2021
  8. Randall S. BeckerJul 14, 2021
  9. 3/3 t1305: add tests for includeIf:worktree.randall.becker@nexbridge.ca, Jul 12, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.