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

[MERGE PATCH 3/3] Merge branch 'master' (early part) into pd/bash-4-completion

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Dec 15, 2010, 06:42 UTC
Message-ID
<20101215064235.GD20492@burratino>
In-Reply-To
<20101215062403.GA20492@burratino>
* 'master' (early part): (529 commits)
  completion: fix zsh check under bash with 'set -u'
  Fix copy-pasted comments related to tree diff handling.
  Git 1.7.3.2
  {cvs,svn}import: use the new 'git read-tree --empty'
  t/t9001-send-email.sh: fix stderr redirection in 'Invalid In-Reply-To'
  Clarify and extend the "git diff" format documentation
  git-show-ref.txt: clarify the pattern matching
  documentation: git-config minor cleanups
  Update test script annotate-tests.sh to handle missing/extra authors
  ...
Conflicts:
	GIT-VERSION-GEN
	RelNotes
	contrib/completion/git-completion.bash
---
Suggestions for further work:
 - check edge cases:
	git log --pretty m<tab><tab>		should complete formats
	git log --pretty =<tab><tab>		should complain
	git log --pretty= m<tab><tab>		should complete commits
 - use a custom function to avoid repeating
	_get_comp_words_by_ref -n =:
   As an application, consider teaching git to complete
	git show HEAD@{<tab><tab>		completes to numbers and "upstream"
   Would the argument to -n need an @ for that?
 - get the zsh completion to actually work. :)  Even without this
   series, it seems it is willing to complete subcommand names and
   switches for git but nothing more.
 - adopt the rest of bash_completion's _get_comp_words_by_ref logic,
   so
	git log --pretty=m<cursor>master <tab><tab>
   completes formats starting with 'm', not formats starting with 'mmaster'.

If something turned out buggy, I'd be glad to fix it, but aside from that I probably will not be working much more on this topic. (Perhaps an actual tab completion user would have a better sense of which aspects are worth working on.) Please feel free to pick it up and run in whatever direction you please.

Good night, Jonathan

diff --cc contrib/completion/git-completion.bash index 1747091,168669b..d117055

--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@@ -321,135 -327,6 +327,162 @@@ __gitcomp_1 (
  	done
  }
  
 +# The following function is based on code from:
 +#
 +#   bash_completion - programmable completion functions for bash 3.2+
 +#
 +#   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
 +#             © 2009-2010, Bash Completion Maintainers
 +#                     <bash-completion-devel@lists.alioth.debian.org>
 +#
 +#   This program is free software; you can redistribute it and/or modify
 +#   it under the terms of the GNU General Public License as published by
 +#   the Free Software Foundation; either version 2, or (at your option)
 +#   any later version.
 +#
 +#   This program is distributed in the hope that it will be useful,
 +#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 +#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +#   GNU General Public License for more details.
 +#
 +#   You should have received a copy of the GNU General Public License
 +#   along with this program; if not, write to the Free Software Foundation,
 +#   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 +#
 +#   The latest version of this software can be obtained here:
 +#
 +#   http://bash-completion.alioth.debian.org/
 +#
 +#   RELEASE: 2.x
 +
 +# This function can be used to access a tokenized list of words
 +# on the command line:
 +#
 +#	__git_reassemble_comp_words_by_ref '=:'
 +#	if test "${words_[cword_-1]}" = -w
 +#	then
 +#		...
 +#	fi
 +#
 +# The argument should be a collection of characters from the list of
 +# word completion separators (COMP_WORDBREAKS) to treat as ordinary
 +# characters.
 +#
 +# This is roughly equivalent to going back in time and setting
 +# COMP_WORDBREAKS to exclude those characters.  The intent is to
 +# make option types like --date=<type> and <rev>:<path> easy to
 +# recognize by treating each shell word as a single token.
 +#
 +# It is best not to set COMP_WORDBREAKS directly because the value is
 +# shared with other completion scripts.  By the time the completion
 +# function gets called, COMP_WORDS has already been populated so local
 +# changes to COMP_WORDBREAKS have no effect.
 +#
 +# Output: words_, cword_, cur_.
 +
 +__git_reassemble_comp_words_by_ref()
 +{
 +	local exclude i j first
 +	# Which word separators to exclude?
 +	exclude="${1//[^$COMP_WORDBREAKS]}"
 +	cword_=$COMP_CWORD
 +	if [ -z "$exclude" ]; then
 +		words_=("${COMP_WORDS[@]}")
 +		return
 +	fi
 +	# List of word completion separators has shrunk;
 +	# re-assemble words to complete.
 +	for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
 +		# Append each nonempty word consisting of just
 +		# word separator characters to the current word.
 +		first=t
 +		while
 +			[ $i -gt 0 ] &&
 +			[ -n "${COMP_WORDS[$i]}" ] &&
 +			# word consists of excluded word separators
 +			[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
 +		do
 +			# Attach to the previous token,
 +			# unless the previous token is the command name.
 +			if [ $j -ge 2 ] && [ -n "$first" ]; then
 +				((j--))
 +			fi
 +			first=
 +			words_[$j]=${words_[j]}${COMP_WORDS[i]}
 +			if [ $i = $COMP_CWORD ]; then
 +				cword_=$j
 +			fi
 +			if (($i < ${#COMP_WORDS[@]} - 1)); then
 +				((i++))
 +			else
 +				# Done.
 +				return
 +			fi
 +		done
 +		words_[$j]=${words_[j]}${COMP_WORDS[i]}
 +		if [ $i = $COMP_CWORD ]; then
 +			cword_=$j
 +		fi
 +	done
 +}
 +
 +if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
++if [[ -z ${ZSH_VERSION:+set} ]]; then
 +_get_comp_words_by_ref ()
 +{
 +	local exclude cur_ words_ cword_
 +	if [ "$1" = "-n" ]; then
 +		exclude=$2
 +		shift 2
 +	fi
 +	__git_reassemble_comp_words_by_ref "$exclude"
 +	cur_=${words_[cword_]}
 +	while [ $# -gt 0 ]; do
 +		case "$1" in
 +		cur)
 +			cur=$cur_
 +			;;
 +		prev)
 +			prev=${words_[$cword_-1]}
 +			;;
 +		words)
 +			words=("${words_[@]}")
 +			;;
 +		cword)
 +			cword=$cword_
 +			;;
 +		esac
 +		shift
 +	done
 +}
++else
++_get_comp_words_by_ref ()
++{
++	while [ $# -gt 0 ]; do
++		case "$1" in
++		cur)
++			cur=${COMP_WORDS[COMP_CWORD]}
++			;;
++		prev)
++			prev=${COMP_WORDS[COMP_CWORD-1]}
++			;;
++		words)
++			words=("${COMP_WORDS[@]}")
++			;;
++		cword)
++			cword=$COMP_CWORD
++			;;
++		-n)
++			# assume COMP_WORDBREAKS is already set sanely
++			shift
++			;;
++		esac
++		shift
++	done
++}
++fi
 +fi
 +
  # __gitcomp accepts 1, 2, 3, or 4 arguments
  # generates completion reply with compgen
  __gitcomp ()
