Skip to content

Commit ed4c861

Browse files
committed
add system_with_error for handling v:shell_error using job
1 parent 90ad034 commit ed4c861

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

plug.vim

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,8 @@ endfunction
865865

866866
function! s:checkout(spec)
867867
let sha = a:spec.commit
868-
let output = s:system('git rev-parse HEAD', a:spec.dir)
869-
if !v:shell_error && !s:hash_match(sha, s:lines(output)[0])
868+
let [output, shellerror] = s:system_with_error('git rev-parse HEAD', a:spec.dir)
869+
if !shellerror && !s:hash_match(sha, s:lines(output)[0])
870870
let output = s:system(
871871
\ 'git fetch --depth 999999 && git checkout '.s:esc(sha), a:spec.dir)
872872
endif
@@ -1053,14 +1053,16 @@ function! s:update_finish()
10531053
if !pos
10541054
continue
10551055
endif
1056+
let shellerror = 0
10561057
if has_key(spec, 'commit')
10571058
call s:log4(name, 'Checking out '.spec.commit)
10581059
let out = s:checkout(spec)
10591060
elseif has_key(spec, 'tag')
10601061
let tag = spec.tag
10611062
if tag =~ '\*'
1062-
let tags = s:lines(s:system('git tag --list '.string(tag).' --sort -version:refname 2>&1', spec.dir))
1063-
if !v:shell_error && !empty(tags)
1063+
let [output, shellerror] = s:system_with_error('git tag --list '.string(tag).' --sort -version:refname 2>&1', spec.dir)
1064+
let tags = s:lines(output)
1065+
if !shellerror && !empty(tags)
10641066
let tag = tags[0]
10651067
call s:log4(name, printf('Latest tag for %s -> %s', spec.tag, tag))
10661068
call append(3, '')
@@ -1074,13 +1076,13 @@ function! s:update_finish()
10741076
let out = s:system('git checkout -q '.branch.' 2>&1'
10751077
\. (has_key(s:update.new, name) ? '' : ('&& git merge --ff-only origin/'.branch.' 2>&1')), spec.dir)
10761078
endif
1077-
if !v:shell_error && filereadable(spec.dir.'/.gitmodules') &&
1079+
if !shellerror && filereadable(spec.dir.'/.gitmodules') &&
10781080
\ (s:update.force || has_key(s:update.new, name) || s:is_updated(spec.dir))
10791081
call s:log4(name, 'Updating submodules. This may take a while.')
10801082
let out .= s:bang('git submodule update --init --recursive 2>&1', spec.dir)
10811083
endif
1082-
let msg = s:format_message(v:shell_error ? 'x': '-', name, out)
1083-
if v:shell_error
1084+
let msg = s:format_message(shellerror ? 'x': '-', name, out)
1085+
if shellerror
10841086
call add(s:update.errors, name)
10851087
call s:regress_bar()
10861088
silent execute pos 'd _'
@@ -1203,8 +1205,9 @@ function! s:spawn(name, cmd, opts)
12031205
endif
12041206
else
12051207
let params = has_key(a:opts, 'dir') ? [a:cmd, a:opts.dir] : [a:cmd]
1206-
let job.lines = s:lines(call('s:system', params))
1207-
let job.error = v:shell_error != 0
1208+
let [output, shellerror] = call('s:system_with_error', params)
1209+
let job.lines = s:lines(output)
1210+
let job.error = shellerror != 0
12081211
let job.running = 0
12091212
endif
12101213
endfunction
@@ -1997,26 +2000,52 @@ function! s:system(cmd, ...)
19972000
endtry
19982001
endfunction
19992002

