Use branch name of origin if not specified

This commit is contained in:
Junegunn Choi 2020-08-29 02:00:01 +09:00
parent 95ef5e8d5f
commit 49be3a8ca9

View File

@ -1206,10 +1206,7 @@ function! s:update_finish()
call s:log4(name, 'Checking out '.tag) call s:log4(name, 'Checking out '.tag)
let out = s:system('git checkout -q '.plug#shellescape(tag).' -- 2>&1', spec.dir) let out = s:system('git checkout -q '.plug#shellescape(tag).' -- 2>&1', spec.dir)
else else
let branch = get(spec, 'branch', '') let branch = s:git_origin_branch(spec)
if empty(branch)
let branch = s:git_get_branch(spec.dir)
endif
call s:log4(name, 'Merging origin/'.s:esc(branch)) call s:log4(name, 'Merging origin/'.s:esc(branch))
let out = s:system('git checkout -q '.plug#shellescape(branch).' -- 2>&1' let out = s:system('git checkout -q '.plug#shellescape(branch).' -- 2>&1'
\. (has_key(s:update.new, name) ? '' : ('&& git merge --ff-only '.plug#shellescape('origin/'.branch).' 2>&1')), spec.dir) \. (has_key(s:update.new, name) ? '' : ('&& git merge --ff-only '.plug#shellescape('origin/'.branch).' 2>&1')), spec.dir)
@ -2211,12 +2208,20 @@ function! s:system_chomp(...)
return v:shell_error ? '' : substitute(ret, '\n$', '', '') return v:shell_error ? '' : substitute(ret, '\n$', '', '')
endfunction endfunction
function! s:git_get_branch(dir) function! s:git_origin_branch(spec)
let result = s:lines(s:system('git symbolic-ref --short HEAD', a:dir)) if len(a:spec.branch)
if v:shell_error return a:spec.branch
return ''
endif endif
return result[-1]
" The file may not be present if this is a local repository
let origin_head = a:spec.dir.'/.git/refs/remotes/origin/HEAD'
if filereadable(origin_head)
return split(readfile(origin_head)[0], 'refs/remotes/origin/')[-1]
endif
" The command may not return the name of a branch in detached HEAD state
let result = s:lines(s:system('git symbolic-ref --short HEAD', a:spec.dir))
return v:shell_error ? '' : result[-1]
endfunction endfunction
function! s:git_validate(spec, check_branch) function! s:git_validate(spec, check_branch)
@ -2241,11 +2246,9 @@ function! s:git_validate(spec, check_branch)
\ 'PlugUpdate required.'], "\n") \ 'PlugUpdate required.'], "\n")
endif endif
elseif a:check_branch elseif a:check_branch
let branch = result[0] let current_branch = result[0]
if empty(branch)
let branch = 'HEAD'
endif
" Check tag " Check tag
let origin_branch = s:git_origin_branch(a:spec)
if has_key(a:spec, 'tag') if has_key(a:spec, 'tag')
let tag = s:system_chomp('git describe --exact-match --tags HEAD 2>&1', a:spec.dir) let tag = s:system_chomp('git describe --exact-match --tags HEAD 2>&1', a:spec.dir)
if a:spec.tag !=# tag && a:spec.tag !~ '\*' if a:spec.tag !=# tag && a:spec.tag !~ '\*'
@ -2253,14 +2256,14 @@ function! s:git_validate(spec, check_branch)
\ (empty(tag) ? 'N/A' : tag), a:spec.tag) \ (empty(tag) ? 'N/A' : tag), a:spec.tag)
endif endif
" Check branch " Check branch
elseif a:spec.branch != '' && a:spec.branch !=# branch elseif origin_branch !=# current_branch
let err = printf('Invalid branch: %s (expected: %s). Try PlugUpdate.', let err = printf('Invalid branch: %s (expected: %s). Try PlugUpdate.',
\ branch, a:spec.branch) \ current_branch, origin_branch)
endif endif
if empty(err) if empty(err)
let [ahead, behind] = split(s:lastline(s:system([ let [ahead, behind] = split(s:lastline(s:system([
\ 'git', 'rev-list', '--count', '--left-right', \ 'git', 'rev-list', '--count', '--left-right',
\ printf('HEAD...origin/%s', branch) \ printf('HEAD...origin/%s', origin_branch)
\ ], a:spec.dir)), '\t') \ ], a:spec.dir)), '\t')
if !v:shell_error && ahead if !v:shell_error && ahead
if behind if behind
@ -2268,11 +2271,11 @@ function! s:git_validate(spec, check_branch)
" pushable (and probably not that messed up). " pushable (and probably not that messed up).
let err = printf( let err = printf(
\ "Diverged from origin/%s (%d commit(s) ahead and %d commit(s) behind!\n" \ "Diverged from origin/%s (%d commit(s) ahead and %d commit(s) behind!\n"
\ .'Backup local changes and run PlugClean and PlugUpdate to reinstall it.', branch, ahead, behind) \ .'Backup local changes and run PlugClean and PlugUpdate to reinstall it.', origin_branch, ahead, behind)
else else
let err = printf("Ahead of origin/%s by %d commit(s).\n" let err = printf("Ahead of origin/%s by %d commit(s).\n"
\ .'Cannot update until local changes are pushed.', \ .'Cannot update until local changes are pushed.',
\ branch, ahead) \ origin_branch, ahead)
endif endif
endif endif
endif endif
@ -2588,18 +2591,6 @@ function! s:append_ul(lnum, text)
call append(a:lnum, ['', a:text, repeat('-', len(a:text))]) call append(a:lnum, ['', a:text, repeat('-', len(a:text))])
endfunction endfunction
function! s:git_origin_branch(spec)
if len(a:spec.branch)
return a:spec.branch
endif
let origin_head = a:spec.dir.'/.git/refs/remotes/origin/HEAD'
if !filereadable(origin_head)
return ''
endif
return split(readfile(origin_head)[0], '/')[-1]
endfunction
function! s:diff() function! s:diff()
call s:prepare() call s:prepare()
call append(0, ['Collecting changes ...', '']) call append(0, ['Collecting changes ...', ''])