diff mbox series

[v2,1/2] completion: use "prev" variable instead of introducing "prevword"

Message ID 20200925211124.1673337-1-uzonyi.akos@gmail.com (mailing list archive)
State Accepted
Commit c09d1280f7787020e28844e8fd615d973d2a811e
Headers show
Series [v2,1/2] completion: use "prev" variable instead of introducing "prevword" | expand

Commit Message

Ákos Uzonyi Sept. 25, 2020, 9:11 p.m. UTC
In both _git_checkout and _git_switch a new "prevword" variable were
introduced, however the "prev" variable already contains the last word.

The "prevword" variable is replaced with "prev", and the case is moved
to the beginning of the function, like it's done in many other places
(e.g. _git_commit). Also the indentaion of the case is fixed.

Signed-off-by: Ákos Uzonyi <uzonyi.akos@gmail.com>
---
 contrib/completion/git-completion.bash | 66 +++++++++++++-------------
 1 file changed, 32 insertions(+), 34 deletions(-)

Comments

Jacob Keller Sept. 25, 2020, 10:53 p.m. UTC | #1
On Fri, Sep 25, 2020 at 3:00 PM Ákos Uzonyi <uzonyi.akos@gmail.com> wrote:
>
> In both _git_checkout and _git_switch a new "prevword" variable were
> introduced, however the "prev" variable already contains the last word.
>
> The "prevword" variable is replaced with "prev", and the case is moved
> to the beginning of the function, like it's done in many other places
> (e.g. _git_commit). Also the indentaion of the case is fixed.
>

Ahhh... didn't realize $prev existed.

> Signed-off-by: Ákos Uzonyi <uzonyi.akos@gmail.com>
> ---
>  contrib/completion/git-completion.bash | 66 +++++++++++++-------------
>  1 file changed, 32 insertions(+), 34 deletions(-)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 8be4a0316e..3d02bd4de7 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -1508,6 +1508,22 @@ _git_checkout ()
>  {
>         __git_has_doubledash && return
>
> +       local dwim_opt="$(__git_checkout_default_dwim_mode)"
> +
> +       case "$prev" in
> +       -b|-B|--orphan)
> +               # Complete local branches (and DWIM branch
> +               # remote branch names) for an option argument
> +               # specifying a new branch name. This is for
> +               # convenience, assuming new branches are
> +               # possibly based on pre-existing branch names.
> +               __git_complete_refs $dwim_opt --mode="heads"
> +               return
> +               ;;
> +       *)
> +               ;;
> +       esac
> +
>         case "$cur" in
>         --conflict=*)
>                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
> @@ -1516,23 +1532,6 @@ _git_checkout ()
>                 __gitcomp_builtin checkout
>                 ;;
>         *)
> -               local dwim_opt="$(__git_checkout_default_dwim_mode)"
> -               local prevword prevword="${words[cword-1]}"
> -
> -               case "$prevword" in
> -                       -b|-B|--orphan)
> -                               # Complete local branches (and DWIM branch
> -                               # remote branch names) for an option argument
> -                               # specifying a new branch name. This is for
> -                               # convenience, assuming new branches are
> -                               # possibly based on pre-existing branch names.
> -                               __git_complete_refs $dwim_opt --mode="heads"
> -                               return
> -                               ;;
> -                       *)
> -                               ;;
> -               esac
> -
>                 # At this point, we've already handled special completion for
>                 # the arguments to -b/-B, and --orphan. There are 3 main
>                 # things left we can possibly complete:
> @@ -2392,6 +2391,22 @@ _git_status ()
>
>  _git_switch ()
>  {
> +       local dwim_opt="$(__git_checkout_default_dwim_mode)"
> +
> +       case "$prev" in
> +       -c|-C|--orphan)
> +               # Complete local branches (and DWIM branch
> +               # remote branch names) for an option argument
> +               # specifying a new branch name. This is for
> +               # convenience, assuming new branches are
> +               # possibly based on pre-existing branch names.
> +               __git_complete_refs $dwim_opt --mode="heads"
> +               return
> +               ;;
> +       *)
> +               ;;
> +       esac
> +
>         case "$cur" in
>         --conflict=*)
>                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
> @@ -2400,23 +2415,6 @@ _git_switch ()
>                 __gitcomp_builtin switch
>                 ;;
>         *)
> -               local dwim_opt="$(__git_checkout_default_dwim_mode)"
> -               local prevword prevword="${words[cword-1]}"
> -
> -               case "$prevword" in
> -                       -c|-C|--orphan)
> -                               # Complete local branches (and DWIM branch
> -                               # remote branch names) for an option argument
> -                               # specifying a new branch name. This is for
> -                               # convenience, assuming new branches are
> -                               # possibly based on pre-existing branch names.
> -                               __git_complete_refs $dwim_opt --mode="heads"
> -                               return
> -                               ;;
> -                       *)
> -                               ;;
> -               esac
> -
>                 # Unlike in git checkout, git switch --orphan does not take
>                 # a start point. Thus we really have nothing to complete after
>                 # the branch name.
> --
> 2.28.0
>
Junio C Hamano Sept. 26, 2020, 10:31 p.m. UTC | #2
Jacob Keller <jacob.keller@gmail.com> writes:

