diff --git a/bundle/git.vim/lua/git/command/stash.lua b/bundle/git.vim/lua/git/command/stash.lua new file mode 100644 index 000000000..9af691256 --- /dev/null +++ b/bundle/git.vim/lua/git/command/stash.lua @@ -0,0 +1,100 @@ +--============================================================================= +-- stash.lua --- :Git stash command +-- Copyright (c) 2016-2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= + +local M = {} + +local job = require('spacevim.api.job') +local nt = require('spacevim.api.notify') +local log = require('git.log') +local jobid = -1 +local show_lines = {} + +local function on_stdout(id, data) + if id ~= jobid then + return + end + nt.notify(table.concat(data, '\n')) +end + +local function on_show_stdout(id, data) + if id ~= jobid then + return + end + + for _, v in ipairs(data) do + table.insert(show_lines, v) + end +end + +local function on_stderr(id, data) + if id ~= jobid then + return + end + nt.notify(table.concat(data, '\n'), 'WarningMsg') +end + +local function on_exit(id, code, signal) + if id ~= jobid then + return + end + log.debug(string.format('git-stash exit code %d, signal: %d', code, signal)) + if #show_lines > 0 and code == 0 and signal == 0 then + vim.cmd([[ + edit git://blame + normal! "_dd + setl nobuflisted + setl nomodifiable + setl nonumber norelativenumber + setl buftype=nofile + setf git-diff + setl syntax=diff + nnoremap q :bd! + ]]) + local bufnr = vim.fn.bufnr('%') + + vim.api.nvim_buf_set_option(bufnr, 'modifiable', true) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, show_lines) + vim.api.nvim_buf_set_option(bufnr, 'modifiable', false) + end +end + +local function is_show(cmd) + for _, v in ipairs(cmd) do + if v == 'show' then + return true + end + end + return false +end + +function M.run(argv) + local cmd = { 'git', 'stash' } + + for _, v in ipairs(argv) do + table.insert(cmd, v) + end + + log.debug('git-stash cmd:' .. vim.inspect(cmd)) + show_lines = {} + + if is_show(cmd) then + jobid = job.start(cmd, { + on_stdout = on_show_stdout, + on_stderr = on_stderr, + on_exit = on_exit, + }) + else + jobid = job.start(cmd, { + on_stdout = on_stdout, + on_stderr = on_stderr, + on_exit = on_exit, + }) + end +end + +return M diff --git a/bundle/git.vim/lua/git/init.lua b/bundle/git.vim/lua/git/init.lua index 1bd0c0b16..f7a7cc33c 100644 --- a/bundle/git.vim/lua/git/init.lua +++ b/bundle/git.vim/lua/git/init.lua @@ -27,6 +27,7 @@ local cmds = { 'status', 'grep', 'rebase', + 'stash', 'tag', } local supported_commands = {} diff --git a/docs/cn/roadmap.md b/docs/cn/roadmap.md index 864b9a74b..a8d341655 100644 --- a/docs/cn/roadmap.md +++ b/docs/cn/roadmap.md @@ -45,7 +45,7 @@ lang: zh - [ ] `:Git shortlog` (viml) - [x] `:Git tag` (lua) - [ ] `:Git tag` (viml) - - [ ] `:Git stash` + - [x] `:Git stash` - [x] `:Git status` - [x] 日志系统整合至 SpaceVim 运行时日志 - [x] 使用 lua 重写 code runner diff --git a/docs/roadmap.md b/docs/roadmap.md index 3529164d8..673054008 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -45,7 +45,7 @@ If you have any suggestions , please checkout feedback section on [community](.. - [ ] `:Git shortlog` (viml) - [x] `:Git tag` (lua) - [ ] `:Git tag` (viml) - - [ ] `:Git stash` + - [x] `:Git stash` - [x] `:Git status` - [x] plugin log manager derived from SPC runtime logger - [x] rewrite code runner with lua