Skip to contentSkip to conversation

Teach bash about git-am/git-apply and their whitespace options.

PATCH New Shawn O. Pearce 1 participant 3 messages Aug 11, 2016 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 139669072795537547ec26b69115509100dcd7ec.1164658223.git.spearce@spearce.org

#1 lore raw

PATCH 2/4 Teach bash about git-commit's options.

Not only can we complete the long options for git-commit, but now we can also complete author strings by looking up all prior author names in all branches and offering them up as possible completions. This is similiar to the support used by the --author= option to git-log.

Unfortunately this changes names which use ' within their string. It seems to be bash's fault here, the ' is being passed out by sed but is getting removed during the completion generation. Fortunately ' appears in only one name in git.git so the damage is likely to be rather low.

---
 contrib/completion/git-completion.bash |   28 ++++++++++++++++++++++++++++
 1 files changed, 28 insertions(+), 0 deletions(-)
contrib/completion/git-completion.bash +28 -0
index e53f040..d4eb138 100755
@@ -332,6 +332,32 @@ _git_cherry_pick ()
 	esac
 }
 
+_git_commit ()
+{
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+	case "$cur" in
+	--author=*)
+		local IFS=$'\n'
+		cur="${cur##--author=}"
+		COMPREPLY=($(compgen -P \' -S \' \
+			-W "$(git log --pretty=raw --all \
+			| sed -n -e "/^author /{
+				s/^author //
+				s/>.*/>/
+				p
+			}")" -- "${cur##\'}"))
+		return
+		;;
+	--*)
+		COMPREPLY=($(compgen -W "
+			--all --author=\' --signoff --verify --no-verify
+			--edit --amend --include --only
+			" -- "$cur"))
+		return
+	esac
+	COMPREPLY=()
+}
+
 _git_diff ()
 {
 	__git_complete_file
@@ -720,6 +746,7 @@ _git ()
 	cat-file)    _git_cat_file ;;
 	checkout)    _git_checkout ;;
 	cherry-pick) _git_cherry_pick ;;
+	commit)      _git_commit ;;
 	diff)        _git_diff ;;
 	diff-tree)   _git_diff_tree ;;
 	fetch)       _git_fetch ;;
@@ -754,6 +781,7 @@ complete -o default            -F _git_branch git-branch
 complete -o default -o nospace -F _git_cat_file git-cat-file
 complete -o default            -F _git_checkout git-checkout
 complete -o default            -F _git_cherry_pick git-cherry-pick
+complete -o default            -F _git_commit git-commit
 complete -o default -o nospace -F _git_diff git-diff
 complete -o default            -F _git_diff_tree git-diff-tree
 complete -o default -o nospace -F _git_fetch git-fetch
Signature · 1 line
1.4.4.1.ge3fb
#2 lore raw

PATCH 3/4 Cache the list of merge strategies and available commands during load.

In reply to a message not in the archive

Since the user's git installation is not likely to grow a new command or merge strategy in the lifespan of the current shell process we can save time during completion operations by caching these lists during sourcing of the completion support.

If the git executable is not available or we run into errors while caching at load time then we defer these to runtime and generate the list on the fly. This might happen if the user doesn't put git into their PATH until after the completion script gets sourced.

---
This is by request of Junio. :-)

 contrib/completion/git-completion.bash |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
contrib/completion/git-completion.bash +19 -1
index d4eb138..ba3adb6 100755
@@ -18,7 +18,13 @@
 #    2) Added the following line to your .bashrc:
 #        source ~/.git-completion.sh
 #
-#    3) Consider changing your PS1 to also show the current branch:
+#    3) You may want to make sure the git executable is available
+#       in your PATH before this script is sourced, as some caching
+#       is performed while the script loads.  If git isn't found
+#       at source time then all lookups will be done on demand,
+#       which may be slightly slower.
+#
+#    4) Consider changing your PS1 to also show the current branch:
 #        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
 #
 #       The argument to __git_ps1 will be displayed only if you
