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
Bug Description
Bash tab completion for the
gtrwrapper function (generated bygit gtr init bash) fails when the user has typed 2 or more characters of the completion target. It works correctly withgit gtr.Example (given branches:
main,feature/login,fix/typo):gtr ai f<TAB>→ works (1-char input, showsfeature/loginandfix/typo)gtr ai fe<TAB>→ nothing (2-char input, should showfeature/login)gtr ai ma<TAB>→ wrong result (returns a different branch instead ofmain)git gtr ai fe<TAB>→ works correctly in all casesRoot Cause
The
_gtr_completionfunction (output ofgit gtr init bash) updatesCOMP_WORDSandCOMP_CWORDwhen delegating to_git_gtr, but does not updateCOMP_LINEandCOMP_POINT:The bash-completion v2 function
_init_completion→_comp_get_words→_comp__get_cword_at_cursorre-parsesCOMP_LINE(notCOMP_WORDS) to determine the current word (cur). BecauseCOMP_LINEstill contains"gtr ai fe"whileCOMP_WORDShas been changed to(git gtr ai fe), the word-boundary matching in_comp__get_cword_at_cursorfails — it tries to matchwords[0]="git"against aCOMP_LINEstarting with"gtr", corrupting thecurvariable.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
curto be wrong or empty.Proposed Fix
Update
COMP_LINEandCOMP_POINTalongsideCOMP_WORDSandCOMP_CWORDin the delegation branches of_gtr_completion. Replace"gtr "(4 chars) with"git gtr "(8 chars) inCOMP_LINE, and shiftCOMP_POINTby 4:This needs to be applied in both delegation sites within
_gtr_completion— thenewbranch and the general fallback branch.Verification
Simulated completion calls before and after the fix (given branches:
main,feature/login,fix/typo):Before (broken):
After (fixed):
Environment