Skip to content

Commit feecfa0

Browse files
committed
feat(statusline): Added detached HEAD branch support
1 parent 586e94a commit feecfa0

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

lua/bars/components/statusline.lua

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,55 @@ slC.branch = function (_, window, main_config)
306306

307307
local branch;
308308

309-
if not vim.w[window].__git_branch then
310-
branch = vim.split(vim.fn.system({
309+
--- Gets the current git branch.
310+
---@return string
311+
local function get_branch ()
312+
---|fS
313+
314+
--- Are we in a repo?
315+
---@type string
316+
local in_repo = vim.fn.system({
317+
"git",
318+
"-C",
319+
cwd,
320+
"rev-parse",
321+
"--is-inside-work-tree"
322+
});
323+
324+
if not in_repo or string.match(in_repo, "^true") == nil then
325+
--- The output doesn't exist or it doesn't
326+
--- start with "true" then return.
327+
return "";
328+
end
329+
330+
local _branch = vim.fn.system({
311331
"git",
312332
"-C",
313333
cwd,
314334
"branch",
315335
"--show-current"
316-
}), "\n", { trimempty = true });
336+
});
337+
338+
if _branch == "" then
339+
--- Detached HEAD.
340+
_branch = vim.fn.system({
341+
"git",
342+
"-C",
343+
cwd,
344+
"rev-parse",
345+
"--short",
346+
"HEAD"
347+
});
348+
end
349+
350+
return _branch or "";
351+
352+
---|fE
353+
end
354+
355+
if not vim.w[window].__git_branch then
356+
get_branch()
357+
branch = vim.split(get_branch(), "\n", { trimempty = true });
317358

318359
vim.w[window].__git_branch = branch;
319360
vim.w[window].__branch_time = vim.uv.hrtime();
@@ -324,13 +365,7 @@ slC.branch = function (_, window, main_config)
324365
local throttle = main_config.throttle or 2000;
325366

326367
if now - bef >= (throttle * 1e6) then
327-
branch = vim.split(vim.fn.system({
328-
"git",
329-
"-C",
330-
cwd,
331-
"branch",
332-
"--show-current"
333-
}), "\n", { trimempty = true });
368+
branch = vim.split(get_branch(), "\n", { trimempty = true });
334369

335370
vim.w[window].__git_branch = branch;
336371
vim.w[window].__branch_time = vim.uv.hrtime();

0 commit comments

Comments
 (0)