Repository navigation
Expand file tree
/
Copy pathgit.lua
More file actions
69 lines (67 loc) · 2.87 KB
/
Copy pathgit.lua
File metadata and controls
69 lines (67 loc) · 2.87 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
-- Keep the repository captured by the graph: changing cwd afterwards must not
-- make a selected commit open in a different repository. Pass arguments directly
-- to Diffview's Lua API so repository paths containing spaces remain intact.
local function open_graph_diff(revision)
local root = vim.b.gitgraph_root
if not root then
vim.notify("Open the Git graph with Space gl before selecting commits.", vim.log.levels.ERROR)
return
end
require("diffview").open({ revision, "-C" .. root })
end
local function draw_graph()
local result = vim.system({ "git", "rev-parse", "--show-toplevel" }, { text = true }):wait()
if result.code ~= 0 then
vim.notify("Cannot open Git graph:\n" .. vim.trim(result.stderr), vim.log.levels.ERROR)
return
end
local root = vim.trim(result.stdout)
local head = vim.system({ "git", "rev-parse", "--verify", "HEAD" }, { text = true }):wait()
if head.code ~= 0 then
vim.notify("Cannot open Git graph: the repository has no valid HEAD commit.\n" .. vim.trim(head.stderr),
vim.log.levels.WARN)
return
end
-- Preserve the editor/Neo-tree layout; invoking the shortcut inside the graph
-- redraws it instead of creating another tab. The limit bounds synchronous work.
if vim.bo.filetype ~= "gitgraph" then vim.cmd("tab split") end
-- Expand only project refs, not --all (which also includes agent checkpoints).
-- gitgraph forwards revision_range to git log, including these revision flags.
require("gitgraph").draw({}, { revision_range = "--branches --remotes --tags", max_count = 500 })
vim.b.gitgraph_root = root
end
return {
{
"sindrets/diffview.nvim",
cmd = { "DiffviewOpen", "DiffviewClose", "DiffviewFileHistory", "DiffviewToggleFiles", "DiffviewFocusFiles", "DiffviewRefresh" },
dependencies = { "nvim-lua/plenary.nvim" },
opts = {},
keys = {
{ "<leader>gd", "<cmd>DiffviewOpen<cr>", desc = "Git: working tree diff" },
{ "<leader>gh", "<cmd>DiffviewFileHistory<cr>", desc = "Git: repository history" },
{ "<leader>gc", "<cmd>DiffviewClose<cr>", desc = "Git: close diff view" },
},
},
{
"isakbm/gitgraph.nvim",
-- lazy.nvim configures Diffview before the graph's selection hooks can run.
dependencies = { "sindrets/diffview.nvim" },
keys = {
{ "<leader>gl", draw_graph, desc = "Git: commit graph" },
},
opts = {
hooks = {
-- ^! compares this commit to its first parent; Diffview also handles a
-- root commit by comparing it to the empty tree.
on_select_commit = function(commit)
open_graph_diff(commit.hash .. "^!")
end,
-- Compare the two endpoint snapshots, not the parent of the older one.
-- This also works when the selection starts at the root commit.
on_select_range_commit = function(from, to)
open_graph_diff(from.hash .. ".." .. to.hash)
end,
},
},
},
}