Skip to content

Commit

Permalink
Updated condition check + remote branch check
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisoncramer committed Oct 11, 2024
1 parent c171f0e commit bcf4fe8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
1 change: 1 addition & 0 deletions lua/gitlab/actions/summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ M.summary = function()
end)

git.current_branch_up_to_date_on_remote(vim.log.levels.WARN)
git.check_mr_in_good_condition()
end

-- Builds a lua list of strings that contain metadata about the current MR. Only builds the
Expand Down
39 changes: 33 additions & 6 deletions lua/gitlab/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ end
---Fetches the name of the remote tracking branch for the current branch
---@return string|nil, string|nil
M.get_remote_branch = function()
return run_system({ "git", "rev-parse", "--abbrev-ref", "symbolic-full-name", "@{u}" })
return run_system({ "git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}" })
end

---Determines whether there are unpushed commits from local to remote
---@param current_branch string
---@param remote_branch string
---@return boolean
M.get_ahead_behind = function(current_branch, remote_branch, log_level)
local u = require("gitlab.utils")
local result, err =
run_system({ "git", "rev-list", "--left-right", "--count", current_branch .. "..." .. remote_branch })
if err ~= nil or result == nil then
Expand Down Expand Up @@ -84,7 +85,10 @@ M.get_ahead_behind = function(current_branch, remote_branch, log_level)

if ahead > 0 and behind > 0 then
u.notify(
string.format("Your branch and the remote %s have diverged. You need to pull and then push.", remote_branch),
string.format(
"Your branch and the remote %s have diverged. You need to pull, possibly rebase, and then push.",
remote_branch
),
log_level
)
return false
Expand Down Expand Up @@ -143,22 +147,45 @@ M.contains_branch = function(current_branch)
return run_system({ "git", "branch", "-r", "--contains", current_branch })
end

---Returns true if `branch` is up-to-date on remote
---Returns true if `branch` is up-to-date on remote, otherwise false and warns user
---@param log_level integer
---@return boolean
M.current_branch_up_to_date_on_remote = function(log_level)
local u = require("gitlab.utils")

-- Get current branch
local current_branch, err = M.get_current_branch()
if not current_branch or err ~= nil then
if err or not current_branch then
u.notify("Could not get current branch", vim.log.levels.ERROR)
return false
end

-- Get remote tracking branch
local remote_branch, err = M.get_current_branch()
if not remote_branch or err ~= nil then
local remote_branch, err = M.get_remote_branch()
if err or not remote_branch then
u.notify("Could not get remote branch", vim.log.levels.ERROR)
return false
end

return M.get_ahead_behind(current_branch, remote_branch, log_level)
end

---Warns user if the current MR is in a bad state (closed, has conflicts, merged)
M.check_mr_in_good_condition = function()
local state = require("gitlab.state")
local u = require("gitlab.utils")

if state.INFO.has_conflicts then
u.notify("This merge request has conflicts!", vim.log.levels.WARN)
end

if state.INFO.state == "closed" then
u.notify(string.format("This MR was closed %s", u.time_since(state.INFO.closed_at)), vim.log.levels.WARN)
end

if state.INFO.state == "merged" then
u.notify(string.format("This MR was merged %s", u.time_since(state.INFO.merged_at)), vim.log.levels.WARN)
end
end

return M
13 changes: 1 addition & 12 deletions lua/gitlab/reviewer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ M.open = function()
)
end

if state.INFO.has_conflicts then
u.notify("This merge request has conflicts!", vim.log.levels.WARN)
end

if state.INFO.state == "closed" then
u.notify(string.format("This MR was closed %s", u.time_since(state.INFO.closed_at)), vim.log.levels.WARN)
end

if state.INFO.state == "merged" then
u.notify(string.format("This MR was merged %s", u.time_since(state.INFO.merged_at)), vim.log.levels.WARN)
end

if state.settings.discussion_diagnostic ~= nil or state.settings.discussion_sign ~= nil then
u.notify(
"Diagnostics are now configured as settings.discussion_signs, see :h gitlab.nvim.signs-and-diagnostics",
Expand All @@ -99,6 +87,7 @@ M.open = function()
end

git.current_branch_up_to_date_on_remote(vim.log.levels.WARN)
git.check_mr_in_good_condition()
end

-- Closes the reviewer and cleans up
Expand Down

0 comments on commit bcf4fe8

Please sign in to comment.