-
Notifications
You must be signed in to change notification settings - Fork 412
/
Copy pathgit.lua
261 lines (232 loc) · 6.49 KB
/
git.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
local Git = require("lazy.manage.git")
local Lock = require("lazy.manage.lock")
local Config = require("lazy.core.config")
local Util = require("lazy.util")
---@type table<string, LazyTaskDef>
local M = {}
M.log = {
---@param opts {updated?:boolean, check?: boolean}
skip = function(plugin, opts)
if opts.check and plugin.pin then
return true
end
if opts.updated and not (plugin._.updated and plugin._.updated.from ~= plugin._.updated.to) then
return true
end
local stat = vim.loop.fs_stat(plugin.dir .. "/.git")
return not (stat and stat.type == "directory")
end,
---@param opts {args?: string[], updated?:boolean, check?:boolean}
run = function(self, opts)
local args = {
"log",
"--pretty=format:%h %s (%cr)",
"--abbrev-commit",
"--decorate",
"--date=short",
"--color=never",
"--no-show-signature",
}
if opts.updated then
table.insert(args, self.plugin._.updated.from .. ".." .. (self.plugin._.updated.to or "HEAD"))
elseif opts.check then
local info = assert(Git.info(self.plugin.dir))
local target = assert(Git.get_target(self.plugin))
if not target.commit then
for k, v in pairs(target) do
error(k .. " '" .. v .. "' not found")
end
error("no target commit found")
end
assert(target.commit, self.plugin.name .. " " .. target.branch)
if not Git.eq(info, target) then
self.plugin._.updates = { from = info, to = target }
end
table.insert(args, info.commit .. ".." .. target.commit)
else
vim.list_extend(args, opts.args or Config.options.git.log)
end
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
})
end,
}
M.clone = {
skip = function(plugin)
return plugin._.installed or plugin._.is_local
end,
run = function(self)
local args = {
"clone",
self.plugin.url,
}
if Config.options.git.filter then
args[#args + 1] = "--filter=blob:none"
end
if self.plugin.submodules ~= false then
args[#args + 1] = "--recurse-submodules"
end
args[#args + 1] = "--progress"
if self.plugin.branch then
vim.list_extend(args, { "-b", self.plugin.branch })
end
table.insert(args, self.plugin.dir)
if vim.fn.isdirectory(self.plugin.dir) == 1 then
require("lazy.manage.task.fs").clean.run(self, {})
end
local marker = self.plugin.dir .. ".cloning"
Util.write_file(marker, "")
self:spawn("git", {
args = args,
on_exit = function(ok)
if ok then
self.plugin._.cloned = true
self.plugin._.installed = true
self.plugin._.dirty = true
vim.loop.fs_unlink(marker)
end
end,
})
end,
}
-- setup origin branches if needed
-- fetch will retrieve the data
M.branch = {
skip = function(plugin)
if not plugin._.installed or plugin._.is_local then
return true
end
local branch = assert(Git.get_branch(plugin))
return Git.get_commit(plugin.dir, branch)
end,
run = function(self)
local args = {
"remote",
"set-branches",
"--add",
"origin",
assert(Git.get_branch(self.plugin)),
}
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
})
end,
}
-- check and switch origin
M.origin = {
skip = function(plugin)
if not plugin._.installed or plugin._.is_local then
return true
end
local origin = Git.get_origin(plugin.dir)
return origin == plugin.url
end,
---@param opts {check?:boolean}
run = function(self, opts)
if opts.check then
local origin = Git.get_origin(self.plugin.dir)
self.error = "Origin has changed:\n"
self.error = self.error .. " * old: " .. origin .. "\n"
self.error = self.error .. " * new: " .. self.plugin.url .. "\n"
self.error = self.error .. "Please run update to fix"
return
end
require("lazy.manage.task.fs").clean.run(self, opts)
M.clone.run(self, opts)
end,
}
-- fetches all needed origin branches
M.fetch = {
skip = function(plugin)
return not plugin._.installed or plugin._.is_local
end,
run = function(self)
local args = {
"fetch",
"--recurse-submodules",
"--tags", -- also fetch remote tags
"--force", -- overwrite existing tags if needed
"--progress",
}
if self.plugin.submodules == false then
table.remove(args, 2)
end
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
})
end,
}
-- checkout to the target commit
-- branches will exists at this point, so so will the commit
M.checkout = {
skip = function(plugin)
return not plugin._.installed or plugin._.is_local
end,
---@param opts {lockfile?:boolean}
run = function(self, opts)
local info = assert(Git.info(self.plugin.dir))
local target = assert(Git.get_target(self.plugin))
-- if the plugin is pinned and we did not just clone it,
-- then don't update
if self.plugin.pin and not self.plugin._.cloned then
target = info
end
local lock
if opts.lockfile then
-- restore to the lock if it exists
lock = Lock.get(self.plugin)
if lock then
---@diagnostic disable-next-line: cast-local-type
target = lock
end
end
-- dont run checkout if target is already reached.
-- unless we just cloned, since then we won't have any data yet
if Git.eq(info, target) and info.branch == target.branch then
self.plugin._.updated = {
from = info.commit,
to = info.commit,
}
return
end
local args = {
"checkout",
"--progress",
"--recurse-submodules",
}
if self.plugin.submodules == false then
table.remove(args, 3)
end
if lock then
table.insert(args, lock.commit)
elseif target.tag then
table.insert(args, "tags/" .. target.tag)
elseif self.plugin.commit then
table.insert(args, self.plugin.commit)
else
table.insert(args, target.commit)
end
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
on_exit = function(ok)
if ok then
local new_info = assert(Git.info(self.plugin.dir))
if not self.plugin._.cloned then
self.plugin._.updated = {
from = info.commit,
to = new_info.commit,
}
if self.plugin._.updated.from ~= self.plugin._.updated.to then
self.plugin._.dirty = true
end
end
end
end,
})
end,
}
return M