-
-
Save sweiss3/9858452 to your computer and use it in GitHub Desktop.
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) |
how do i include this in my local cmder?
@rrubiorr81 - Save it to your cmder config dir and open a new shell.
e.g. C:/cmder/config/git-checkout.lua
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
Works perfectly in Cmder. Thank You!
Thanks
Works like a champ -- thanks so much!
This worked great, thanks a lot
Bear with me, I'm a lua noob... If anyone is interested, I've extended the above to also do path completions when any text is found and it doesn't match branches -- for checking out the original version of files within the repo. It's probably not pretty, but seems to do the trick.
function git_checkout_match_generator(text, first, last)
found_matches = false
found_branch_match = 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_branch_match = true
found_matches = true
end
end
end
end
if not found_matches or (#text > 0 and not found_branch_match) then
local where = "."
if #text > 0 then
where = text
end
local lsresult = ls(where)
for k,line in pairs(lsresult) do
if #text > 0 and line:find(text) then
clink.add_match(line)
found_matches = true
elseif #text == 0 then
clink.add_match(line)
found_matches = true
end
end
end
return found_matches
end
clink.register_match_generator(git_checkout_match_generator, 10)
My only problem is it doesn't support aliases for checkout.
Example .gitconfig:
[alias]
br = branch
co = checkout
ca = commit --amend
cm = commit -m
cv = commit -v
df = diff
dc = diff --cached
f = fetch --all --prune
pl = pull
rbi = rebase -i
rh = reset HEAD
st = status
Ideally there would be autocomplete after I type git co
.
I don't know Lua, but I made it work with "git co" by changing lines 3 and 4 to this:
if rl_state.line_buffer:find("^git checkout ") or rl_state.line_buffer:find("^git co ") then
has_start_branch = not rl_state.line_buffer:find("^git checkout[ ]*$") and not rl_state.line_buffer:find("^git co[ ]*$")
Update: I rewrote it to support a list of git commands (as well as fixing a bug handling branches with "-" in them, by using plain-text find): https://gist.github.com/bhank/a85113b06632fc52f053533f81c2da2d
So the fork from @bhank looks most promising: https://gist.github.com/bhank/a85113b06632fc52f053533f81c2da2d
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
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
In Cmder, works like a charm. Thanks