Skip to content

Commit 08d2ea9

Browse files
committed
feat: pull the remote branch after rebasing
1 parent 679d2ad commit 08d2ea9

3 files changed

Lines changed: 55 additions & 15 deletions

File tree

lua/gitlab/actions/rebase.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local M = {}
77

88
local can_rebase = function()
99
local git = require("gitlab.git")
10-
-- Check if there are local changes (couldn't run `git pull` after rebasing)
10+
-- Check if there are local changes (we wouldn't be able to run `git pull` after rebasing)
1111
local has_clean_tree, err = git.has_clean_tree()
1212
if not has_clean_tree then
1313
u.notify("Cannot rebase when there are changed files", vim.log.levels.ERROR)
@@ -41,8 +41,19 @@ M.confirm_rebase = function(merge_body)
4141
local job = require("gitlab.job")
4242
job.run_job("/mr/rebase", "POST", merge_body, function(data)
4343
u.notify(data.message, vim.log.levels.INFO)
44-
u.notify("Implement pulling", vim.log.levels.INFO)
45-
u.notify("Implement updating the reviewer", vim.log.levels.INFO)
44+
local git = require("gitlab.git")
45+
local state = require("gitlab.state")
46+
local success = git.pull(state.settings.connection_settings.remote, state.INFO.source_branch, { "--rebase" })
47+
if success then
48+
u.notify(
49+
string.format(
50+
"Pulled `%s %s` successfully",
51+
state.settings.connection_settings.remote,
52+
state.INFO.source_branch
53+
),
54+
vim.log.levels.INFO
55+
)
56+
end
4657
end)
4758
end
4859

lua/gitlab/git.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,34 @@ M.get_ahead_behind = function(current_branch, remote_branch, log_level)
124124
return true -- Checks passed, branch is up-to-date
125125
end
126126

127+
---Pull the branch from remote
128+
---@param remote string
129+
---@param branch string
130+
---@param opts string[]?
131+
---@return boolean success True if the branch has been pulled successfully
132+
M.pull = function(remote, branch, opts)
133+
local current_branch = M.get_current_branch()
134+
if not current_branch then
135+
return false
136+
end
137+
if current_branch ~= branch then
138+
local u = require("gitlab.utils")
139+
u.notify("Cannot pull. Remote branch is not the same as current branch", vim.log.levels.ERROR)
140+
return false
141+
end
142+
local args = { "git", "pull" }
143+
for _, opt in ipairs(opts or {}) do
144+
table.insert(args, opt)
145+
end
146+
table.insert(args, remote)
147+
table.insert(args, branch)
148+
local _, err = run_system(args)
149+
if err ~= nil then
150+
return false
151+
end
152+
return true
153+
end
154+
127155
---Return the name of the current branch or nil if it can't be retrieved
128156
---@return string|nil
129157
M.get_current_branch = function()

lua/gitlab/reviewer/init.lua

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
local List = require("gitlab.utils.list")
77
local u = require("gitlab.utils")
88
local state = require("gitlab.state")
9-
local git = require("gitlab.git")
109
local hunks = require("gitlab.hunks")
1110
local async = require("diffview.async")
1211
local diffview_lib = require("diffview.lib")
@@ -29,18 +28,16 @@ M.init = function()
2928
end
3029
end
3130

32-
-- Opens the reviewer window.
31+
-- Opens the reviewer windows.
3332
M.open = function()
34-
local diff_refs = state.INFO.diff_refs
35-
if diff_refs == nil then
36-
u.notify("Gitlab did not provide diff refs required to review this MR", vim.log.levels.ERROR)
37-
return
38-
end
33+
local git = require("gitlab.git")
3934

40-
if diff_refs.base_sha == "" or diff_refs.head_sha == "" then
41-
u.notify("Merge request contains no changes", vim.log.levels.ERROR)
35+
local remote_target_branch =
36+
string.format("%s/%s", state.settings.connection_settings.remote, state.INFO.target_branch)
37+
if not git.fetch_remote_branch(remote_target_branch) then
4238
return
4339
end
40+
git.check_current_branch_up_to_date_on_remote(vim.log.levels.WARN)
4441

4542
local diffview_open_command = "DiffviewOpen"
4643

@@ -53,17 +50,22 @@ M.open = function()
5350
diffview_open_command = diffview_open_command .. " --imply-local"
5451
else
5552
u.notify(
56-
"Your working tree has changes, cannot use 'imply_local' setting for gitlab reviews.\n Stash or commit all changes to use.",
53+
"Working tree unclean, cannot use 'imply_local' for review. Stash or commit all changes to use.",
5754
vim.log.levels.WARN
5855
)
5956
state.settings.reviewer_settings.diffview.imply_local = false
6057
end
6158
end
6259

63-
vim.api.nvim_command(string.format("%s %s..%s", diffview_open_command, diff_refs.base_sha, diff_refs.head_sha))
60+
local full_command = string.format("%s %s..%s", diffview_open_command, remote_target_branch, state.INFO.source_branch)
61+
vim.api.nvim_command(full_command)
6462

6563
M.is_open = true
6664
local cur_view = diffview_lib.get_current_view()
65+
if cur_view == nil then
66+
u.notify("Could not find Diffview view", vim.log.levels.ERROR)
67+
return
68+
end
6769
M.diffview_layout = cur_view.cur_layout
6870
M.tabnr = vim.api.nvim_get_current_tabpage()
6971

@@ -94,7 +96,6 @@ M.open = function()
9496
require("gitlab").toggle_discussions() -- Fetches data and opens discussions
9597
end
9698

97-
git.check_current_branch_up_to_date_on_remote(vim.log.levels.WARN)
9899
git.check_mr_in_good_condition()
99100
end
100101

0 commit comments

Comments
 (0)