Skip to content

Instantly share code, notes, and snippets.

@whut
Forked from sweiss3/git_checkout.lua
Last active October 29, 2015 10:55
Show Gist options
  • Save whut/6a5423abf837cdfb07e9 to your computer and use it in GitHub Desktop.
Save whut/6a5423abf837cdfb07e9 to your computer and use it in GitHub Desktop.
A clink script for supporting tab-completion of git branches when using "git checkout"
-- copy&pasted the code to support not only git checkout but also git merge
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
if rl_state.line_buffer:find("^git merge ") then
has_start_branch = not rl_state.line_buffer:find("^git merge[ ]*$")
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment