git_validate: better error with checkout being ahead/diverged

The `--count` and `--left-right` options seem to be in Git since before
1.7 already.

The error message is improved, and distinguishes between only being
ahead (which is the case if you could push), or being diverged (ahead
and defined).

I think it could be bad to accidentally clean/delete a local checkout
which is ahead, and therefore PlugClean is not mentioned then, which
appears to trigger it to show up in :PlugClean.
This commit is contained in:
Daniel Hahler 2016-10-28 00:19:14 +02:00
parent 6ad18f5fb8
commit bfd8881374

View File

@ -2013,10 +2013,24 @@ function! s:git_validate(spec, check_branch)
\ branch, a:spec.branch) \ branch, a:spec.branch)
endif endif
if empty(err) if empty(err)
let commits = len(s:lines(s:system(printf('git rev-list origin/%s..HEAD', a:spec.branch), a:spec.dir))) let [ahead, behind] = split(s:lastline(s:system(printf(
if !v:shell_error && commits \ 'git rev-list --count --left-right HEAD...origin/%s',
let err = join([printf('Diverged from origin/%s by %d commit(s).', a:spec.branch, commits), \ a:spec.branch), a:spec.dir)), '\t')
\ 'Reinstall after PlugClean.'], "\n") if !v:shell_error && ahead
if behind
let base_error = printf(
\ 'Diverged from origin/%%s (%%d commit(s) ahead and %d commit(s) behind!', behind)
else
let base_error = 'Ahead of origin/%s by %d commit(s).'
endif
let err = join([printf(base_error, a:spec.branch, ahead),
\ 'Can you push it yourself?',
\ 'Otherwise try reinstalling it after cleaning it manually.'], "\n")
" Only mention PlugClean if diverged, otherwise it's likely to be
" pushable (and probably not that messed up).
if behind
let err .= "\nTry PlugClean after backing it up."
endif
endif endif
endif endif
endif endif