Skip to content

Commit

Permalink
Fix prompt lua for inaccessible git branch method
Browse files Browse the repository at this point in the history
clink.lua in Cmder changed get_git_branch() from a global method to a local method. This copies the method over, to make this lua independent of the other one.
  • Loading branch information
Matt Kerr committed Mar 6, 2018
1 parent d1a3a40 commit 466687d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion powerline_prompt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ local function get_folder_name(path)
return string.sub(path, string.len(path) - slashIndex + 2)
end

---
-- Find out current branch
-- @return {nil|git branch name}
---
local function get_git_branch(git_dir)
git_dir = git_dir or get_git_dir()

-- If git directory not found then we're probably outside of repo
-- or something went wrong. The same is when head_file is nil
local head_file = git_dir and io.open(git_dir..'/HEAD')
if not head_file then return end

local HEAD = head_file:read()
head_file:close()

-- if HEAD matches branch expression, then we're on named branch
-- otherwise it is a detached commit
local branch_name = HEAD:match('ref: refs/heads/(.+)')

return branch_name or 'HEAD detached at '..HEAD:sub(1, 7)
end

-- Resets the prompt
function lambda_prompt_filter()
cwd = clink.get_cwd()
Expand Down Expand Up @@ -216,4 +238,4 @@ end
-- override the built-in filters
clink.prompt.register_filter(lambda_prompt_filter, 55)
clink.prompt.register_filter(colorful_hg_prompt_filter, 60)
clink.prompt.register_filter(colorful_git_prompt_filter, 60)
clink.prompt.register_filter(colorful_git_prompt_filter, 60)

0 comments on commit 466687d

Please sign in to comment.