> On Fri, Sep 25, 2020 at 3:00 PM Ákos Uzonyi <uzonyi.akos@gmail.com> wrote:
>>
>> In both _git_checkout and _git_switch a new "prevword" variable were
>> introduced, however the "prev" variable already contains the last word.
>>
>> The "prevword" variable is replaced with "prev", and the case is moved
>> to the beginning of the function, like it's done in many other places
>> (e.g. _git_commit). Also the indentaion of the case is fixed.
>>
>
> Ahhh... didn't realize $prev existed.

Yup, makes a lot of sense.

Thanks, Ákos.  Will queue.

>
>> Signed-off-by: Ákos Uzonyi <uzonyi.akos@gmail.com>
>> ---
>>  contrib/completion/git-completion.bash | 66 +++++++++++++-------------
>>  1 file changed, 32 insertions(+), 34 deletions(-)
>>
>> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
>> index 8be4a0316e..3d02bd4de7 100644
>> --- a/contrib/completion/git-completion.bash
>> +++ b/contrib/completion/git-completion.bash
>> @@ -1508,6 +1508,22 @@ _git_checkout ()
>>  {
>>         __git_has_doubledash && return
>>
>> +       local dwim_opt="$(__git_checkout_default_dwim_mode)"
>> +
>> +       case "$prev" in
>> +       -b|-B|--orphan)
>> +               # Complete local branches (and DWIM branch
>> +               # remote branch names) for an option argument
>> +               # specifying a new branch name. This is for
>> +               # convenience, assuming new branches are
>> +               # possibly based on pre-existing branch names.
>> +               __git_complete_refs $dwim_opt --mode="heads"
>> +               return
>> +               ;;
>> +       *)
>> +               ;;
>> +       esac
>> +
>>         case "$cur" in
>>         --conflict=*)
>>                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
>> @@ -1516,23 +1532,6 @@ _git_checkout ()
>>                 __gitcomp_builtin checkout
>>                 ;;
>>         *)
>> -               local dwim_opt="$(__git_checkout_default_dwim_mode)"
>> -               local prevword prevword="${words[cword-1]}"
>> -
>> -               case "$prevword" in
>> -                       -b|-B|--orphan)
>> -                               # Complete local branches (and DWIM branch
>> -                               # remote branch names) for an option argument
>> -                               # specifying a new branch name. This is for
>> -                               # convenience, assuming new branches are
>> -                               # possibly based on pre-existing branch names.
>> -                               __git_complete_refs $dwim_opt --mode="heads"
>> -                               return
>> -                               ;;
>> -                       *)
>> -                               ;;
>> -               esac
>> -
>>                 # At this point, we've already handled special completion for
>>                 # the arguments to -b/-B, and --orphan. There are 3 main
>>                 # things left we can possibly complete:
>> @@ -2392,6 +2391,22 @@ _git_status ()
>>
>>  _git_switch ()
>>  {
>> +       local dwim_opt="$(__git_checkout_default_dwim_mode)"
>> +
>> +       case "$prev" in
>> +       -c|-C|--orphan)
>> +               # Complete local branches (and DWIM branch
>> +               # remote branch names) for an option argument
>> +               # specifying a new branch name. This is for
>> +               # convenience, assuming new branches are
>> +               # possibly based on pre-existing branch names.
>> +               __git_complete_refs $dwim_opt --mode="heads"
>> +               return
>> +               ;;
>> +       *)
>> +               ;;
>> +       esac
>> +
>>         case "$cur" in
>>         --conflict=*)
>>                 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
>> @@ -2400,23 +2415,6 @@ _git_switch ()
>>                 __gitcomp_builtin switch
>>                 ;;
>>         *)
>> -               local dwim_opt="$(__git_checkout_default_dwim_mode)"
>> -               local prevword prevword="${words[cword-1]}"
>> -
>> -               case "$prevword" in
>> -                       -c|-C|--orphan)
>> -                               # Complete local branches (and DWIM branch
>> -                               # remote branch names) for an option argument
>> -                               # specifying a new branch name. This is for
>> -                               # convenience, assuming new branches are
>> -                               # possibly based on pre-existing branch names.
>> -                               __git_complete_refs $dwim_opt --mode="heads"
>> -                               return
>> -                               ;;
>> -                       *)
>> -                               ;;
>> -               esac
>> -
>>                 # Unlike in git checkout, git switch --orphan does not take
>>                 # a start point. Thus we really have nothing to complete after
>>                 # the branch name.
>> --
>> 2.28.0
>>
diff mbox series

