Skip to content

Commit 40b6ef7

Browse files
committed
feat(git): allow git commands to be used from all sources, close #529
1 parent 3ff2ae7 commit 40b6ef7

File tree

2 files changed

+103
-96
lines changed

2 files changed

+103
-96
lines changed

lua/neo-tree/sources/common/commands.lua

+103
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local fs_actions = require("neo-tree.sources.filesystem.lib.fs_actions")
55
local utils = require("neo-tree.utils")
66
local renderer = require("neo-tree.ui.renderer")
77
local events = require("neo-tree.events")
8+
local inputs = require("neo-tree.ui.inputs")
9+
local popups = require("neo-tree.ui.popups")
810
local log = require("neo-tree.log")
911
local help = require("neo-tree.sources.common.help")
1012
local Preview = require("neo-tree.sources.common.preview")
@@ -205,6 +207,107 @@ M.cut_to_clipboard_visual = function(state, selected_nodes, callback)
205207
end
206208
end
207209

210+
--------------------------------------------------------------------------------
211+
-- Git commands
212+
--------------------------------------------------------------------------------
213+
214+
M.git_add_file = function(state)
215+
local node = state.tree:get_node()
216+
if node.type == "message" then
217+
return
218+
end
219+
local path = node:get_id()
220+
local cmd = { "git", "add", path }
221+
vim.fn.system(cmd)
222+
events.fire_event(events.GIT_EVENT)
223+
end
224+
225+
M.git_add_all = function(state)
226+
local cmd = { "git", "add", "-A" }
227+
vim.fn.system(cmd)
228+
events.fire_event(events.GIT_EVENT)
229+
end
230+
231+
M.git_commit = function(state, and_push)
232+
local width = vim.fn.winwidth(0) - 2
233+
local row = vim.api.nvim_win_get_height(0) - 3
234+
local popup_options = {
235+
relative = "win",
236+
position = {
237+
row = row,
238+
col = 0,
239+
},
240+
size = width,
241+
}
242+
243+
inputs.input("Commit message: ", "", function(msg)
244+
local cmd = { "git", "commit", "-m", msg }
245+
local title = "git commit"
246+
local result = vim.fn.systemlist(cmd)
247+
if vim.v.shell_error ~= 0 or (#result > 0 and vim.startswith(result[1], "fatal:")) then
248+
popups.alert("ERROR: git commit", result)
249+
return
250+
end
251+
if and_push then
252+
title = "git commit && git push"
253+
cmd = { "git", "push" }
254+
local result2 = vim.fn.systemlist(cmd)
255+
table.insert(result, "")
256+
for i = 1, #result2 do
257+
table.insert(result, result2[i])
258+
end
259+
end
260+
events.fire_event(events.GIT_EVENT)
261+
popups.alert(title, result)
262+
end, popup_options)
263+
end
264+
265+
M.git_commit_and_push = function(state)
266+
M.git_commit(state, true)
267+
end
268+
269+
M.git_push = function(state)
270+
inputs.confirm("Are you sure you want to push your changes?", function(yes)
271+
if yes then
272+
local result = vim.fn.systemlist({ "git", "push" })
273+
events.fire_event(events.GIT_EVENT)
274+
popups.alert("git push", result)
275+
end
276+
end)
277+
end
278+
279+
M.git_unstage_file = function(state)
280+
local node = state.tree:get_node()
281+
if node.type == "message" then
282+
return
283+
end
284+
local path = node:get_id()
285+
local cmd = { "git", "reset", "--", path }
286+
vim.fn.system(cmd)
287+
events.fire_event(events.GIT_EVENT)
288+
end
289+
290+
M.git_revert_file = function(state)
291+
local node = state.tree:get_node()
292+
if node.type == "message" then
293+
return
294+
end
295+
local path = node:get_id()
296+
local cmd = { "git", "checkout", "HEAD", "--", path }
297+
local msg = string.format("Are you sure you want to revert %s?", node.name)
298+
inputs.confirm(msg, function(yes)
299+
if yes then
300+
vim.fn.system(cmd)
301+
events.fire_event(events.GIT_EVENT)
302+
end
303+
end)
304+
end
305+
306+
--------------------------------------------------------------------------------
307+
-- END Git commands
308+
--------------------------------------------------------------------------------
309+
310+
208311
M.next_source = function(state)
209312
local sources = require("neo-tree").config.sources
210313
local next_source = sources[1]

lua/neo-tree/sources/git_status/commands.lua

-96
Original file line numberDiff line numberDiff line change
@@ -3,109 +3,13 @@
33
local vim = vim
44
local cc = require("neo-tree.sources.common.commands")
55
local utils = require("neo-tree.utils")
6-
local inputs = require("neo-tree.ui.inputs")
7-
local popups = require("neo-tree.ui.popups")
86
local manager = require("neo-tree.sources.manager")
9-
local events = require("neo-tree.events")
10-
local log = require("neo-tree.log")
117

128
local M = {}
139

1410
local refresh = utils.wrap(manager.refresh, "git_status")
1511
local redraw = utils.wrap(manager.redraw, "git_status")
1612

17-
M.git_add_file = function(state)
18-
local node = state.tree:get_node()
19-
if node.type == "message" then
20-
return
21-
end
22-
local path = node:get_id()
23-
local cmd = { "git", "add", path }
24-
vim.fn.system(cmd)
25-
events.fire_event(events.GIT_EVENT)
26-
end
27-
28-
M.git_add_all = function(state)
29-
local cmd = { "git", "add", "-A" }
30-
vim.fn.system(cmd)
31-
events.fire_event(events.GIT_EVENT)
32-
end
33-
34-
M.git_commit = function(state, and_push)
35-
local width = vim.fn.winwidth(0) - 2
36-
local row = vim.api.nvim_win_get_height(0) - 3
37-
local popup_options = {
38-
relative = "win",
39-
position = {
40-
row = row,
41-
col = 0,
42-
},
43-
size = width,
44-
}
45-
46-
inputs.input("Commit message: ", "", function(msg)
47-
local cmd = { "git", "commit", "-m", msg }
48-
local title = "git commit"
49-
local result = vim.fn.systemlist(cmd)
50-
if vim.v.shell_error ~= 0 or (#result > 0 and vim.startswith(result[1], "fatal:")) then
51-
popups.alert("ERROR: git commit", result)
52-
return
53-
end
54-
if and_push then
55-
title = "git commit && git push"
56-
cmd = { "git", "push" }
57-
local result2 = vim.fn.systemlist(cmd)
58-
table.insert(result, "")
59-
for i = 1, #result2 do
60-
table.insert(result, result2[i])
61-
end
62-
end
63-
events.fire_event(events.GIT_EVENT)
64-
popups.alert(title, result)
65-
end, popup_options)
66-
end
67-
68-
M.git_commit_and_push = function(state)
69-
M.git_commit(state, true)
70-
end
71-
72-
M.git_push = function(state)
73-
inputs.confirm("Are you sure you want to push your changes?", function(yes)
74-
if yes then
75-
local result = vim.fn.systemlist({ "git", "push" })
76-
events.fire_event(events.GIT_EVENT)
77-
popups.alert("git push", result)
78-
end
79-
end)
80-
end
81-
82-
M.git_unstage_file = function(state)
83-
local node = state.tree:get_node()
84-
if node.type == "message" then
85-
return
86-
end
87-
local path = node:get_id()
88-
local cmd = { "git", "reset", "--", path }
89-
vim.fn.system(cmd)
90-
events.fire_event(events.GIT_EVENT)
91-
end
92-
93-
M.git_revert_file = function(state)
94-
local node = state.tree:get_node()
95-
if node.type == "message" then
96-
return
97-
end
98-
local path = node:get_id()
99-
local cmd = { "git", "checkout", "HEAD", "--", path }
100-
local msg = string.format("Are you sure you want to revert %s?", node.name)
101-
inputs.confirm(msg, function(yes)
102-
if yes then
103-
vim.fn.system(cmd)
104-
events.fire_event(events.GIT_EVENT)
105-
end
106-
end)
107-
end
108-
10913
-- ----------------------------------------------------------------------------
11014
-- Common commands
11115
-- ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)