Minor refactoring

/cc @vheon
This commit is contained in:
Junegunn Choi 2014-10-01 11:37:58 +09:00
parent afc20ecff3
commit e1e04cabd5

View file

@ -541,11 +541,11 @@ function! s:do(pull, force, todo)
if !isdirectory(spec.dir) if !isdirectory(spec.dir)
continue continue
endif endif
execute 'cd' s:esc(spec.dir)
let installed = has_key(s:prev_update.new, name) let installed = has_key(s:prev_update.new, name)
let updated = installed ? 0 : let updated = installed ? 0 :
\ (a:pull && !empty(s:system_chomp('git log --pretty=format:"%h" "HEAD...HEAD@{1}"'))) \ (a:pull && !empty(s:system_chomp('git log --pretty=format:"%h" "HEAD...HEAD@{1}"', spec.dir)))
if a:force || installed || updated if a:force || installed || updated
execute 'cd' s:esc(spec.dir)
call append(3, '- Post-update hook for '. name .' ... ') call append(3, '- Post-update hook for '. name .' ... ')
let type = type(spec.do) let type = type(spec.do)
if type == s:TYPE.string if type == s:TYPE.string
@ -570,8 +570,8 @@ function! s:do(pull, force, todo)
let result = 'Error: Invalid type!' let result = 'Error: Invalid type!'
endif endif
call setline(4, getline(4) . result) call setline(4, getline(4) . result)
cd -
endif endif
cd -
endfor endfor
endfunction endfunction
@ -696,19 +696,17 @@ function! s:update_serial(pull, todo)
for [name, spec] in items(todo) for [name, spec] in items(todo)
let done[name] = 1 let done[name] = 1
if isdirectory(spec.dir) if isdirectory(spec.dir)
execute 'cd' s:esc(spec.dir) let [valid, msg] = s:git_valid(spec, 0)
let [valid, msg] = s:git_valid(spec, 0, 0)
if valid if valid
let result = a:pull ? let result = a:pull ?
\ s:system( \ s:system(
\ printf('git checkout -q %s 2>&1 && git pull --no-rebase origin %s 2>&1 && git submodule update --init --recursive 2>&1', \ printf('git checkout -q %s 2>&1 && git pull --no-rebase origin %s 2>&1 && git submodule update --init --recursive 2>&1',
\ s:shellesc(spec.branch), s:shellesc(spec.branch))) : 'Already installed' \ s:shellesc(spec.branch), s:shellesc(spec.branch)), spec.dir) : 'Already installed'
let error = a:pull ? v:shell_error != 0 : 0 let error = a:pull ? v:shell_error != 0 : 0
else else
let result = msg let result = msg
let error = 1 let error = 1
endif endif
cd -
else else
let result = s:system( let result = s:system(
\ printf('git clone --recursive %s -b %s %s 2>&1', \ printf('git clone --recursive %s -b %s %s 2>&1',
@ -965,21 +963,21 @@ function! s:format_message(ok, name, message)
endif endif
endfunction endfunction
function! s:system(cmd) function! s:system(cmd, ...)
return system(s:is_win ? '('.a:cmd.')' : a:cmd) let cmd = a:0 > 0 ? 'cd '.s:esc(a:1).' && '.a:cmd : a:cmd
return system(s:is_win ? '('.cmd.')' : cmd)
endfunction endfunction
function! s:system_chomp(str) function! s:system_chomp(...)
let ret = s:system(a:str) let ret = call('s:system', a:000)
return v:shell_error ? '' : substitute(ret, '\n$', '', '') return v:shell_error ? '' : substitute(ret, '\n$', '', '')
endfunction endfunction
function! s:git_valid(spec, check_branch, cd) function! s:git_valid(spec, check_branch)
let ret = 1 let ret = 1
let msg = 'OK' let msg = 'OK'
if isdirectory(a:spec.dir) if isdirectory(a:spec.dir)
if a:cd | execute 'cd' s:esc(a:spec.dir) | endif let result = split(s:system('git rev-parse --abbrev-ref HEAD 2>&1 && git config remote.origin.url', a:spec.dir), '\n')
let result = split(s:system('git rev-parse --abbrev-ref HEAD 2>&1 && git config remote.origin.url'), '\n')
let remote = result[-1] let remote = result[-1]
if v:shell_error if v:shell_error
let msg = join([remote, 'PlugClean required.'], "\n") let msg = join([remote, 'PlugClean required.'], "\n")
@ -992,7 +990,7 @@ function! s:git_valid(spec, check_branch, cd)
elseif a:check_branch elseif a:check_branch
let branch = result[0] let branch = result[0]
if a:spec.branch !=# branch if a:spec.branch !=# branch
let tag = s:system_chomp('git describe --exact-match --tags HEAD 2>&1') let tag = s:system_chomp('git describe --exact-match --tags HEAD 2>&1', a:spec.dir)
if a:spec.branch !=# tag if a:spec.branch !=# tag
let msg = printf('Invalid branch/tag: %s (expected: %s). Try PlugUpdate.', let msg = printf('Invalid branch/tag: %s (expected: %s). Try PlugUpdate.',
\ (empty(tag) ? branch : tag), a:spec.branch) \ (empty(tag) ? branch : tag), a:spec.branch)
@ -1000,7 +998,6 @@ function! s:git_valid(spec, check_branch, cd)
endif endif
endif endif
endif endif
if a:cd | cd - | endif
else else
let msg = 'Not found' let msg = 'Not found'
let ret = 0 let ret = 0
@ -1017,7 +1014,7 @@ function! s:clean(force)
let dirs = [] let dirs = []
let [cnt, total] = [0, len(g:plugs)] let [cnt, total] = [0, len(g:plugs)]
for [name, spec] in items(g:plugs) for [name, spec] in items(g:plugs)
if !s:is_managed(name) || s:git_valid(spec, 0, 1)[0] if !s:is_managed(name) || s:git_valid(spec, 0)[0]
call add(dirs, spec.dir) call add(dirs, spec.dir)
endif endif
let cnt += 1 let cnt += 1
@ -1121,7 +1118,7 @@ function! s:status()
for [name, spec] in items(g:plugs) for [name, spec] in items(g:plugs)
if has_key(spec, 'uri') if has_key(spec, 'uri')
if isdirectory(spec.dir) if isdirectory(spec.dir)
let [valid, msg] = s:git_valid(spec, 1, 1) let [valid, msg] = s:git_valid(spec, 1)
else else
let [valid, msg] = [0, 'Not found. Try PlugInstall.'] let [valid, msg] = [0, 'Not found. Try PlugInstall.']
endif endif
@ -1219,9 +1216,7 @@ function! s:preview_commit()
execute 'pedit' sha execute 'pedit' sha
wincmd P wincmd P
setlocal filetype=git buftype=nofile nobuflisted setlocal filetype=git buftype=nofile nobuflisted
execute 'cd' s:esc(g:plugs[name].dir) execute 'silent read !cd' s:esc(g:plugs[name].dir) '&& git show' sha
execute 'silent read !git show' sha
cd -
normal! gg"_dd normal! gg"_dd
wincmd p wincmd p
endfunction endfunction
@ -1242,9 +1237,8 @@ function! s:diff()
continue continue
endif endif
execute 'cd' s:esc(v.dir) let diff = s:system_chomp('git log --pretty=format:"%h %s (%cr)" "HEAD...HEAD@{1}"', v.dir)
let diff = system('git log --pretty=format:"%h %s (%cr)" "HEAD...HEAD@{1}"') if !empty(diff)
if !v:shell_error && !empty(diff)
call append(1, '') call append(1, '')
call append(2, '- '.k.':') call append(2, '- '.k.':')
call append(3, map(split(diff, '\n'), '" ". v:val')) call append(3, map(split(diff, '\n'), '" ". v:val'))
@ -1252,7 +1246,6 @@ function! s:diff()
normal! gg normal! gg
redraw redraw
endif endif
cd -
endfor endfor
call setline(1, cnt == 0 ? 'No updates.' : 'Last update:') call setline(1, cnt == 0 ? 'No updates.' : 'Last update:')
@ -1272,9 +1265,7 @@ function! s:revert()
return return
endif endif
execute 'cd' s:esc(g:plugs[name].dir) call s:system('git reset --hard HEAD@{1} && git checkout '.s:esc(g:plugs[name].branch), g:plugs[name].dir)
call system('git reset --hard HEAD@{1} && git checkout '.s:esc(g:plugs[name].branch))
cd -
setlocal modifiable setlocal modifiable
normal! "_dap normal! "_dap
setlocal nomodifiable setlocal nomodifiable
@ -1301,10 +1292,8 @@ function! s:snapshot(...) abort
\'has_key(v:val, "uri") && isdirectory(v:val.dir)')), 'v:val.dir')) \'has_key(v:val, "uri") && isdirectory(v:val.dir)')), 'v:val.dir'))
let anchor = line('$') - 1 let anchor = line('$') - 1
for dir in reverse(dirs) for dir in reverse(dirs)
execute 'cd' dir let sha = s:system_chomp('git rev-parse --short HEAD', dir)
let sha = substitute(system('git rev-parse --short HEAD'), '\n', '', '') if !empty(sha)
cd -
if !v:shell_error
call append(anchor, printf('cd %s && git reset --hard %s', call append(anchor, printf('cd %s && git reset --hard %s',
\ substitute(dir, '^'.g:plug_home, var, ''), sha)) \ substitute(dir, '^'.g:plug_home, var, ''), sha))
redraw redraw