[Patch 0/3] includeIf series for worktrees

STALE1884d

5 messages, 2 authors, 2021-07-13 · open the first message on its own page

[Patch 0/3] includeIf series for worktrees

From: <hidden>
Date: 2021-07-12 22:32:03

From: "Randall S. Becker" <redacted>

Add support for includeIf with a worktree: prefix. This permits
conditional includes that are specific to individual worktrees.
The set of patches differ slightly from gitdir: as worktrees to not
have .git/ directories, so matching repo/ is somewhat problematic -
suggestings on dealing with that case are more than welcome.

Also added worktree test condition to verify that onbranch: works
in a worktree scenario as different from the main repository.

Randall S. Becker (3):
  config.c: add conditional include based on worktree.
  Documentation/config.txt: add worktree includeIf conditionals.
  t1305: add tests for includeIf:worktree.

 Documentation/config.txt  | 11 +++++-
 config.c                  | 63 ++++++++++++++++++++++++++++++
 t/t1305-config-include.sh | 81 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+), 1 deletion(-)

-- 
2.32.0

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

From: <hidden>
Date: 2021-07-12 22:32:05

From: "Randall S. Becker" <redacted>

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 <redacted>
---
 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

[Patch 2/3] Documentation/config.txt: add worktree includeIf conditionals.

From: <hidden>
Date: 2021-07-12 22:32:06

From: "Randall S. Becker" <redacted>

Documentation of the worktree and worktree/i conditionals is add based on
gitdir rules except that the trailing / form of the path is not supported.

Signed-off-by: Randall S. Becker <redacted>
---
 Documentation/config.txt | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index bf82766a6a..7e951937ae 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -143,7 +143,16 @@ refer to linkgit:gitignore[5] for details. For convenience:
 
 `gitdir/i`::
 	This is the same as `gitdir` except that matching is done
-	case-insensitively (e.g. on case-insensitive file systems)
+	case-insensitively (e.g. on case-insensitive file systems).
+
+`worktree`::
+	This is similar to `gitdir` except that matching is done with
+	the path of a worktree instead of the main repository. Unlike
+	`gitdir`, the trailing / form of the worktree path is not supported.
+
+`worktree/i`::
+	This is the same as `worktree` except that matching is done
+	case-insensitively (e.g. on case-insensitive file systems).
 
 `onbranch`::
 	The data that follows the keyword `onbranch:` is taken to be a
-- 
2.32.0

[Patch 3/3] t1305: add tests for includeIf:worktree.

From: <hidden>
Date: 2021-07-12 22:32:07

From: "Randall S. Becker" <redacted>

The tests are a subset of those for gitdir:, taking into account that
the worktree: form does not support the trailing / at this time in
pattern matches. Some resets of the .git/config file are done to restrict
the set of includeIf paths being evaluated that conflict with prior
subtests.

Signed-off-by: Randall S. Becker <redacted>
---
 t/t1305-config-include.sh | 81 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh
index ccbb116c01..fe1ad106c3 100755
--- a/t/t1305-config-include.sh
+++ b/t/t1305-config-include.sh
@@ -309,6 +309,69 @@ test_expect_success SYMLINKS 'conditional include, gitdir matching symlink, icas
 	)
 '
 
+test_expect_success 'conditional worktree include, unanchored' '
+	(
+		cd foo &&
+		# Must add a commit for worktree add
+		git commit --allow-empty --allow-empty-message &&
+		sed -i "/includeIf/,\$d" .git/config &&
+		git worktree add ../foo.wt &&
+		echo "[includeIf \"worktree:foo.wt\"]path=bar" >>.git/config &&
+		echo "[test]one=1" >.git/bar &&
+		cd ../foo.wt &&
+		echo 1 >expect &&
+		git config test.one >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'conditional worktree include, $HOME expansion' '
+	(
+		cd foo &&
+		echo "[includeIf \"worktree:~/foo.wt\"]path=bar2" >>.git/config &&
+		echo "[test]two=2" >.git/bar2 &&
+		cd ../foo.wt &&
+		echo 2 >expect &&
+		git config test.two >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'conditional worktree include, full pattern' '
+	(
+		cd foo &&
+		echo "[includeIf \"worktree:**/foo.wt\"]path=bar3" >>.git/config &&
+		echo "[test]three=3" >.git/bar3 &&
+		cd ../foo.wt &&
+		echo 3 >expect &&
+		git config test.three >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'conditional worktree include, relative path' '
+	echo "[includeIf \"worktree:./foo.wt\"]path=bar4" >>.gitconfig &&
+	echo "[test]four=4" >bar4 &&
+	(
+		cd foo.wt &&
+		echo 4 >expect &&
+		git config test.four >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'conditional worktree include, both unanchored, icase' '
+	(
+		cd foo &&
+		echo "[includeIf \"worktree/i:FOO.WT\"]path=bar5" >>.git/config &&
+		echo "[test]five=5" >.git/bar5 &&
+		cd ../foo.wt &&
+		echo 5 >expect &&
+		git config test.five >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'conditional include, onbranch' '
 	echo "[includeIf \"onbranch:foo-branch\"]path=bar9" >>.git/config &&
 	echo "[test]nine=9" >.git/bar9 &&
@@ -348,6 +411,24 @@ test_expect_success 'conditional include, onbranch, implicit /** for /' '
 	test_cmp expect actual
 '
 
+test_expect_success 'conditional worktree include, onbranch' '
+	(
+		cd foo &&
+		sed -i "/includeIf/,\$d" .git/config &&
+		echo "[includeIf \"onbranch:foo.wt2\"]path=bar12" >>.git/config &&
+		echo "[test]twelve=12" >.git/bar12
+	) &&
+	(
+		cd foo.wt &&
+		git checkout -b main &&
+		test_must_fail git config test.twelve &&
+		git checkout -b foo.wt2 &&
+		echo 12 >expect &&
+		git config test.twelve >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'include cycles are detected' '
 	git init --bare cycle &&
 	git -C cycle config include.path cycle &&
-- 
2.32.0

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

From: Johannes Schindelin <hidden>
Date: 2021-07-13 13:03:38

Hi Randall,

On Mon, 12 Jul 2021, randall.becker@nexbridge.ca wrote:
quoted hunk
@@ -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);
Thank you for not forgetting the `/i` case.

Ciao,
Dscho
 	else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
 		return include_by_branch(cond, cond_len);

--
2.32.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help