Repository navigation
Expand file tree
/
Copy pathutils.lua
More file actions
367 lines (333 loc) · 10.3 KB
/
Copy pathutils.lua
File metadata and controls
367 lines (333 loc) · 10.3 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
local M = {}
---@param next I.Commit
---@param prev_commit_row I.Row
---@param prev_connector_row I.Row
---@param commit_row I.Row
---@param connector_row I.Row
function M.resolve_bi_crossing(prev_commit_row, prev_connector_row, commit_row, connector_row, next)
-- if false then
-- if false then -- get_is_bi_crossing(graph, next_commit, #graph) then
-- print 'we have a bi crossing'
-- void all repeated reservations of `next` from
-- this and the previous row
local prev_row = commit_row
local this_row = connector_row
assert(prev_row and this_row, 'expecting two prior rows due to bi-connector')
--- example of what this does
---
--- input:
---
--- j i i │ │ │
--- j i i ⓮ │ │ <- prev
--- g i i h ⓸─⓵─ⓥ─╮ <- bi connector
---
--- output:
---
--- j i i │ ⓶─╯
--- j i ⓮ │ <- prev
--- g i h ⓸─│───╮ <- bi connector
---
---@param row I.Row
---@return integer
local function void_repeats(row)
local start_voiding = false
local ctr = 0
for k, cell in ipairs(row.cells) do
if cell.commit and cell.commit.hash == next.hash then
if not start_voiding then
start_voiding = true
elseif not row.cells[k].emphasis then
-- else
row.cells[k] = { connector = ' ' } -- void it
ctr = ctr + 1
end
end
end
return ctr
end
void_repeats(prev_row)
void_repeats(this_row)
-- we must also take care when the prev prev has a repeat where
-- the repeat is not the direct parent of its child
--
-- G ⓯
-- e d c ⓸─ⓢ─╮
-- E D C F │ │ │ ⓯
-- e D C c b a d ⓶─⓵─│─⓴─ⓢ─ⓢ─? <--- to resolve this
-- E D C C B A ⓮ │ │ │ │ │
-- c D C C b A ⓸─│─ⓥ─ⓥ─⓷ │
-- C D B A │ ⓮ │ │
-- C c b a ⓶─ⓥ─────⓵─⓷
-- C B A ⓮ │ │
-- b B a ⓸───────ⓥ─⓷
-- B A ⓚ │
-- a A ⓶─────────╯
-- A ⓚ
local prev_prev_row = prev_connector_row -- graph[#graph - 2]
local prev_prev_prev_row = prev_commit_row -- graph[#graph - 3]
assert(prev_prev_row and prev_prev_prev_row)
do
local start_voiding = false
local ctr = 0
---@type I.Cell?
local replacer = nil
for k, cell in ipairs(prev_prev_row.cells) do
if cell.commit and cell.commit.hash == next.hash then
if not start_voiding then
start_voiding = true
replacer = cell
elseif k ~= prev_prev_prev_row.commit.j then
local ppcell = prev_prev_prev_row.cells[k]
if (not ppcell) or (ppcell and ppcell.connector == ' ') then
prev_prev_row.cells[k] = { connector = ' ' } -- void it
replacer.emphasis = true
ctr = ctr + 1
end
end
end
end
end
-- assert(prev_rep_ctr == this_rep_ctr)
-- newly introduced tracking cells can be squeezed in
--
-- before:
--
-- j i i │ ⓶─╯
-- j i ⓮ │
-- g i h ⓸─│───╮
--
-- after:
--
-- j i i │ ⓶─╯
-- j i ⓮ │
-- g i h ⓸─│─╮
--
-- can think of this as scooting the cell to the left
-- when the cell was just introduced
-- TODO: implement this at some point
-- for k, cell in ipairs(this_row.cells) do
-- if cell.commit and not prev_row.cells[k].commit and not this_row.cells[k - 2] then
-- end
-- end
end
-- heuristic to check if this row contains a "bi-crossing" of branches
--
-- a bi-crossing is when we have more than one branch "propagating" horizontally
-- on a connector row
--
-- this can only happen when the commit on the row
-- above the connector row is a merge commit
-- but it doesn't always happen
--
-- in addition to needing a merge commit on the row above
-- we need the span (interval) of the "emphasized" connector cells
-- (they correspond to connectors to the parents of the merge commit)
-- we need that span to overlap with at least one connector cell that
-- is destined for the commit on the next row
-- (the commit before the merge commit)
-- in addition, we need there to be more than one connector cell
-- destined to the next commit
--
-- here is an example
--
--
-- j i i ⓮ │ │ j -> g h
-- g i i h ?─?─?─╮
-- g i h │ ⓚ │ i
--
--
-- overlap:
--
-- g-----h 1 4
-- i-i 2 3
--
-- NOTE how `i` is the commit that the `i` cells are destined for
-- notice how there is more than on `i` in the connector row
-- and that it lies in the span of g-h
--
-- some more examples
--
-- -------------------------------------
--
-- S T S │ ⓮ │ T -> R S
-- S R S ?─?─?
-- S R ⓚ │ S
--
--
-- overlap:
--
-- S-R 1 2
-- S---S 1 3
--
-- -------------------------------------
--
--
-- c b a b ⓮ │ │ │ c -> Z a
-- Z b a b ?─?─?─?
-- Z b a │ ⓚ │ b
--
-- overlap:
--
-- Z---a 1 3
-- b---b 2 4
--
-- -------------------------------------
--
-- finally a negative example where there is no problem
--
--
-- W V V ⓮ │ │ W -> S V
-- S V V ⓸─⓵─╯
-- S V │ ⓚ V
--
-- no overlap:
--
-- S-V 1 2
-- V-V 2 3
--
-- the reason why there is no problem (bi-crossing) above
-- follows from the fact that the span from V <- V only
-- touches the span S -> V it does not overlap it, so
-- figuratively we have S -> V <- V which is fine
--
-- TODO:
-- FIXME: need to test if we handle two bi-connectors in succession
-- correctly
--
---@param commit_row I.Row
---@param connector_row I.Row
---@param next_commit I.Commit?
---@return boolean -- whether or not this is a bi crossing
---@return boolean -- whether or not it can be resolved safely by edge lifting
function M.get_is_bi_crossing(commit_row, connector_row, next_commit)
if not next_commit then
return false, false
end
local prev = commit_row.commit
assert(prev, 'expected a prev commit')
if #prev.parents < 2 then
return false, false -- bi-crossings only happen when prev is a merge commit
end
local row = connector_row
---@param k integer
local function interval_upd(x, k)
if k < x.start then
x.start = k
end
if k > x.stop then
x.stop = k
end
end
-- compute the emphasized interval (merge commit parent interval)
local emi = { start = #row.cells, stop = 1 }
for k, cell in ipairs(row.cells) do
if cell.commit and cell.emphasis then
interval_upd(emi, k)
end
end
-- compute connector interval
local coi = { start = #row.cells, stop = 1 }
for k, cell in ipairs(row.cells) do
if cell.commit and cell.commit.hash == next_commit.hash then
interval_upd(coi, k)
end
end
-- unsafe if starts of intervals overlap and are equal to direct parent location
local safe = not (emi.start == coi.start and prev.j == emi.start)
-- return earily when connector interval is trivial
if coi.start == coi.stop then
return false, safe
end
-- print('emi:', vim.inspect(emi))
-- print('coi:', vim.inspect(coi))
-- check overlap
do
-- are intervals identical, then that counts as overlap
if coi.start == emi.start and coi.stop == emi.stop then
return true, safe
end
end
for _, k in pairs(emi) do
-- emi endpoints inside coi ?
if coi.start < k and k < coi.stop then
return true, safe
end
end
for _, k in pairs(coi) do
-- coi endpoints inside emi ?
if emi.start < k and k < emi.stop then
return true, safe
end
end
return false, safe
end
---@param graph I.Row[]
---@param r integer
function M.get_commit_from_row(graph, r)
-- trick to map both the commit row and the message row to the provided commit
local row = 2 * (math.floor((r - 1) / 2)) + 1 -- 1 1 3 3 5 5 7 7
local commit = graph[row].commit
return commit
end
function M.apply_buffer_options(buf)
vim.api.nvim_set_option_value('modifiable', false, { buf = buf })
vim.cmd('set filetype=gitgraph')
vim.api.nvim_buf_set_name(buf, 'GitGraph')
local options = {
'foldcolumn=0',
'foldlevel=999',
'norelativenumber',
'nospell',
'noswapfile',
}
-- Vim's `setlocal` is currently more robust compared to `opt_local`
vim.cmd(('silent! noautocmd setlocal %s'):format(table.concat(options, ' ')))
end
---@param buf_id integer
---@param graph I.Row[]
---@param hooks I.Hooks
function M.apply_buffer_mappings(buf_id, graph, hooks)
vim.keymap.set('n', '<CR>', function()
local row = vim.api.nvim_win_get_cursor(0)[1]
local commit = M.get_commit_from_row(graph, row)
if commit then
hooks.on_select_commit(commit)
end
end, { buffer = buf_id, desc = 'select commit under cursor' })
vim.keymap.set('v', '<CR>', function()
-- make sure visual selection is done
vim.cmd('noau normal! "vy"')
local start_row = vim.fn.getpos("'<")[2]
local end_row = vim.fn.getpos("'>")[2]
local to_commit = M.get_commit_from_row(graph, start_row)
local from_commit = M.get_commit_from_row(graph, end_row)
if from_commit and to_commit then
hooks.on_select_range_commit(from_commit, to_commit)
end
end, { buffer = buf_id, desc = 'select range of commit' })
end
---@param cmd string
---@return boolean -- true if failure (exit code ~= 0) false otherwise (exit code == 0)
--- note that this method was sadly neede since there's some strange bug with lua's handle:close?
--- it doesn't get the exit code correctly by itself?
function M.check_cmd(cmd)
local is_windows = package.config:sub(1, 1) == '\\'
local final_cmd = cmd
if is_windows then
final_cmd = final_cmd .. ' && echo 0 || echo 1'
else
final_cmd = final_cmd .. ' 2>&1; echo $?'
end
local res = io.popen(final_cmd)
if not res then
return true
end
local output, last_line = {}, '1'
for line in res:lines() do
table.insert(output, line)
end
last_line = output[#output] -- in both cases, the last line contains the exit status
res:close()
return vim.trim(last_line or '') ~= '0'
end
return M