@@@ -2522,10 -2345,13 +2555,15 @@@ _git (
  {
  	local i c=1 command __git_dir
  
+ 	if [[ -n ${ZSH_VERSION-} ]]; then
+ 		emulate -L bash
+ 		setopt KSH_TYPESET
+ 	fi
+ 
 -	while [ $c -lt $COMP_CWORD ]; do
 -		i="${COMP_WORDS[c]}"
 +	local cur words cword
 +	_get_comp_words_by_ref -n =: cur words cword
 +	while [ $c -lt $cword ]; do
 +		i="${words[c]}"
  		case "$i" in
  		--git-dir=*) __git_dir="${i#--git-dir=}" ;;
  		--bare)      __git_dir="." ;;
@@@ -2568,9 -2394,14 +2606,14 @@@
  
  _gitk ()
  {
+ 	if [[ -n ${ZSH_VERSION-} ]]; then
+ 		emulate -L bash
+ 		setopt KSH_TYPESET
+ 	fi
+ 
  	__git_has_doubledash && return
  
 -	local cur="${COMP_WORDS[COMP_CWORD]}"
 +	local cur
  	local g="$(__gitdir)"
  	local merge=""
  	if [ -f "$g/MERGE_HEAD" ]; then
Previous: Jonathan Nieder
Message 22 of 22 in “*** SUBJECT HERE ***”
  1. 0/2 *** SUBJECT HERE ***Peter van der Does, Dec 1, 2010
  2. 1/2 Introduce functions from bash-completion project.Peter van der Does, Dec 1, 2010
  3. 2/2 Use the new functions to get the current cword.Peter van der Does, Dec 1, 2010
  4. Jonathan NiederDec 2, 2010
  5. SZEDER GáborDec 2, 2010
  6. Jonathan NiederDec 1, 2010
  7. SZEDER GáborDec 2, 2010
  8. Jonathan NiederDec 2, 2010
  9. Peter van der DoesDec 2, 2010
  10. 2/1 bash: eliminate dependency on bash_completion libJonathan Nieder, Dec 2, 2010
  11. SZEDER GáborDec 2, 2010
  12. Jonathan NiederDec 3, 2010
  13. Stephen BoydDec 3, 2010
  14. SZEDER GáborDec 7, 2010
  15. Jonathan NiederDec 7, 2010
  16. SZEDER GáborDec 7, 2010
  17. Jonathan NiederDec 7, 2010
  18. Junio C HamanoDec 7, 2010
  19. 0/3 Make git-completion Bash 4 compatibleJonathan Nieder, Dec 15, 2010
  20. 1/3 bash: get --pretty=m<tab> completion to work with bash v4Jonathan Nieder, Dec 15, 2010
  21. 2/3 bash: simple reimplementation of _get_comp_words_by_refJonathan Nieder, Dec 15, 2010
  22. 3/3 Merge branch 'master' (early part) into pd/bash-4-completionJonathan Nieder, Dec 15, 2010

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.