From ee343ab562f3b539b6f81c8753cce532a4e6284b Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 17 Sep 2013 12:35:10 +0900 Subject: [PATCH] Git remote validation --- README.md | 3 +-- plug.vim | 75 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 294a73a..cf1ad03 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,6 @@ A single-file Vim plugin manager. - Easier to setup - Parallel installation/update (requires +ruby) -- Alternative directory structure: user/repo - - Make it easier to switch plugins of the same name from different authors ### Cons. @@ -47,6 +45,7 @@ call plug#begin() Plug 'junegunn/seoul256' Plug 'junegunn/vim-easy-align' " Plug 'user/repo', 'branch_or_tag' +" Plug 'git@github.com:junegunn/vim-github-dashboard.git' " ... call plug#end() diff --git a/plug.vim b/plug.vim index 1e23c1b..d85a2ba 100644 --- a/plug.vim +++ b/plug.vim @@ -105,15 +105,20 @@ function! s:add(...) return endif - if plugin !~ '/' - let plugin = 'vim-scripts/'. plugin + if plugin =~ ':' + let uri = plugin + else + if plugin !~ '/' + let plugin = 'vim-scripts/'. plugin + endif + let uri = 'https://git:@github.com/' . plugin . '.git' endif - let name = split(plugin, '/')[-1] - let dir = fnamemodify(join([g:plug_home, plugin], '/'), ':p') - let uri = 'https://git:@github.com/' . plugin . '.git' - let spec = { 'name': name, 'dir': dir, 'uri': uri, 'branch': branch } - 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 function! s:install(...) @@ -159,7 +164,8 @@ function! s:lpad(str, len) endfunction function! s:system(cmd) - return split(system(a:cmd), '\n')[-1] + let lines = split(system(a:cmd), '\n') + return get(lines, -1, '') endfunction function! s:prepare() @@ -233,11 +239,16 @@ function! s:update_serial(pull) let d = shellescape(spec.dir) if isdirectory(spec.dir) execute 'cd '.spec.dir - let result = a:pull ? - \ s:system( - \ printf('git checkout -q %s && git pull origin %s 2>&1', - \ spec.branch, spec.branch)) : 'Already installed' - let error = a:pull ? v:shell_error != 0 : 0 + if s:git_valid(spec, 0) + let result = a:pull ? + \ s:system( + \ printf('git checkout -q %s && git pull origin %s 2>&1', + \ spec.branch, spec.branch)) : 'Already installed' + let error = a:pull ? v:shell_error != 0 : 0 + else + let result = "PlugClean required. Invalid remote repository." + let error = 1 + endif else if !isdirectory(base) call mkdir(base, 'p') @@ -290,17 +301,27 @@ function! s:update_parallel(pull, threads) VIM::evaluate('a:threads').to_i.times.map { |i| Thread.new(i) do |ii| while pair = take1.call - name, dir, uri, branch = pair.last.values_at *%w[name dir uri branch] - result = + name = pair.first + dir, uri, branch = pair.last.values_at *%w[dir uri branch] + ok, result = if File.directory? dir - pull ? - `#{cd} #{dir} && git checkout -q #{branch} && git pull origin #{branch} 2>&1` - : skip + current_uri = `#{cd} #{dir} && git config remote.origin.url`.chomp + if $? == 0 && current_uri == uri + 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 FileUtils.mkdir_p(base) - `#{cd} #{base} && git clone --recursive #{uri} -b #{branch} #{dir} 2>&1` - end.lines.to_a.last.strip - log.call name, result, ($? == 0 || result == skip) + r = `#{cd} #{base} && git clone --recursive #{uri} -b #{branch} #{dir} 2>&1` + [$? == 0, r] + end + result = result.lines.to_a.last.strip + log.call name, result, ok end end }.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)') 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() call s:prepare() call append(0, 'Removing unused plugins in '.g:plug_home) - " List of files - let dirs = map(values(g:plugs), 's:path(v:val.dir)') + " List of valid directories + let dirs = map(filter(values(g:plugs), 's:git_valid(v:val, 1)'), 'v:val.dir') + let alldirs = dirs + \ map(copy(dirs), 'fnamemodify(v:val, ":h")') for dir in dirs