Fix branch name in a newly inited repo.

"git branch" does not work in a newly inited repo.  So in a newly inited
using reftables, the branch name was not able to be retrieved, and the
"Loading..." placeholder remained indefinitely.

This change updates it to try again with "git branch --show-current"
when "git branch" prints empty output.
This commit is contained in:
Chris Antos
2025-12-07 19:52:40 -08:00
parent c3f239267d
commit 13dd021d6a

23
vendor/clink.lua vendored
View File

@@ -348,9 +348,14 @@ local function get_git_branch(git_dir, fast)
-- If the branch name is ".invalid" and the fast method wasn't requested, -- If the branch name is ".invalid" and the fast method wasn't requested,
-- then invoke git.exe to get accurate current branch info (slow method). -- then invoke git.exe to get accurate current branch info (slow method).
if branch_name == ".invalid" and not fast then if branch_name == ".invalid" and not fast then
local file = io_popenyield("git --no-optional-locks branch 2>nul") local file
branch_name = nil
-- Handle the most common case first.
if not branch_name then
file = io_popenyield("git --no-optional-locks branch 2>nul")
if file then if file then
for line in file:lines() do -- luacheck: ignore 512 for line in file:lines() do
local b = line:match("^%*%s+(.*)") local b = line:match("^%*%s+(.*)")
if b then if b then
b = b:match("^%((HEAD detached at .*)%)") or b b = b:match("^%((HEAD detached at .*)%)") or b
@@ -360,6 +365,20 @@ local function get_git_branch(git_dir, fast)
end end
file:close() file:close()
end end
end
-- Handle the cases where "git branch" output is empty, but "git
-- branch --show-current" shows the branch name (e.g. a new repo).
if not branch_name then
file = io_popenyield("git --no-optional-locks branch --show-current 2>nul")
if file then
for line in file:lines() do -- luacheck: ignore 512
branch_name = line
break
end
file:close()
end
end
else else
branch_name = branch_name or 'HEAD detached at '..HEAD:sub(1, 7) branch_name = branch_name or 'HEAD detached at '..HEAD:sub(1, 7)
end end