Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(manage/git.lua): improved readability, removed redundant else-branches, and replaced an unnecessary loop with the condition. #1833

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions lua/lazy/manage/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,19 @@ end
function M.get_branch(plugin)
if plugin.branch then
return plugin.branch
else
-- we need to return the default branch
-- Try origin first
local main = M.ref(plugin.dir, "remotes/origin/HEAD")
if main then
local branch = main:match("ref: refs/remotes/origin/(.*)")
if branch then
return branch
end
end
-- we need to return the default branch
-- Try origin first
local main = M.ref(plugin.dir, "remotes/origin/HEAD")
if main then
local branch = main:match("ref: refs/remotes/origin/(.*)")
if branch then
return branch
end

-- fallback to local HEAD
main = assert(M.head(plugin.dir))
return main and main:match("ref: refs/heads/(.*)")
end
-- fallback to local HEAD
main = assert(M.head(plugin.dir))
return main and main:match("ref: refs/heads/(.*)")
end

-- Return the last commit for the given branch
Expand All @@ -108,9 +106,8 @@ function M.get_commit(repo, branch, origin)
if origin then
-- origin ref might not exist if it is the same as local
return M.ref(repo, "remotes/origin", branch) or M.ref(repo, "heads", branch)
else
return M.ref(repo, "heads", branch)
end
return M.ref(repo, "heads", branch)
end

---@param plugin LazyPlugin
Expand Down Expand Up @@ -159,8 +156,8 @@ function M.ref(repo, ...)
-- if this is a tag ref, then dereference it instead
if ref:find("tags/", 1, true) == 1 then
local tags = M.get_tag_refs(repo, ref)
for _, tag_ref in pairs(tags) do
return tag_ref
if next(tags) ~= nil then
return tags[1]
end
end

Expand Down Expand Up @@ -225,12 +222,10 @@ function M.get_config(repo)
if section then
---@type string
current_section = section:gsub('%s+"', "."):gsub('"+%s*$', "")
else
-- Ignore comments and blank lines
if not line:match("^%s*[#;]") and line:match("%S") then
local key, value = line:match("^%s*(%S+)%s*=%s*(.+)%s*$")
ret[current_section .. "." .. key] = value
end
elseif not line:match("^%s*[#;]") and line:match("%S") then
local key, value = line:match("^%s*(%S+)%s*=%s*(.+)%s*$")
ret[current_section .. "." .. key] = value
end
end
return ret
Expand Down
Loading