From 604f80fca9966d34e4f4c92c43b7348f8f732723 Mon Sep 17 00:00:00 2001
From: Felipe Contreras <felipe.contreras@gmail.com>
Date: Tue, 3 Nov 2020 12:20:19 -0600
Subject: [PATCH 1/3] completion: zsh: fix loading

We need to load bashcominit, otherwise complete() isn't available.

  __git_complete:5: command not found: complete

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/tig-completion.bash | 7 -------
 contrib/tig-completion.zsh  | 1 +
 2 files changed, 1 insertion(+), 7 deletions(-)

diff --git a/contrib/tig-completion.bash b/contrib/tig-completion.bash
index 5aef3fd8f..0f9f23453 100755
--- a/contrib/tig-completion.bash
+++ b/contrib/tig-completion.bash
@@ -86,13 +86,6 @@ _tig() {
 	esac
 }
 
-# Detect if current shell is ZSH, and if so, load this file in bash
-# compatibility mode.
-if [ -n "$ZSH_VERSION" ]; then
-	autoload bashcompinit
-	bashcompinit
-fi
-
 # we use internal git-completion functions, so wrap _tig for all necessary
 # variables (like cword and prev) to be defined
 __git_complete tig _tig 
diff --git a/contrib/tig-completion.zsh b/contrib/tig-completion.zsh
index bad33bad9..0aad9cee8 100644
--- a/contrib/tig-completion.zsh
+++ b/contrib/tig-completion.zsh
@@ -14,6 +14,7 @@
 
 _tig () {
   local e
+  autoload -U bashcompinit && bashcompinit
   e=$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
   if [ -f $e ]; then
     GIT_SOURCING_ZSH_COMPLETION=y . $e

From 51e499c22ef07ef960c724287208094deaad7a0b Mon Sep 17 00:00:00 2001
From: Felipe Contreras <felipe.contreras@gmail.com>
Date: Tue, 3 Nov 2020 14:24:37 -0600
Subject: [PATCH 2/3] completion: bash: align with git completion

COMPREPLY and compgen aren't supposed to be used directly.

Also in order for the Git's zsh emulation to work correctly, _tig
shouldn't be exposed.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/tig-completion.bash | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/contrib/tig-completion.bash b/contrib/tig-completion.bash
index 0f9f23453..4a9deb05d 100755
--- a/contrib/tig-completion.bash
+++ b/contrib/tig-completion.bash
@@ -33,8 +33,6 @@ __tig_options="
 	-v --version
 	-h --help
 	-C
-	--
-	+
 "
 __tig_commands="
 	blame
@@ -47,38 +45,31 @@ __tig_commands="
 	show
 "
 
-_tig() {
+__tig_main () {
 	# parse already existing parameters
 	local i c=1 command
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
 		case "$i" in
 		--)	command="log"; break;;
+		-C)	return;;
 		-*)	;;
 		*)	command="$i"; break ;;
 		esac
 		c=$((++c))
 	done
 
-	# options -- only before command
-	case "$command$cur" in
-		-C*)
-			COMPREPLY=( $(compgen -d -P '-C' -- ${cur##-C}) )
-			return
-			;;
-	esac
-
 	# commands
 	case "$command" in
 		refs|status|stash)
-			COMPREPLY=( $(compgen -W "$__tig_options" -- "$cur") )
+			__gitcomp "$__tig_options"
 			;;
 		reflog)
 			__git_complete_command log
 			;;
 		"")
 			__git_complete_command log
-			__gitcompappend "$(compgen -W "$__tig_options $__tig_commands" -- "$cur")"
+			__gitcomp "$__tig_options $__tig_commands"
 			;;
 		*)
 			__git_complete_command $command
@@ -88,11 +79,11 @@ _tig() {
 
 # we use internal git-completion functions, so wrap _tig for all necessary
 # variables (like cword and prev) to be defined
-__git_complete tig _tig 
+__git_complete tig __tig_main
 
 # The following are necessary only for Cygwin, and only are needed
 # when the user has tab-completed the executable name and consequently
 # included the '.exe' suffix.
 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
-	__git_complete tig.exe _tig
+	__git_complete tig.exe __tig_main
 fi

From e073f1b5e6b939f55692407183cf24d972b81831 Mon Sep 17 00:00:00 2001
From: Felipe Contreras <felipe.contreras@gmail.com>
Date: Tue, 3 Nov 2020 14:32:08 -0600
Subject: [PATCH 3/3] completion: zsh: improve completion

We don't need all Zsh's bash completion emulation (which is buggy), it's
better to use Git's Zsh completion.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/tig-completion.zsh | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/contrib/tig-completion.zsh b/contrib/tig-completion.zsh
index 0aad9cee8..facefc605 100644
--- a/contrib/tig-completion.zsh
+++ b/contrib/tig-completion.zsh
@@ -10,17 +10,23 @@
 # then add following to your ~/.zshrc file:
 #
 #  fpath=(~/.zsh $fpath)
+#
+# You also need Git's Zsh completion installed:
+#
+# https://github.com/felipec/git-completion/blob/master/git-completion.zsh
 
 
 _tig () {
   local e
-  autoload -U bashcompinit && bashcompinit
-  e=$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
-  if [ -f $e ]; then
-    GIT_SOURCING_ZSH_COMPLETION=y . $e
-  fi
+
+  compdef _git tig
+
   e=$(dirname ${funcsourcetrace[1]%:*})/tig-completion.bash
   if [ -f $e ]; then
+    # Temporarily override __git_complete so the bash script doesn't complain
+    local old="$functions[__git_complete]"
+    functions[__git_complete]=:
     . $e
+    functions[__git_complete]="$old"
   fi
 }