Skip to content

Commit

Permalink
Merge pull request #29 from mattdkerr/master
Browse files Browse the repository at this point in the history
Fix prompt lua for inaccessible git branch method
  • Loading branch information
AmrEldib committed Mar 9, 2018
2 parents d1a3a40 + 466687d commit 070af0e
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 070af0e

Please sign in to comment.