@@ -74,54 +74,34 @@ M.fetch_remote_branch = function(remote_branch)
7474 return true
7575end
7676
77- --- Determines whether the tracking branch is ahead of or behind the current branch, and warns the user if so
78- --- @param current_branch string
79- --- @param remote_branch string
80- --- @param log_level number
81- --- @return boolean
82- M .get_ahead_behind = function (current_branch , remote_branch , log_level )
77+ --- Determines whether the tracking branch is ahead of or behind the current branch and returns the
78+ --- number of ahead and behind commits or nil values in case of errors.
79+ --- @param current_branch string | nil
80+ --- @param remote_branch string | nil
81+ --- @return integer | nil ahead , integer | nil behind
82+ M .get_ahead_behind = function (current_branch , remote_branch )
83+ if current_branch == nil or remote_branch == nil then
84+ return nil , nil
85+ end
8386 if not M .fetch_remote_branch (remote_branch ) then
84- return false
87+ return nil , nil
8588 end
8689
8790 local u = require (" gitlab.utils" )
8891 local result , err =
8992 run_system ({ " git" , " rev-list" , " --left-right" , " --count" , current_branch .. " ..." .. remote_branch })
9093 if err ~= nil or result == nil then
9194 u .notify (" Could not determine if branch is up-to-date: " .. err , vim .log .levels .ERROR )
92- return false
95+ return nil , nil
9396 end
9497
9598 local ahead , behind = result :match (" (%d+)%s+(%d+)" )
9699 if ahead == nil or behind == nil then
97- u .notify (" Error parsing ahead/behind information." , vim .log .levels .ERROR )
98- return false
99- end
100-
101- ahead = tonumber (ahead )
102- behind = tonumber (behind )
103-
104- if ahead > 0 and behind == 0 then
105- u .notify (string.format (" There are local changes that haven't been pushed to %s yet" , remote_branch ), log_level )
106- return false
107- end
108- if behind > 0 and ahead == 0 then
109- u .notify (string.format (" There are remote changes on %s that haven't been pulled yet" , remote_branch ), log_level )
110- return false
111- end
112-
113- if ahead > 0 and behind > 0 then
114- u .notify (
115- string.format (
116- " Your branch and the remote %s have diverged. You need to pull, possibly rebase, and then push." ,
117- remote_branch
118- ),
119- log_level
120- )
121- return false
100+ u .notify (" Error parsing ahead/behind information" , vim .log .levels .ERROR )
101+ return nil , nil
122102 end
123103
124- return true -- Checks passed, branch is up-to-date
104+ return tonumber ( ahead ), tonumber ( behind )
125105end
126106
127107--- Return the name of the current branch or nil if it can't be retrieved
@@ -184,16 +164,35 @@ end
184164--- @return boolean
185165M .check_current_branch_up_to_date_on_remote = function (log_level )
186166 local current_branch = M .get_current_branch ()
187- if current_branch == nil then
167+ local remote_branch = M .get_remote_branch ()
168+ local ahead , behind = M .get_ahead_behind (current_branch , remote_branch )
169+ if ahead == nil or behind == nil then
188170 return false
189171 end
190172
191- local remote_branch = M .get_remote_branch ()
192- if remote_branch == nil then
173+ local u = require (" gitlab.utils" )
174+
175+ if ahead > 0 and behind == 0 then
176+ u .notify (string.format (" There are local changes that haven't been pushed to %s yet" , remote_branch ), log_level )
177+ return false
178+ end
179+ if behind > 0 and ahead == 0 then
180+ u .notify (string.format (" There are remote changes on %s that haven't been pulled yet" , remote_branch ), log_level )
193181 return false
194182 end
195183
196- return M .get_ahead_behind (current_branch , remote_branch , log_level )
184+ if ahead > 0 and behind > 0 then
185+ u .notify (
186+ string.format (
187+ " Your branch and the remote %s have diverged. You need to pull, possibly rebase, and then push." ,
188+ remote_branch
189+ ),
190+ log_level
191+ )
192+ return false
193+ end
194+
195+ return true -- Checks passed, branch is up-to-date
197196end
198197
199198--- Warns user if the current MR is in a bad state (closed, has conflicts, merged)
0 commit comments