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

116 lines
3.3 KiB
VimL
Raw Normal View History

2021-01-23 22:16:59 +08:00
"=============================================================================
" manager.vim --- Git branch manager
" Copyright (c) 2016-2019 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
scriptencoding utf-8
2021-01-02 18:15:08 +08:00
let s:JOB = SpaceVim#api#import('job')
let s:BUFFER = SpaceVim#api#import('vim#buffer')
2021-01-23 22:16:59 +08:00
let s:branch_manager_bufnr = 0
2021-01-02 18:15:08 +08:00
2021-01-23 22:16:59 +08:00
function! git#branch#manager#open() abort
if s:branch_manager_bufnr != 0 && bufexists(s:branch_manager_bufnr)
exe 'bd ' . s:branch_manager_bufnr
endif
topleft vsplit __git_branch_manager__
let s:winid = win_getid(winnr('#'))
let lines = &columns * 30 / 100
exe 'vertical resize ' . lines
setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber winfixheight nomodifiable
set filetype=SpaceVimGitBranchManager
let s:branch_manager_bufnr = bufnr('%')
call s:update()
augroup git_branch_manager
autocmd! * <buffer>
autocmd WinEnter <buffer> call s:WinEnter()
augroup END
nnoremap <buffer><silent> <Enter> :call <SID>checkout_branch()<cr>
nnoremap <buffer><silent> dd :call <SID>delete_branch()<cr>
2021-01-02 18:15:08 +08:00
endfunction
function! s:WinEnter() abort
2021-01-23 22:16:59 +08:00
let s:winid = win_getid(winnr('#'))
2021-01-02 18:15:08 +08:00
endfunction
function! s:checkout_branch() abort
2021-01-23 22:16:59 +08:00
let line = getline('.')
if line =~# '^\s\+\S\+'
let branch = split(line)[0]
exe 'Git checkout ' . branch
endif
endfunction
function! git#branch#manager#update() abort
call s:update()
endfunction
function! s:delete_branch() abort
let line = getline('.')
if line =~# '^\s\+\S\+'
let branch = split(line)[0]
exe 'Git branch -d ' . branch
endif
2021-01-02 18:15:08 +08:00
endfunction
function! s:update() abort
2021-01-23 22:16:59 +08:00
let s:branchs = []
let cmd = ['git', 'branch', '--all']
call git#logger#info('git-branch cmd:' . string(cmd))
call s:JOB.start(cmd,
\ {
\ '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:update_buffer_context() abort
2021-01-23 22:16:59 +08:00
let lines = []
call add(lines, 'local')
let remote = ''
for branch in s:branchs
if branch.remote != remote
call add(lines, 'r:' . branch.remote)
let remote = branch.remote
endif
call add(lines, ' ' . branch.name)
endfor
call s:BUFFER.buf_set_lines(s:branch_manager_bufnr, 0 , -1, 0, lines)
2021-01-02 18:15:08 +08:00
endfunction
function! s:on_stdout(id, data, event) abort
2021-01-23 22:16:59 +08:00
for line in filter(a:data, '!empty(v:val)')
call SpaceVim#logger#info('>>' . line)
if stridx(line, 'remotes/') == -1
let branch = {
2021-01-02 18:15:08 +08:00
\ 'name': trim(line),
\ 'remote': '',
\ 'islocal': 1,
\ }
2021-01-23 22:16:59 +08:00
else
let branch = {
2021-01-02 18:15:08 +08:00
\ 'name': line[stridx(line, '/', 10) + 1 :],
\ 'remote': split(line, '/')[1],
\ 'islocal': 0,
\ }
2021-01-23 22:16:59 +08:00
endif
call add(s:branchs, branch)
endfor
2021-01-02 18:15:08 +08:00
endfunction
function! s:on_stderr(id, data, event) abort
2021-01-23 22:16:59 +08:00
for line in filter(a:data, '!empty(v:val)')
exe 'Echoerr ' . line
endfor
2021-01-02 18:15:08 +08:00
endfunction
function! s:on_exit(id, data, event) abort
2021-01-23 22:16:59 +08:00
call git#logger#info('git-branch exit data:' . string(a:data))
if a:data ==# 0
call s:update_buffer_context()
endif
2021-01-02 18:15:08 +08:00
endfunction