Skip to content

Instantly share code, notes, and snippets.

@thomasnorris
Forked from bhank/git_checkout.lua
Last active February 4, 2019 19:10
Show Gist options
  • Save thomasnorris/b14647a63b850b22ef6cb1a09f18466b to your computer and use it in GitHub Desktop.
Save thomasnorris/b14647a63b850b22ef6cb1a09f18466b to your computer and use it in GitHub Desktop.
A clink script for supporting tab-completion of git branches when using git aliases
-- git commands which will autocomplete branch names after them:
local git_commands = {"co", "merge", "del"}
-- "g" is an alias I have for "git" so both of them are triggers
local git_triggers = {"g", "git"}
function git_checkout_match_generator(text, first, last)
local commandLine = rl_state.line_buffer
local matchedCommand = false
local matchedBranches = false
for _,command in pairs(git_commands) do
for _,trigger in pairs(git_triggers) do
if commandLine:find(trigger .. " " .. command .. " ", 1, true) then
matchedCommand = true
break
end
end
end
if matchedCommand then
-- get all branches and remove "HEAD" and "remotes/origin/"
for line in io.popen("git branch -a 2>nul"):lines() do
local branch = line:match("[%* ] (.+)$")
if not branch:find("HEAD", 1, true) then
branch = string.gsub(branch, "remotes/origin/", "")
if branch:find(text, 1, true) then
matchedBranches = true
clink.add_match(branch)
end
end
end
end
return matchedBranches
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