From c6ed41f47e264a2033f4d22dc1229d596d0c40a2 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 10 Apr 2016 20:33:37 +0900 Subject: [PATCH] Detect plugins that are diverged their origins Close #462 --- plug.vim | 36 +++++++++++++++++++++++++++--------- test/workflow.vader | 23 +++++++++++++++++++++-- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/plug.vim b/plug.vim index 2140220..2b1be94 100644 --- a/plug.vim +++ b/plug.vim @@ -1145,7 +1145,7 @@ while 1 " Without TCO, Vim stack is bound to explode let has_tag = has_key(spec, 'tag') if !new - let error = s:git_validate(spec, 0) + let [error, _] = s:git_validate(spec, 0) if empty(error) if pull let fetch_opt = (has_tag && !empty(globpath(spec.dir, '.git/shallow'))) ? '--depth 99999999' : '' @@ -1860,11 +1860,18 @@ function! s:git_validate(spec, check_branch) let err = printf('Invalid branch: %s (expected: %s). Try PlugUpdate.', \ branch, a:spec.branch) endif + if empty(err) + let commits = len(s:lines(s:system(printf('git rev-list origin/%s..HEAD', a:spec.branch), a:spec.dir))) + if !v:shell_error && commits + let err = join([printf('Diverged from origin/%s by %d commit(s).', a:spec.branch, commits), + \ 'Reinstall after PlugClean.'], "\n") + endif + endif endif else let err = 'Not found' endif - return err + return [err, err =~# 'PlugClean'] endfunction function! s:rm_rf(dir) @@ -1875,15 +1882,23 @@ endfunction function! s:clean(force) call s:prepare() - call append(0, 'Searching for unused plugins in '.g:plug_home) + call append(0, 'Searching for invalid plugins in '.g:plug_home) call append(1, '') " List of valid directories let dirs = [] + let errs = {} let [cnt, total] = [0, len(g:plugs)] for [name, spec] in items(g:plugs) - if !s:is_managed(name) || empty(s:git_validate(spec, 0)) + if !s:is_managed(name) call add(dirs, spec.dir) + else + let [err, clean] = s:git_validate(spec, 1) + if clean + let errs[spec.dir] = s:lines(err)[0] + else + call add(dirs, spec.dir) + endif endif let cnt += 1 call s:progress_bar(2, repeat('=', cnt), total) @@ -1907,11 +1922,14 @@ function! s:clean(force) if !has_key(allowed, f) && isdirectory(f) call add(todo, f) call append(line('$'), '- ' . f) + if has_key(errs, f) + call append(line('$'), ' ' . errs[f]) + endif let found = filter(found, 'stridx(v:val, f) != 0') end endwhile - normal! G + 4 redraw if empty(todo) call append(line('$'), 'Already clean.') @@ -1920,12 +1938,12 @@ function! s:clean(force) for dir in todo call s:rm_rf(dir) endfor - call append(line('$'), 'Removed.') + call append(3, ['Removed.', '']) else - call append(line('$'), 'Cancelled.') + call append(3, ['Cancelled.', '']) endif endif - normal! G + 4 endfunction function! s:upgrade() @@ -1972,7 +1990,7 @@ function! s:status() for [name, spec] in items(g:plugs) if has_key(spec, 'uri') if isdirectory(spec.dir) - let err = s:git_validate(spec, 1) + let [err, _] = s:git_validate(spec, 1) let [valid, msg] = [empty(err), empty(err) ? 'OK' : err] else let [valid, msg] = [0, 'Not found. Try PlugInstall.'] diff --git a/test/workflow.vader b/test/workflow.vader index 4e83ab8..bdc3fff 100644 --- a/test/workflow.vader +++ b/test/workflow.vader @@ -212,6 +212,24 @@ Expect: [==] x seoul256.vim: +Execute (Corrected the URI but diverged from master): + call plug#begin() + Plug 'junegunn/seoul256.vim' + Plug 'file:///tmp/jg/vim-emoji' + call plug#end() + for _ in range(3) + call system(printf('cd "%s" && git commit --allow-empty -m "dummy"', g:plugs['seoul256.vim'].dir)) + endfor + call PlugStatusSorted() + +Expect: + Diverged from origin/master by 3 commit(s). + Reinstall after PlugClean. + - vim-emoji: OK + Finished. 1 error(s). + [==] + x seoul256.vim: + # TODO: does not work due to inputsave() # Do (PlugClean): # :PlugClean\y\ @@ -222,8 +240,9 @@ Expect: Execute (PlugClean! to remove seoul256.vim): PlugClean! " Three removed, emoji left + AssertEqual 'Removed.', getline(4) AssertExpect '^- ', 3 - AssertExpect 'Removed', 1 + AssertExpect 'Diverged', 1 Assert empty(globpath(&rtp, 'colors/seoul256.vim')) Assert !empty(globpath(&rtp, 'autoload/emoji.vim')) q @@ -250,7 +269,7 @@ Expect: Execute (PlugClean! to remove vim-emoji): PlugClean! AssertExpect '^- ', 1 - AssertExpect 'Removed', 1 + AssertEqual 'Removed.', getline(4) Assert empty(globpath(&rtp, 'colors/seoul256.vim')) Assert empty(globpath(&rtp, 'autoload/emoji.vim')) q