All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH/RFC] The new IncludeIf facility doesn't DWIM when the repo is symlinked
@ 2017-05-15 15:20 Ævar Arnfjörð Bjarmason
  2017-05-15 18:30 ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2017-05-15 15:20 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy, Git Mailing List

I have a ~/git_tree in my homedir that's symlinked to an external
drive, and doing "gitdir:~/git_tree/" doesn't work, because instead of
matching against ~/git_tree it's matched against
/mnt/some-other-storage/.

Here's a WIP patch that makes this work for me, any reason I shouldn't
finish this up & that we shouldn't be doing this? The doc don't say
"we'll only match gitdir against the absolute resolved path" or
anything like that, so until I checked out the implementation I didn't
realize what was going on:

diff --git a/config.c b/config.c
index b4a3205da3..606acaa3f1 100644
--- a/config.c
+++ b/config.c
@@ -214,6 +214,7 @@ static int include_by_gitdir(const struct
config_options *opts,
        struct strbuf pattern = STRBUF_INIT;
        int ret = 0, prefix;
        const char *git_dir;
+       int tried_absolute = 0;

        if (opts->git_dir)
                git_dir = opts->git_dir;
@@ -226,6 +227,7 @@ static int include_by_gitdir(const struct
config_options *opts,
        strbuf_add(&pattern, cond, cond_len);
        prefix = prepare_include_condition_pattern(&pattern);

+again:
        if (prefix < 0)
                goto done;

@@ -245,6 +247,12 @@ static int include_by_gitdir(const struct
config_options *opts,
        ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
                         icase ? WM_CASEFOLD : 0, NULL);

+       if (!ret && !tried_absolute) {
+               tried_absolute = 1;
+               strbuf_reset(&text);
+               strbuf_add_absolute_path(&text, git_dir);
+               goto again;
+       }
 done:
        strbuf_release(&pattern);
        strbuf_release(&text);

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH/RFC] The new IncludeIf facility doesn't DWIM when the repo is symlinked
  2017-05-15 15:20 [PATCH/RFC] The new IncludeIf facility doesn't DWIM when the repo is symlinked Ævar Arnfjörð Bjarmason
@ 2017-05-15 18:30 ` Ævar Arnfjörð Bjarmason
  2017-05-15 19:15   ` [PATCH] config: match both symlink & realpath versions in IncludeIf.gitdir:* Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2017-05-15 18:30 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy, Git Mailing List

On Mon, May 15, 2017 at 5:20 PM, Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
> I have a ~/git_tree in my homedir that's symlinked to an external
> drive, and doing "gitdir:~/git_tree/" doesn't work, because instead of
> matching against ~/git_tree it's matched against
> /mnt/some-other-storage/.
>
> Here's a WIP patch that makes this work for me, any reason I shouldn't
> finish this up & that we shouldn't be doing this? The doc don't say
> "we'll only match gitdir against the absolute resolved path" or
> anything like that, so until I checked out the implementation I didn't
> realize what was going on:

On closer inspection I can see that this was actually broken in
86f9515708 ("config: resolve symlinks in conditional include's
patterns", 2017-04-05), but works in the initial addition of
IncludeIf.

> diff --git a/config.c b/config.c
> index b4a3205da3..606acaa3f1 100644
> --- a/config.c
> +++ b/config.c
> @@ -214,6 +214,7 @@ static int include_by_gitdir(const struct
> config_options *opts,
>         struct strbuf pattern = STRBUF_INIT;
>         int ret = 0, prefix;
>         const char *git_dir;
> +       int tried_absolute = 0;
>
>         if (opts->git_dir)
>                 git_dir = opts->git_dir;
> @@ -226,6 +227,7 @@ static int include_by_gitdir(const struct
> config_options *opts,
>         strbuf_add(&pattern, cond, cond_len);
>         prefix = prepare_include_condition_pattern(&pattern);
>
> +again:
>         if (prefix < 0)
>                 goto done;
>
> @@ -245,6 +247,12 @@ static int include_by_gitdir(const struct
> config_options *opts,
>         ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
>                          icase ? WM_CASEFOLD : 0, NULL);
>
> +       if (!ret && !tried_absolute) {
> +               tried_absolute = 1;
> +               strbuf_reset(&text);
> +               strbuf_add_absolute_path(&text, git_dir);
> +               goto again;
> +       }
>  done:
>         strbuf_release(&pattern);
>         strbuf_release(&text);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] config: match both symlink & realpath versions in IncludeIf.gitdir:*
  2017-05-15 18:30 ` Ævar Arnfjörð Bjarmason
@ 2017-05-15 19:15   ` Ævar Arnfjörð Bjarmason
  2017-05-16  1:15     ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2017-05-15 19:15 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy,
	Torsten Bögershausen,
	Ævar Arnfjörð Bjarmason

Change the conditional inclusion mechanism to support
e.g. gitdir:~/git_tree/repo where ~/git_tree is a symlink to to
/mnt/stuff/repo.

This worked in the initial version of this facility[1], but regressed
later in the series while solving a related bug[2].

Now gitdir: will match against the symlinked
path (e.g. gitdir:~/git_tree/repo) in addition to the current
/mnt/stuff/repo path.

Since this is already in a release version note in the documentation
that this behavior changed, so users who expect their configuration to
work on both v2.13.0 and some future version of git with this fix
aren't utterly confused.

1. commit 3efd0bedc6 ("config: add conditional include", 2017-03-01)
2. commit 86f9515708 ("config: resolve symlinks in conditional
   include's patterns", 2017-04-05)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---

Here's a non-RFC patch to fix this bug.

 Documentation/config.txt  | 11 +++++++++++
 config.c                  | 16 ++++++++++++++++
 t/t1305-config-include.sh | 23 +++++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 475e874d51..137502a289 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -140,6 +140,17 @@ A few more notes on matching via `gitdir` and `gitdir/i`:
 
  * Symlinks in `$GIT_DIR` are not resolved before matching.
 
+ * Both the symlink & realpath versions of paths will be matched
+   outside of `$GIT_DIR`. E.g. if ~/git is a symlink to
+   /mnt/storage/git, both `gitdir:~/git` and `gitdir:/mnt/storage/git`
+   will match.
+
+   This was not the case in the initial release of this feature in
+   v2.13.0, which only matched the realpath version. Configuration
+   that wants to be compatible with the initial release of this
+   feature needs to either specify only the realpath version, or both
+   versions.
+
  * Note that "../" is not special and will match literally, which is
    unlikely what you want.
 
diff --git a/config.c b/config.c
index b4a3205da3..0498746112 100644
--- a/config.c
+++ b/config.c
@@ -214,6 +214,7 @@ static int include_by_gitdir(const struct config_options *opts,
 	struct strbuf pattern = STRBUF_INIT;
 	int ret = 0, prefix;
 	const char *git_dir;
+	int retry = 1;
 
 	if (opts->git_dir)
 		git_dir = opts->git_dir;
@@ -226,6 +227,7 @@ static int include_by_gitdir(const struct config_options *opts,
 	strbuf_add(&pattern, cond, cond_len);
 	prefix = prepare_include_condition_pattern(&pattern);
 
+again:
 	if (prefix < 0)
 		goto done;
 
@@ -245,6 +247,20 @@ static int include_by_gitdir(const struct config_options *opts,
 	ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
 			 icase ? WM_CASEFOLD : 0, NULL);
 
+	if (!ret && retry) {
+		/*
+		 * We've tried e.g. matching gitdir:~/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, git_dir);
+		retry = 0;
+		goto again;
+	}
 done:
 	strbuf_release(&pattern);
 	strbuf_release(&text);
diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh
index 933915ec06..d9d2f545a4 100755
--- a/t/t1305-config-include.sh
+++ b/t/t1305-config-include.sh
@@ -273,6 +273,29 @@ test_expect_success SYMLINKS 'conditional include, relative path with symlinks'
 	)
 '
 
+test_expect_success SYMLINKS 'conditional include, gitdir matching symlink' '
+	ln -s foo bar &&
+	(
+		cd bar &&
+		echo "[includeIf \"gitdir:bar/\"]path=bar7" >>.git/config &&
+		echo "[test]seven=7" >.git/bar7 &&
+		echo 7 >expect &&
+		git config test.seven >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success SYMLINKS 'conditional include, gitdir matching symlink, icase' '
+	(
+		cd bar &&
+		echo "[includeIf \"gitdir/i:BAR/\"]path=bar8" >>.git/config &&
+		echo "[test]eight=8" >.git/bar8 &&
+		echo 8 >expect &&
+		git config test.eight >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'include cycles are detected' '
 	cat >.gitconfig <<-\EOF &&
 	[test]value = gitconfig
-- 
2.13.0.rc2.291.g57267f2277


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] config: match both symlink & realpath versions in IncludeIf.gitdir:*
  2017-05-15 19:15   ` [PATCH] config: match both symlink & realpath versions in IncludeIf.gitdir:* Ævar Arnfjörð Bjarmason
@ 2017-05-16  1:15     ` Junio C Hamano
  2017-05-16  8:28       ` [PATCH v2] " Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2017-05-16  1:15 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: git, Nguyễn Thái Ngọc Duy, Torsten Bögershausen

Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Change the conditional inclusion mechanism to support
> e.g. gitdir:~/git_tree/repo where ~/git_tree is a symlink to to

s/to to/to/;

> /mnt/stuff/repo.
>
> This worked in the initial version of this facility[1], but regressed
> later in the series while solving a related bug[2].
>
> Now gitdir: will match against the symlinked
> path (e.g. gitdir:~/git_tree/repo) in addition to the current
> /mnt/stuff/repo path.
>
> Since this is already in a release version note in the documentation
> that this behavior changed, so users who expect their configuration to
> work on both v2.13.0 and some future version of git with this fix
> aren't utterly confused.
>
> 1. commit 3efd0bedc6 ("config: add conditional include", 2017-03-01)
> 2. commit 86f9515708 ("config: resolve symlinks in conditional
>    include's patterns", 2017-04-05)
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>
> Here's a non-RFC patch to fix this bug.
>
>  Documentation/config.txt  | 11 +++++++++++
>  config.c                  | 16 ++++++++++++++++
>  t/t1305-config-include.sh | 23 +++++++++++++++++++++++
>  3 files changed, 50 insertions(+)
>
> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index 475e874d51..137502a289 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -140,6 +140,17 @@ A few more notes on matching via `gitdir` and `gitdir/i`:
>  
>   * Symlinks in `$GIT_DIR` are not resolved before matching.
>  
> + * Both the symlink & realpath versions of paths will be matched
> +   outside of `$GIT_DIR`. E.g. if ~/git is a symlink to
> +   /mnt/storage/git, both `gitdir:~/git` and `gitdir:/mnt/storage/git`
> +   will match.
> +
> +   This was not the case in the initial release of this feature in
> +   v2.13.0, which only matched the realpath version. Configuration
> +   that wants to be compatible with the initial release of this
> +   feature needs to either specify only the realpath version, or both
> +   versions.
> +

Does the second paragraph format correctly, or do we need the usual
(and ugly) "+ by itself on a line and then dedent the next
paragraph" trick?

> diff --git a/config.c b/config.c
> index b4a3205da3..0498746112 100644
> --- a/config.c
> +++ b/config.c
> @@ -214,6 +214,7 @@ static int include_by_gitdir(const struct config_options *opts,
>  	struct strbuf pattern = STRBUF_INIT;
>  	int ret = 0, prefix;
>  	const char *git_dir;
> +	int retry = 1;

I think your earlier RFC used a variable with a better name than
this; it is not like we are preparing ourselves for adding a
mechanism here to retry for many random reasons.

The logic would be clearer With an "already_tried_absolute" variable
that is initially 0, retrying unless it is already set, and set it
when we retry, like you did in your RFC.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] config: match both symlink & realpath versions in IncludeIf.gitdir:*
  2017-05-16  1:15     ` Junio C Hamano
@ 2017-05-16  8:28       ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2017-05-16  8:28 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy,
	Torsten Bögershausen,
	Ævar Arnfjörð Bjarmason

Change the conditional inclusion mechanism to support
e.g. gitdir:~/git_tree/repo where ~/git_tree is a symlink to
/mnt/stuff/repo.

This worked in the initial version of this facility[1], but regressed
later in the series while solving a related bug[2].

Now gitdir: will match against the symlinked
path (e.g. gitdir:~/git_tree/repo) in addition to the current
/mnt/stuff/repo path.

Since this is already in a release version note in the documentation
that this behavior changed, so users who expect their configuration to
work on both v2.13.0 and some future version of git with this fix
aren't utterly confused.

1. commit 3efd0bedc6 ("config: add conditional include", 2017-03-01)
2. commit 86f9515708 ("config: resolve symlinks in conditional
   include's patterns", 2017-04-05)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---

Thanks. All fixed in the v2 below.

 Documentation/config.txt  | 10 ++++++++++
 config.c                  | 16 ++++++++++++++++
 t/t1305-config-include.sh | 23 +++++++++++++++++++++++
 3 files changed, 49 insertions(+)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 475e874d51..302b8af192 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -140,6 +140,16 @@ A few more notes on matching via `gitdir` and `gitdir/i`:
 
  * Symlinks in `$GIT_DIR` are not resolved before matching.
 
+ * Both the symlink & realpath versions of paths will be matched
+   outside of `$GIT_DIR`. E.g. if ~/git is a symlink to
+   /mnt/storage/git, both `gitdir:~/git` and `gitdir:/mnt/storage/git`
+   will match.
++
+This was not the case in the initial release of this feature in
+v2.13.0, which only matched the realpath version. Configuration that
+wants to be compatible with the initial release of this feature needs
+to either specify only the realpath version, or both versions.
+
  * Note that "../" is not special and will match literally, which is
    unlikely what you want.
 
diff --git a/config.c b/config.c
index b4a3205da3..f978428695 100644
--- a/config.c
+++ b/config.c
@@ -214,6 +214,7 @@ static int include_by_gitdir(const struct config_options *opts,
 	struct strbuf pattern = STRBUF_INIT;
 	int ret = 0, prefix;
 	const char *git_dir;
+	int already_tried_absolute = 0;
 
 	if (opts->git_dir)
 		git_dir = opts->git_dir;
@@ -226,6 +227,7 @@ static int include_by_gitdir(const struct config_options *opts,
 	strbuf_add(&pattern, cond, cond_len);
 	prefix = prepare_include_condition_pattern(&pattern);
 
+again:
 	if (prefix < 0)
 		goto done;
 
@@ -245,6 +247,20 @@ static int include_by_gitdir(const struct config_options *opts,
 	ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
 			 icase ? WM_CASEFOLD : 0, NULL);
 
+	if (!ret && !already_tried_absolute) {
+		/*
+		 * We've tried e.g. matching gitdir:~/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, git_dir);
+		already_tried_absolute = 1;
+		goto again;
+	}
 done:
 	strbuf_release(&pattern);
 	strbuf_release(&text);
diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh
index 933915ec06..d9d2f545a4 100755
--- a/t/t1305-config-include.sh
+++ b/t/t1305-config-include.sh
@@ -273,6 +273,29 @@ test_expect_success SYMLINKS 'conditional include, relative path with symlinks'
 	)
 '
 
+test_expect_success SYMLINKS 'conditional include, gitdir matching symlink' '
+	ln -s foo bar &&
+	(
+		cd bar &&
+		echo "[includeIf \"gitdir:bar/\"]path=bar7" >>.git/config &&
+		echo "[test]seven=7" >.git/bar7 &&
+		echo 7 >expect &&
+		git config test.seven >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success SYMLINKS 'conditional include, gitdir matching symlink, icase' '
+	(
+		cd bar &&
+		echo "[includeIf \"gitdir/i:BAR/\"]path=bar8" >>.git/config &&
+		echo "[test]eight=8" >.git/bar8 &&
+		echo 8 >expect &&
+		git config test.eight >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'include cycles are detected' '
 	cat >.gitconfig <<-\EOF &&
 	[test]value = gitconfig
-- 
2.13.0.303.g4ebf302169


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-05-16  8:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-15 15:20 [PATCH/RFC] The new IncludeIf facility doesn't DWIM when the repo is symlinked Ævar Arnfjörð Bjarmason
2017-05-15 18:30 ` Ævar Arnfjörð Bjarmason
2017-05-15 19:15   ` [PATCH] config: match both symlink & realpath versions in IncludeIf.gitdir:* Ævar Arnfjörð Bjarmason
2017-05-16  1:15     ` Junio C Hamano
2017-05-16  8:28       ` [PATCH v2] " Ævar Arnfjörð Bjarmason

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.