git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 25, 2018, 09:40 UTC
Message-ID
<20180125094031.6584-1-pclouds@gmail.com>

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(-)
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
Next: Junio C Hamano
Message 1 of 4 in “merge: support --strategy '?' for git-completion.bash”
  1. merge: support --strategy '?' for git-completion.bashNguyễn Thái Ngọc Duy, Jan 25, 2018
  2. Junio C HamanoJan 25, 2018
  3. Duy NguyenJan 26, 2018
  4. Junio C HamanoJan 26, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.