Last active
April 26, 2020 20:07
-
-
Save borekb/47ff1ebca7ee837eca4e232b2f091e2f to your computer and use it in GitHub Desktop.
cmder `clink.lua` to show number of commits to push. See comment below for description.
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
function get_git_num_unpushed_commits(branch_name) | |
for line in io.popen("git rev-list --count origin/"..branch_name..".."..branch_name.." 2>nul"):lines() do | |
return tonumber((string.gsub(line, "%s+", ""))) | |
end | |
return 0 | |
end | |
function git_prompt_filter() | |
-- Colors for git status | |
local colors = { | |
clean = "\x1b[1;37;40m", | |
dirty = "\x1b[31;1m", | |
unpushed_commits = "\x1b[38;5;123m", | |
} | |
local git_dir = get_git_dir() | |
if git_dir then | |
-- if we're inside of git repo then try to detect current branch | |
local branch = get_git_branch(git_dir) | |
if branch then | |
-- Has branch => therefore it is a git folder, now figure out status | |
if get_git_status() then | |
color = colors.clean | |
else | |
color = colors.dirty | |
end | |
local unpushed_commits = get_git_num_unpushed_commits(branch) | |
if (unpushed_commits > 0) then | |
branch = branch..colors.unpushed_commits.." ➚"..unpushed_commits..color | |
end | |
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", color.."("..branch..")") | |
return false | |
end | |
end | |
-- No git present or not in git file | |
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", "") | |
return false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds number of commits that the branch is ahead of origin, something like:
There is an assumption that the remote is
origin
. Also, this is my first attempt at Lua, some things are probably wrong.