-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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