Skip to content

Instantly share code, notes, and snippets.

@sweiss3
Created March 29, 2014 17:22
Show Gist options
  • Save sweiss3/9858452 to your computer and use it in GitHub Desktop.
Save sweiss3/9858452 to your computer and use it in GitHub Desktop.
A clink script for supporting tab-completion of git branches when using "git checkout"
function git_checkout_match_generator(text, first, last)
found_matches = false;
if rl_state.line_buffer:find("^git checkout ") then
has_start_branch = not rl_state.line_buffer:find("^git checkout[ ]*$")
for line in io.popen("git branch 2>nul"):lines() do
local m = line:match("[%* ] (.+)$")
if m then
if not has_start_branch then
clink.add_match(m)
found_matches = true;
elseif #text > 0 and m:find(text) then
clink.add_match(m)
found_matches = true;
end
end
end
end
return found_matches
end
clink.register_match_generator(git_checkout_match_generator, 10)
@wald-tq
Copy link

wald-tq commented Jun 20, 2017

@wald-tq
Copy link

wald-tq commented Jun 20, 2017

I was not satisfied with any of the solutions provided here, so I created a own implementation using the parser of clink.

It features better tab completion for example if you have the branches:

private/deva/ISU-124
private/devb/ISU-243

you can type:

git checkout pri<TAB>b<TAB>

which you can't do with the other solutions.

It's still work in progress but here is it: https://gist.github.com/wald-tq/0f274c3dd561dd8f0f7189c65b825f55

enjoy

@keocra
Copy link

keocra commented Nov 28, 2019

Thanks for this :-) Works like a charm @sweiss3

And thanks for this addition @glucas

After trying this for a bit, I found that I wanted completion to use any entered text as the beginning the branch name (rather than matching branch names that contain the text anywhere). Changing line 12 works:

                            elseif #text > 0 and m:sub(1, string.len(text)) == text then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment