threads / patch / 47688

patchmerge: support --strategy '?' for git-completion.bash

Subject: [PATCH] merge: support --strategy '?' for git-completion.bash

## tl;dr

4 messages between Jan 25, 2018 and Jan 26, 2018. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Nguyễn Thái Ngọc Duy· Jan 25, 2018, 09:40 UTC · lore

Bash completion support gets the list of available strategies with a grep and sed trick which does not work on non-C locale since the anchor string is translated and it does not cover custom strategies either.

Let's do it a better way and make git-merge provide all available strategies in machine-readable form.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Another, perhaps better, option is add "git merge --list-strategies".
 It requires some code movement, so I'll try with a simpler approach
 first.
 Documentation/merge-options.txt        | 3 +++
 builtin/merge.c                        | 7 +++++++
 contrib/completion/git-completion.bash | 9 +--------
 3 files changed, 11 insertions(+), 8 deletions(-)
Show changes to 3 files +11 −8

Documentation/merge-options.txt, builtin/merge.c, contrib/completion/git-completion.bash

diff --git a/Documentation/merge-options.txt b/Documentation/merge-options.txt
index 3888c3ff85..cd4342844f 100644
--- a/Documentation/merge-options.txt
+++ b/Documentation/merge-options.txt
@@ -97,6 +97,9 @@ option can be used to override --squash.
 	If there is no `-s` option, a built-in list of strategies
 	is used instead ('git merge-recursive' when merging a single
 	head, 'git merge-octopus' otherwise).
++
+The special strategy '?' lists all available strategies and exits
+immediately. No merge operation is done.
 
 -X <option>::
 --strategy-option=<option>::
diff --git a/builtin/merge.c b/builtin/merge.c
index 30264cfd7c..a09d04302c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -140,6 +140,13 @@ static struct strategy *get_strategy(const char *name)
 		}
 		exclude_cmds(&main_cmds, &not_strategies);
 	}
+	if (!strcmp(name, "?")) {
+		for (i = 0; i < main_cmds.cnt; i++)
+			puts(main_cmds.names[i]->name);
+		for (i = 0; i < other_cmds.cnt; i++)
+			puts(other_cmds.names[i]->name);
+		exit(0);
+	}
 	if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
 		fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
 		fprintf(stderr, _("Available strategies are:"));
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3683c772c5..6d947be858 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -594,14 +594,7 @@ __git_is_configured_remote ()
 
 __git_list_merge_strategies ()
 {
-	git merge -s help 2>&1 |
-	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
-		s/\.$//
-		s/.*://
-		s/^[ 	]*//
-		s/[ 	]*$//
-		p
-	}'
+	git merge --strategy '?'
 }
 
 __git_merge_strategies=
-- 
2.16.0.47.g3d9b0fac3a
Junio C Hamano· Jan 25, 2018, 18:49 UTC · re: Nguyễn Thái Ngọc Duy · lore

Re: [PATCH] merge: support --strategy '?' for git-completion.bash

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 12 quoted lines
> Bash completion support gets the list of available strategies with a
> grep and sed trick which does not work on non-C locale since the anchor
> string is translated and it does not cover custom strategies either.
>
> Let's do it a better way and make git-merge provide all available
> strategies in machine-readable form.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  Another, perhaps better, option is add "git merge --list-strategies".
>  It requires some code movement, so I'll try with a simpler approach
>  first.

If you run the probing "merge -s help" under C locale, that would just be a one-liner, no ;-) I.e.

> -	git merge -s help 2>&1 |
> +	LANG=C LC_ALL=C git merge -s help 2>&1 |
Not rejecting the patch, but just wondering.
Duy Nguyen· Jan 26, 2018, 01:31 UTC · re: Junio C Hamano · lore

Re: [PATCH] merge: support --strategy '?' for git-completion.bash

On Thu, Jan 25, 2018 at 10:49:45AM -0800, Junio C Hamano wrote:
Show 17 quoted lines
> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
> 
> > Bash completion support gets the list of available strategies with a
> > grep and sed trick which does not work on non-C locale since the anchor
> > string is translated and it does not cover custom strategies either.
> >
> > Let's do it a better way and make git-merge provide all available
> > strategies in machine-readable form.
> >
> > Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> > ---
> >  Another, perhaps better, option is add "git merge --list-strategies".
> >  It requires some code movement, so I'll try with a simpler approach
> >  first.
> 
> If you run the probing "merge -s help" under C locale, that would
> just be a one-liner, no ;-)  I.e.

That was my first choice but I was worried that it failed to catch custom strategies which are preceded with a different anchor string "Available custom strategies are:".

I didn't look carefully at those sed magic. But it looks like it correctly handles this case too. So v2 follows below. It still feels dirty to do this kind of text manipulation though. But that can wait.

-- 8< --
Subject: [PATCH] completion: fix completing merge strategies on non-C locales

The anchor string "Available strategies are:" is translatable so __git_list_merge_strategies may fail to collect available strategies from 'git merge' on non-C locales. Force C locale on this command.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 contrib/completion/git-completion.bash | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to contrib/completion/git-completion.bash +1 −1
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3683c772c5..88813e9124 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -594,7 +594,7 @@ __git_is_configured_remote ()
 
 __git_list_merge_strategies ()
 {
-	git merge -s help 2>&1 |
+	LANG=C LC_ALL=C git merge -s help 2>&1 |
 	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 		s/\.$//
 		s/.*://
-- 
2.16.1.196.g4f5118c359



-- 8< --
Junio C Hamano· Jan 26, 2018, 17:47 UTC · re: Duy Nguyen · lore

Re: [PATCH] merge: support --strategy '?' for git-completion.bash

Duy Nguyen <pclouds@gmail.com> writes:
> I didn't look carefully at those sed magic. But it looks like it
> correctly handles this case too. So v2 follows below. It still feels
> dirty to do this kind of text manipulation though. But that can wait.

I do like "introduce and use helper feature to produce machine parseable text" when it is appropriate. But this alternative looks more in line with the current design of completion script and can easily be a maint material as pure bugfix for non C locale folks.

Will queue.
Show 9 quoted lines
> -- 8< --
> Subject: [PATCH] completion: fix completing merge strategies on non-C locales
>
> The anchor string "Available strategies are:" is translatable so
> __git_list_merge_strategies may fail to collect available strategies
> from 'git merge' on non-C locales. Force C locale on this command.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
Show 16 quoted lines
>  contrib/completion/git-completion.bash | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 3683c772c5..88813e9124 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -594,7 +594,7 @@ __git_is_configured_remote ()
>  
>  __git_list_merge_strategies ()
>  {
> -	git merge -s help 2>&1 |
> +	LANG=C LC_ALL=C git merge -s help 2>&1 |
>  	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
>  		s/\.$//
>  		s/.*://

← back to recent threads