Add Git config query functions

Querying the remote URL is a behavior needed by Rhubarb and similar
plugins, so add a function for it in particular.
This commit is contained in:
Tim Pope
2018-07-21 16:33:34 -04:00
parent b8046d1c5e
commit be68a8ec8a

View File

@@ -110,7 +110,7 @@ function! fugitive#Prepare(...) abort
let args = copy(a:000) let args = copy(a:000)
if empty(args) if empty(args)
return g:fugitive_git_executable return g:fugitive_git_executable
elseif args[0] !~# '^-' && args[0] =~# '[\/.]' elseif args[0] !~# '^-' && args[0] =~# '[\/.]\|^$'
let args[0] = '--git-dir=' . args[0] let args[0] = '--git-dir=' . args[0]
endif endif
return g:fugitive_git_executable . ' ' . join(map(args, 's:shellesc(v:val)'), ' ') return g:fugitive_git_executable . ' ' . join(map(args, 's:shellesc(v:val)'), ' ')
@@ -124,6 +124,23 @@ function! fugitive#GitVersion(...) abort
return s:git_versions[g:fugitive_git_executable] return s:git_versions[g:fugitive_git_executable]
endfunction endfunction
function! fugitive#Config(name, ...) abort
let cmd = fugitive#Prepare(a:0 ? a:1 : get(b:, 'git_dir', ''), 'config', '--get', a:name)
let out = matchstr(system(cmd), "[^\r\n]*")
return v:shell_error ? '' : out
endfunction
function! fugitive#RemoteUrl(...) abort
let dir = a:0 > 1 ? a:2 : get(b:, 'git_dir', '')
let remote = !a:0 || a:1 =~# '^\.\=$' ? 'origin' : a:1
if fugitive#GitVersion() =~# '^[01]\.\|^2\.[0-6]\.'
return fugitive#Config('remote.' . remote . '.url')
endif
let cmd = fugitive#Prepare(dir, 'remote', 'get-url', remote)
let out = substitute(system(cmd), "\n$", '', '')
return v:shell_error ? '' : out
endfunction
function! s:recall() abort function! s:recall() abort
let rev = s:sub(s:buffer().rev(), '^/', '') let rev = s:sub(s:buffer().rev(), '^/', '')
if rev ==# ':' if rev ==# ':'
@@ -422,15 +439,15 @@ endfunction
call s:add_methods('repo',['dirglob','superglob']) call s:add_methods('repo',['dirglob','superglob'])
function! s:repo_config(conf) dict abort function! s:repo_config(name) dict abort
return matchstr(s:repo().git_chomp('config',a:conf),"[^\r\n]*") return fugitive#Config(a:name, self.git_dir)
endfun endfunction
function! s:repo_user() dict abort function! s:repo_user() dict abort
let username = s:repo().config('user.name') let username = self.config('user.name')
let useremail = s:repo().config('user.email') let useremail = self.config('user.email')
return username.' <'.useremail.'>' return username.' <'.useremail.'>'
endfun endfunction
function! s:repo_aliases() dict abort function! s:repo_aliases() dict abort
if !has_key(self,'_aliases') if !has_key(self,'_aliases')
@@ -2518,16 +2535,9 @@ function! s:Browse(bang,line1,count,...) abort
if empty(remote) if empty(remote)
let remote = '.' let remote = '.'
let remote_for_url = 'origin'
else
let remote_for_url = remote
endif endif
if fugitive#GitVersion() =~# '^[01]\.\|^2\.[0-6]\.' let raw = fugitive#RemoteUrl(remote)
let raw = s:repo().git_chomp('config','remote.'.remote_for_url.'.url') if empty(raw)
else
let raw = s:repo().git_chomp('remote','get-url',remote_for_url)
endif
if raw ==# ''
let raw = remote let raw = remote
endif endif