@@ -6,11 +6,9 @@ local M = {}
6
6
--- @param command table
7
7
--- @return string | nil , string | nil
8
8
local run_system = function (command )
9
- -- Load here to prevent loop
10
- local u = require (" gitlab.utils" )
11
9
local result = vim .fn .trim (vim .fn .system (command ))
12
10
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 )
14
12
return nil , result
15
13
end
16
14
return result , nil
@@ -52,10 +50,28 @@ M.switch_branch = function(branch)
52
50
return run_system ({ " git" , " checkout" , " -q" , branch })
53
51
end
54
52
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
57
55
M .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
59
75
end
60
76
61
77
--- Determines whether the tracking branch is ahead of or behind the current branch, and warns the user if so
64
80
--- @param log_level number
65
81
--- @return boolean
66
82
M .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
+
67
87
local u = require (" gitlab.utils" )
68
88
local result , err =
69
89
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)
104
124
return true -- Checks passed, branch is up-to-date
105
125
end
106
126
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
109
129
M .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
111
136
end
112
137
113
138
--- Return the list of possible merge targets.
114
139
--- @return table | nil
115
140
M .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
118
143
return
119
144
end
120
145
return List .new (M .get_all_remote_branches ()):filter (function (branch )
@@ -158,19 +183,13 @@ end
158
183
--- @param log_level integer
159
184
--- @return boolean
160
185
M .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
167
188
return false
168
189
end
169
190
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
174
193
return false
175
194
end
176
195
0 commit comments