Skip to content

Bash completion for gtr wrapper breaks with 2+ character input #156

@zhfeng

Description

@zhfeng

Bug Description

Bash tab completion for the gtr wrapper function (generated by git gtr init bash) fails when the user has typed 2 or more characters of the completion target. It works correctly with git gtr.

Example (given branches: main, feature/login, fix/typo):

  • gtr ai f<TAB> → works (1-char input, shows feature/login and fix/typo)
  • gtr ai fe<TAB>nothing (2-char input, should show feature/login)
  • gtr ai ma<TAB>wrong result (returns a different branch instead of main)
  • git gtr ai fe<TAB> → works correctly in all cases

Root Cause

The _gtr_completion function (output of git gtr init bash) updates COMP_WORDS and COMP_CWORD when delegating to _git_gtr, but does not update COMP_LINE and COMP_POINT:

# Current code in _gtr_completion (delegation branch):
COMP_WORDS=(git gtr "${COMP_WORDS[@]:1}")
(( COMP_CWORD += 1 ))
_git_gtr

The bash-completion v2 function _init_completion_comp_get_words_comp__get_cword_at_cursor re-parses COMP_LINE (not COMP_WORDS) to determine the current word (cur). Because COMP_LINE still contains "gtr ai fe" while COMP_WORDS has been changed to (git gtr ai fe), the word-boundary matching in _comp__get_cword_at_cursor fails — it tries to match words[0]="git" against a COMP_LINE starting with "gtr", corrupting the cur variable.

With single-character inputs it happens to work by coincidence (the misaligned parsing still lands on the right character), but with 2+ characters the misalignment causes cur to be wrong or empty.

Proposed Fix

Update COMP_LINE and COMP_POINT alongside COMP_WORDS and COMP_CWORD in the delegation branches of _gtr_completion. Replace "gtr " (4 chars) with "git gtr " (8 chars) in COMP_LINE, and shift COMP_POINT by 4:

COMP_WORDS=(git gtr "${COMP_WORDS[@]:1}")
(( COMP_CWORD += 1 ))
COMP_LINE="git gtr ${COMP_LINE#gtr }"
(( COMP_POINT += 4 ))
_git_gtr

This needs to be applied in both delegation sites within _gtr_completion — the new branch and the general fallback branch.

Verification

Simulated completion calls before and after the fix (given branches: main, feature/login, fix/typo):

Before (broken):

gtr ai ""    => 4 matches (ok)
gtr ai "f"   => 2 matches (ok - by coincidence)
gtr ai "fe"  => 0 matches (WRONG - should be 1)
gtr ai "fi"  => 0 matches (WRONG - should be 1)
gtr ai "ma"  => wrong branch returned (WRONG - should be "main")

After (fixed):

gtr ai ""    => 4 matches (ok)
gtr ai "f"   => 2 matches (ok)
gtr ai "fe"  => 1 match: feature/login (ok)
gtr ai "fi"  => 1 match: fix/typo (ok)
gtr ai "ma"  => 1 match: main (ok)

Environment

  • macOS (Darwin)
  • git-gtr 2.5.0 (Homebrew)
  • bash 5.x (Homebrew) with bash-completion@2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions