Skip to content

Commit 96efdc2

Browse files
fix: Incorrect warning about commits (#395)
Fixed some bad warnings when the branch was out of date or ahead of the remote tracking branch. --------- Co-authored-by: Jakub F. Bortlík <jakub.bortlik@proton.me>
1 parent 0e19857 commit 96efdc2

File tree

4 files changed

+89
-41
lines changed

4 files changed

+89
-41
lines changed

lua/gitlab/actions/create_mr.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
--- continue working on it.
4848
---@param args? Mr
4949
M.start = function(args)
50-
if not git.current_branch_up_to_date_on_remote(vim.log.levels.ERROR) then
50+
if not git.check_current_branch_up_to_date_on_remote(vim.log.levels.ERROR) then
5151
return
5252
end
5353

lua/gitlab/actions/summary.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ M.summary = function()
8282
vim.api.nvim_set_current_buf(description_popup.bufnr)
8383
end)
8484

85-
git.current_branch_up_to_date_on_remote(vim.log.levels.WARN)
85+
git.check_current_branch_up_to_date_on_remote(vim.log.levels.WARN)
86+
git.check_mr_in_good_condition()
8687
end
8788

8889
-- Builds a lua list of strings that contain metadata about the current MR. Only builds the

lua/gitlab/git.lua

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,58 @@ M.switch_branch = function(branch)
4343
return run_system({ "git", "checkout", "-q", branch })
4444
end
4545

46+
---Fetches the name of the remote tracking branch for the current branch
47+
---@return string|nil, string|nil
48+
M.get_remote_branch = function()
49+
return run_system({ "git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}" })
50+
end
51+
52+
---Determines whether the tracking branch is ahead of or behind the current branch, and warns the user if so
53+
---@param current_branch string
54+
---@param remote_branch string
55+
---@param log_level number
56+
---@return boolean
57+
M.get_ahead_behind = function(current_branch, remote_branch, log_level)
58+
local u = require("gitlab.utils")
59+
local result, err =
60+
run_system({ "git", "rev-list", "--left-right", "--count", current_branch .. "..." .. remote_branch })
61+
if err ~= nil or result == nil then
62+
u.notify("Could not determine if branch is up-to-date: " .. err, vim.log.levels.ERROR)
63+
return false
64+
end
65+
66+
local ahead, behind = result:match("(%d+)%s+(%d+)")
67+
if ahead == nil or behind == nil then
68+
u.notify("Error parsing ahead/behind information.", vim.log.levels.ERROR)
69+
return false
70+
end
71+
72+
ahead = tonumber(ahead)
73+
behind = tonumber(behind)
74+
75+
if ahead > 0 and behind == 0 then
76+
u.notify(string.format("There are local changes that haven't been pushed to %s yet", remote_branch), log_level)
77+
return false
78+
end
79+
if behind > 0 and ahead == 0 then
80+
u.notify(string.format("There are remote changes on %s that haven't been pulled yet", remote_branch), log_level)
81+
return false
82+
end
83+
84+
if ahead > 0 and behind > 0 then
85+
u.notify(
86+
string.format(
87+
"Your branch and the remote %s have diverged. You need to pull, possibly rebase, and then push.",
88+
remote_branch
89+
),
90+
log_level
91+
)
92+
return false
93+
end
94+
95+
return true -- Checks passed, branch is up-to-date
96+
end
97+
4698
---Return the name of the current branch
4799
---@return string|nil, string|nil
48100
M.get_current_branch = function()
@@ -93,39 +145,45 @@ M.contains_branch = function(current_branch)
93145
return run_system({ "git", "branch", "-r", "--contains", current_branch })
94146
end
95147

