1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-23 01:09:56 +08:00

fix(git): improve branch detection

close https://github.com/SpaceVim/SpaceVim/issues/4142
This commit is contained in:
wsdjeg 2022-04-15 23:45:50 +08:00
parent d5f7ef3332
commit 0f623af900

View File

@ -20,11 +20,11 @@ function! git#branch#run(args) abort
call git#logger#info('git-branch cmd:' . string(cmd)) call git#logger#info('git-branch cmd:' . string(cmd))
call s:JOB.start(cmd, call s:JOB.start(cmd,
\ { \ {
\ 'on_stderr' : function('s:on_stderr'), \ 'on_stderr' : function('s:on_stderr'),
\ 'on_stdout' : function('s:on_stdout'), \ 'on_stdout' : function('s:on_stdout'),
\ 'on_exit' : function('s:on_exit'), \ 'on_exit' : function('s:on_exit'),
\ } \ }
\ ) \ )
endfunction endfunction
@ -59,20 +59,21 @@ let s:branch = ''
let s:branch_info = {} let s:branch_info = {}
" { " {
" branch_name : 'xxx', " branch_name : 'xxx',
" last_update : 111111, ms " last_update_done : 111111, ms
" } " }
let s:job_pwds = {} let s:job_pwds = {}
function! s:update_branch_name(pwd, ...) abort function! s:update_branch_name(pwd, ...) abort
let force = get(a:000, 0, 0) let force = get(a:000, 0, 0)
let cmd = 'git rev-parse --abbrev-ref HEAD' let cmd = 'git rev-parse --abbrev-ref HEAD'
if force || get(get(s:branch_info, a:pwd, {}), 'last_update', 0) >= localtime() - 1 if force || get(get(s:branch_info, a:pwd, {}), 'last_update_done', 0) >= localtime() - 1
let jobid = s:JOB.start(cmd, let jobid = s:JOB.start(cmd,
\ { \ {
\ 'on_stdout' : function('s:on_stdout_show_branch'), \ 'on_stdout' : function('s:on_stdout_show_branch'),
\ 'cwd' : a:pwd, \ 'on_exit' : function('s:on_exit_show_branch'),
\ } \ 'cwd' : a:pwd,
\ ) \ }
\ )
if jobid > 0 if jobid > 0
call extend(s:job_pwds, {'jobid' . jobid : a:pwd}) call extend(s:job_pwds, {'jobid' . jobid : a:pwd})
endif endif
@ -84,10 +85,17 @@ function! s:on_stdout_show_branch(id, data, event) abort
let pwd = get(s:job_pwds, 'jobid' . a:id, '') let pwd = get(s:job_pwds, 'jobid' . a:id, '')
let s:branch_info[pwd] = { let s:branch_info[pwd] = {
\ 'name' : b, \ 'name' : b,
\ 'last_update' : localtime(),
\ } \ }
endif endif
endfunction endfunction
function! s:on_exit_show_branch(id, data, event) abort
let pwd = get(s:job_pwds, 'jobid' . a:id, '')
call extend(s:branch_info[pwd], {
\ 'last_update_done' : localtime(),
\ })
endfunction
function! git#branch#current() abort function! git#branch#current() abort
let pwd = getcwd() let pwd = getcwd()
let branch = get(s:branch_info, pwd, {}) let branch = get(s:branch_info, pwd, {})