@@ -150,6 +156,10 @@ __git_remotes ()
 
 __git_merge_strategies ()
 {
+	if [ -n "$__git_merge_strategylist" ]; then
+		echo "$__git_merge_strategylist"
+		return
+	fi
 	sed -n "/^all_strategies='/{
 		s/^all_strategies='//
 		s/'//
@@ -157,6 +167,8 @@ __git_merge_strategies ()
 		q
 		}" "$(git --exec-path)/git-merge"
 }
+__git_merge_strategylist=
+__git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
 
 __git_complete_file ()
 {
@@ -214,6 +226,10 @@ __git_complete_revlist ()
 
 __git_commands ()
 {
+	if [ -n "$__git_commandlist" ]; then
+		echo "$__git_commandlist"
+		return
+	fi
 	local i IFS=" "$'\n'
 	for i in $(git help -a|egrep '^ ')
 	do
@@ -263,6 +279,8 @@ __git_commands ()
 		esac
 	done
 }
+__git_commandlist=
+__git_commandlist="$(__git_commands 2>/dev/null)"
 
 __git_aliases ()
 {
Signature · 1 line
1.4.4.1.ge3fb
#3 lore raw

PATCH 4/4 Teach bash about git-am/git-apply and their whitespace options.

In reply to a message not in the archive
---
 contrib/completion/git-completion.bash |   53 ++++++++++++++++++++++++++++++++
 1 files changed, 53 insertions(+), 0 deletions(-)
contrib/completion/git-completion.bash +53 -0
index ba3adb6..5b7d9d3 100755
@@ -307,6 +307,54 @@ __git_aliased_command ()
 	done
 }
 
+__git_whitespacelist="nowarn warn error error-all strip"
+
+_git_am ()
+{
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+	if [ -d .dotest ]; then
+		COMPREPLY=($(compgen -W "
+			--skip --resolved
+			" -- "$cur"))
+		return
+	fi
+	case "$cur" in
+	--whitespace=*)
+		COMPREPLY=($(compgen -W "$__git_whitespacelist" \
+			-- "${cur##--whitespace=}"))
+		return
+		;;
+	--*)
+		COMPREPLY=($(compgen -W "
+			--signoff --utf8 --binary --3way --interactive
+			--whitespace=
+			" -- "$cur"))
+		return
+	esac
+	COMPREPLY=()
+}
+
+_git_apply ()
+{
+	local cur="${COMP_WORDS[COMP_CWORD]}"
+	case "$cur" in
+	--whitespace=*)
+		COMPREPLY=($(compgen -W "$__git_whitespacelist" \
+			-- "${cur##--whitespace=}"))
+		return
+		;;
+	--*)
+		COMPREPLY=($(compgen -W "
+			--stat --numstat --summary --check --index
+			--cached --index-info --reverse --reject --unidiff-zero
+			--apply --no-add --exclude=
+			--whitespace= --inaccurate-eof --verbose
+			" -- "$cur"))
+		return
+	esac
+	COMPREPLY=()
+}
+
 _git_branch ()
 {
 	local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -760,6 +808,8 @@ _git ()
 	[ "$expansion" ] && command="$expansion"
 
 	case "$command" in
+	am)          _git_am ;;
+	apply)       _git_apply ;;
 	branch)      _git_branch ;;
 	cat-file)    _git_cat_file ;;
 	checkout)    _git_checkout ;;
@@ -795,6 +845,8 @@ _gitk ()
 
 complete -o default -o nospace -F _git git
 complete -o default            -F _gitk gitk
+complete -o default            -F _git_am git-am
+complete -o default            -F _git_apply git-apply
 complete -o default            -F _git_branch git-branch
 complete -o default -o nospace -F _git_cat_file git-cat-file
 complete -o default            -F _git_checkout git-checkout
@@ -824,6 +876,7 @@ complete -o default -o nospace -F _git_log git-whatchanged
 # included the '.exe' suffix.
 #
 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
+complete -o default            -F _git_apply git-apply.exe
 complete -o default -o nospace -F _git git.exe
 complete -o default            -F _git_branch git-branch.exe
 complete -o default -o nospace -F _git_cat_file git-cat-file.exe