Patch

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 8be4a0316e..3d02bd4de7 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1508,6 +1508,22 @@  _git_checkout ()
 {
 	__git_has_doubledash && return
 
+	local dwim_opt="$(__git_checkout_default_dwim_mode)"
+
+	case "$prev" in
+	-b|-B|--orphan)
+		# Complete local branches (and DWIM branch
+		# remote branch names) for an option argument
+		# specifying a new branch name. This is for
+		# convenience, assuming new branches are
+		# possibly based on pre-existing branch names.
+		__git_complete_refs $dwim_opt --mode="heads"
+		return
+		;;
+	*)
+		;;
+	esac
+
 	case "$cur" in
 	--conflict=*)
 		__gitcomp "diff3 merge" "" "${cur##--conflict=}"
@@ -1516,23 +1532,6 @@  _git_checkout ()
 		__gitcomp_builtin checkout
 		;;
 	*)
-		local dwim_opt="$(__git_checkout_default_dwim_mode)"
-		local prevword prevword="${words[cword-1]}"
-
-		case "$prevword" in
-			-b|-B|--orphan)
-				# Complete local branches (and DWIM branch
-				# remote branch names) for an option argument
-				# specifying a new branch name. This is for
-				# convenience, assuming new branches are
-				# possibly based on pre-existing branch names.
-				__git_complete_refs $dwim_opt --mode="heads"
-				return
-				;;
-			*)
-				;;
-		esac
-
 		# At this point, we've already handled special completion for
 		# the arguments to -b/-B, and --orphan. There are 3 main
 		# things left we can possibly complete:
@@ -2392,6 +2391,22 @@  _git_status ()
 
 _git_switch ()
 {
+	local dwim_opt="$(__git_checkout_default_dwim_mode)"
+
+	case "$prev" in
+	-c|-C|--orphan)
+		# Complete local branches (and DWIM branch
+		# remote branch names) for an option argument
+		# specifying a new branch name. This is for
+		# convenience, assuming new branches are
+		# possibly based on pre-existing branch names.
+		__git_complete_refs $dwim_opt --mode="heads"
+		return
+		;;
+	*)
+		;;
+	esac
+
 	case "$cur" in
 	--conflict=*)
 		__gitcomp "diff3 merge" "" "${cur##--conflict=}"
@@ -2400,23 +2415,6 @@  _git_switch ()
 		__gitcomp_builtin switch
 		;;
 	*)
-		local dwim_opt="$(__git_checkout_default_dwim_mode)"
-		local prevword prevword="${words[cword-1]}"
-
-		case "$prevword" in
-			-c|-C|--orphan)
-				# Complete local branches (and DWIM branch
-				# remote branch names) for an option argument
-				# specifying a new branch name. This is for
-				# convenience, assuming new branches are
-				# possibly based on pre-existing branch names.
-				__git_complete_refs $dwim_opt --mode="heads"
-				return
-				;;
-			*)
-				;;
-		esac
-
 		# Unlike in git checkout, git switch --orphan does not take
 		# a start point. Thus we really have nothing to complete after
 		# the branch name.