Last active
December 24, 2018 21:54
-
-
Save wald-tq/0f274c3dd561dd8f0f7189c65b825f55 to your computer and use it in GitHub Desktop.
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
-- This is a script to provide git branch autocompletion to clink | |
-- which can be used in windows cmd, conemu and cmder | |
-- | |
-- Ctrl-Q in conemu to reload Clink Lua scripts | |
-- Clink match generators: https://github.com/mridgers/clink/blob/master/docs/clink.md#user-content-match-generators | |
-- based on: | |
-- https://gist.github.com/sweiss3/9858452 | |
-- https://gist.github.com/bhank/a85113b06632fc52f053533f81c2da2d | |
-- copied from clink.lua | |
local git_argument_tree = { | |
-- Porcelain and ancillary commands from git's man page. | |
"add", "am", "archive", "bisect", "branch", "bundle", "checkout", | |
"cherry-pick", "citool", "clean", "clone", "commit", "describe", "diff", | |
"fetch", "format-patch", "gc", "grep", "gui", "init", "log", "merge", "mv", | |
"notes", "pull", "push", "rebase", "reset", "revert", "rm", "shortlog", | |
"show", "stash", "status", "submodule", "tag", "config", "fast-export", | |
"fast-import", "filter-branch", "lost-found", "mergetool", "pack-refs", | |
"prune", "reflog", "relink", "remote", "repack", "replace", "repo-config", | |
"annotate", "blame", "cherry", "count-objects", "difftool", "fsck", | |
"get-tar-commit-id", "help", "instaweb", "merge-tree", "rerere", | |
"rev-parse", "show-branch", "verify-tag", "whatchanged" | |
} | |
parser = clink.arg.new_parser | |
function git_branches(word) | |
local matches = {} | |
for line in io.popen("git branch -a 2>nul"):lines() do | |
local branch = line:match("[%* ] (.+)$") | |
if branch then | |
if branch:find(word, 1, true) then | |
-- remove "remotes/" as we don't want to supply it for remote branches. | |
local branch_cleanup = branch:gsub("remotes/", "") | |
table.insert(matches, branch_cleanup) | |
end | |
end | |
end | |
return matches | |
end | |
function dir_match_generator_impl(text) | |
-- Strip off any path components that may be on text. | |
local prefix = "" | |
local i = text:find("[\\/:][^\\/:]*$") | |
if i then | |
prefix = text:sub(1, i) | |
end | |
local include_dots = text:find("%.+$") ~= nil | |
local matches = {} | |
local mask = text.."*" | |
-- Find matches. | |
for _, dir in ipairs(clink.find_dirs(mask, true)) do | |
local file = prefix..dir | |
if include_dots or (dir ~= "." and dir ~= "..") then | |
if clink.is_match(text, file) then | |
table.insert(matches, prefix..dir) | |
end | |
end | |
end | |
for _, file in ipairs(clink.find_files(mask, true)) do | |
if clink.is_match(text, file) then | |
table.insert(matches, prefix..file) | |
end | |
end | |
return matches | |
end | |
-------------------------------------------------------------------------------- | |
local function dir_match_generator(word) | |
local matches = dir_match_generator_impl(word) | |
-- If there was no matches but text is a dir then use it as the single match. | |
-- Otherwise tell readline that matches are files and it will do magic. | |
if #matches == 0 then | |
if clink.is_dir(rl_state.text) then | |
table.insert(matches, rl_state.text) | |
end | |
else | |
clink.matches_are_files() | |
end | |
return matches | |
end | |
-- create the parser: | |
-- first argument is from standard clink.lua | |
-- then provide 4 times the git_branches argument which should be enough | |
git_parser = clink.arg.new_parser() | |
git_parser:add_arguments(git_argument_tree, {git_branches, dir_match_generator}, {git_branches, dir_match_generator}, {git_branches, dir_match_generator}) | |
clink.arg.register_parser("git", git_parser) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment