Skip to content

Commit 7fc7718

Browse files
committed
feat: implement stash view
1 parent 5b2ea2e commit 7fc7718

24 files changed

Lines changed: 4024 additions & 312 deletions

lua/vgit.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ controller.execute_command = event.async(function(args)
214214
status = true,
215215
branch = true,
216216
debug = true,
217+
stash = true,
217218
}
218219

219220
if porcelain_commands[cmd] then

lua/vgit/cli/commands/stash.lua

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
local lazy = require('vgit.core.lazy')
2+
local event = lazy('vgit.core.event')
3+
local console = lazy('vgit.core.console')
4+
local git_stash = lazy('vgit.git.git_stash')
5+
local repository = lazy('vgit.git.repository')
6+
local display_service = lazy('vgit.ui.display_service')
7+
8+
local stash_command = {}
9+
10+
local function parse_args(args)
11+
local opts = { action = 'screen', index = nil }
12+
if not args or #args == 0 then return opts end
13+
14+
local first = args[1]
15+
16+
if first == 'add' then
17+
opts.action = 'add'
18+
elseif first == 'pop' then
19+
opts.action = 'pop'
20+
opts.index = args[2] and tonumber(args[2]) or 0
21+
elseif first == 'apply' then
22+
opts.action = 'apply'
23+
opts.index = args[2] and tonumber(args[2]) or 0
24+
elseif first == 'drop' then
25+
opts.action = 'drop'
26+
opts.index = args[2] and tonumber(args[2]) or 0
27+
elseif first == 'clear' then
28+
opts.action = 'clear'
29+
end
30+
31+
return opts
32+
end
33+
34+
stash_command.execute = event.async(function(args)
35+
args = args or {}
36+
local opts = parse_args(args)
37+
38+
event.await()
39+
40+
local repo, repo_err = repository.current()
41+
if repo_err then
42+
console.error(repo_err)
43+
return
44+
end
45+
46+
local repo_path = repo:get_path()
47+
48+
if opts.action == 'add' then
49+
local _, err = git_stash.add(repo_path)
50+
if err then
51+
console.error(err[1] or tostring(err))
52+
return
53+
end
54+
console.info('Changes stashed')
55+
return
56+
end
57+
58+
if opts.action == 'pop' then
59+
local stash_ref = string.format('stash@{%d}', opts.index)
60+
local _, err = git_stash.pop(repo_path, stash_ref)
61+
if err then
62+
console.error(err[1] or tostring(err))
63+
return
64+
end
65+
console.info('Stash popped: ' .. stash_ref)
66+
return
67+
end
68+
69+
if opts.action == 'apply' then
70+
local stash_ref = string.format('stash@{%d}', opts.index)
71+
local _, err = git_stash.apply(repo_path, stash_ref)
72+
if err then
73+
console.error(err[1] or tostring(err))
74+
return
75+
end
76+
console.info('Stash applied: ' .. stash_ref)
77+
return
78+
end
79+
80+
if opts.action == 'drop' then
81+
local stash_ref = string.format('stash@{%d}', opts.index)
82+
local _, err = git_stash.drop(repo_path, stash_ref)
83+
if err then
84+
console.error(err[1] or tostring(err))
85+
return
86+
end
87+
console.info('Stash dropped: ' .. stash_ref)
88+
return
89+
end
90+
91+
if opts.action == 'clear' then
92+
local _, err = git_stash.clear(repo_path)
93+
if err then
94+
console.error(err[1] or tostring(err))
95+
return
96+
end
97+
console.info('All stashes cleared')
98+
return
99+
end
100+
101+
-- Default: show stash screen
102+
local stashes, list_err = git_stash.list(repo_path)
103+
if list_err then
104+
console.error(list_err[1] or tostring(list_err))
105+
return
106+
end
107+
108+
if not stashes or #stashes == 0 then
109+
console.info('No stashes found')
110+
return
111+
end
112+
113+
display_service.show_stash({
114+
stashes = stashes,
115+
repo = repo,
116+
})
117+
end)
118+
119+
return stash_command

0 commit comments

Comments
 (0)