@@ -6,11 +6,9 @@ local M = {}
66--- @param command table
77--- @return string | nil , string | nil
88local run_system = function (command )
9- -- Load here to prevent loop
10- local u = require (" gitlab.utils" )
119 local result = vim .fn .trim (vim .fn .system (command ))
1210 if vim .v .shell_error ~= 0 then
13- u .notify (result , vim .log .levels .ERROR )
11+ require ( " gitlab.utils " ) .notify (result , vim .log .levels .ERROR )
1412 return nil , result
1513 end
1614 return result , nil
@@ -52,10 +50,28 @@ M.switch_branch = function(branch)
5250 return run_system ({ " git" , " checkout" , " -q" , branch })
5351end
5452
55- --- Fetches the name of the remote tracking branch for the current branch
56- --- @return string | nil , string | nil
53+ --- Returns the name of the remote- tracking branch for the current branch or nil if it can't be found
54+ --- @return string | nil
5755M .get_remote_branch = function ()
58- return run_system ({ " git" , " rev-parse" , " --abbrev-ref" , " --symbolic-full-name" , " @{u}" })
56+ local remote_branch , err = run_system ({ " git" , " rev-parse" , " --abbrev-ref" , " --symbolic-full-name" , " @{u}" })
57+ if err or remote_branch == " " then
58+ require (" gitlab.utils" ).notify (" Could not get remote branch: " .. err , vim .log .levels .ERROR )
59+ return nil
60+ end
61+ return remote_branch
62+ end
63+
64+ --- Fetch the remote branch
65+ --- @param remote_branch string The name of the repo and branch to fetch (e.g. , " origin/some_branch" )
66+ --- @return boolean fetch_successfull False if an error occurred while fetching , true otherwise.
67+ M .fetch_remote_branch = function (remote_branch )
68+ local remote , branch = string.match (remote_branch , " ([^/]+)/(.*)" )
69+ local _ , fetch_err = run_system ({ " git" , " fetch" , remote , branch })
70+ if fetch_err ~= nil then
71+ require (" gitlab.utils" ).notify (" Error fetching remote-tracking branch: " .. fetch_err , vim .log .levels .ERROR )
72+ return false
73+ end
74+ return true
5975end
6076
6177--- Determines whether the tracking branch is ahead of or behind the current branch, and warns the user if so
6480--- @param log_level number
6581--- @return boolean
6682M .get_ahead_behind = function (current_branch , remote_branch , log_level )
83+ if not M .fetch_remote_branch (remote_branch ) then
84+ return false
85+ end
86+
6787 local u = require (" gitlab.utils" )
6888 local result , err =
6989 run_system ({ " git" , " rev-list" , " --left-right" , " --count" , current_branch .. " ..." .. remote_branch })
@@ -104,17 +124,22 @@ M.get_ahead_behind = function(current_branch, remote_branch, log_level)
104124 return true -- Checks passed, branch is up-to-date
105125end
106126
107- --- Return the name of the current branch
108- --- @return string | nil , string | nil
127+ --- Return the name of the current branch or nil if it can't be retrieved
128+ --- @return string | nil
109129M .get_current_branch = function ()
110- return run_system ({ " git" , " branch" , " --show-current" })
130+ local current_branch , err = run_system ({ " git" , " branch" , " --show-current" })
131+ if err or current_branch == " " then
132+ require (" gitlab.utils" ).notify (" Could not get current branch: " .. err , vim .log .levels .ERROR )
133+ return nil
134+ end
135+ return current_branch
111136end
112137
113138--- Return the list of possible merge targets.
114139--- @return table | nil
115140M .get_all_merge_targets = function ()
116- local current_branch , err = M .get_current_branch ()
117- if not current_branch or err ~ = nil then
141+ local current_branch = M .get_current_branch ()
142+ if current_branch = = nil then
118143 return
119144 end
120145 return List .new (M .get_all_remote_branches ()):filter (function (branch )
@@ -158,19 +183,13 @@ end
158183--- @param log_level integer
159184--- @return boolean
160185M .check_current_branch_up_to_date_on_remote = function (log_level )
161- local u = require (" gitlab.utils" )
162-
163- -- Get current branch
164- local current_branch , err_current_branch = M .get_current_branch ()
165- if err_current_branch or not current_branch then
166- u .notify (" Could not get current branch: " .. err_current_branch , vim .log .levels .ERROR )
186+ local current_branch = M .get_current_branch ()
187+ if current_branch == nil then
167188 return false
168189 end
169190
170- -- Get remote tracking branch
171- local remote_branch , err_remote_branch = M .get_remote_branch ()
172- if err_remote_branch or not remote_branch then
173- u .notify (" Could not get remote branch: " .. err_remote_branch , vim .log .levels .ERROR )
191+ local remote_branch = M .get_remote_branch ()
192+ if remote_branch == nil then
174193 return false
175194 end
176195
0 commit comments