Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remote: align --verbose output with spaces #1837

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

louiswpf
Copy link

@louiswpf louiswpf commented Dec 14, 2024

Changes in v2:

  • Use for_each_string_list_item() to traverse string lists.
  • Calculate the max width outside of the loop.

cc: shejialuo [email protected]

Copy link

gitgitgadget bot commented Dec 14, 2024

Welcome to GitGitGadget

Hi @louiswpf, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that either:

  • Your Pull Request has a good description, if it consists of multiple commits, as it will be used as cover letter.
  • Your Pull Request description is empty, if it consists of a single commit, as the commit message should be descriptive enough by itself.

You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <[email protected]>, Ill Takalook <[email protected]>

NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description,
because it will result in a malformed CC list on the mailing list. See
example.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join [email protected], where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

@louiswpf louiswpf force-pushed the remote-align-verbose-output branch from 25bcdd1 to 960a18e Compare December 14, 2024 15:54
@dscho
Copy link
Member

dscho commented Dec 17, 2024

/allow

Copy link

gitgitgadget bot commented Dec 17, 2024

User louiswpf is now allowed to use GitGitGadget.

WARNING: louiswpf has no public email address set on GitHub;
GitGitGadget needs an email address to Cc: you on your contribution, so that you receive any feedback on the Git mailing list. Go to https://github.com/settings/profile to make your preferred email public to let GitGitGadget know which email address to use.

@louiswpf
Copy link
Author

/preview

Copy link

gitgitgadget bot commented Dec 17, 2024

Error: Could not determine full name of louiswpf

@louiswpf
Copy link
Author

/preview

Copy link

gitgitgadget bot commented Dec 17, 2024

Preview email sent as [email protected]

@louiswpf
Copy link
Author

/submit

Copy link

gitgitgadget bot commented Dec 17, 2024

Submitted as [email protected]

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1837/louiswpf/remote-align-verbose-output-v1

To fetch this version to local tag pr-1837/louiswpf/remote-align-verbose-output-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1837/louiswpf/remote-align-verbose-output-v1

Copy link

gitgitgadget bot commented Dec 17, 2024

On the Git mailing list, shejialuo wrote (reply to this):

On Tue, Dec 17, 2024 at 12:39:36PM +0000, Wang Bing-hua via GitGitGadget wrote:
> From: Wang Bing-hua <[email protected]>
> 
> Remote names exceeding a tab width could cause misalignment.
> Align --verbose output with spaces instead of a tab.
> 

Good enhancement.

> Signed-off-by: Wang Bing-hua <[email protected]>
> ---
>     remote: align --verbose output with spaces
> 
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1837%2Flouiswpf%2Fremote-align-verbose-output-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1837/louiswpf/remote-align-verbose-output-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/1837
> 
>  builtin/remote.c  | 30 ++++++++++++++++++++++++++----
>  t/t5505-remote.sh |  4 ++--
>  2 files changed, 28 insertions(+), 6 deletions(-)
> 
> diff --git a/builtin/remote.c b/builtin/remote.c
> index 1ad3e70a6b4..876274d9dca 100644
> --- a/builtin/remote.c
> +++ b/builtin/remote.c
> @@ -16,6 +16,7 @@
>  #include "strvec.h"
>  #include "commit-reach.h"
>  #include "progress.h"
> +#include "utf8.h"
>  
>  static const char * const builtin_remote_usage[] = {
>  	"git remote [-v | --verbose]",
> @@ -1279,6 +1280,20 @@ static int get_one_entry(struct remote *remote, void *priv)
>  	return 0;
>  }
>  
> +static int calc_maxwidth(struct string_list *list)
> +{
> +	int max = 0;
> +
> +	for (int i = 0; i < list->nr; i++) {

Nit: we should use "size_t" to declare/define loop variable `i`
because the type of `list-nr` is `size_t`.

Recently, Patrick has provided a patch to start warn unsigned value
compared with signed value in [1] which has not been merged into the
master.

[1] https://lore.kernel.org/git/[email protected]

> +		struct string_list_item *item = list->items + i;
> +		int w = utf8_strwidth(item->string);
> +
> +		if (w > max)
> +			max = w;
> +	}
> +	return max;
> +}
> +

