diff mbox series

[v2] builtin/remote.c: teach `-v` to list filters for promisor remotes

Message ID pull.1227.v2.git.1651591253333.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series [v2] builtin/remote.c: teach `-v` to list filters for promisor remotes | expand

Commit Message

Abhradeep Chakraborty May 3, 2022, 3:20 p.m. UTC
From: Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>

`git remote -v` (`--verbose`) lists down the names of remotes along with
their urls. It would be beneficial for users to also specify the filter
types for promisor remotes. Something like this -

	origin	remote-url (fetch) [blob:none]
	origin	remote-url (push)

Teach `git remote -v` to also specify the filters for promisor remotes.

Signed-off-by: Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>
---
    builtin/remote.c: teach -v to list filters for promisor remotes
    
    Fixes #1211 [1]
    
    In this version, documentation is updated (describing the proposed
    change) and url_buf is renamed into remote_info_buf.
    
    [1] https://github.com/gitgitgadget/git/issues/1211

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1227%2FAbhra303%2Fpromisor_remote-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1227/Abhra303/promisor_remote-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1227

Range-diff vs v1:

 1:  fe3bf755e63 ! 1:  e7ced852fd5 builtin/remote.c: teach `-v` to list filters for promisor remotes
     @@ Commit message
      
          Signed-off-by: Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>
      
     + ## Documentation/git-remote.txt ##
     +@@ Documentation/git-remote.txt: OPTIONS
     + -v::
     + --verbose::
     + 	Be a little more verbose and show remote url after name.
     ++  For promisor remotes it will show an extra information
     ++  (wrapped in square brackets) describing which filter
     ++  (`blob:none` etc.) that promisor remote use.
     + 	NOTE: This must be placed between `remote` and subcommand.
     + 
     + 
     +
       ## builtin/remote.c ##
     -@@ builtin/remote.c: static int get_one_entry(struct remote *remote, void *priv)
     +@@ builtin/remote.c: static int show_push_info_item(struct string_list_item *item, void *cb_data)
     + static int get_one_entry(struct remote *remote, void *priv)
     + {
     + 	struct string_list *list = priv;
     +-	struct strbuf url_buf = STRBUF_INIT;
     ++	struct strbuf remote_info_buf = STRBUF_INIT;
     + 	const char **url;
       	int i, url_nr;
       
       	if (remote->url_nr > 0) {
     +-		strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
      +		struct strbuf promisor_config = STRBUF_INIT;
      +		const char *partial_clone_filter = NULL;
      +
      +		strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
     - 		strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
     ++		strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
      +		if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
     -+			strbuf_addf(&url_buf, " [%s]", partial_clone_filter);
     ++			strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
      +
      +		strbuf_release(&promisor_config);
       		string_list_append(list, remote->name)->util =
     - 				strbuf_detach(&url_buf, NULL);
     +-				strbuf_detach(&url_buf, NULL);
     ++				strbuf_detach(&remote_info_buf, NULL);
       	} else
     + 		string_list_append(list, remote->name)->util = NULL;
     + 	if (remote->pushurl_nr) {
     +@@ builtin/remote.c: static int get_one_entry(struct remote *remote, void *priv)
     + 	}
     + 	for (i = 0; i < url_nr; i++)
     + 	{
     +-		strbuf_addf(&url_buf, "%s (push)", url[i]);
     ++		strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
     + 		string_list_append(list, remote->name)->util =
     +-				strbuf_detach(&url_buf, NULL);
     ++				strbuf_detach(&remote_info_buf, NULL);
     + 	}
     + 
     + 	return 0;
      
       ## t/t5616-partial-clone.sh ##
      @@ t/t5616-partial-clone.sh: test_expect_success 'do partial clone 1' '


 Documentation/git-remote.txt |  3 +++
 builtin/remote.c             | 18 +++++++++++++-----
 t/t5616-partial-clone.sh     | 11 +++++++++++
 3 files changed, 27 insertions(+), 5 deletions(-)


base-commit: 0f828332d5ac36fc63b7d8202652efa152809856

Comments

Junio C Hamano May 4, 2022, 5:10 p.m. UTC | #1
"Abhradeep Chakraborty via GitGitGadget" <gitgitgadget@gmail.com>
writes:

> diff --git a/Documentation/git-remote.txt b/Documentation/git-remote.txt
> index cde9614e362..71a0e85990d 100644
> --- a/Documentation/git-remote.txt
> +++ b/Documentation/git-remote.txt
> @@ -35,6 +35,9 @@ OPTIONS
>  -v::
>  --verbose::
>  	Be a little more verbose and show remote url after name.
> +  For promisor remotes it will show an extra information
> +  (wrapped in square brackets) describing which filter
> +  (`blob:none` etc.) that promisor remote use.
>  	NOTE: This must be placed between `remote` and subcommand.

Broken indentation.  You can save embarrassment by double checking
what you committed by sending e-mail to yourself (or checking output
from "git show") before sending it to the list.

> diff --git a/builtin/remote.c b/builtin/remote.c
> index 5f4cde9d784..d4b69fe7789 100644
> --- a/builtin/remote.c
> +++ b/builtin/remote.c
> @@ -1185,14 +1185,22 @@ static int show_push_info_item(struct string_list_item *item, void *cb_data)
>  static int get_one_entry(struct remote *remote, void *priv)
>  {
>  	struct string_list *list = priv;
> -	struct strbuf url_buf = STRBUF_INIT;
> +	struct strbuf remote_info_buf = STRBUF_INIT;
>  	const char **url;
>  	int i, url_nr;
>  
>  	if (remote->url_nr > 0) {
> -		strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
> +		struct strbuf promisor_config = STRBUF_INIT;
> +		const char *partial_clone_filter = NULL;
> +
> +		strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
> +		strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
> +		if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
> +			strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
> +
> +		strbuf_release(&promisor_config);
>  		string_list_append(list, remote->name)->util =
> -				strbuf_detach(&url_buf, NULL);
> +				strbuf_detach(&remote_info_buf, NULL);

It is unfortunate that the "we borrow without copying" variant of
git_config_get_string() is called git_config_get_string_tmp(), which
is an utterly misleading name that might confuse readers into
mistaking it may make a temporary copy for the caller to release.
Perhaps we would want to rename it to git_config_peek_string() or
something, but that is totally outside the topic, of course.

In any case, what I wanted to say is that I just made sure that the
value in the partial_clone_filter variable is not leaked.

Looking good.

> diff --git a/t/t5616-partial-clone.sh b/t/t5616-partial-clone.sh
> index 4a3778d04a8..bf8f3644d3c 100755
> --- a/t/t5616-partial-clone.sh
> +++ b/t/t5616-partial-clone.sh
> @@ -49,6 +49,17 @@ test_expect_success 'do partial clone 1' '
>  	test "$(git -C pc1 config --local remote.origin.partialclonefilter)" = "blob:none"
>  '
>  
> +test_expect_success 'filters for promisor remotes is listed by git remote -v' '
> +	git clone --filter=blob:none "file://$(pwd)/srv.bare" pc2 &&
> +	git -C pc2 remote -v >out &&
> +	grep "[blob:none]" out &&
> +
> +	git -C pc2 config remote.origin.partialCloneFilter object:type=commit &&
> +	git -C pc2 remote -v >out &&
> +	grep "[object:type=commit]" out &&
> +	rm -rf pc2
> +'

I doubt that these "grep" do what you think it is doing.  It would
say "I am happy" on any line that has one of these characters listed
inside the [].

Do not clean up with an extra "&& clean up" step at the end of
&&-cascade.  Instead use test_when_finished to make sure that after
any failure in the cascade the clean-up step would still trigger.

	test_expect_success 'title' '
		test_when_finished "rm -fr pc2" &&
		git clone ... &&
		...
		grep "srv.bare (fetch) \[object:type=commit\]" out
	'

or something.

Having tests that show how this new feature works is of course
necessary, but we must have negative tests that ensure that it does
*not* trigger when it should not.  E.g. the new [filter-spec] should
not be given for a remote if the user didn't ask for "-v", or the
remote is not a promisor.

Thanks.
Abhradeep Chakraborty May 5, 2022, 2:12 p.m. UTC | #2
Junio C Hamano <gitster@pobox.com> wrote:

> Broken indentation.  You can save embarrassment by double checking
> what you committed by sending e-mail to yourself (or checking output
> from "git show") before sending it to the list.

Thanks for the suggestions. Will keep it in mind next time.

> I doubt that these "grep" do what you think it is doing.  It would
> say "I am happy" on any line that has one of these characters listed
> inside the [].
>
> Do not clean up with an extra "&& clean up" step at the end of
> &&-cascade.  Instead use test_when_finished to make sure that after
> any failure in the cascade the clean-up step would still trigger.
>
>	test_expect_success 'title' '
>		test_when_finished "rm -fr pc2" &&
>		git clone ... &&
>		...
> 		grep "srv.bare (fetch) \[object:type=commit\]" out
> 	'
>
> or something.
>
> Having tests that show how this new feature works is of course
> necessary, but we must have negative tests that ensure that it does
> *not* trigger when it should not.  E.g. the new [filter-spec] should
> not be given for a remote if the user didn't ask for "-v", or the
> remote is not a promisor.

Got it. Will send the necessary changes by the day after tommorow.

Thanks :)
diff mbox series

Patch

diff --git a/Documentation/git-remote.txt b/Documentation/git-remote.txt
index cde9614e362..71a0e85990d 100644
--- a/Documentation/git-remote.txt
+++ b/Documentation/git-remote.txt
@@ -35,6 +35,9 @@  OPTIONS
 -v::
 --verbose::
 	Be a little more verbose and show remote url after name.
+  For promisor remotes it will show an extra information
+  (wrapped in square brackets) describing which filter
+  (`blob:none` etc.) that promisor remote use.
 	NOTE: This must be placed between `remote` and subcommand.
 
 
diff --git a/builtin/remote.c b/builtin/remote.c
index 5f4cde9d784..d4b69fe7789 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1185,14 +1185,22 @@  static int show_push_info_item(struct string_list_item *item, void *cb_data)
 static int get_one_entry(struct remote *remote, void *priv)
 {
 	struct string_list *list = priv;
-	struct strbuf url_buf = STRBUF_INIT;
+	struct strbuf remote_info_buf = STRBUF_INIT;
 	const char **url;
 	int i, url_nr;
 
 	if (remote->url_nr > 0) {
-		strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
+		struct strbuf promisor_config = STRBUF_INIT;
+		const char *partial_clone_filter = NULL;
+
+		strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
+		strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
+		if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
+			strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
+
+		strbuf_release(&promisor_config);
 		string_list_append(list, remote->name)->util =
-				strbuf_detach(&url_buf, NULL);
+				strbuf_detach(&remote_info_buf, NULL);
 	} else
 		string_list_append(list, remote->name)->util = NULL;
 	if (remote->pushurl_nr) {
@@ -1204,9 +1212,9 @@  static int get_one_entry(struct remote *remote, void *priv)
 	}
 	for (i = 0; i < url_nr; i++)
 	{
-		strbuf_addf(&url_buf, "%s (push)", url[i]);
+		strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
 		string_list_append(list, remote->name)->util =
-				strbuf_detach(&url_buf, NULL);
+				strbuf_detach(&remote_info_buf, NULL);
 	}
 
 	return 0;
diff --git a/t/t5616-partial-clone.sh b/t/t5616-partial-clone.sh
index 4a3778d04a8..bf8f3644d3c 100755
--- a/t/t5616-partial-clone.sh
+++ b/t/t5616-partial-clone.sh
@@ -49,6 +49,17 @@  test_expect_success 'do partial clone 1' '
 	test "$(git -C pc1 config --local remote.origin.partialclonefilter)" = "blob:none"
 '
 
+test_expect_success 'filters for promisor remotes is listed by git remote -v' '
+	git clone --filter=blob:none "file://$(pwd)/srv.bare" pc2 &&
+	git -C pc2 remote -v >out &&
+	grep "[blob:none]" out &&
+
+	git -C pc2 config remote.origin.partialCloneFilter object:type=commit &&
+	git -C pc2 remote -v >out &&
+	grep "[object:type=commit]" out &&
+	rm -rf pc2
+'
+
 test_expect_success 'verify that .promisor file contains refs fetched' '
 	ls pc1/.git/objects/pack/pack-*.promisor >promisorlist &&
 	test_line_count = 1 promisorlist &&