Skip to content

Commit ffd61db

Browse files
committed
Merge pull request #697 from vladimir-kotikov/patch-2
Speed up git prompt filtering
2 parents 3657795 + 53e0307 commit ffd61db

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

Diff for: config/cmder.lua

+19-12
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,23 @@ end
118118

119119
---
120120
-- Find out current branch
121-
-- @return {false|git branch name}
121+
-- @return {nil|git branch name}
122122
---
123-
function get_git_branch()
124-
for line in io.popen("git branch 2>nul"):lines() do
125-
local m = line:match("%* (.+)$")
126-
if m then
127-
return m
128-
end
129-
end
123+
function get_git_branch(git_dir)
124+
local git_dir = git_dir or get_git_dir()
130125

131-
return false
126+
-- If git directory not found then we're probably outside of repo
127+
-- or something went wrong. The same is when head_file is nil
128+
local head_file = git_dir and io.open(git_dir..'/HEAD')
129+
if not head_file then return end
130+
131+
local HEAD = head_file:read()
132+
head_file:close()
133+
134+
-- if HEAD matches branch expression, then we're on named branch
135+
-- otherwise it is a detached commit
136+
local branch_name = HEAD:match('ref: refs/heads/(.+)')
137+
return branch_name or 'HEAD detached at '..HEAD:sub(1, 7)
132138
end
133139

134140
---
@@ -147,9 +153,10 @@ function git_prompt_filter()
147153
dirty = "\x1b[31;1m",
148154
}
149155

150-
if get_git_dir() then
156+
local git_dir = get_git_dir()
157+
if git_dir then
151158
-- if we're inside of git repo then try to detect current branch
152-
local branch = get_git_branch()
159+
local branch = get_git_branch(git_dir)
153160
if branch then
154161
-- Has branch => therefore it is a git folder, now figure out status
155162
if get_git_status() then
@@ -181,4 +188,4 @@ for _,lua_module in ipairs(clink.find_files(completions_dir..'*.lua')) do
181188
-- so config reloading using Alt-Q won't reload updated modules.
182189
dofile(filename)
183190
end
184-
end
191+
end

0 commit comments

Comments
 (0)