@@ -118,17 +118,23 @@ end
118
118
119
119
---
120
120
-- Find out current branch
121
- -- @return {false |git branch name}
121
+ -- @return {nil |git branch name}
122
122
---
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 ()
130
125
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 )
132
138
end
133
139
134
140
---
@@ -147,9 +153,10 @@ function git_prompt_filter()
147
153
dirty = " \x1b [31;1m" ,
148
154
}
149
155
150
- if get_git_dir () then
156
+ local git_dir = get_git_dir ()
157
+ if git_dir then
151
158
-- 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 )
153
160
if branch then
154
161
-- Has branch => therefore it is a git folder, now figure out status
155
162
if get_git_status () then
@@ -181,4 +188,4 @@ for _,lua_module in ipairs(clink.find_files(completions_dir..'*.lua')) do
181
188
-- so config reloading using Alt-Q won't reload updated modules.
182
189
dofile (filename )
183
190
end
184
- end
191
+ end
0 commit comments