1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-04 02:30:04 +08:00
SpaceVim/bundle/git.vim/autoload/git/commit.vim

151 lines
4.0 KiB
VimL
Raw Normal View History

2021-01-02 18:15:08 +08:00
let s:JOB = SpaceVim#api#import('job')
let s:BUFFER = SpaceVim#api#import('vim#buffer')
let s:NOTI = SpaceVim#api#import('notify')
2021-01-02 18:15:08 +08:00
2021-01-23 22:16:59 +08:00
let s:commit_bufnr = -1
" Init
let s:commit_context = []
let s:commit_jobid = -1
2021-01-23 22:16:59 +08:00
function! git#commit#run(...) abort
if index(a:1, '-m') ==# -1
if bufexists(s:commit_bufnr) && index(tabpagebuflist(), s:commit_bufnr) !=# -1
let winnr = bufwinnr(s:commit_bufnr)
exe winnr . 'wincmd w'
2021-01-02 18:15:08 +08:00
else
2021-01-23 22:16:59 +08:00
let s:commit_bufnr = s:openCommitBuffer()
2021-01-02 18:15:08 +08:00
endif
2021-01-23 22:16:59 +08:00
else
let s:commit_bufnr = -1
endif
let s:commit_context = []
2021-01-23 22:16:59 +08:00
if empty(a:1)
let cmd = ['git', '--no-pager', '-c',
\ 'core.editor=cat', '-c',
\ 'color.status=always',
\ '-C',
\ expand(getcwd(), ':p'),
\ 'commit', '--edit']
elseif index(a:1, '-m') != -1
let cmd = ['git', '--no-pager', '-c',
\ 'core.editor=cat', '-c',
\ 'color.status=always',
\ '-C',
\ expand(getcwd(), ':p'),
\ 'commit'] + a:1
else
let cmd = ['git', '--no-pager', '-c',
\ 'core.editor=cat', '-c',
\ 'color.status=always',
\ '-C',
\ expand(getcwd(), ':p'),
\ 'commit',] + a:1
endif
let s:commit_jobid = s:JOB.start(cmd,
2021-01-23 22:16:59 +08:00
\ {
\ 'on_stderr' : function('s:on_stderr'),
\ 'on_stdout' : function('s:on_stdout'),
\ 'on_exit' : function('s:on_exit'),
\ }
\ )
2021-01-02 18:15:08 +08:00
endfunction
function! s:on_stdout(id, data, event) abort
if a:id !=# s:commit_jobid
" ignore previous git commit job
return
endif
2021-01-23 22:16:59 +08:00
for data in a:data
call git#logger#debug('git-commit stdout:' . data)
2021-01-23 22:16:59 +08:00
endfor
let s:commit_context += a:data
2021-01-02 18:15:08 +08:00
endfunction
function! s:on_stderr(id, data, event) abort
if a:id !=# s:commit_jobid
" ignore previous git commit job
return
endif
2021-01-23 22:16:59 +08:00
for data in a:data
call git#logger#debug('git-commit stderr:' . data)
2021-01-23 22:16:59 +08:00
endfor
" stderr should not be added to commit buffer
" let s:commit_context += a:data
2021-01-02 18:15:08 +08:00
endfunction
function! s:on_exit(id, data, event) abort
if a:id !=# s:commit_jobid
" ignore previous git commit job
return
endif
call git#logger#debug('git-exit exit data:' . string(a:data))
2021-01-23 22:16:59 +08:00
if s:commit_bufnr == -1
if a:data ==# 0
call s:NOTI.notify('commit done!')
2021-01-02 18:15:08 +08:00
else
call s:NOTI.notify('commit failed!' , 'WarningMsg')
2021-01-02 18:15:08 +08:00
endif
2021-01-23 22:16:59 +08:00
else
call s:BUFFER.buf_set_lines(s:commit_bufnr, 0 , -1, 0, s:commit_context)
2021-01-23 22:16:59 +08:00
endif
2021-01-02 18:15:08 +08:00
endfunction
function! s:openCommitBuffer() abort
2021-01-23 22:16:59 +08:00
10split git://commit
normal! "_dd
setlocal nobuflisted
setlocal buftype=acwrite
setlocal bufhidden=wipe
setlocal noswapfile
setlocal modifiable
setf git-commit
nnoremap <buffer><silent> q :bd!<CR>
let b:git_commit_quitpre = 0
2021-01-02 18:15:08 +08:00
2021-01-23 22:16:59 +08:00
augroup git_commit_buffer
autocmd! * <buffer>
autocmd BufWriteCmd <buffer> call s:BufWriteCmd()
autocmd QuitPre <buffer> call s:QuitPre()
autocmd WinLeave <buffer> call s:WinLeave()
autocmd WinEnter <buffer> let b:git_commit_quitpre = 0
augroup END
return bufnr('%')
2021-01-02 18:15:08 +08:00
endfunction
" NOTE:
" :w -- BufWriteCmd
" <C-w>p -- WinLeave
" :wq -- QuitPre -> BufWriteCmd -> WinLeave
2021-03-14 15:12:00 +08:00
" when run `:wq` the commit window will not be closed
2021-01-02 18:15:08 +08:00
" :q -- QuitPre -> WinLeave
function! s:BufWriteCmd() abort
let s:commit_context = getline(1, '$')
2021-01-23 22:16:59 +08:00
setlocal nomodified
2021-01-02 18:15:08 +08:00
endfunction
function! s:QuitPre() abort
2021-01-23 22:16:59 +08:00
let b:git_commit_quitpre = 1
2021-01-02 18:15:08 +08:00
endfunction
function! s:WinLeave() abort
2021-01-23 22:16:59 +08:00
if b:git_commit_quitpre == 1
let cmd = ['git', 'commit', '-F', '-']
let id = s:JOB.start(cmd,
\ {
\ 'on_exit' : function('s:on_commit_exit'),
\ }
\ )
2021-01-23 22:16:59 +08:00
" line start with # should be ignored
call s:JOB.send(id, filter(s:commit_context, 'v:val !~# "^\s*#"'))
2021-01-23 22:16:59 +08:00
call s:JOB.chanclose(id, 'stdin')
endif
2021-01-02 18:15:08 +08:00
endfunction
function! s:on_commit_exit(id, data, event) abort
call git#logger#debug('git-commit exit data:' . string(a:data))
2021-01-23 22:16:59 +08:00
if a:data ==# 0
call s:NOTI.notify('commit done!')
2021-01-23 22:16:59 +08:00
else
call s:NOTI.notify('commit failed!' , 'WarningMsg')
2021-01-23 22:16:59 +08:00
endif
2021-01-02 18:15:08 +08:00
endfunction