So, here we traverse the list to find the max "utf8_strwidth". However,
we should not EXPLICITLY traverse the string list. There are two ways
you could do:

1. Use the helper macro "for_each_string_list_item" in "string-list.h"
to do above.
2. Use the helper function "for_each_string_list" in "string-list.c" to
do above.

>  static int show_all(void)
>  {
>  	struct string_list list = STRING_LIST_INIT_DUP;
> @@ -1292,10 +1307,17 @@ static int show_all(void)
>  		string_list_sort(&list);
>  		for (i = 0; i < list.nr; i++) {
>  			struct string_list_item *item = list.items + i;
> -			if (verbose)
> -				printf("%s\t%s\n", item->string,
> -					item->util ? (const char *)item->util : "");
> -			else {
> +			if (verbose) {
> +				struct strbuf s = STRBUF_INIT;
> +
> +				strbuf_utf8_align(&s, ALIGN_LEFT,
> +						  calc_maxwidth(&list) + 1,
> +						  item->string);

So, here we call `calc_maxwidth` in the loop. That does not make sense.
We should not call this function when we are traversing the string list.
I think we should firstly calculate the max width outside of the loop.

Thanks,
Jialuo

Copy link

gitgitgadget bot commented Dec 17, 2024

User shejialuo <[email protected]> has been added to the cc: list.

@louiswpf louiswpf force-pushed the remote-align-verbose-output branch from 960a18e to 8d5ef78 Compare December 17, 2024 15:27
Copy link

gitgitgadget bot commented Dec 17, 2024

On the Git mailing list, "Wang Bing-hua" wrote (reply to this):

On 17/12/2024 21:23, shejialuo wrote:
> 
> Good enhancement.
> 

Thank you.

> 
> Nit: we should use "size_t" to declare/define loop variable `i`
> because the type of `list-nr` is `size_t`.
> 
> Recently, Patrick has provided a patch to start warn unsigned value
> compared with signed value in [1] which has not been merged into the
> master.
> 
> [1] https://lore.kernel.org/git/[email protected]
> 

> 
> So, here we traverse the list to find the max "utf8_strwidth". However,
> we should not EXPLICITLY traverse the string list. There are two ways
> you could do:
> 
> 1. Use the helper macro "for_each_string_list_item" in "string-list.h"
> to do above.
> 2. Use the helper function "for_each_string_list" in "string-list.c" to
> do above.
> 

> 
> So, here we call `calc_maxwidth` in the loop. That does not make sense.
> We should not call this function when we are traversing the string list.
> I think we should firstly calculate the max width outside of the loop.
> 

Thank you for the detailed comments. It really helps.
I will address these issues in v2.

@louiswpf
Copy link
Author

/preview

Copy link

gitgitgadget bot commented Dec 17, 2024

Preview email sent as [email protected]

Remote names exceeding a tab width could cause misalignment.
Align --verbose output with spaces instead of a tab.

Signed-off-by: Wang Bing-hua <[email protected]>
@louiswpf louiswpf force-pushed the remote-align-verbose-output branch from 8d5ef78 to 648881d Compare December 17, 2024 16:37
@louiswpf
Copy link
Author

/submit

Copy link

gitgitgadget bot commented Dec 17, 2024

Submitted as [email protected]

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1837/louiswpf/remote-align-verbose-output-v2

To fetch this version to local tag pr-1837/louiswpf/remote-align-verbose-output-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1837/louiswpf/remote-align-verbose-output-v2

Copy link

gitgitgadget bot commented Dec 17, 2024

On the Git mailing list, Junio C Hamano wrote (reply to this):

shejialuo <[email protected]> writes:

> On Tue, Dec 17, 2024 at 12:39:36PM +0000, Wang Bing-hua via GitGitGadget wrote:
>> From: Wang Bing-hua <[email protected]>
>> 
>> Remote names exceeding a tab width could cause misalignment.

If all of them are named with ten ASCII characters, on a terminal
with fixed-width font, things will still display aligned ;-)

>> Align --verbose output with spaces instead of a tab.
>> 
>
> Good enhancement.

With a Devil's advocate hat on, a change like this will completely
break tools when they are reading the "--verbose" output and
expecting that the fields are separated with a TAB (in other words,
the tab is *not* about alignment in the first place for them).

Now with that hat off.

For users with that many remotes where the alignment of URLs in the
interactive "git remote -v" output matter, I am not sure if this
change is a real improvement enough that it is worth the possible
risk of breaking existing tools.  With that many remotes defined,
wouldn't they be doing "git remote -v" piped to "grep '^name<TAB>'"
or something?  That use case would break with the change, too.

Thanks.

Copy link

gitgitgadget bot commented Dec 17, 2024

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Wang Bing-hua via GitGitGadget" <[email protected]> writes:

> From: Wang Bing-hua <[email protected]>
>
> Remote names exceeding a tab width could cause misalignment.
> Align --verbose output with spaces instead of a tab.

While I am still not convinced if this change is a good idea (see
my earlier comment in a separate message)...

> +static int calc_maxwidth(struct string_list *list)
> +{
> +	int max = 0;
> +	struct string_list_item *item;
> +
> +	for_each_string_list_item (item, list) {
> +		int w = utf8_strwidth(item->string);
> +
> +		if (w > max)
> +			max = w;
> +	}
> +	return max;
> +}
> +
>  static int show_all(void)
>  {
>  	struct string_list list = STRING_LIST_INIT_DUP;
> @@ -1287,16 +1302,25 @@ static int show_all(void)
>  	result = for_each_remote(get_one_entry, &list);
>  
>  	if (!result) {
> -		int i;
> +		int maxwidth = 0;
> +		struct string_list_item *item;
>  
> +		if (verbose)
> +			maxwidth = calc_maxwidth(&list);

I wonder if it is a better idea to extend get_one_entry() interface
to take not just a string_list but something like

	struct remotes_data {
		int maxwidth;
		struct string_list *list_of_remotes;
	};

if we think it is a good idea to give richer output to show_all()
function (instead of keep it spartan and compatible for the sake of
not breaking machine readers).  There may be things other than
maxwidth that future changes to "git remote [-v]" may find needed.
And with such a change, you do not need a separate iteration over
the list of remotes just to call calc_maxwidth() callback.  Keeping
a tally of "max length we have seen" inside get_one_entry() regardless
of "--verbose" setting shouldn't be too costly and help reduce the
complexity of the code.

>  		string_list_sort(&list);
> -		for (i = 0; i < list.nr; i++) {
> -			struct string_list_item *item = list.items + i;
> -			if (verbose)
> -				printf("%s\t%s\n", item->string,
> -					item->util ? (const char *)item->util : "");
> -			else {
> -				if (i && !strcmp((item - 1)->string, item->string))
> +		for_each_string_list_item (item, &list) {

Use of for_each_string_list_item() instead of a manual iteration is
probably a good idea here.  If this were a larger change, that may
deserve to be a preparatory step on its own, but it is probably OK
to do so in the same patch.

> +			if (verbose) {
> +				struct strbuf s = STRBUF_INIT;
> +
> +				strbuf_utf8_align(&s, ALIGN_LEFT, maxwidth + 1,
> +						  item->string);
> +				if (item->util)
> +					strbuf_addstr(&s, item->util);
> +				printf("%s\n", s.buf);
> +				strbuf_release(&s);

Wouldn't it work to just do (totally untested code snippet below;
may have off-by-one around maxwidth)

				printf("%.*s%s", maxwidth, item->string,
					item->util ? "" : item->util);

without using any strbuf operation?

Copy link

gitgitgadget bot commented Dec 18, 2024

On the Git mailing list, "Wang Bing-hua" wrote (reply to this):

On 17/12/2024 12:21, Junio C Hamano wrote:
>> On Tue, Dec 17, 2024 at 12:39:36PM +0000, Wang Bing-hua via GitGitGadget wrote:
>>> From: Wang Bing-hua <[email protected]>
>>> 
>>> Remote names exceeding a tab width could cause misalignment.
> 
> If all of them are named with ten ASCII characters, on a terminal
> with fixed-width font, things will still display aligned ;-)

Indeed :)
I did consider this scenario and wrote "could". I should have worded
this more clearly.

> With a Devil's advocate hat on, a change like this will completely
> break tools when they are reading the "--verbose" output and
> expecting that the fields are separated with a TAB (in other words,
> the tab is *not* about alignment in the first place for them).
> 
> Now with that hat off.
> 
> For users with that many remotes where the alignment of URLs in the
> interactive "git remote -v" output matter, I am not sure if this
> change is a real improvement enough that it is worth the possible
> risk of breaking existing tools.  With that many remotes defined,
> wouldn't they be doing "git remote -v" piped to "grep '^name<TAB>'"
> or something?  That use case would break with the change, too.

Agreed on all points. Another point to consider is to align with
"git branch -v" since it also uses spaces for alignment.
This patch was also originally lifted from "builtin/branch.c".

Copy link

gitgitgadget bot commented Dec 18, 2024

On the Git mailing list, "Wang Bing-hua" wrote (reply to this):

On 17/12/2024 12:47, Junio C Hamano wrote:
> I wonder if it is a better idea to extend get_one_entry() interface
> to take not just a string_list but something like
> 
> 	struct remotes_data {
> 		int maxwidth;
> 		struct string_list *list_of_remotes;
> 	};
> 
> if we think it is a good idea to give richer output to show_all()
> function (instead of keep it spartan and compatible for the sake of
> not breaking machine readers).  There may be things other than
> maxwidth that future changes to "git remote [-v]" may find needed.
> And with such a change, you do not need a separate iteration over
> the list of remotes just to call calc_maxwidth() callback.  Keeping
> a tally of "max length we have seen" inside get_one_entry() regardless
> of "--verbose" setting shouldn't be too costly and help reduce the
> complexity of the code.

This is a great idea.

> 
>>  		string_list_sort(&list);
>> -		for (i = 0; i < list.nr; i++) {
>> -			struct string_list_item *item = list.items + i;
>> -			if (verbose)
>> -				printf("%s\t%s\n", item->string,
>> -					item->util ? (const char *)item->util : "");
>> -			else {
>> -				if (i && !strcmp((item - 1)->string, item->string))
>> +		for_each_string_list_item (item, &list) {
> 
> Use of for_each_string_list_item() instead of a manual iteration is
> probably a good idea here.  If this were a larger change, that may
> deserve to be a preparatory step on its own, but it is probably OK
> to do so in the same patch.

Thanks for the reminder.

> 
>> +			if (verbose) {
>> +				struct strbuf s = STRBUF_INIT;
>> +
>> +				strbuf_utf8_align(&s, ALIGN_LEFT, maxwidth + 1,
>> +						  item->string);
>> +				if (item->util)
>> +					strbuf_addstr(&s, item->util);
>> +				printf("%s\n", s.buf);
>> +				strbuf_release(&s);
> 
> Wouldn't it work to just do (totally untested code snippet below;
> may have off-by-one around maxwidth)
> 
> 				printf("%.*s%s", maxwidth, item->string,
> 					item->util ? "" : item->util);
> 
> without using any strbuf operation?

I did try to use printf at first.

				printf("%-*s %s\n", maxwidth, item->string,
				       item->util ? (const char *)item->util :
						    "");

But it broke when there are non-ASCII characters. For example:

$ git remote -v
a  url (fetch)
a  url (push)
å url (fetch)
å url (push)
åå url (fetch)
åå url (push)


Thank you for reviewing. I'm also debating. It's great to align
"remote -v" and make it behave similarly to "branch -v". But it might
not be worth it to complicate the code and break machine readers.
Do we continue working on this?

Copy link

gitgitgadget bot commented Dec 18, 2024

On the Git mailing list, shejialuo wrote (reply to this):

On Tue, Dec 17, 2024 at 12:21:37PM -0800, Junio C Hamano wrote:
> shejialuo <[email protected]> writes:
> 
> > On Tue, Dec 17, 2024 at 12:39:36PM +0000, Wang Bing-hua via GitGitGadget wrote:
> >> From: Wang Bing-hua <[email protected]>
> >> 
> >> Remote names exceeding a tab width could cause misalignment.
> 
> If all of them are named with ten ASCII characters, on a terminal
> with fixed-width font, things will still display aligned ;-)
> 
> >> Align --verbose output with spaces instead of a tab.
> >> 
> >
> > Good enhancement.
> 
> With a Devil's advocate hat on, a change like this will completely
> break tools when they are reading the "--verbose" output and
> expecting that the fields are separated with a TAB (in other words,
> the tab is *not* about alignment in the first place for them).
> 

Yes, I agree. Although we may provide convenience for the end user, but
we may break other tools parsing the output. I think I bring confusion
to the Wang. Actually, I have seen that Wang contributes to Git in the
first time, so I just want to courage.

> Now with that hat off.
> 
> For users with that many remotes where the alignment of URLs in the
> interactive "git remote -v" output matter, I am not sure if this
> change is a real improvement enough that it is worth the possible
> risk of breaking existing tools.  With that many remotes defined,
> wouldn't they be doing "git remote -v" piped to "grep '^name<TAB>'"
> or something?  That use case would break with the change, too.
> 
> Thanks.

Copy link

gitgitgadget bot commented Dec 18, 2024

On the Git mailing list, "Wang Bing-hua" wrote (reply to this):

On 18/12/2024 21:52, shejialuo wrote:
> Yes, I agree. Although we may provide convenience for the end user, but
> we may break other tools parsing the output. I think I bring confusion
> to the Wang. Actually, I have seen that Wang contributes to Git in the
> first time, so I just want to courage.

No worries. Thank you for encouraging words :)

Copy link

gitgitgadget bot commented Dec 18, 2024

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Wang Bing-hua" <[email protected]> writes:

>> Wouldn't it work to just do (totally untested code snippet below;
>> may have off-by-one around maxwidth)
>> 
>> 				printf("%.*s%s", maxwidth, item->string,
>> 					item->util ? "" : item->util);
>> 
>> without using any strbuf operation?
>
> I did try to use printf at first.
>
> 				printf("%-*s %s\n", maxwidth, item->string,
> 				       item->util ? (const char *)item->util :
> 						    "");
>
> But it broke when there are non-ASCII characters. For example:

Ah, of course, I should have double-checked, but it should be more
like

	printf("%.*s%s%s",
	       maxwidth + 1 - utf8_strwidth(item->string), "",
	       item->string,
	       item->util ? item->util : "");

meaning

 (1) the first output field must have maxwidth+1 - the display width
     the second output field takes.  The field's contents is empty, so
     we get enough SP padded to make the total of this first field
     and the second field to make maxwidth+1.

 (2) the second output field is item->string itself.

 (3) the third output field has item->util if exists.

> Thank you for reviewing. I'm also debating. It's great to align
> "remote -v" and make it behave similarly to "branch -v". But it might
> not be worth it to complicate the code and break machine readers.
> Do we continue working on this?

You already know from my initial reaction what my answer would be,
but I am inclined to hear from others before we make a decision.

THanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants