1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-13 02:05:40 +08:00

feat(git): rewrite :Git rebase in lua

This commit is contained in:
Eric Wong 2024-03-23 23:18:05 +08:00
parent f5c6730177
commit 2083a13ad6
7 changed files with 144 additions and 23 deletions

View File

@ -97,6 +97,7 @@ local function openCommitBuffer()
setlocal noswapfile
setlocal modifiable
setf git-commit
set syntax=gitcommit
nnoremap <buffer><silent> q :bd!<CR>
let b:git_commit_quitpre = 0
]])

View File

@ -0,0 +1,139 @@
local m = {}
local job = require('spacevim.api.job')
local nt = require('spacevim.api.notify')
local log = require('git.log')
local std_data = {}
local rebase_todo_bufnr = -1
local rebase_nvim_channal = ''
local function BufWriteCmd()
vim.bo.modified = false
end
local function QuitPre()
vim.b.git_rebase_quitpre = true
end
local function WinLeave()
if vim.b.git_rebase_quitpre then
local text = vim.fn.getline(1, '$')
vim.fn.rpcrequest(rebase_nvim_channal, 'nvim_buf_set_lines', 0, 0, -1, false, text)
vim.fn.rpcnotify(rebase_nvim_channal, 'nvim_command', 'wq')
end
end
local function open_rebase()
vim.cmd([[
10split git://rebase
normal! "_dd
setlocal nobuflisted
setlocal buftype=acwrite
setlocal bufhidden=wipe
setlocal noswapfile
setlocal modifiable
setf git-rebase
set syntax=gitrebase
nnoremap <buffer><silent> q :bd!<CR>
let b:git_commit_quitpre = 0
]])
end
local function open_commit()
vim.cmd([[
10split git://commit
normal! "_dd
setlocal nobuflisted
setlocal buftype=acwrite
setlocal bufhidden=wipe
setlocal noswapfile
setlocal modifiable
setf git-commit
set syntax=gitcommit
nnoremap <buffer><silent> q :bd!<CR>
let b:git_commit_quitpre = 0
]])
end
local function create_autocmd_return_bufnr()
local bufid = vim.fn.bufnr('%')
local id = vim.api.nvim_create_augroup('git_rebase_todo_buffer', { clear = true })
vim.api.nvim_create_autocmd({ 'BufWriteCmd' }, {
group = id,
buffer = bufid,
callback = BufWriteCmd,
})
vim.api.nvim_create_autocmd({ 'QuitPre' }, {
group = id,
buffer = bufid,
callback = QuitPre,
})
vim.api.nvim_create_autocmd({ 'WinLeave' }, {
group = id,
buffer = bufid,
callback = WinLeave,
})
vim.api.nvim_create_autocmd({ 'WinEnter' }, {
group = id,
buffer = bufid,
callback = function()
vim.b.git_rebase_quitpre = false
end,
})
return bufid
end
local function on_exit(id, code, single)
log.debug('git-rebase exit code:' .. code .. ' single:' .. single)
if code == 0 and single == 0 then
nt.notify('git rebase successfully')
else
nt.notify(table.concat(std_data, "\n"), "WarningMsg")
end
end
local function on_std(id, data)
for _, v in ipairs(data) do
if vim.startswith(v, 'git-rebase-nvim-serverlist:') then
local address = string.sub(v, 28)
rebase_nvim_channal = vim.fn.sockconnect('pipe', address, { rpc = true })
local bufname = vim.fn.rpcrequest(rebase_nvim_channal, 'nvim_buf_get_name', 0)
log.debug(bufname)
if vim.fn.fnamemodify(bufname, ':t') == 'git-rebase-todo' then
open_rebase()
else
open_commit()
end
rebase_todo_bufnr = create_autocmd_return_bufnr()
local text = vim.fn.rpcrequest(rebase_nvim_channal, 'nvim_buf_get_lines', 0, 0, -1, false)
vim.api.nvim_buf_set_lines(rebase_todo_bufnr, 0, -1, false, text)
vim.bo.modified = false
else
table.insert(std_data, v)
end
end
end
function m.run(argv)
local cmd = {
'git',
'--no-pager',
'-c',
[[core.editor=nvim -u NONE --headless -n --cmd "call chansend(v:stderr, ['', 'git-rebase-nvim-serverlist:' . serverlist()[0], ''])"]],
'-c',
'color.status=always',
'-C',
vim.fn.expand(vim.fn.getcwd(), ':p'),
'rebase',
}
for _, v in ipairs(argv) do
table.insert(cmd, v)
end
log.debug('git-rebase cmd:' .. vim.inspect(cmd))
std_data = {}
job.start(cmd, {
on_exit = on_exit,
on_stdout = on_std,
on_stderr = on_std,
})
end
return m

View File

@ -26,6 +26,7 @@ local cmds = {
'shortlog',
'status',
'grep',
'rebase',
'tag',
}
local supported_commands = {}

View File

@ -1,9 +0,0 @@
if exists('b:current_syntax')
finish
endif
let b:current_syntax = 'git-commit'
syntax case ignore
syn match GitCommitIgnore /^\s*#.*/
hi def link GitCommitIgnore Comment

View File

@ -1,11 +0,0 @@
if exists('b:current_syntax')
finish
endif
let b:current_syntax = 'git-rebase'
syntax case ignore
syn match GitRebaseIgnore /^\s*#.*/
syn match GitRebaseKeyword /^pick\s/
hi def link GitRebaseIgnore Comment
hi def link GitRebaseKeyword Statement

View File

@ -36,8 +36,8 @@ lang: zh
- [x] `:Git mv`
- [x] `:Git pull`
- [x] `:Git push`
- [ ] `:Git rebase`
- [ ] `:Git reflog`
- [x] `:Git rebase`
- [x] `:Git reflog`
- [x] `:Git remote`
- [x] `:Git reset`
- [x] `:Git rm`

View File

@ -36,7 +36,7 @@ If you have any suggestions , please checkout feedback section on [community](..
- [x] `:Git mv`
- [x] `:Git pull`
- [x] `:Git push`
- [ ] `:Git rebase`
- [x] `:Git rebase`
- [x] `:Git reflog`
- [x] `:Git remote`
- [x] `:Git reset`