96-
---Returns true if `branch` is up-to-date on remote, false otherwise.
148+
---Returns true if `branch` is up-to-date on remote, otherwise false and warns user
97149
---@param log_level integer
98-
---@return boolean|nil
99-
M.current_branch_up_to_date_on_remote = function(log_level)
100-
local state = require("gitlab.state")
101-
local current_branch = M.get_current_branch()
102-
local handle = io.popen("git branch -r --contains " .. current_branch .. " 2>&1")
103-
if not handle then
104-
require("gitlab.utils").notify("Error running 'git branch' command.", vim.log.levels.ERROR)
105-
return nil
150+
---@return boolean
151+
M.check_current_branch_up_to_date_on_remote = function(log_level)
152+
local u = require("gitlab.utils")
153+
154+
-- Get current branch
155+
local current_branch, err_current_branch = M.get_current_branch()
156+
if err_current_branch or not current_branch then
157+
u.notify("Could not get current branch: " .. err_current_branch, vim.log.levels.ERROR)
158+
return false
106159
end
107160

108-
local remote_branches_with_current_head = {}
109-
for line in handle:lines() do
110-
table.insert(remote_branches_with_current_head, line)
161+
-- Get remote tracking branch
162+
local remote_branch, err_remote_branch = M.get_remote_branch()
163+
if err_remote_branch or not remote_branch then
164+
u.notify("Could not get remote branch: " .. err_remote_branch, vim.log.levels.ERROR)
165+
return false
111166
end
112-
handle:close()
113167

114-
local current_head_on_remote = List.new(remote_branches_with_current_head):filter(function(line)
115-
return line == string.format(" %s/", state.settings.connection_settings.remote) .. current_branch
116-
end)
117-
local remote_up_to_date = #current_head_on_remote == 1
168+
return M.get_ahead_behind(current_branch, remote_branch, log_level)
169+
end
118170

119-
if not remote_up_to_date then
120-
require("gitlab.utils").notify(
121-
string.format(
122-
"You have local commits that are not on %s. Have you forgotten to push?",
123-
state.settings.connection_settings.remote
124-
),
125-
log_level
126-
)
171+
---Warns user if the current MR is in a bad state (closed, has conflicts, merged)
172+
M.check_mr_in_good_condition = function()
173+
local state = require("gitlab.state")
174+
local u = require("gitlab.utils")
175+
176+
if state.INFO.has_conflicts then
177+
u.notify("This merge request has conflicts!", vim.log.levels.WARN)
178+
end
179+
180+
if state.INFO.state == "closed" then
181+
u.notify(string.format("This MR was closed %s", u.time_since(state.INFO.closed_at)), vim.log.levels.WARN)
182+
end
183+
184+
if state.INFO.state == "merged" then
185+
u.notify(string.format("This MR was merged %s", u.time_since(state.INFO.merged_at)), vim.log.levels.WARN)
127186
end
128-
return remote_up_to_date
129187
end
130188

131189
return M

lua/gitlab/reviewer/init.lua

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ M.open = function()
6262
)
6363
end
6464

65-
if state.INFO.has_conflicts then
66-
u.notify("This merge request has conflicts!", vim.log.levels.WARN)
67-
end
68-
69-
if state.INFO.state == "closed" then
70-
u.notify(string.format("This MR was closed %s", u.time_since(state.INFO.closed_at)), vim.log.levels.WARN)
71-
end
72-
73-
if state.INFO.state == "merged" then
74-
u.notify(string.format("This MR was merged %s", u.time_since(state.INFO.merged_at)), vim.log.levels.WARN)
75-
end
76-
7765
if state.settings.discussion_diagnostic ~= nil or state.settings.discussion_sign ~= nil then
7866
u.notify(
7967
"Diagnostics are now configured as settings.discussion_signs, see :h gitlab.nvim.signs-and-diagnostics",
@@ -98,7 +86,8 @@ M.open = function()
9886
require("gitlab").toggle_discussions() -- Fetches data and opens discussions
9987
end
10088

101-
git.current_branch_up_to_date_on_remote(vim.log.levels.WARN)
89+
git.check_current_branch_up_to_date_on_remote(vim.log.levels.WARN)
90+
git.check_mr_in_good_condition()
10291
end
10392

10493
-- Closes the reviewer and cleans up

0 commit comments

Comments
 (0)