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

Re: [PATCH] bash-completion: fix getting strategy list

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 20, 2008, 21:13 UTC
Message-ID
<7vfxozpeyh.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<fcaeb9bf0808200958u65ad8fa3oa118b88e16c9c50c@mail.gmail.com>
"Nguyen Thai Ngoc Duy" <pclouds@gmail.com> writes:
Show 12 quoted lines
> On 8/19/08, Johannes Sixt <j.sixt@viscovery.net> wrote:
>> Nguyen Thai Ngoc Duy schrieb:
>>
>> > +--show-strategies::
>>  > +     Show all available strategies. For internal use only.
>>  > +
>>
>>
>> IMO, you don't need to declare this option as internal; offering it for
>>  the public is fine...
>
> On second thought, I don't think the patch's worth it.
How about doing this instead?

-- >8 -- completion: find out supported merge strategies correctly

"git-merge" is a binary executable these days, and looking for assignment to $all_strategies variable does not work well.

When asked for an unknown strategy, pre-1.6.0 and post-1.6.0 "git merge" commands respectively say:

    $ $HOME/git-snap-v1.5.6.5/bin/git merge -s help
    available strategies are: recur recursive octopus resolve stupid ours subtree 
    $ $HOME/git-snap-v1.6.0/bin/git merge -s help
    Could not find merge strategy 'help'.
    Available strategies are: recursive octopus resolve ours subtree.

both on its standard error stream. We can use this to learn what strategies are supported.

The sed script is written in such a way that it catches both old and new message styles ("Available" vs "available", and the full stop at the end). It also allows future versions of "git merge" to line-wrap the list of strategies, and add extra comments, like this:

    $ $HOME/git-snap-v1.6.1/bin/git merge -s help
    Could not find merge strategy 'help'.
    Available strategies are: blame recursive octopus resolve ours
    subtree.
    Also you have custom strategies: theirs
    Make sure you spell strategy names correctly.
---
diff --git c/contrib/completion/git-completion.bash w/contrib/completion/git-completion.bash
index 158b912..a310040 100755
--- c/contrib/completion/git-completion.bash
+++ w/contrib/completion/git-completion.bash
@@ -271,15 +271,17 @@ __git_merge_strategies ()
 		echo "$__git_merge_strategylist"
 		return
 	fi
-	sed -n "/^all_strategies='/{
-		s/^all_strategies='//
-		s/'//
+	git merge -s help 2>&1 |
+	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
+		s/\.$//
+		s/.*://
+		s/^[ 	]*//
+		s/[ 	]*$//
 		p
-		q
-		}" "$(git --exec-path)/git-merge"
+	}'
 }
 __git_merge_strategylist=
-__git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
+__git_merge_strategylist=$(__git_merge_strategies 2>/dev/null)
 
 __git_complete_file ()
 {
Previous: Junio C HamanoNext: Miklos Vajna
Message 10 of 16 in “[BUG] git completion do sed on binary file”
  1. Nguyen Thai Ngoc DuyAug 19, 2008
  2. bash-completion: fix getting strategy listNguyen Thai Ngoc Duy, Aug 19, 2008
  3. Matthieu MoyAug 19, 2008
  4. Nguyen Thai Ngoc DuyAug 19, 2008
  5. Shawn O. PearceAug 19, 2008
  6. Johannes SixtAug 19, 2008
  7. Johannes SixtAug 19, 2008
  8. Nguyen Thai Ngoc DuyAug 20, 2008
  9. Junio C HamanoAug 20, 2008
  10. Junio C HamanoAug 20, 2008
  11. Miklos VajnaAug 20, 2008
  12. Nguyen Thai Ngoc DuyAug 21, 2008
  13. Mikael MagnussonAug 21, 2008
  14. Junio C HamanoAug 21, 2008
  15. Junio C HamanoAug 21, 2008
  16. Miklos VajnaAug 21, 2008

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.