Git remote validation

This commit is contained in:
Junegunn Choi 2013-09-17 12:35:10 +09:00
parent 09df71c3db
commit ee343ab562
2 changed files with 53 additions and 25 deletions

View File

@ -23,8 +23,6 @@ A single-file Vim plugin manager.
- Easier to setup - Easier to setup
- Parallel installation/update (requires +ruby) - Parallel installation/update (requires +ruby)
- Alternative directory structure: user/repo
- Make it easier to switch plugins of the same name from different authors
### Cons. ### Cons.
@ -47,6 +45,7 @@ call plug#begin()
Plug 'junegunn/seoul256' Plug 'junegunn/seoul256'
Plug 'junegunn/vim-easy-align' Plug 'junegunn/vim-easy-align'
" Plug 'user/repo', 'branch_or_tag' " Plug 'user/repo', 'branch_or_tag'
" Plug 'git@github.com:junegunn/vim-github-dashboard.git'
" ... " ...
call plug#end() call plug#end()

View File

@ -105,15 +105,20 @@ function! s:add(...)
return return
endif endif
if plugin =~ ':'
let uri = plugin
else
if plugin !~ '/' if plugin !~ '/'
let plugin = 'vim-scripts/'. plugin let plugin = 'vim-scripts/'. plugin
endif endif
let name = split(plugin, '/')[-1]
let dir = fnamemodify(join([g:plug_home, plugin], '/'), ':p')
let uri = 'https://git:@github.com/' . plugin . '.git' let uri = 'https://git:@github.com/' . plugin . '.git'
let spec = { 'name': name, 'dir': dir, 'uri': uri, 'branch': branch } endif
let g:plugs[plugin] = spec
let name = substitute(split(plugin, '/')[-1], '\.git$', '', '')
let dir = fnamemodify(join([g:plug_home, name], '/'), ':p')
let spec = { 'dir': dir, 'uri': uri, 'branch': branch }
let g:plugs[name] = spec
endfunction endfunction
function! s:install(...) function! s:install(...)
@ -159,7 +164,8 @@ function! s:lpad(str, len)
endfunction endfunction
function! s:system(cmd) function! s:system(cmd)
return split(system(a:cmd), '\n')[-1] let lines = split(system(a:cmd), '\n')
return get(lines, -1, '')
endfunction endfunction
function! s:prepare() function! s:prepare()
@ -233,11 +239,16 @@ function! s:update_serial(pull)
let d = shellescape(spec.dir) let d = shellescape(spec.dir)
if isdirectory(spec.dir) if isdirectory(spec.dir)
execute 'cd '.spec.dir execute 'cd '.spec.dir
if s:git_valid(spec, 0)
let result = a:pull ? let result = a:pull ?
\ s:system( \ s:system(
\ printf('git checkout -q %s && git pull origin %s 2>&1', \ printf('git checkout -q %s && git pull origin %s 2>&1',
\ spec.branch, spec.branch)) : 'Already installed' \ spec.branch, spec.branch)) : 'Already installed'
let error = a:pull ? v:shell_error != 0 : 0 let error = a:pull ? v:shell_error != 0 : 0
else
let result = "PlugClean required. Invalid remote repository."
let error = 1
endif
else else
if !isdirectory(base) if !isdirectory(base)
call mkdir(base, 'p') call mkdir(base, 'p')
@ -290,17 +301,27 @@ function! s:update_parallel(pull, threads)
VIM::evaluate('a:threads').to_i.times.map { |i| VIM::evaluate('a:threads').to_i.times.map { |i|
Thread.new(i) do |ii| Thread.new(i) do |ii|
while pair = take1.call while pair = take1.call
name, dir, uri, branch = pair.last.values_at *%w[name dir uri branch] name = pair.first
result = dir, uri, branch = pair.last.values_at *%w[dir uri branch]
ok, result =
if File.directory? dir if File.directory? dir
pull ? current_uri = `#{cd} #{dir} && git config remote.origin.url`.chomp
`#{cd} #{dir} && git checkout -q #{branch} && git pull origin #{branch} 2>&1` if $? == 0 && current_uri == uri
: skip if pull
[true, `#{cd} #{dir} && git checkout -q #{branch} && git pull origin #{branch} 2>&1`]
else
[true, skip]
end
else
[false, "PlugClean required. Invalid remote repository."]
end
else else
FileUtils.mkdir_p(base) FileUtils.mkdir_p(base)
`#{cd} #{base} && git clone --recursive #{uri} -b #{branch} #{dir} 2>&1` r = `#{cd} #{base} && git clone --recursive #{uri} -b #{branch} #{dir} 2>&1`
end.lines.to_a.last.strip [$? == 0, r]
log.call name, result, ($? == 0 || result == skip) end
result = result.lines.to_a.last.strip
log.call name, result, ok
end end
end end
}.each(&:join) }.each(&:join)
@ -317,12 +338,20 @@ function! s:glob_dir(path)
return map(filter(split(globpath(a:path, '**'), '\n'), 'isdirectory(v:val)'), 's:path(v:val)') return map(filter(split(globpath(a:path, '**'), '\n'), 'isdirectory(v:val)'), 's:path(v:val)')
endfunction endfunction
function! s:git_valid(spec, cd)
if a:cd | execute "cd " . a:spec.dir | endif
let ret = s:system("git config remote.origin.url") == a:spec.uri
if a:cd | cd - | endif
return ret
endfunction
function! s:clean() function! s:clean()
call s:prepare() call s:prepare()
call append(0, 'Removing unused plugins in '.g:plug_home) call append(0, 'Removing unused plugins in '.g:plug_home)
" List of files " List of valid directories
let dirs = map(values(g:plugs), 's:path(v:val.dir)') let dirs = map(filter(values(g:plugs), 's:git_valid(v:val, 1)'), 'v:val.dir')
let alldirs = dirs + let alldirs = dirs +
\ map(copy(dirs), 'fnamemodify(v:val, ":h")') \ map(copy(dirs), 'fnamemodify(v:val, ":h")')
for dir in dirs for dir in dirs