2003+
function! s:system_with_error(cmd, ...)
2004+
try
2005+
let maxfuncdepth = &maxfuncdepth
2006+
set maxfuncdepth=99999
2007+
let [sh, shrd] = s:chsh(1)
2008+
let cmd = a:0 > 0 ? s:with_cd(a:cmd, a:1) : a:cmd
2009+
if s:vim8
2010+
let [out, exit_code] = ['', 0]
2011+
let job = job_start([&shell, &shellcmdflag, cmd], {
2012+
\ 'out_cb': {ch,msg->[execute("let out .= msg"), out]},
2013+
\ 'err_cb': {ch,msg->[execute("let out .= msg"), out]},
2014+
\ 'exit_cb': {job,code->[execute("let exit_code=code"), exit_code]},
2015+
\ 'out_mode': 'raw'})
2016+
while job_status(job) == 'run'
2017+
sleep 10m
2018+
endwhile
2019+
return [out, exit_code]
2020+
endif
2021+
return [system(s:is_win ? '('.cmd.')' : cmd), v:shell_error]
2022+
finally
2023+
let [&shell, &shellredir, &maxfuncdepth] = [sh, shrd, maxfuncdepth]
2024+
endtry
2025+
endfunction
2026+
20002027
function! s:system_chomp(...)
2001-
let ret = call('s:system', a:000)
2002-
return v:shell_error ? '' : substitute(ret, '\n$', '', '')
2028+
let [ret, shellerror] = call('s:system_with_error', a:000)
2029+
return shellerror ? '' : substitute(ret, '\n$', '', '')
20032030
endfunction
20042031

20052032
function! s:git_validate(spec, check_branch)
20062033
let err = ''
20072034
if isdirectory(a:spec.dir)
2008-
let result = s:lines(s:system('git rev-parse --abbrev-ref HEAD 2>&1 && git config -f .git/config remote.origin.url', a:spec.dir))
2035+
let [output, shellerror] = s:system_with_error('git rev-parse --abbrev-ref HEAD 2>&1 && git config -f .git/config remote.origin.url', a:spec.dir)
2036+
let result = s:lines(output)
20092037
let remote = result[-1]
2010-
if v:shell_error
2038+
if shellerror
20112039
let err = join([remote, 'PlugClean required.'], "\n")
20122040
elseif !s:compare_git_uri(remote, a:spec.uri)
20132041
let err = join(['Invalid URI: '.remote,
20142042
\ 'Expected: '.a:spec.uri,
20152043
\ 'PlugClean required.'], "\n")
20162044
elseif a:check_branch && has_key(a:spec, 'commit')
2017-
let result = s:lines(s:system('git rev-parse HEAD 2>&1', a:spec.dir))
2045+
let [output, shellerror] = s:system_with_error('git rev-parse HEAD 2>&1', a:spec.dir)
2046+
let result = s:lines(output)
20182047
let sha = result[-1]
2019-
if v:shell_error
2048+
if shellerror
20202049
let err = join(add(result, 'PlugClean required.'), "\n")
20212050
elseif !s:hash_match(sha, a:spec.commit)
20222051
let err = join([printf('Invalid HEAD (expected: %s, actual: %s)',
@@ -2038,10 +2067,11 @@ function! s:git_validate(spec, check_branch)
20382067
\ branch, a:spec.branch)
20392068
endif
20402069
if empty(err)
2041-
let [ahead, behind] = split(s:lastline(s:system(printf(
2070+
let [output, shellerror] = s:system_with_error(printf(
20422071
\ 'git rev-list --count --left-right HEAD...origin/%s',
2043-
\ a:spec.branch), a:spec.dir)), '\t')
2044-
if !v:shell_error && ahead
2072+
\ a:spec.branch), a:spec.dir)
2073+
let [ahead, behind] = split(s:lastline(output), '\t')
2074+
if !shellerror && ahead
20452075
if behind
20462076
" Only mention PlugClean if diverged, otherwise it's likely to be
20472077
" pushable (and probably not that messed up).
@@ -2173,8 +2203,8 @@ function! s:upgrade()
21732203
let new = tmp . '/plug.vim'
21742204

21752205
try
2176-
let out = s:system(printf('git clone --depth 1 %s %s', s:plug_src, tmp))
2177-
if v:shell_error
2206+
let [out, shellerror] = s:system_with_error(printf('git clone --depth 1 %s %s', s:plug_src, tmp))
2207+
if shellerror
21782208
return s:err('Error upgrading vim-plug: '. out)
21792209
endif
21802210

0 commit comments

Comments
 (0)