2010-02-14 14:16:30 -05:00
|
|
|
" fugitive.vim - A Git wrapper so awesome, it should be illegal
|
2010-01-09 18:23:16 -05:00
|
|
|
" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
|
2010-03-21 17:18:43 -04:00
|
|
|
" Version: 1.1
|
2010-02-15 14:03:22 -05:00
|
|
|
" GetLatestVimScripts: 2975 1 :AutoInstall: fugitive.vim
|
2009-10-10 19:47:14 -04:00
|
|
|
|
2010-02-15 14:03:04 -05:00
|
|
|
if exists('g:loaded_fugitive') || &cp
|
2009-10-10 19:47:14 -04:00
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_fugitive = 1
|
|
|
|
|
|
|
|
if !exists('g:fugitive_git_executable')
|
|
|
|
let g:fugitive_git_executable = 'git'
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Utility {{{1
|
|
|
|
|
|
|
|
function! s:function(name) abort
|
|
|
|
return function(substitute(a:name,'^s:',matchstr(expand('<sfile>'), '<SNR>\d\+_'),''))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:sub(str,pat,rep) abort
|
|
|
|
return substitute(a:str,'\v\C'.a:pat,a:rep,'')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:gsub(str,pat,rep) abort
|
|
|
|
return substitute(a:str,'\v\C'.a:pat,a:rep,'g')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:shellesc(arg) abort
|
|
|
|
if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
|
|
|
|
return a:arg
|
2010-01-13 22:01:43 -05:00
|
|
|
elseif &shell =~# 'cmd' && a:arg !~# '"'
|
|
|
|
return '"'.a:arg.'"'
|
2009-10-10 19:47:14 -04:00
|
|
|
else
|
|
|
|
return shellescape(a:arg)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:fnameescape(file) abort
|
|
|
|
if exists('*fnameescape')
|
|
|
|
return fnameescape(a:file)
|
|
|
|
else
|
|
|
|
return escape(a:file," \t\n*?[{`$\\%#'\"|!<")
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:throw(string) abort
|
|
|
|
let v:errmsg = 'fugitive: '.a:string
|
|
|
|
throw v:errmsg
|
|
|
|
endfunction
|
|
|
|
|
2010-01-09 19:12:45 -05:00
|
|
|
function! s:warn(str)
|
|
|
|
echohl WarningMsg
|
|
|
|
echomsg a:str
|
|
|
|
echohl None
|
|
|
|
let v:warningmsg = a:str
|
|
|
|
endfunction
|
|
|
|
|
2010-03-09 19:03:44 -05:00
|
|
|
function! s:shellslash(path)
|
|
|
|
if exists('+shellslash') && !&shellslash
|
|
|
|
return s:gsub(a:path,'\\','/')
|
|
|
|
else
|
|
|
|
return a:path
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2009-10-10 19:47:14 -04:00
|
|
|
function! s:add_methods(namespace, method_names) abort
|
|
|
|
for name in a:method_names
|
|
|
|
let s:{a:namespace}_prototype[name] = s:function('s:'.a:namespace.'_'.name)
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let s:commands = []
|
|
|
|
function! s:command(definition) abort
|
|
|
|
let s:commands += [a:definition]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:define_commands()
|
|
|
|
for command in s:commands
|
|
|
|
exe 'command! -buffer '.command
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
2010-01-24 23:07:19 -05:00
|
|
|
function! s:compatibility_check()
|
|
|
|
if exists('b:git_dir') && exists('*GitBranchInfoCheckGitDir') && !exists('g:fugitive_did_compatibility_warning')
|
|
|
|
let g:fugitive_did_compatibility_warning = 1
|
|
|
|
call s:warn("See http://github.com/tpope/vim-fugitive/issues#issue/1 for why you should remove git-branch-info.vim")
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup fugitive_utility
|
2009-10-10 19:47:14 -04:00
|
|
|
autocmd!
|
|
|
|
autocmd User Fugitive call s:define_commands()
|
2010-01-24 23:07:19 -05:00
|
|
|
autocmd VimEnter * call s:compatibility_check()
|
2009-10-10 19:47:14 -04:00
|
|
|
augroup END
|
|
|
|
|
|
|
|
let s:abstract_prototype = {}
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Initialization {{{1
|
|
|
|
|
|
|
|
function! s:ExtractGitDir(path) abort
|
2010-03-09 19:03:44 -05:00
|
|
|
let path = s:shellslash(a:path)
|
2010-03-08 22:51:10 -05:00
|
|
|
if path =~? '^fugitive://.*//'
|
|
|
|
return matchstr(path,'fugitive://\zs.\{-\}\ze//')
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
2010-03-08 22:51:10 -05:00
|
|
|
let fn = fnamemodify(path,':s?[\/]$??')
|
2009-10-10 19:47:14 -04:00
|
|
|
let ofn = ""
|
|
|
|
let nfn = fn
|
|
|
|
while fn != ofn
|
|
|
|
if isdirectory(fn . '/.git')
|
2010-01-13 17:39:54 -05:00
|
|
|
return s:sub(simplify(fnamemodify(fn . '/.git',':p')),'\W$','')
|
2009-10-10 19:47:14 -04:00
|
|
|
elseif fn =~ '\.git$' && filereadable(fn . '/HEAD')
|
2010-01-13 17:39:54 -05:00
|
|
|
return s:sub(simplify(fnamemodify(fn,':p')),'\W$','')
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
let ofn = fn
|
|
|
|
let fn = fnamemodify(ofn,':h')
|
|
|
|
endwhile
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2010-02-07 21:54:07 -05:00
|
|
|
function! s:Detect(path)
|
2010-01-24 22:59:42 -05:00
|
|
|
if exists('b:git_dir') && b:git_dir ==# ''
|
|
|
|
unlet b:git_dir
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
if !exists('b:git_dir')
|
2010-02-07 21:54:07 -05:00
|
|
|
let dir = s:ExtractGitDir(a:path)
|
2009-10-10 19:47:14 -04:00
|
|
|
if dir != ''
|
|
|
|
let b:git_dir = dir
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
if exists('b:git_dir')
|
|
|
|
silent doautocmd User Fugitive
|
|
|
|
cnoremap <expr> <buffer> <C-R><C-G> fugitive#buffer().rev()
|
2009-12-13 19:49:35 -05:00
|
|
|
if expand('%:p') =~# '//'
|
|
|
|
let buffer = fugitive#buffer()
|
|
|
|
call buffer.setvar('&path',s:sub(buffer.getvar('&path'),'^\.%(,|$)',''))
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup fugitive
|
|
|
|
autocmd!
|
2010-02-07 21:54:07 -05:00
|
|
|
autocmd BufNewFile,BufReadPost * call s:Detect(expand('<amatch>:p'))
|
|
|
|
autocmd FileType netrw call s:Detect(expand('<amatch>:p'))
|
|
|
|
autocmd VimEnter * if expand('<amatch>')==''|call s:Detect(getcwd())|endif
|
2010-05-25 15:34:53 -04:00
|
|
|
autocmd BufWinLeave * execute getwinvar(+winnr(), 'fugitive_restore')
|
2009-10-10 19:47:14 -04:00
|
|
|
augroup END
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Repository {{{1
|
|
|
|
|
|
|
|
let s:repo_prototype = {}
|
|
|
|
let s:repos = {}
|
|
|
|
|
|
|
|
function! s:repo(...) abort
|
2010-01-24 22:59:42 -05:00
|
|
|
let dir = a:0 ? a:1 : (exists('b:git_dir') && b:git_dir !=# '' ? b:git_dir : s:ExtractGitDir(expand('%:p')))
|
2009-11-10 21:17:21 -05:00
|
|
|
if dir !=# ''
|
2009-10-10 19:47:14 -04:00
|
|
|
if has_key(s:repos,dir)
|
|
|
|
let repo = get(s:repos,dir)
|
|
|
|
else
|
|
|
|
let repo = {'git_dir': dir}
|
|
|
|
let s:repos[dir] = repo
|
|
|
|
endif
|
|
|
|
return extend(extend(repo,s:repo_prototype,'keep'),s:abstract_prototype,'keep')
|
|
|
|
endif
|
2009-11-10 21:17:21 -05:00
|
|
|
call s:throw('not a git repository: '.expand('%:p'))
|
2009-10-10 19:47:14 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_dir(...) dict abort
|
|
|
|
return join([self.git_dir]+a:000,'/')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_tree(...) dict abort
|
2009-11-10 21:17:21 -05:00
|
|
|
if !self.bare()
|
2009-10-10 19:47:14 -04:00
|
|
|
let dir = fnamemodify(self.git_dir,':h')
|
|
|
|
return join([dir]+a:000,'/')
|
|
|
|
endif
|
2009-11-10 21:17:21 -05:00
|
|
|
call s:throw('no work tree')
|
2009-10-10 19:47:14 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_bare() dict abort
|
|
|
|
return self.dir() !~# '/\.git$'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_translate(spec) dict abort
|
|
|
|
if a:spec ==# '.' || a:spec ==# '/.'
|
|
|
|
return self.bare() ? self.dir() : self.tree()
|
|
|
|
elseif a:spec =~# '^/'
|
|
|
|
return fnamemodify(self.dir(),':h').a:spec
|
|
|
|
elseif a:spec =~# '^:[0-3]:'
|
|
|
|
return 'fugitive://'.self.dir().'//'.a:spec[1].'/'.a:spec[3:-1]
|
|
|
|
elseif a:spec ==# ':'
|
2010-01-09 18:19:07 -05:00
|
|
|
if $GIT_INDEX_FILE =~# '/[^/]*index[^/]*\.lock$' && fnamemodify($GIT_INDEX_FILE,':p')[0:strlen(s:repo().dir())] ==# s:repo().dir('') && filereadable($GIT_INDEX_FILE)
|
|
|
|
return fnamemodify($GIT_INDEX_FILE,':p')
|
|
|
|
else
|
|
|
|
return self.dir('index')
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
elseif a:spec =~# '^:/'
|
|
|
|
let ref = self.rev_parse(matchstr(a:spec,'.[^:]*'))
|
|
|
|
return 'fugitive://'.self.dir().'//'.ref
|
|
|
|
elseif a:spec =~# '^:'
|
|
|
|
return 'fugitive://'.self.dir().'//0/'.a:spec[1:-1]
|
|
|
|
elseif a:spec =~# 'HEAD\|^refs/' && a:spec !~ ':' && filereadable(self.dir(a:spec))
|
|
|
|
return self.dir(a:spec)
|
|
|
|
elseif filereadable(s:repo().dir('refs/'.a:spec))
|
|
|
|
return self.dir('refs/'.a:spec)
|
|
|
|
elseif filereadable(s:repo().dir('refs/tags/'.a:spec))
|
|
|
|
return self.dir('refs/tags/'.a:spec)
|
|
|
|
elseif filereadable(s:repo().dir('refs/heads/'.a:spec))
|
|
|
|
return self.dir('refs/heads/'.a:spec)
|
|
|
|
elseif filereadable(s:repo().dir('refs/remotes/'.a:spec))
|
|
|
|
return self.dir('refs/remotes/'.a:spec)
|
|
|
|
elseif filereadable(s:repo().dir('refs/remotes/'.a:spec.'/HEAD'))
|
|
|
|
return self.dir('refs/remotes/'.a:spec,'/HEAD')
|
|
|
|
else
|
|
|
|
try
|
|
|
|
let ref = self.rev_parse(matchstr(a:spec,'[^:]*'))
|
|
|
|
let path = s:sub(matchstr(a:spec,':.*'),'^:','/')
|
|
|
|
return 'fugitive://'.self.dir().'//'.ref.path
|
|
|
|
catch /^fugitive:/
|
|
|
|
return self.tree(a:spec)
|
|
|
|
endtry
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:add_methods('repo',['dir','tree','bare','translate'])
|
|
|
|
|
|
|
|
function! s:repo_git_command(...) dict abort
|
|
|
|
let git = g:fugitive_git_executable . ' --git-dir='.s:shellesc(self.git_dir)
|
|
|
|
return git.join(map(copy(a:000),'" ".s:shellesc(v:val)'),'')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_git_chomp(...) dict abort
|
|
|
|
return s:sub(system(call(self.git_command,a:000,self)),'\n$','')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_git_chomp_in_tree(...) dict abort
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
|
|
|
try
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=s:repo().tree()`'
|
2009-10-10 19:47:14 -04:00
|
|
|
return call(s:repo().git_chomp, a:000, s:repo())
|
|
|
|
finally
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=dir`'
|
2009-10-10 19:47:14 -04:00
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_rev_parse(rev) dict abort
|
|
|
|
let hash = self.git_chomp('rev-parse','--verify',a:rev)
|
|
|
|
if hash =~ '^\x\{40\}$'
|
|
|
|
return hash
|
|
|
|
endif
|
2009-11-10 21:17:21 -05:00
|
|
|
call s:throw('rev-parse '.a:rev.': '.hash)
|
2009-10-10 19:47:14 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:add_methods('repo',['git_command','git_chomp','git_chomp_in_tree','rev_parse'])
|
|
|
|
|
|
|
|
function! s:repo_dirglob(base) dict abort
|
|
|
|
let base = s:sub(a:base,'^/','')
|
|
|
|
let matches = split(glob(self.tree(s:gsub(base,'/','*&').'*/')),"\n")
|
|
|
|
call map(matches,'v:val[ strlen(self.tree())+(a:base !~ "^/") : -1 ]')
|
|
|
|
return matches
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repo_superglob(base) dict abort
|
|
|
|
if a:base =~# '^/' || a:base !~# ':'
|
|
|
|
let results = []
|
|
|
|
if a:base !~# '^/'
|
|
|
|
let heads = ["HEAD","ORIG_HEAD","FETCH_HEAD","MERGE_HEAD"]
|
|
|
|
let heads += sort(split(s:repo().git_chomp("rev-parse","--symbolic","--branches","--tags","--remotes"),"\n"))
|
|
|
|
call filter(heads,'v:val[ 0 : strlen(a:base)-1 ] ==# a:base')
|
|
|
|
let results += heads
|
|
|
|
endif
|
|
|
|
if !self.bare()
|
|
|
|
let base = s:sub(a:base,'^/','')
|
|
|
|
let matches = split(glob(self.tree(s:gsub(base,'/','*&').'*')),"\n")
|
2010-03-09 19:03:44 -05:00
|
|
|
call map(matches,'s:shellslash(v:val)')
|
2009-10-10 19:47:14 -04:00
|
|
|
call map(matches,'v:val !~ "/$" && isdirectory(v:val) ? v:val."/" : v:val')
|
|
|
|
call map(matches,'v:val[ strlen(self.tree())+(a:base !~ "^/") : -1 ]')
|
|
|
|
let results += matches
|
|
|
|
endif
|
|
|
|
return results
|
|
|
|
|
|
|
|
elseif a:base =~# '^:'
|
|
|
|
let entries = split(self.git_chomp('ls-files','--stage'),"\n")
|
|
|
|
call map(entries,'s:sub(v:val,".*(\\d)\\t(.*)",":\\1:\\2")')
|
|
|
|
if a:base !~# '^:[0-3]\%(:\|$\)'
|
|
|
|
call filter(entries,'v:val[1] == "0"')
|
|
|
|
call map(entries,'v:val[2:-1]')
|
|
|
|
endif
|
|
|
|
call filter(entries,'v:val[ 0 : strlen(a:base)-1 ] ==# a:base')
|
|
|
|
return entries
|
|
|
|
|
|
|
|
else
|
|
|
|
let tree = matchstr(a:base,'.*[:/]')
|
|
|
|
let entries = split(self.git_chomp('ls-tree',tree),"\n")
|
|
|
|
call map(entries,'s:sub(v:val,"^04.*\\zs$","/")')
|
|
|
|
call map(entries,'tree.s:sub(v:val,".*\t","")')
|
|
|
|
return filter(entries,'v:val[ 0 : strlen(a:base)-1 ] ==# a:base')
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:add_methods('repo',['dirglob','superglob'])
|
|
|
|
|
|
|
|
function! s:repo_keywordprg() dict abort
|
|
|
|
let args = ' --git-dir='.escape(self.dir(),"\\\"' ").' show'
|
2010-03-08 22:57:18 -05:00
|
|
|
if has('gui_running') && !has('win32')
|
2009-10-10 19:47:14 -04:00
|
|
|
return g:fugitive_git_executable . ' --no-pager' . args
|
|
|
|
else
|
|
|
|
return g:fugitive_git_executable . args
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:add_methods('repo',['keywordprg'])
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Buffer {{{1
|
|
|
|
|
|
|
|
let s:buffer_prototype = {}
|
|
|
|
|
|
|
|
function! s:buffer(...) abort
|
|
|
|
let buffer = {'#': bufnr(a:0 ? a:1 : '%')}
|
|
|
|
call extend(extend(buffer,s:buffer_prototype,'keep'),s:abstract_prototype,'keep')
|
2009-11-10 21:17:21 -05:00
|
|
|
if buffer.getvar('git_dir') !=# ''
|
|
|
|
return buffer
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
2009-11-10 21:17:21 -05:00
|
|
|
call s:throw('not a git repository: '.expand('%:p'))
|
2009-10-10 19:47:14 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! fugitive#buffer(...) abort
|
|
|
|
return s:buffer(a:0 ? a:1 : '%')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_getvar(var) dict abort
|
|
|
|
return getbufvar(self['#'],a:var)
|
|
|
|
endfunction
|
|
|
|
|
2009-12-13 19:49:35 -05:00
|
|
|
function! s:buffer_setvar(var,value) dict abort
|
|
|
|
return setbufvar(self['#'],a:var,a:value)
|
|
|
|
endfunction
|
|
|
|
|
2009-10-10 19:47:14 -04:00
|
|
|
function! s:buffer_getline(lnum) dict abort
|
|
|
|
return getbufline(self['#'],a:lnum)[0]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_repo() dict abort
|
|
|
|
return s:repo(self.getvar('git_dir'))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_type(...) dict abort
|
|
|
|
if self.getvar('fugitive_type') != ''
|
|
|
|
let type = self.getvar('fugitive_type')
|
|
|
|
elseif fnamemodify(self.name(),':p') =~# '.\git/refs/\|\.git/\w*HEAD$'
|
|
|
|
let type = 'head'
|
|
|
|
elseif self.getline(1) =~ '^tree \x\{40\}$' && self.getline(2) == ''
|
|
|
|
let type = 'tree'
|
|
|
|
elseif self.getline(1) =~ '^\d\{6\} \w\{4\} \x\{40\}\>\t'
|
|
|
|
let type = 'tree'
|
|
|
|
elseif self.getline(1) =~ '^\d\{6\} \x\{40\}\> \d\t'
|
|
|
|
let type = 'index'
|
|
|
|
elseif isdirectory(self.name())
|
|
|
|
let type = 'directory'
|
2010-02-07 21:54:07 -05:00
|
|
|
elseif self.name() == ''
|
|
|
|
let type = 'null'
|
2009-10-10 19:47:14 -04:00
|
|
|
elseif filereadable(self.name())
|
|
|
|
let type = 'file'
|
|
|
|
else
|
|
|
|
let type = ''
|
|
|
|
endif
|
|
|
|
if a:0
|
|
|
|
return !empty(filter(copy(a:000),'v:val ==# type'))
|
|
|
|
else
|
|
|
|
return type
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_name() dict abort
|
2010-02-07 21:54:07 -05:00
|
|
|
let bufname = bufname(self['#'])
|
2010-03-09 19:03:44 -05:00
|
|
|
return s:shellslash(bufname == '' ? '' : fnamemodify(bufname,':p'))
|
2009-10-10 19:47:14 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_commit() dict abort
|
|
|
|
return matchstr(self.name(),'^fugitive://.\{-\}//\zs\w*')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_path(...) dict abort
|
|
|
|
let rev = matchstr(self.name(),'^fugitive://.\{-\}//\zs.*')
|
|
|
|
if rev != ''
|
|
|
|
let rev = s:sub(rev,'\w*','')
|
|
|
|
else
|
|
|
|
let rev = self.name()[strlen(self.repo().tree()) : -1]
|
|
|
|
endif
|
|
|
|
return s:sub(rev,'^/',a:0 ? a:1 : '')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_rev() dict abort
|
|
|
|
let rev = matchstr(self.name(),'^fugitive://.\{-\}//\zs.*')
|
|
|
|
if rev =~ '^\x/'
|
|
|
|
return ':'.rev[0].':'.rev[2:-1]
|
|
|
|
elseif rev =~ '.'
|
|
|
|
return s:sub(rev,'/',':')
|
|
|
|
elseif self.name() =~ '\.git/index$'
|
|
|
|
return ':'
|
|
|
|
elseif self.name() =~ '\.git/refs/\|\.git/.*HEAD$'
|
|
|
|
return self.name()[strlen(self.repo().dir())+1 : -1]
|
|
|
|
else
|
|
|
|
return self.path()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_sha1() dict abort
|
|
|
|
if self.name() =~ '^fugitive://' || self.name() =~ '\.git/refs/\|\.git/.*HEAD$'
|
|
|
|
return self.repo().rev_parse(self.rev())
|
|
|
|
else
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_expand(rev) dict abort
|
|
|
|
if a:rev =~# '^:[0-3]$'
|
|
|
|
let file = a:rev.self.path(':')
|
|
|
|
elseif a:rev =~# '^-'
|
|
|
|
let file = 'HEAD^{}'.a:rev[1:-1].self.path(':')
|
|
|
|
elseif a:rev =~# '^@{'
|
|
|
|
let file = 'HEAD'.a:rev.self.path(':')
|
|
|
|
elseif a:rev =~# '^[~^]'
|
|
|
|
let commit = s:sub(self.commit(),'^\d=$','HEAD')
|
|
|
|
let file = commit.a:rev.self.path(':')
|
|
|
|
else
|
|
|
|
let file = a:rev
|
|
|
|
endif
|
|
|
|
return s:sub(file,'\%$',self.path())
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:buffer_containing_commit() dict abort
|
2009-11-11 00:16:02 -05:00
|
|
|
if self.commit() =~# '^\d$'
|
2009-10-10 19:47:14 -04:00
|
|
|
return ':'
|
2009-11-11 00:16:02 -05:00
|
|
|
elseif self.commit() =~# '.'
|
|
|
|
return self.commit()
|
2009-10-10 19:47:14 -04:00
|
|
|
else
|
|
|
|
return 'HEAD'
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2009-12-13 19:49:35 -05:00
|
|
|
call s:add_methods('buffer',['getvar','setvar','getline','repo','type','name','commit','path','rev','sha1','expand','containing_commit'])
|
2009-10-10 19:47:14 -04:00
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Git {{{1
|
|
|
|
|
2010-03-19 19:59:08 -04:00
|
|
|
call s:command("-bang -nargs=? -complete=customlist,s:GitComplete Git :execute s:Git(<bang>0,<q-args>)")
|
2009-10-10 19:47:14 -04:00
|
|
|
|
|
|
|
function! s:ExecuteInTree(cmd) abort
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
|
|
|
try
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=s:repo().tree()`'
|
2009-10-10 19:47:14 -04:00
|
|
|
execute a:cmd
|
|
|
|
finally
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=dir`'
|
2009-10-10 19:47:14 -04:00
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:Git(bang,cmd) abort
|
|
|
|
let git = s:repo().git_command()
|
2010-03-08 22:57:18 -05:00
|
|
|
if has('gui_running') && !has('win32')
|
2009-10-10 19:47:14 -04:00
|
|
|
let git .= ' --no-pager'
|
|
|
|
endif
|
2010-03-19 19:59:08 -04:00
|
|
|
let cmd = matchstr(a:cmd,'\v\C.{-}%($|\\@<!%(\\\\)*\|)@=')
|
|
|
|
call s:ExecuteInTree('!'.git.' '.cmd)
|
2009-11-03 22:23:58 -05:00
|
|
|
call fugitive#reload_status()
|
2010-03-19 19:59:08 -04:00
|
|
|
return matchstr(a:cmd,'\v\C\\@<!%(\\\\)*\|\zs.*')
|
2009-10-10 19:47:14 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:GitComplete(A,L,P) abort
|
|
|
|
if !exists('s:exec_path')
|
|
|
|
let s:exec_path = s:sub(system(g:fugitive_git_executable.' --exec-path'),'\n$','')
|
|
|
|
endif
|
2010-03-10 19:37:49 -05:00
|
|
|
let cmds = map(split(glob(s:exec_path.'/git-*'),"\n"),'s:sub(v:val[strlen(s:exec_path)+5 : -1],"\\.exe$","")')
|
2009-10-10 19:47:14 -04:00
|
|
|
if a:L =~ ' [[:alnum:]-]\+ '
|
|
|
|
return s:repo().superglob(a:A)
|
|
|
|
elseif a:A == ''
|
|
|
|
return cmds
|
|
|
|
else
|
|
|
|
return filter(cmds,'v:val[0 : strlen(a:A)-1] ==# a:A')
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Gcd, Glcd {{{1
|
|
|
|
|
|
|
|
function! s:DirComplete(A,L,P) abort
|
|
|
|
let matches = s:repo().dirglob(a:A)
|
|
|
|
return matches
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:DirComplete Gcd :cd<bang> `=s:repo().bare() ? s:repo().dir(<q-args>) : s:repo().tree(<q-args>)`")
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:DirComplete Glcd :lcd<bang> `=s:repo().bare() ? s:repo().dir(<q-args>) : s:repo().tree(<q-args>)`")
|
|
|
|
|
2009-10-19 22:11:40 -04:00
|
|
|
" }}}1
|
|
|
|
" Gstatus {{{1
|
|
|
|
|
|
|
|
call s:command("-bar Gstatus :execute s:Status()")
|
|
|
|
|
2010-01-09 17:01:44 -05:00
|
|
|
function! s:Status() abort
|
2009-10-19 22:11:40 -04:00
|
|
|
try
|
|
|
|
Gpedit :
|
|
|
|
wincmd P
|
|
|
|
nnoremap <buffer> <silent> q :<C-U>bdelete<CR>
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
2010-01-09 17:01:44 -05:00
|
|
|
function! fugitive#reload_status() abort
|
2009-10-19 22:11:40 -04:00
|
|
|
let mytab = tabpagenr()
|
|
|
|
for tab in [mytab] + range(1,tabpagenr('$'))
|
|
|
|
for winnr in range(1,tabpagewinnr(tab,'$'))
|
|
|
|
if getbufvar(tabpagebuflist(tab)[winnr-1],'fugitive_type') ==# 'index'
|
|
|
|
execute 'tabnext '.tab
|
|
|
|
if winnr != winnr()
|
|
|
|
execute winnr.'wincmd w'
|
|
|
|
let restorewinnr = 1
|
|
|
|
endif
|
|
|
|
try
|
|
|
|
if !&modified
|
|
|
|
call s:BufReadIndex()
|
|
|
|
endif
|
|
|
|
finally
|
|
|
|
if exists('restorewinnr')
|
|
|
|
wincmd p
|
|
|
|
endif
|
|
|
|
execute 'tabnext '.mytab
|
|
|
|
endtry
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
2010-03-19 21:23:41 -04:00
|
|
|
function! s:StageDiff() abort
|
2010-03-19 21:48:41 -04:00
|
|
|
let section = getline(search('^# .*:$','bnW'))
|
2010-03-19 21:23:41 -04:00
|
|
|
let line = getline('.')
|
|
|
|
let filename = matchstr(line,'^#\t\%([[:alpha:] ]\+: *\)\=\zs.*')
|
2010-03-19 21:48:41 -04:00
|
|
|
if filename ==# '' && section == '# Changes to be committed:'
|
|
|
|
return 'Git diff --cached'
|
|
|
|
elseif filename ==# ''
|
|
|
|
return 'Git diff'
|
|
|
|
elseif line =~# '^#\trenamed:' && filename =~ ' -> '
|
2010-03-19 21:23:41 -04:00
|
|
|
let [old, new] = split(filename,' -> ')
|
|
|
|
execute 'Gedit '.s:fnameescape(':0:'.new)
|
|
|
|
return 'Gdiff HEAD:'.s:fnameescape(old)
|
|
|
|
elseif section == '# Changes to be committed:'
|
|
|
|
execute 'Gedit '.s:fnameescape(':0:'.filename)
|
|
|
|
return 'Gdiff -'
|
|
|
|
else
|
|
|
|
execute 'Gedit '.s:fnameescape('/'.filename)
|
|
|
|
return 'Gdiff'
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2010-01-09 17:01:44 -05:00
|
|
|
function! s:StageToggle(lnum1,lnum2) abort
|
2009-10-19 22:11:40 -04:00
|
|
|
try
|
2009-10-22 21:54:55 -04:00
|
|
|
let output = ''
|
|
|
|
for lnum in range(a:lnum1,a:lnum2)
|
|
|
|
let line = getline(lnum)
|
2010-01-09 23:31:22 -05:00
|
|
|
if getline('.') == '# Changes to be committed:'
|
|
|
|
return 'Gcommit'
|
|
|
|
endif
|
2010-02-14 13:03:26 -05:00
|
|
|
let filename = matchstr(line,'^#\t\%([[:alpha:] ]\+: *\)\=\zs.*')
|
2009-10-22 21:54:55 -04:00
|
|
|
if filename ==# ''
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
if !exists('first_filename')
|
|
|
|
let first_filename = filename
|
|
|
|
endif
|
|
|
|
execute lnum
|
|
|
|
let section = getline(search('^# .*:$','bnW'))
|
|
|
|
if line =~# '^#\trenamed:' && filename =~ ' -> '
|
|
|
|
let cmd = ['mv','--'] + reverse(split(filename,' -> '))
|
|
|
|
let filename = cmd[-1]
|
|
|
|
elseif section =~? ' to be '
|
|
|
|
let cmd = ['reset','-q','--',filename]
|
|
|
|
elseif line =~# '^#\tdeleted:'
|
|
|
|
let cmd = ['rm','--',filename]
|
|
|
|
else
|
|
|
|
let cmd = ['add','--',filename]
|
|
|
|
endif
|
|
|
|
let output .= call(s:repo().git_chomp_in_tree,cmd,s:repo())."\n"
|
|
|
|
endfor
|
|
|
|
if exists('first_filename')
|
2010-02-14 13:03:26 -05:00
|
|
|
let jump = first_filename
|
|
|
|
let f = matchstr(getline(a:lnum1-1),'^#\t\%([[:alpha:] ]\+: *\)\=\zs.*')
|
|
|
|
if f !=# '' | let jump = f | endif
|
|
|
|
let f = matchstr(getline(a:lnum2+1),'^#\t\%([[:alpha:] ]\+: *\)\=\zs.*')
|
|
|
|
if f !=# '' | let jump = f | endif
|
2009-10-22 21:54:55 -04:00
|
|
|
silent! edit!
|
|
|
|
1
|
|
|
|
redraw
|
2010-02-14 13:03:26 -05:00
|
|
|
call search('^#\t\%([[:alpha:] ]\+: *\)\=\V'.jump.'\$','W')
|
2009-10-19 22:11:40 -04:00
|
|
|
endif
|
2009-10-22 21:54:55 -04:00
|
|
|
echo s:sub(s:gsub(output,'\n+','\n'),'\n$','')
|
2009-10-19 22:11:40 -04:00
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
return 'checktime'
|
|
|
|
endfunction
|
|
|
|
|
2010-01-11 22:51:54 -05:00
|
|
|
function! s:StagePatch(lnum1,lnum2) abort
|
|
|
|
let add = []
|
|
|
|
let reset = []
|
|
|
|
|
|
|
|
for lnum in range(a:lnum1,a:lnum2)
|
|
|
|
let line = getline(lnum)
|
|
|
|
if line == '# Changes to be committed:'
|
|
|
|
return 'Git reset --patch'
|
|
|
|
elseif line == '# Changed but not updated:'
|
|
|
|
return 'Git add --patch'
|
|
|
|
endif
|
2010-02-14 13:03:26 -05:00
|
|
|
let filename = matchstr(line,'^#\t\%([[:alpha:] ]\+: *\)\=\zs.*')
|
2010-01-11 22:51:54 -05:00
|
|
|
if filename ==# ''
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
if !exists('first_filename')
|
|
|
|
let first_filename = filename
|
|
|
|
endif
|
|
|
|
execute lnum
|
|
|
|
let section = getline(search('^# .*:$','bnW'))
|
|
|
|
if line =~# '^#\trenamed:' && filename =~ ' -> '
|
|
|
|
let reset += [split(filename,' -> ')[1]]
|
|
|
|
elseif section =~? ' to be '
|
|
|
|
let reset += [filename]
|
|
|
|
elseif line !~# '^#\tdeleted:'
|
|
|
|
let add += [filename]
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
try
|
|
|
|
if !empty(add)
|
|
|
|
execute "Git add --patch -- ".join(map(add,'s:shellesc(v:val)'))
|
|
|
|
endif
|
|
|
|
if !empty(reset)
|
|
|
|
execute "Git reset --patch -- ".join(map(add,'s:shellesc(v:val)'))
|
|
|
|
endif
|
|
|
|
if exists('first_filename')
|
|
|
|
silent! edit!
|
|
|
|
1
|
|
|
|
redraw
|
|
|
|
call search('^#\t\%([[:alpha:] ]\+: *\)\=\V'.first_filename.'\$','W')
|
|
|
|
endif
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
return 'checktime'
|
|
|
|
endfunction
|
|
|
|
|
2010-01-09 23:31:22 -05:00
|
|
|
" }}}1
|
|
|
|
" Gcommit {{{1
|
|
|
|
|
|
|
|
call s:command("-nargs=? -complete=customlist,s:CommitComplete Gcommit :execute s:Commit(<q-args>)")
|
|
|
|
|
|
|
|
function! s:Commit(args) abort
|
|
|
|
let old_type = s:buffer().type()
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
2010-01-13 17:50:18 -05:00
|
|
|
let dir = getcwd()
|
2010-01-09 23:31:22 -05:00
|
|
|
let msgfile = s:repo().dir('COMMIT_EDITMSG')
|
|
|
|
let outfile = tempname()
|
|
|
|
let errorfile = tempname()
|
|
|
|
try
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=s:repo().tree()`'
|
2010-01-13 22:01:43 -05:00
|
|
|
let command = ''
|
|
|
|
if &shell =~# 'cmd'
|
|
|
|
let old_editor = $GIT_EDITOR
|
|
|
|
let $GIT_EDITOR = 'false'
|
|
|
|
elseif &shell !~# 'csh'
|
|
|
|
let command = 'GIT_EDITOR=false '
|
|
|
|
endif
|
|
|
|
let command .= s:repo().git_command('commit').' '.a:args
|
|
|
|
if &shell =~# 'csh'
|
|
|
|
silent execute '!setenv GIT_EDITOR false; ('.command.' > '.outfile.') >& '.errorfile
|
|
|
|
elseif a:args =~# '\%(^\| \)--interactive\>'
|
2010-01-09 23:31:22 -05:00
|
|
|
execute '!'.command.' 2> '.errorfile
|
|
|
|
else
|
|
|
|
silent execute '!'.command.' > '.outfile.' 2> '.errorfile
|
2010-01-13 22:01:43 -05:00
|
|
|
endif
|
2010-01-09 23:31:22 -05:00
|
|
|
if !v:shell_error
|
|
|
|
if filereadable(outfile)
|
|
|
|
for line in readfile(outfile)
|
|
|
|
echo line
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
else
|
|
|
|
let error = get(readfile(errorfile,'',1),0,'!')
|
|
|
|
if error =~# "'false'\\.$"
|
|
|
|
let args = a:args
|
2010-03-10 19:44:38 -05:00
|
|
|
let args = s:gsub(args,'%(%(^| )-- )@<!%(^| )@<=%(-[se]|--edit|--interactive)%($| )','')
|
2010-01-09 23:31:22 -05:00
|
|
|
let args = s:gsub(args,'%(%(^| )-- )@<!%(^| )@<=%(-F|--file|-m|--message)%(\s+|\=)%(''[^'']*''|"%(\\.|[^"])*"|\\.|\S)*','')
|
|
|
|
let args = s:gsub(args,'%(^| )@<=[%#]%(:\w)*','\=expand(submatch(0))')
|
|
|
|
let args = '-F '.s:shellesc(msgfile).' '.args
|
|
|
|
if args !~# '\%(^\| \)--cleanup\>'
|
|
|
|
let args = '--cleanup=strip '.args
|
|
|
|
endif
|
2010-02-07 21:54:07 -05:00
|
|
|
if bufname('%') == '' && line('$') == 1 && getline(1) == '' && !&mod
|
|
|
|
edit `=msgfile`
|
|
|
|
else
|
|
|
|
split `=msgfile`
|
|
|
|
endif
|
2010-01-09 23:31:22 -05:00
|
|
|
if old_type ==# 'index'
|
|
|
|
bdelete #
|
|
|
|
endif
|
|
|
|
let b:fugitive_commit_arguments = args
|
|
|
|
setlocal bufhidden=delete filetype=gitcommit
|
|
|
|
return '1'
|
|
|
|
elseif error ==# '!'
|
|
|
|
return s:Status()
|
|
|
|
else
|
|
|
|
call s:throw(error)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
finally
|
2010-01-13 22:01:43 -05:00
|
|
|
if exists('old_editor')
|
|
|
|
let $GIT_EDITOR = old_editor
|
|
|
|
endif
|
2010-01-09 23:31:22 -05:00
|
|
|
call delete(outfile)
|
|
|
|
call delete(errorfile)
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=dir`'
|
2010-01-09 23:31:22 -05:00
|
|
|
call fugitive#reload_status()
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:CommitComplete(A,L,P) abort
|
|
|
|
if a:A =~ '^-' || type(a:A) == type(0) " a:A is 0 on :Gcommit -<Tab>
|
|
|
|
let args = ['-C', '-F', '-a', '-c', '-e', '-i', '-m', '-n', '-o', '-q', '-s', '-t', '-u', '-v', '--all', '--allow-empty', '--amend', '--author=', '--cleanup=', '--dry-run', '--edit', '--file=', '--include', '--interactive', '--message=', '--no-verify', '--only', '--quiet', '--reedit-message=', '--reuse-message=', '--signoff', '--template=', '--untracked-files', '--verbose']
|
|
|
|
return filter(args,'v:val[0 : strlen(a:A)-1] ==# a:A')
|
|
|
|
else
|
|
|
|
return s:repo().superglob(a:A)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:FinishCommit()
|
|
|
|
let args = getbufvar(+expand('<abuf>'),'fugitive_commit_arguments')
|
|
|
|
let g:args = args
|
|
|
|
if !empty(args)
|
|
|
|
call setbufvar(+expand('<abuf>'),'fugitive_commit_arguments','')
|
|
|
|
return s:Commit(args)
|
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup fugitive_commit
|
|
|
|
autocmd!
|
2010-03-11 22:14:38 -05:00
|
|
|
autocmd VimLeavePre,BufDelete *.git/COMMIT_EDITMSG execute s:sub(s:FinishCommit(), '^echoerr (.*)', 'echohl ErrorMsg|echo \1|echohl NONE')
|
2010-01-09 23:31:22 -05:00
|
|
|
augroup END
|
|
|
|
|
2009-10-10 19:47:14 -04:00
|
|
|
" }}}1
|
|
|
|
" Ggrep, Glog {{{1
|
|
|
|
|
2009-12-14 00:00:03 -05:00
|
|
|
if !exists('g:fugitive_summary_format')
|
|
|
|
let g:fugitive_summary_format = '%s'
|
|
|
|
endif
|
|
|
|
|
2010-03-19 19:59:08 -04:00
|
|
|
call s:command("-bang -nargs=? -complete=customlist,s:EditComplete Ggrep :execute s:Grep(<bang>0,<q-args>)")
|
2009-11-14 23:47:17 -05:00
|
|
|
call s:command("-bar -bang -nargs=* -complete=customlist,s:EditComplete Glog :execute s:Log('grep<bang>',<f-args>)")
|
2009-10-10 19:47:14 -04:00
|
|
|
|
|
|
|
function! s:Grep(bang,arg) abort
|
|
|
|
let grepprg = &grepprg
|
|
|
|
let grepformat = &grepformat
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
2010-01-13 17:50:18 -05:00
|
|
|
let dir = getcwd()
|
2009-10-10 19:47:14 -04:00
|
|
|
try
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=s:repo().tree()`'
|
2009-10-10 19:47:14 -04:00
|
|
|
let &grepprg = s:repo().git_command('--no-pager', 'grep', '-n')
|
|
|
|
let &grepformat = '%f:%l:%m'
|
2010-03-19 19:59:08 -04:00
|
|
|
exe 'grep! '.escape(matchstr(a:arg,'\v\C.{-}%($|[''" ]\@=\|)@='),'|')
|
2009-10-10 19:47:14 -04:00
|
|
|
let list = getqflist()
|
|
|
|
for entry in list
|
|
|
|
if bufname(entry.bufnr) =~ ':'
|
|
|
|
let entry.filename = s:repo().translate(bufname(entry.bufnr))
|
|
|
|
unlet! entry.bufnr
|
|
|
|
elseif a:arg =~# '\%(^\| \)--cached\>'
|
|
|
|
let entry.filename = s:repo().translate(':0:'.bufname(entry.bufnr))
|
|
|
|
unlet! entry.bufnr
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
call setqflist(list,'r')
|
|
|
|
if !a:bang && !empty(list)
|
2010-03-19 19:59:08 -04:00
|
|
|
return 'cfirst'.matchstr(a:arg,'\v\C[''" ]\zs\|.*')
|
2009-10-14 18:57:11 -04:00
|
|
|
else
|
2010-03-19 19:59:08 -04:00
|
|
|
return matchstr(a:arg,'\v\C[''" ]\|\zs.*')
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
finally
|
|
|
|
let &grepprg = grepprg
|
|
|
|
let &grepformat = grepformat
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=dir`'
|
2009-10-10 19:47:14 -04:00
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
2009-11-14 23:47:17 -05:00
|
|
|
function! s:Log(cmd,...)
|
2009-11-10 22:26:08 -05:00
|
|
|
let path = s:buffer().path('/')
|
2009-11-14 23:47:17 -05:00
|
|
|
if path =~# '^/\.git\%(/\|$\)' || index(a:000,'--') != -1
|
2009-11-10 22:26:08 -05:00
|
|
|
let path = ''
|
|
|
|
endif
|
2009-11-14 23:47:17 -05:00
|
|
|
let cmd = ['--no-pager', 'log', '--no-color']
|
2009-12-14 00:00:03 -05:00
|
|
|
let cmd += [escape('--pretty=format:fugitive://'.s:repo().dir().'//%H'.path.'::'.g:fugitive_summary_format,'%')]
|
2009-11-14 23:47:17 -05:00
|
|
|
if empty(filter(a:000[0 : index(a:000,'--')],'v:val !~# "^-"'))
|
|
|
|
if s:buffer().commit() =~# '\x\{40\}'
|
|
|
|
let cmd += [s:buffer().commit()]
|
|
|
|
elseif s:buffer().path() =~# '^\.git/refs/\|^\.git/.*HEAD$'
|
|
|
|
let cmd += [s:buffer().path()[5:-1]]
|
|
|
|
endif
|
|
|
|
end
|
|
|
|
let cmd += map(copy(a:000),'s:sub(v:val,"^\\%(%(:\\w)*)","\\=fnamemodify(s:buffer().path(),submatch(1))")')
|
2009-11-10 22:26:08 -05:00
|
|
|
if path =~# '/.'
|
2009-11-14 23:47:17 -05:00
|
|
|
let cmd += ['--',path[1:-1]]
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
let grepformat = &grepformat
|
|
|
|
let grepprg = &grepprg
|
2009-11-14 23:47:17 -05:00
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
2010-01-13 17:50:18 -05:00
|
|
|
let dir = getcwd()
|
2009-10-10 19:47:14 -04:00
|
|
|
try
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=s:repo().tree()`'
|
2009-11-14 23:47:17 -05:00
|
|
|
let &grepprg = call(s:repo().git_command,cmd,s:repo())
|
2009-10-10 19:47:14 -04:00
|
|
|
let &grepformat = '%f::%m'
|
|
|
|
exe a:cmd
|
|
|
|
finally
|
|
|
|
let &grepformat = grepformat
|
|
|
|
let &grepprg = grepprg
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=dir`'
|
2009-10-10 19:47:14 -04:00
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Gedit, Gpedit, Gsplit, Gvsplit, Gtabedit, Gread {{{1
|
|
|
|
|
|
|
|
function! s:Edit(cmd,...) abort
|
|
|
|
if a:0 && a:1 == ''
|
|
|
|
return ''
|
|
|
|
elseif a:0
|
|
|
|
let file = s:buffer().expand(a:1)
|
2009-10-19 22:11:40 -04:00
|
|
|
elseif s:buffer().commit() ==# '' && s:buffer().path('/') !~# '^/.git\>'
|
2009-10-10 19:47:14 -04:00
|
|
|
let file = s:buffer().path(':')
|
|
|
|
else
|
|
|
|
let file = s:buffer().path('/')
|
|
|
|
endif
|
2009-10-15 18:47:03 -04:00
|
|
|
try
|
|
|
|
let file = s:repo().translate(file)
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
2010-01-11 15:21:43 -05:00
|
|
|
if a:cmd =~# 'read!$' || a:cmd ==# 'read'
|
2010-02-07 21:53:23 -05:00
|
|
|
if a:cmd =~# '!$'
|
|
|
|
call s:warn(':Gread! is deprecated. Use :Gread')
|
|
|
|
endif
|
|
|
|
return 'silent %delete|read '.s:fnameescape(file).'|silent 1delete_|diffupdate|'.line('.')
|
2009-10-10 19:47:14 -04:00
|
|
|
else
|
2009-10-19 22:11:40 -04:00
|
|
|
if &previewwindow && getbufvar('','fugitive_type') ==# 'index'
|
|
|
|
wincmd p
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
return a:cmd.' '.s:fnameescape(file)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:EditComplete(A,L,P) abort
|
|
|
|
return s:repo().superglob(a:A)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Ge :execute s:Edit('edit<bang>',<f-args>)")
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Gedit :execute s:Edit('edit<bang>',<f-args>)")
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Gpedit :execute s:Edit('pedit<bang>',<f-args>)")
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Gsplit :execute s:Edit('split<bang>',<f-args>)")
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Gvsplit :execute s:Edit('vsplit<bang>',<f-args>)")
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Gtabedit :execute s:Edit('tabedit<bang>',<f-args>)")
|
2010-01-09 13:41:51 -05:00
|
|
|
call s:command("-bar -bang -nargs=? -count -complete=customlist,s:EditComplete Gread :execute s:Edit((!<count> && <line1> ? '' : <count>).'read<bang>',<f-args>)")
|
2009-10-10 19:47:14 -04:00
|
|
|
|
2009-10-14 23:15:03 -04:00
|
|
|
" }}}1
|
|
|
|
" Gwrite {{{1
|
|
|
|
|
|
|
|
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Gwrite :execute s:Write(<bang>0,<f-args>)")
|
|
|
|
|
|
|
|
function! s:Write(force,...) abort
|
2010-01-09 23:31:22 -05:00
|
|
|
if exists('b:fugitive_commit_arguments')
|
|
|
|
return 'write|bdelete'
|
|
|
|
elseif expand('%:t') == 'COMMIT_EDITMSG' && $GIT_INDEX_FILE != ''
|
|
|
|
return 'wq'
|
2010-01-10 00:04:58 -05:00
|
|
|
elseif s:buffer().type() == 'index'
|
|
|
|
return 'Gcommit'
|
2010-01-09 23:31:22 -05:00
|
|
|
endif
|
2009-10-14 23:15:03 -04:00
|
|
|
let mytab = tabpagenr()
|
|
|
|
let mybufnr = bufnr('')
|
|
|
|
let path = a:0 ? a:1 : s:buffer().path()
|
|
|
|
if path =~# '^:\d\>'
|
|
|
|
return 'write'.(a:force ? '! ' : ' ').s:fnameescape(s:repo().translate(s:buffer().expand(path)))
|
|
|
|
endif
|
|
|
|
let always_permitted = (s:buffer().path() ==# path && s:buffer().commit() =~# '^0\=$')
|
|
|
|
if !always_permitted && !a:force && s:repo().git_chomp_in_tree('diff','--name-status','HEAD','--',path) . s:repo().git_chomp_in_tree('ls-files','--others','--',path) !=# ''
|
|
|
|
let v:errmsg = 'fugitive: file has uncommitted changes (use ! to override)'
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endif
|
|
|
|
let file = s:repo().translate(path)
|
|
|
|
let treebufnr = 0
|
|
|
|
for nr in range(1,bufnr('$'))
|
2010-01-09 17:35:27 -05:00
|
|
|
if fnamemodify(bufname(nr),':p') ==# file
|
2009-10-14 23:15:03 -04:00
|
|
|
let treebufnr = nr
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
if treebufnr > 0 && treebufnr != bufnr('')
|
|
|
|
let temp = tempname()
|
|
|
|
silent execute '%write '.temp
|
|
|
|
for tab in [mytab] + range(1,tabpagenr('$'))
|
|
|
|
for winnr in range(1,tabpagewinnr(tab,'$'))
|
|
|
|
if tabpagebuflist(tab)[winnr-1] == treebufnr
|
|
|
|
execute 'tabnext '.tab
|
2009-10-19 21:42:48 -04:00
|
|
|
if winnr != winnr()
|
|
|
|
execute winnr.'wincmd w'
|
|
|
|
let restorewinnr = 1
|
|
|
|
endif
|
2009-10-14 23:15:03 -04:00
|
|
|
try
|
|
|
|
let lnum = line('.')
|
|
|
|
let last = line('$')
|
|
|
|
silent execute '$read '.temp
|
|
|
|
silent execute '1,'.last.'delete_'
|
|
|
|
silent write!
|
|
|
|
silent execute lnum
|
|
|
|
let did = 1
|
|
|
|
finally
|
2009-10-19 21:42:48 -04:00
|
|
|
if exists('restorewinnr')
|
|
|
|
wincmd p
|
|
|
|
endif
|
2009-10-18 19:54:30 -04:00
|
|
|
execute 'tabnext '.mytab
|
2009-10-14 23:15:03 -04:00
|
|
|
endtry
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
if !exists('did')
|
|
|
|
call writefile(readfile(temp,'b'),file,'b')
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
execute 'write! '.s:fnameescape(s:repo().translate(path))
|
|
|
|
endif
|
|
|
|
|
|
|
|
let error = s:repo().git_chomp_in_tree('add', file)
|
|
|
|
if v:shell_error
|
|
|
|
let v:errmsg = 'fugitive: '.error
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endif
|
2010-01-09 17:35:27 -05:00
|
|
|
if s:buffer().path() ==# path && s:buffer().commit() =~# '^\d$'
|
2009-10-14 23:15:03 -04:00
|
|
|
set nomodified
|
|
|
|
endif
|
|
|
|
|
|
|
|
let one = s:repo().translate(':1:'.path)
|
|
|
|
let two = s:repo().translate(':2:'.path)
|
|
|
|
let three = s:repo().translate(':3:'.path)
|
|
|
|
for nr in range(1,bufnr('$'))
|
2009-10-23 14:02:01 -04:00
|
|
|
if bufloaded(nr) && !getbufvar(nr,'&modified') && (bufname(nr) == one || bufname(nr) == two || bufname(nr) == three)
|
2009-10-14 23:15:03 -04:00
|
|
|
execute nr.'bdelete'
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2009-10-19 21:42:48 -04:00
|
|
|
unlet! restorewinnr
|
2009-10-14 23:15:03 -04:00
|
|
|
let zero = s:repo().translate(':0:'.path)
|
|
|
|
for tab in range(1,tabpagenr('$'))
|
|
|
|
for winnr in range(1,tabpagewinnr(tab,'$'))
|
|
|
|
let bufnr = tabpagebuflist(tab)[winnr-1]
|
|
|
|
let bufname = bufname(bufnr)
|
|
|
|
if bufname ==# zero && bufnr != mybufnr
|
|
|
|
execute 'tabnext '.tab
|
2009-10-19 21:42:48 -04:00
|
|
|
if winnr != winnr()
|
|
|
|
execute winnr.'wincmd w'
|
|
|
|
let restorewinnr = 1
|
|
|
|
endif
|
2009-10-14 23:15:03 -04:00
|
|
|
try
|
|
|
|
let lnum = line('.')
|
|
|
|
let last = line('$')
|
2009-10-15 12:52:19 -04:00
|
|
|
silent $read `=file`
|
2009-10-14 23:15:03 -04:00
|
|
|
silent execute '1,'.last.'delete_'
|
|
|
|
silent execute lnum
|
|
|
|
set nomodified
|
|
|
|
diffupdate
|
|
|
|
finally
|
2009-10-19 21:42:48 -04:00
|
|
|
if exists('restorewinnr')
|
|
|
|
wincmd p
|
|
|
|
endif
|
2009-10-14 23:15:03 -04:00
|
|
|
execute 'tabnext '.mytab
|
|
|
|
endtry
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
2009-11-03 22:23:58 -05:00
|
|
|
call fugitive#reload_status()
|
2009-10-14 23:15:03 -04:00
|
|
|
return 'checktime'
|
|
|
|
endfunction
|
|
|
|
|
2009-10-10 19:47:14 -04:00
|
|
|
" }}}1
|
|
|
|
" Gdiff {{{1
|
|
|
|
|
2010-04-14 10:48:42 -04:00
|
|
|
call s:command("-bang -bar -nargs=? -complete=customlist,s:EditComplete Gdiff :execute s:Diff(<bang>0,<f-args>)")
|
2009-10-10 19:47:14 -04:00
|
|
|
|
2009-10-14 23:26:10 -04:00
|
|
|
augroup fugitive_diff
|
2010-05-25 15:32:35 -04:00
|
|
|
autocmd!
|
2010-06-02 23:43:33 -04:00
|
|
|
autocmd BufWinLeave * if s:diff_window_count() == 2 && &diff && getbufvar(+expand('<abuf>'), 'git_dir') !=# '' | execute 'windo call s:diff_off()' | endif
|
2010-05-25 16:48:34 -04:00
|
|
|
autocmd BufWinEnter * if s:diff_window_count() == 1 && &diff && getbufvar(+expand('<abuf>'), 'git_dir') !=# '' | call s:diff_off() | endif
|
2009-10-14 23:26:10 -04:00
|
|
|
augroup END
|
|
|
|
|
2010-04-14 11:05:53 -04:00
|
|
|
function! s:diff_window_count()
|
|
|
|
let c = 0
|
|
|
|
for nr in range(1,winnr('$'))
|
|
|
|
let c += getwinvar(nr,'&diff')
|
|
|
|
endfor
|
|
|
|
return c
|
|
|
|
endfunction
|
|
|
|
|
2010-05-25 16:48:34 -04:00
|
|
|
function! s:diff_off()
|
|
|
|
if &l:diff
|
|
|
|
diffoff
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2010-01-30 00:31:10 -05:00
|
|
|
function! s:buffer_compare_age(commit) dict abort
|
|
|
|
let scores = {':0': 1, ':1': 2, ':2': 3, ':': 4, ':3': 5}
|
|
|
|
let my_score = get(scores,':'.self.commit(),0)
|
|
|
|
let their_score = get(scores,':'.a:commit,0)
|
|
|
|
if my_score || their_score
|
|
|
|
return my_score < their_score ? -1 : my_score != their_score
|
|
|
|
elseif self.commit() ==# a:commit
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
let base = self.repo().git_chomp('merge-base',self.commit(),a:commit)
|
|
|
|
if base ==# self.commit()
|
|
|
|
return -1
|
|
|
|
elseif base ==# a:commit
|
|
|
|
return 1
|
|
|
|
endif
|
2010-01-30 00:38:00 -05:00
|
|
|
let my_time = +self.repo().git_chomp('log','--max-count=1','--pretty=format:%at',self.commit())
|
|
|
|
let their_time = +self.repo().git_chomp('log','--max-count=1','--pretty=format:%at',a:commit)
|
2010-01-30 00:31:10 -05:00
|
|
|
return my_time < their_time ? -1 : my_time != their_time
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:add_methods('buffer',['compare_age'])
|
|
|
|
|
2010-04-14 10:48:42 -04:00
|
|
|
function! s:Diff(bang,...) abort
|
|
|
|
let split = a:bang ? 'split' : 'vsplit'
|
2009-10-10 19:47:14 -04:00
|
|
|
if exists(':DiffGitCached')
|
|
|
|
return 'DiffGitCached'
|
2009-10-14 23:26:10 -04:00
|
|
|
elseif (!a:0 || a:1 == ':') && s:buffer().commit() =~# '^[0-1]\=$' && s:repo().git_chomp_in_tree('ls-files', '--unmerged', '--', s:buffer().path()) !=# ''
|
2010-05-25 16:32:09 -04:00
|
|
|
execute 'leftabove '.split.' `=fugitive#buffer().repo().translate(s:buffer().expand('':2''))`'
|
|
|
|
diffthis
|
|
|
|
wincmd p
|
|
|
|
execute 'rightbelow '.split.' `=fugitive#buffer().repo().translate(s:buffer().expand('':3''))`'
|
|
|
|
diffthis
|
|
|
|
wincmd p
|
|
|
|
diffthis
|
|
|
|
return ''
|
2009-10-10 19:47:14 -04:00
|
|
|
elseif a:0
|
|
|
|
if a:1 ==# ''
|
|
|
|
return ''
|
2009-10-14 23:26:10 -04:00
|
|
|
elseif a:1 ==# '/'
|
|
|
|
let file = s:buffer().path('/')
|
|
|
|
elseif a:1 ==# ':'
|
|
|
|
let file = s:buffer().path(':0:')
|
2009-10-15 18:47:03 -04:00
|
|
|
elseif a:1 =~# '^:/'
|
|
|
|
try
|
2010-02-14 15:55:09 -05:00
|
|
|
let file = s:repo().rev_parse(a:1).s:buffer().path(':')
|
2009-10-15 18:47:03 -04:00
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
2009-10-10 19:47:14 -04:00
|
|
|
else
|
|
|
|
let file = s:buffer().expand(a:1)
|
|
|
|
endif
|
2010-02-14 15:55:09 -05:00
|
|
|
if file !~# ':' && file !~# '^/' && s:repo().git_chomp('cat-file','-t',file) =~# '^\%(tag\|commit\)$'
|
2009-10-15 18:33:08 -04:00
|
|
|
let file = file.s:buffer().path(':')
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
else
|
2009-10-14 23:26:10 -04:00
|
|
|
let file = s:buffer().path(s:buffer().commit() == '' ? ':0:' : '/')
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
try
|
2010-01-30 00:31:10 -05:00
|
|
|
let spec = s:repo().translate(file)
|
|
|
|
let commit = matchstr(spec,'\C[^:/]//\zs\x\+')
|
|
|
|
if s:buffer().compare_age(commit) < 0
|
2010-04-14 10:48:42 -04:00
|
|
|
execute 'rightbelow '.split.' `=spec`'
|
2010-01-30 00:31:10 -05:00
|
|
|
else
|
2010-04-14 10:48:42 -04:00
|
|
|
execute 'leftabove '.split.' `=spec`'
|
2010-01-30 00:31:10 -05:00
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
diffthis
|
|
|
|
wincmd p
|
|
|
|
diffthis
|
|
|
|
return ''
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Gmove, Gremove {{{1
|
|
|
|
|
|
|
|
function! s:Move(force,destination)
|
|
|
|
if a:destination =~# '^/'
|
|
|
|
let destination = a:destination[1:-1]
|
|
|
|
else
|
|
|
|
let destination = fnamemodify(s:sub(a:destination,'[%#]%(:\w)*','\=expand(submatch(0))'),':p')
|
2010-01-09 17:35:27 -05:00
|
|
|
if destination[0:strlen(s:repo().tree())] ==# s:repo().tree('')
|
2009-10-10 19:47:14 -04:00
|
|
|
let destination = destination[strlen(s:repo().tree('')):-1]
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let message = call(s:repo().git_chomp_in_tree,['mv']+(a:force ? ['-f'] : [])+['--', s:buffer().path(), destination], s:repo())
|
|
|
|
if v:shell_error
|
|
|
|
let v:errmsg = 'fugitive: '.message
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endif
|
|
|
|
let destination = s:repo().tree(destination)
|
|
|
|
if isdirectory(destination)
|
|
|
|
let destination = fnamemodify(s:sub(destination,'/$','').'/'.expand('%:t'),':.')
|
|
|
|
endif
|
2009-11-03 22:23:58 -05:00
|
|
|
call fugitive#reload_status()
|
2009-10-10 19:47:14 -04:00
|
|
|
if s:buffer().commit() == ''
|
|
|
|
return 'saveas! '.s:fnameescape(destination)
|
|
|
|
else
|
2010-03-19 21:06:08 -04:00
|
|
|
return 'file '.s:fnameescape(s:repo().translate(':0:'.destination)
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:MoveComplete(A,L,P)
|
|
|
|
if a:A =~ '^/'
|
|
|
|
return s:repo().superglob(a:A)
|
|
|
|
else
|
2010-01-31 16:04:10 -05:00
|
|
|
let matches = split(glob(a:A.'*'),"\n")
|
|
|
|
call map(matches,'v:val !~ "/$" && isdirectory(v:val) ? v:val."/" : v:val')
|
|
|
|
return matches
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:Remove(force)
|
|
|
|
if s:buffer().commit() ==# ''
|
|
|
|
let cmd = ['rm']
|
|
|
|
elseif s:buffer().commit() ==# '0'
|
|
|
|
let cmd = ['rm','--cached']
|
|
|
|
else
|
|
|
|
let v:errmsg = 'fugitive: rm not supported here'
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endif
|
|
|
|
if a:force
|
|
|
|
let cmd += ['--force']
|
|
|
|
endif
|
|
|
|
let message = call(s:repo().git_chomp_in_tree,cmd+['--',s:buffer().path()],s:repo())
|
|
|
|
if v:shell_error
|
|
|
|
let v:errmsg = 'fugitive: '.s:sub(message,'error:.*\zs\n\(.*-f.*',' (add ! to force)')
|
|
|
|
return 'echoerr '.string(v:errmsg)
|
|
|
|
else
|
2009-11-03 22:23:58 -05:00
|
|
|
call fugitive#reload_status()
|
2009-10-10 19:47:14 -04:00
|
|
|
return 'bdelete'.(a:force ? '!' : '')
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup fugitive_remove
|
|
|
|
autocmd!
|
|
|
|
autocmd User Fugitive if s:buffer().commit() =~# '^0\=$' |
|
|
|
|
\ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,s:MoveComplete Gmove :execute s:Move(<bang>0,<q-args>)" |
|
|
|
|
\ exe "command! -buffer -bar -bang Gremove :execute s:Remove(<bang>0)" |
|
|
|
|
\ endif
|
|
|
|
augroup END
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Gblame {{{1
|
|
|
|
|
|
|
|
augroup fugitive_blame
|
|
|
|
autocmd!
|
|
|
|
autocmd BufReadPost *.fugitiveblame setfiletype fugitiveblame
|
|
|
|
autocmd FileType fugitiveblame setlocal nomodeline | if exists('b:git_dir') | let &l:keywordprg = s:repo().keywordprg() | endif
|
|
|
|
autocmd Syntax fugitiveblame call s:BlameSyntax()
|
2010-03-05 22:28:53 -05:00
|
|
|
autocmd User Fugitive if s:buffer().type('file', 'blob') | exe "command! -buffer -bar -bang -range=0 -nargs=* Gblame :execute s:Blame(<bang>0,<line1>,<line2>,<count>,[<f-args>])" | endif
|
2009-10-10 19:47:14 -04:00
|
|
|
augroup END
|
|
|
|
|
2010-03-05 22:28:53 -05:00
|
|
|
function! s:Blame(bang,line1,line2,count,args) abort
|
2009-10-10 19:47:14 -04:00
|
|
|
try
|
|
|
|
if s:buffer().path() == ''
|
|
|
|
call s:throw('file or blob required')
|
|
|
|
endif
|
2010-03-05 22:28:53 -05:00
|
|
|
if filter(copy(a:args),'v:val !~# "^\\%(--root\|--show-name\\|-\\=\\%([ltwfs]\\|[MC]\\d*\\)\\+\\)$"') != []
|
|
|
|
call s:throw('unsupported option')
|
|
|
|
endif
|
|
|
|
call map(a:args,'s:sub(v:val,"^\\ze[^-]","-")')
|
2009-10-10 19:47:14 -04:00
|
|
|
let git_dir = s:repo().dir()
|
2010-03-05 22:28:53 -05:00
|
|
|
let cmd = ['--no-pager', 'blame', '--show-number'] + a:args
|
2009-11-11 00:16:02 -05:00
|
|
|
if s:buffer().commit() =~# '\D\|..'
|
2009-10-10 19:47:14 -04:00
|
|
|
let cmd += [s:buffer().commit()]
|
|
|
|
else
|
2009-10-12 21:48:16 -04:00
|
|
|
let cmd += ['--contents', '-']
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
let basecmd = call(s:repo().git_command,cmd+['--',s:buffer().path()],s:repo())
|
2009-10-12 21:48:16 -04:00
|
|
|
try
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
if !s:repo().bare()
|
|
|
|
let dir = getcwd()
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=s:repo().tree()`'
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
2009-10-12 21:48:16 -04:00
|
|
|
if a:count
|
|
|
|
execute 'write !'.substitute(basecmd,' blame ',' blame -L '.a:line1.','.a:line2.' ','g')
|
|
|
|
else
|
2010-03-09 08:43:57 -05:00
|
|
|
let error = tempname()
|
|
|
|
let temp = error.'.fugitiveblame'
|
|
|
|
silent! exe '%write !'.basecmd.' > '.temp.' 2> '.error
|
2009-10-12 21:48:16 -04:00
|
|
|
if v:shell_error
|
2010-03-09 08:43:57 -05:00
|
|
|
call s:throw(join(readfile(error),"\n"))
|
2009-10-12 21:48:16 -04:00
|
|
|
endif
|
|
|
|
let bufnr = bufnr('')
|
2010-05-25 15:34:53 -04:00
|
|
|
let restore = 'call setwinvar(bufwinnr('.bufnr.'),"&scrollbind",0)'
|
2009-10-12 21:48:16 -04:00
|
|
|
if &l:wrap
|
2010-05-25 15:34:53 -04:00
|
|
|
let restore .= '|call setwinvar(bufwinnr('.bufnr.'),"&wrap",1)'
|
2009-10-12 21:48:16 -04:00
|
|
|
endif
|
|
|
|
if &l:foldenable
|
2010-05-25 15:34:53 -04:00
|
|
|
let restore .= '|call setwinvar(bufwinnr('.bufnr.'),"&foldenable",1)'
|
2009-10-12 21:48:16 -04:00
|
|
|
endif
|
|
|
|
let winnr = winnr()
|
|
|
|
windo set noscrollbind
|
|
|
|
exe winnr.'wincmd w'
|
|
|
|
setlocal scrollbind nowrap nofoldenable
|
|
|
|
let top = line('w0') + &scrolloff
|
|
|
|
let current = line('.')
|
|
|
|
exe 'leftabove vsplit '.temp
|
|
|
|
let b:git_dir = git_dir
|
|
|
|
let b:fugitive_type = 'blame'
|
|
|
|
let b:fugitive_blamed_bufnr = bufnr
|
2010-05-25 15:34:53 -04:00
|
|
|
let w:fugitive_restore = restore
|
2010-03-05 22:28:53 -05:00
|
|
|
let b:fugitive_blame_arguments = join(a:args,' ')
|
2010-02-07 21:54:07 -05:00
|
|
|
call s:Detect(expand('%:p'))
|
2009-10-12 21:48:16 -04:00
|
|
|
execute top
|
|
|
|
normal! zt
|
|
|
|
execute current
|
|
|
|
execute "vertical resize ".(match(getline('.'),'\s\+\d\+)')+1)
|
2010-02-16 09:28:00 -05:00
|
|
|
setlocal nomodified nomodifiable bufhidden=delete nonumber scrollbind nowrap foldcolumn=0 nofoldenable filetype=fugitiveblame
|
2009-10-18 22:43:08 -04:00
|
|
|
nnoremap <buffer> <silent> q :<C-U>bdelete<CR>
|
2010-01-15 13:37:00 -05:00
|
|
|
nnoremap <buffer> <silent> <CR> :<C-U>exe <SID>BlameJump('')<CR>
|
|
|
|
nnoremap <buffer> <silent> P :<C-U>exe <SID>BlameJump('^'.v:count1)<CR>
|
|
|
|
nnoremap <buffer> <silent> ~ :<C-U>exe <SID>BlameJump('~'.v:count1)<CR>
|
2009-10-12 21:48:16 -04:00
|
|
|
nnoremap <buffer> <silent> o :<C-U>exe <SID>Edit((&splitbelow ? "botright" : "topleft")." split", matchstr(getline('.'),'\x\+'))<CR>
|
|
|
|
nnoremap <buffer> <silent> O :<C-U>exe <SID>Edit("tabedit", matchstr(getline('.'),'\x\+'))<CR>
|
|
|
|
syncbind
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
2009-10-12 21:48:16 -04:00
|
|
|
finally
|
|
|
|
if exists('l:dir')
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=dir`'
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
2009-10-12 21:48:16 -04:00
|
|
|
endtry
|
2009-10-10 19:47:14 -04:00
|
|
|
return ''
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
2010-01-15 13:37:00 -05:00
|
|
|
function! s:BlameJump(suffix) abort
|
2009-10-10 19:47:14 -04:00
|
|
|
let commit = matchstr(getline('.'),'^\^\=\zs\x\+')
|
|
|
|
if commit =~# '^0\+$'
|
|
|
|
let commit = ':0'
|
|
|
|
endif
|
2010-03-05 22:28:53 -05:00
|
|
|
let lnum = matchstr(getline('.'),'\d\+\ze\s\+[([:digit:]]')
|
|
|
|
let path = matchstr(getline('.'),'^\^\=\zs\x\+\s\+\zs.\{-\}\ze\s*\d\+ ')
|
2009-10-10 19:47:14 -04:00
|
|
|
if path ==# ''
|
|
|
|
let path = s:buffer(b:fugitive_blamed_bufnr).path()
|
|
|
|
endif
|
2010-03-05 22:28:53 -05:00
|
|
|
let args = b:fugitive_blame_arguments
|
2009-10-10 19:47:14 -04:00
|
|
|
let offset = line('.') - line('w0')
|
|
|
|
let bufnr = bufnr('%')
|
|
|
|
let winnr = bufwinnr(b:fugitive_blamed_bufnr)
|
|
|
|
if winnr > 0
|
|
|
|
exe winnr.'wincmd w'
|
|
|
|
endif
|
2010-01-15 13:37:00 -05:00
|
|
|
execute s:Edit('edit',commit.a:suffix.':'.path)
|
2009-10-10 19:47:14 -04:00
|
|
|
if winnr > 0
|
|
|
|
exe bufnr.'bdelete'
|
|
|
|
endif
|
2010-03-05 22:28:53 -05:00
|
|
|
execute 'Gblame '.args
|
2009-10-10 19:47:14 -04:00
|
|
|
execute lnum
|
|
|
|
let delta = line('.') - line('w0') - offset
|
|
|
|
if delta > 0
|
|
|
|
execute 'norm! 'delta."\<C-E>"
|
|
|
|
elseif delta < 0
|
|
|
|
execute 'norm! '(-delta)."\<C-Y>"
|
|
|
|
endif
|
|
|
|
syncbind
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:BlameSyntax() abort
|
|
|
|
let b:current_syntax = 'fugitiveblame'
|
|
|
|
syn match FugitiveblameBoundary "^\^"
|
2010-03-05 22:28:53 -05:00
|
|
|
syn match FugitiveblameBlank "^\s\+\s\@=" nextgroup=FugitiveblameAnnotation,fugitiveblameOriginalFile,FugitiveblameOriginalLineNumber skipwhite
|
|
|
|
syn match FugitiveblameHash "\%(^\^\=\)\@<=\x\{7,40\}\>" nextgroup=FugitiveblameAnnotation,FugitiveblameOriginalLineNumber,fugitiveblameOriginalFile skipwhite
|
2009-10-10 19:47:14 -04:00
|
|
|
syn match FugitiveblameUncommitted "\%(^\^\=\)\@<=0\{7,40\}\>" nextgroup=FugitiveblameAnnotation,FugitiveblameOriginalLineNumber,fugitiveblameOriginalFile skipwhite
|
|
|
|
syn region FugitiveblameAnnotation matchgroup=FugitiveblameDelimiter start="(" end="\%( \d\+\)\@<=)" contained keepend oneline
|
|
|
|
syn match FugitiveblameTime "[0-9:/+-][0-9:/+ -]*[0-9:/+-]\%( \+\d\+)\)\@=" contained containedin=FugitiveblameAnnotation
|
|
|
|
syn match FugitiveblameLineNumber " \@<=\d\+)\@=" contained containedin=FugitiveblameAnnotation
|
2010-03-05 22:28:53 -05:00
|
|
|
syn match FugitiveblameOriginalFile " \%(\f\+\D\@<=\|\D\@=\f\+\)\%(\%(\s\+\d\+\)\=\s\%((\|\s*\d\+)\)\)\@=" contained nextgroup=FugitiveblameOriginalLineNumber,FugitiveblameAnnotation skipwhite
|
|
|
|
syn match FugitiveblameOriginalLineNumber " \@<=\d\+\%(\s(\)\@=" contained nextgroup=FugitiveblameAnnotation skipwhite
|
|
|
|
syn match FugitiveblameOriginalLineNumber " \@<=\d\+\%(\s\+\d\+)\)\@=" contained nextgroup=FugitiveblameShort skipwhite
|
|
|
|
syn match FugitiveblameShort "\d\+)" contained contains=FugitiveblameLineNumber
|
2009-10-10 19:47:14 -04:00
|
|
|
syn match FugitiveblameNotCommittedYet "(\@<=Not Committed Yet\>" contained containedin=FugitiveblameAnnotation
|
|
|
|
hi def link FugitiveblameBoundary Keyword
|
|
|
|
hi def link FugitiveblameHash Identifier
|
|
|
|
hi def link FugitiveblameUncommitted Function
|
|
|
|
hi def link FugitiveblameTime PreProc
|
|
|
|
hi def link FugitiveblameLineNumber Number
|
|
|
|
hi def link FugitiveblameOriginalFile String
|
|
|
|
hi def link FugitiveblameOriginalLineNumber Float
|
2010-03-05 22:28:53 -05:00
|
|
|
hi def link FugitiveblameShort FugitiveblameDelimiter
|
2009-10-10 19:47:14 -04:00
|
|
|
hi def link FugitiveblameDelimiter Delimiter
|
|
|
|
hi def link FugitiveblameNotCommittedYet Comment
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" File access {{{1
|
|
|
|
|
2010-01-13 22:01:43 -05:00
|
|
|
function! s:ReplaceCmd(cmd,...) abort
|
2009-10-10 19:47:14 -04:00
|
|
|
let fn = bufname('')
|
|
|
|
let tmp = tempname()
|
|
|
|
let aw = &autowrite
|
2010-01-13 22:01:43 -05:00
|
|
|
let prefix = ''
|
2009-10-10 19:47:14 -04:00
|
|
|
try
|
2010-01-13 22:01:43 -05:00
|
|
|
if a:0 && a:1 != ''
|
|
|
|
if &shell =~# 'cmd'
|
|
|
|
let old_index = $GIT_INDEX_FILE
|
|
|
|
let $GIT_INDEX_FILE = a:1
|
|
|
|
elseif &shell =~# 'csh'
|
|
|
|
let prefix = 'setenv GIT_INDEX_FILE '.s:shellesc(a:1).'; '
|
|
|
|
else
|
|
|
|
let prefix = 'GIT_INDEX_FILE='.s:shellesc(a:1).' '
|
|
|
|
endif
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
set noautowrite
|
2010-01-13 22:01:43 -05:00
|
|
|
silent exe '!'.escape(prefix.a:cmd,'%#').' > '.tmp
|
2009-10-10 19:47:14 -04:00
|
|
|
finally
|
|
|
|
let &autowrite = aw
|
2010-01-13 22:01:43 -05:00
|
|
|
if exists('old_index')
|
2010-03-09 19:13:28 -05:00
|
|
|
let $GIT_INDEX_FILE = old_index
|
2010-01-13 22:01:43 -05:00
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
endtry
|
|
|
|
silent exe 'keepalt file '.tmp
|
|
|
|
silent edit!
|
|
|
|
silent exe 'keepalt file '.s:fnameescape(fn)
|
|
|
|
call delete(tmp)
|
|
|
|
silent exe 'doau BufReadPost '.s:fnameescape(fn)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:BufReadIndex()
|
2009-10-19 22:11:40 -04:00
|
|
|
if !exists('b:fugitive_display_format')
|
2010-01-09 19:17:43 -05:00
|
|
|
let b:fugitive_display_format = filereadable(expand('%').'.lock')
|
2009-10-19 22:11:40 -04:00
|
|
|
endif
|
|
|
|
let b:fugitive_display_format = b:fugitive_display_format % 2
|
|
|
|
let b:fugitive_type = 'index'
|
2009-10-10 19:47:14 -04:00
|
|
|
try
|
|
|
|
let b:git_dir = s:repo().dir()
|
|
|
|
setlocal noro ma
|
2010-01-09 19:17:43 -05:00
|
|
|
if fnamemodify($GIT_INDEX_FILE !=# '' ? $GIT_INDEX_FILE : b:git_dir . '/index', ':p') ==# expand('%:p')
|
2010-01-13 22:01:43 -05:00
|
|
|
let index = ''
|
2010-01-09 18:19:07 -05:00
|
|
|
else
|
2010-01-13 22:01:43 -05:00
|
|
|
let index = expand('%')
|
2010-01-09 18:19:07 -05:00
|
|
|
endif
|
2009-10-19 22:11:40 -04:00
|
|
|
if b:fugitive_display_format
|
2010-01-13 22:01:43 -05:00
|
|
|
call s:ReplaceCmd(s:repo().git_command('ls-files','--stage'),index)
|
2009-10-19 22:11:40 -04:00
|
|
|
set ft=git nospell
|
|
|
|
else
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
|
|
|
try
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=s:repo().tree()`'
|
2010-01-13 22:01:43 -05:00
|
|
|
call s:ReplaceCmd(s:repo().git_command('status'),index)
|
2009-10-19 22:11:40 -04:00
|
|
|
finally
|
2010-01-13 17:50:18 -05:00
|
|
|
execute cd.'`=dir`'
|
2009-10-19 22:11:40 -04:00
|
|
|
endtry
|
|
|
|
set ft=gitcommit
|
|
|
|
endif
|
|
|
|
setlocal ro noma nomod nomodeline bufhidden=delete
|
|
|
|
nnoremap <buffer> <silent> a :<C-U>let b:fugitive_display_format += 1<Bar>exe <SID>BufReadIndex()<CR>
|
|
|
|
nnoremap <buffer> <silent> i :<C-U>let b:fugitive_display_format -= 1<Bar>exe <SID>BufReadIndex()<CR>
|
2010-03-19 21:23:41 -04:00
|
|
|
nnoremap <buffer> <silent> D :<C-U>execute <SID>StageDiff()<CR>
|
2009-10-22 21:54:55 -04:00
|
|
|
nnoremap <buffer> <silent> - :<C-U>execute <SID>StageToggle(line('.'),line('.')+v:count1-1)<CR>
|
|
|
|
xnoremap <buffer> <silent> - :<C-U>execute <SID>StageToggle(line("'<"),line("'>"))<CR>
|
2010-01-11 22:51:54 -05:00
|
|
|
nnoremap <buffer> <silent> p :<C-U>execute <SID>StagePatch(line('.'),line('.')+v:count1-1)<CR>
|
|
|
|
xnoremap <buffer> <silent> p :<C-U>execute <SID>StagePatch(line("'<"),line("'>"))<CR>
|
2009-10-19 22:11:40 -04:00
|
|
|
call s:JumpInit()
|
2010-01-11 20:20:55 -05:00
|
|
|
nunmap <buffer> P
|
|
|
|
nunmap <buffer> ~
|
2010-01-09 23:31:22 -05:00
|
|
|
nnoremap <buffer> <silent> C :<C-U>Gcommit<CR>
|
2009-10-10 19:47:14 -04:00
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:FileRead()
|
|
|
|
try
|
|
|
|
let repo = s:repo(s:ExtractGitDir(expand('<amatch>')))
|
|
|
|
let path = s:sub(s:sub(matchstr(expand('<amatch>'),'fugitive://.\{-\}//\zs.*'),'/',':'),'^\d:',':&')
|
|
|
|
let hash = repo.rev_parse(path)
|
|
|
|
if path =~ '^:'
|
|
|
|
let type = 'blob'
|
|
|
|
else
|
|
|
|
let type = repo.git_chomp('cat-file','-t',hash)
|
|
|
|
endif
|
|
|
|
" TODO: use count, if possible
|
|
|
|
return "read !".escape(repo.git_command('cat-file',type,hash),'%#\')
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:BufReadIndexFile()
|
|
|
|
try
|
|
|
|
let b:fugitive_type = 'blob'
|
|
|
|
let b:git_dir = s:repo().dir()
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('cat-file','blob',s:buffer().sha1()))
|
|
|
|
return ''
|
|
|
|
catch /^fugitive: rev-parse/
|
|
|
|
silent exe 'doau BufNewFile '.s:fnameescape(bufname(''))
|
|
|
|
return ''
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:BufWriteIndexFile()
|
|
|
|
let tmp = tempname()
|
|
|
|
try
|
2009-10-14 22:30:36 -04:00
|
|
|
let path = matchstr(expand('<amatch>'),'//\d/\zs.*')
|
|
|
|
let stage = matchstr(expand('<amatch>'),'//\zs\d')
|
2009-10-10 19:47:14 -04:00
|
|
|
silent execute 'write !'.s:repo().git_command('hash-object','-w','--stdin').' > '.tmp
|
|
|
|
let sha1 = readfile(tmp)[0]
|
2009-10-14 22:30:36 -04:00
|
|
|
let old_mode = matchstr(s:repo().git_chomp('ls-files','--stage',path),'^\d\+')
|
2009-10-10 19:47:14 -04:00
|
|
|
if old_mode == ''
|
2009-10-14 22:30:36 -04:00
|
|
|
let old_mode = executable(s:repo().tree(path)) ? '100755' : '100644'
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
2009-10-14 22:30:36 -04:00
|
|
|
let info = old_mode.' '.sha1.' '.stage."\t".path
|
2009-10-10 19:47:14 -04:00
|
|
|
call writefile([info],tmp)
|
2010-06-02 22:42:41 -04:00
|
|
|
if has('win32')
|
|
|
|
let error = system('type '.tmp.'|'.s:repo().git_command('update-index','--index-info'))
|
|
|
|
else
|
|
|
|
let error = system(s:repo().git_command('update-index','--index-info').' < '.tmp)
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
if v:shell_error == 0
|
|
|
|
setlocal nomodified
|
2009-10-22 22:22:17 -04:00
|
|
|
silent execute 'doautocmd BufWritePost '.s:fnameescape(expand('%:p'))
|
2009-11-03 22:23:58 -05:00
|
|
|
call fugitive#reload_status()
|
2009-10-10 19:47:14 -04:00
|
|
|
return ''
|
|
|
|
else
|
|
|
|
return 'echoerr '.string('fugitive: '.error)
|
|
|
|
endif
|
|
|
|
finally
|
|
|
|
call delete(tmp)
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:BufReadObject()
|
|
|
|
try
|
|
|
|
setlocal noro ma
|
|
|
|
let b:git_dir = s:repo().dir()
|
|
|
|
let hash = s:buffer().sha1()
|
|
|
|
if !exists("b:fugitive_type")
|
|
|
|
let b:fugitive_type = s:repo().git_chomp('cat-file','-t',hash)
|
|
|
|
endif
|
|
|
|
if b:fugitive_type !~# '^\%(tag\|commit\|tree\|blob\)$'
|
2009-10-12 22:11:34 -04:00
|
|
|
return "echoerr 'fugitive: unrecognized git type'"
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
let firstline = getline('.')
|
|
|
|
if !exists('b:fugitive_display_format') && b:fugitive_type != 'blob'
|
|
|
|
let b:fugitive_display_format = +getbufvar('#','fugitive_display_format')
|
|
|
|
endif
|
|
|
|
|
|
|
|
let pos = getpos('.')
|
|
|
|
silent %delete
|
|
|
|
setlocal endofline
|
|
|
|
|
|
|
|
if b:fugitive_type == 'tree'
|
|
|
|
let b:fugitive_display_format = b:fugitive_display_format % 2
|
|
|
|
if b:fugitive_display_format
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('ls-tree',hash))
|
|
|
|
else
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('show',hash))
|
|
|
|
endif
|
|
|
|
elseif b:fugitive_type == 'tag'
|
|
|
|
let b:fugitive_display_format = b:fugitive_display_format % 2
|
|
|
|
if b:fugitive_display_format
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('cat-file',b:fugitive_type,hash))
|
|
|
|
else
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('cat-file','-p',hash))
|
|
|
|
endif
|
|
|
|
elseif b:fugitive_type == 'commit'
|
|
|
|
let b:fugitive_display_format = b:fugitive_display_format % 2
|
|
|
|
if b:fugitive_display_format
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('cat-file',b:fugitive_type,hash))
|
|
|
|
else
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('show','--pretty=format:tree %T%nparent %P%nauthor %an <%ae> %ad%ncommitter %cn <%ce> %cd%nencoding %e%n%n%s%n%n%b',hash))
|
|
|
|
call search('^parent ')
|
2010-01-09 17:35:27 -05:00
|
|
|
if getline('.') ==# 'parent '
|
2009-11-11 21:37:42 -05:00
|
|
|
silent delete_
|
|
|
|
else
|
|
|
|
silent s/\%(^parent\)\@<! /\rparent /ge
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
if search('^encoding \%(<unknown>\)\=$','W',line('.')+3)
|
2009-11-11 21:37:42 -05:00
|
|
|
silent delete_
|
2009-10-10 19:47:14 -04:00
|
|
|
end
|
|
|
|
1
|
|
|
|
endif
|
|
|
|
elseif b:fugitive_type ==# 'blob'
|
|
|
|
call s:ReplaceCmd(s:repo().git_command('cat-file',b:fugitive_type,hash))
|
|
|
|
endif
|
|
|
|
call setpos('.',pos)
|
|
|
|
setlocal ro noma nomod nomodeline
|
|
|
|
if b:fugitive_type !=# 'blob'
|
|
|
|
set filetype=git
|
2009-10-18 22:43:08 -04:00
|
|
|
nnoremap <buffer> <silent> a :<C-U>let b:fugitive_display_format += v:count1<Bar>exe <SID>BufReadObject()<CR>
|
|
|
|
nnoremap <buffer> <silent> i :<C-U>let b:fugitive_display_format -= v:count1<Bar>exe <SID>BufReadObject()<CR>
|
2009-10-10 19:47:14 -04:00
|
|
|
else
|
|
|
|
call s:JumpInit()
|
|
|
|
endif
|
|
|
|
|
|
|
|
return ''
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
augroup fugitive_files
|
|
|
|
autocmd!
|
|
|
|
autocmd BufReadCmd *.git/index exe s:BufReadIndex()
|
2010-01-09 18:19:07 -05:00
|
|
|
autocmd BufReadCmd *.git/*index*.lock exe s:BufReadIndex()
|
2009-10-10 19:47:14 -04:00
|
|
|
autocmd FileReadCmd fugitive://**//[0-3]/** exe s:FileRead()
|
|
|
|
autocmd BufReadCmd fugitive://**//[0-3]/** exe s:BufReadIndexFile()
|
|
|
|
autocmd BufWriteCmd fugitive://**//[0-3]/** exe s:BufWriteIndexFile()
|
|
|
|
autocmd BufReadCmd fugitive://**//[0-9a-f][0-9a-f]* exe s:BufReadObject()
|
|
|
|
autocmd FileReadCmd fugitive://**//[0-9a-f][0-9a-f]* exe s:FileRead()
|
|
|
|
autocmd FileType git call s:JumpInit()
|
|
|
|
augroup END
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
" Go to file {{{1
|
|
|
|
|
|
|
|
function! s:JumpInit() abort
|
|
|
|
nnoremap <buffer> <silent> <CR> :<C-U>exe <SID>GF("edit")<CR>
|
|
|
|
if !&modifiable
|
|
|
|
nnoremap <buffer> <silent> o :<C-U>exe <SID>GF("split")<CR>
|
|
|
|
nnoremap <buffer> <silent> O :<C-U>exe <SID>GF("tabedit")<CR>
|
|
|
|
nnoremap <buffer> <silent> P :<C-U>exe <SID>Edit('edit',<SID>buffer().commit().'^'.v:count1.<SID>buffer().path(':'))<CR>
|
|
|
|
nnoremap <buffer> <silent> ~ :<C-U>exe <SID>Edit('edit',<SID>buffer().commit().'~'.v:count1.<SID>buffer().path(':'))<CR>
|
|
|
|
nnoremap <buffer> <silent> C :<C-U>exe <SID>Edit('edit',<SID>buffer().containing_commit())<CR>
|
2010-02-14 13:56:33 -05:00
|
|
|
nnoremap <buffer> <silent> cc :<C-U>exe <SID>Edit('edit',<SID>buffer().containing_commit())<CR>
|
|
|
|
nnoremap <buffer> <silent> co :<C-U>exe <SID>Edit('split',<SID>buffer().containing_commit())<CR>
|
|
|
|
nnoremap <buffer> <silent> cO :<C-U>exe <SID>Edit('tabedit',<SID>buffer().containing_commit())<CR>
|
|
|
|
nnoremap <buffer> <silent> cp :<C-U>exe <SID>Edit('pedit',<SID>buffer().containing_commit())<CR>
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:GF(mode) abort
|
|
|
|
try
|
|
|
|
let buffer = s:buffer()
|
|
|
|
let myhash = buffer.sha1()
|
|
|
|
|
2009-10-19 22:11:40 -04:00
|
|
|
if buffer.type('tree')
|
2009-10-10 19:47:14 -04:00
|
|
|
let showtree = (getline(1) =~# '^tree ' && getline(2) == "")
|
|
|
|
if showtree && line('.') == 1
|
|
|
|
return ""
|
|
|
|
elseif showtree && line('.') > 2
|
|
|
|
return s:Edit(a:mode,buffer.commit().':'.(buffer.path() == '' ? '' : buffer.path().'/').s:sub(getline('.'),'/$',''))
|
|
|
|
elseif getline('.') =~# '^\d\{6\} \l\{3,8\} \x\{40\}\t'
|
|
|
|
return s:Edit(a:mode,buffer.commit().':'.(buffer.path() == '' ? '' : buffer.path().'/').s:sub(matchstr(getline('.'),'\t\zs.*'),'/$',''))
|
|
|
|
endif
|
|
|
|
|
|
|
|
elseif buffer.type('blob')
|
|
|
|
let ref = expand("<cfile>")
|
|
|
|
try
|
|
|
|
let sha1 = buffer.repo().rev_parse(ref)
|
|
|
|
catch /^fugitive:/
|
|
|
|
endtry
|
|
|
|
if exists('sha1')
|
|
|
|
return s:Edit(a:mode,ref)
|
|
|
|
endif
|
|
|
|
|
|
|
|
else
|
2009-10-19 22:11:40 -04:00
|
|
|
|
|
|
|
" Index
|
|
|
|
if getline('.') =~# '^\d\{6\} \x\{40\} \d\t'
|
|
|
|
let ref = matchstr(getline('.'),'\x\{40\}')
|
|
|
|
let file = ':'.s:sub(matchstr(getline('.'),'\d\t.*'),'\t',':')
|
|
|
|
return s:Edit(a:mode,file)
|
|
|
|
|
|
|
|
elseif getline('.') =~# '^#\trenamed:.* -> '
|
|
|
|
let file = '/'.matchstr(getline('.'),' -> \zs.*')
|
|
|
|
return s:Edit(a:mode,file)
|
|
|
|
elseif getline('.') =~# '^#\t[[:alpha:] ]\+: *.'
|
|
|
|
let file = '/'.matchstr(getline('.'),': *\zs.*')
|
|
|
|
return s:Edit(a:mode,file)
|
|
|
|
elseif getline('.') =~# '^#\t.'
|
|
|
|
let file = '/'.matchstr(getline('.'),'#\t\zs.*')
|
|
|
|
return s:Edit(a:mode,file)
|
|
|
|
elseif getline('.') =~# ': needs merge$'
|
|
|
|
let file = '/'.matchstr(getline('.'),'.*\ze: needs merge$')
|
|
|
|
return s:Edit(a:mode,file).'|Gdiff'
|
|
|
|
|
|
|
|
elseif getline('.') ==# '# Not currently on any branch.'
|
|
|
|
return s:Edit(a:mode,'HEAD')
|
|
|
|
elseif getline('.') =~# '^# On branch '
|
|
|
|
let file = 'refs/heads/'.getline('.')[12:]
|
|
|
|
return s:Edit(a:mode,file)
|
|
|
|
elseif getline('.') =~# "^# Your branch .*'"
|
2010-01-09 23:20:30 -05:00
|
|
|
let file = matchstr(getline('.'),"'\\zs\\S\\+\\ze'")
|
2009-10-19 22:11:40 -04:00
|
|
|
return s:Edit(a:mode,file)
|
|
|
|
endif
|
|
|
|
|
2009-10-10 19:47:14 -04:00
|
|
|
let showtree = (getline(1) =~# '^tree ' && getline(2) == "")
|
|
|
|
|
|
|
|
if getline('.') =~# '^ref: '
|
|
|
|
let ref = strpart(getline('.'),5)
|
|
|
|
|
|
|
|
elseif getline('.') =~# '^parent \x\{40\}\>'
|
|
|
|
let ref = matchstr(getline('.'),'\x\{40\}')
|
|
|
|
let line = line('.')
|
|
|
|
let parent = 0
|
|
|
|
while getline(line) =~# '^parent '
|
|
|
|
let parent += 1
|
|
|
|
let line -= 1
|
|
|
|
endwhile
|
|
|
|
return s:Edit(a:mode,ref)
|
|
|
|
|
|
|
|
elseif getline('.') =~ '^tree \x\{40\}$'
|
|
|
|
let ref = matchstr(getline('.'),'\x\{40\}')
|
|
|
|
if s:repo().rev_parse(myhash.':') == ref
|
|
|
|
let ref = myhash.':'
|
|
|
|
endif
|
|
|
|
return s:Edit(a:mode,ref)
|
|
|
|
|
|
|
|
elseif getline('.') =~# '^object \x\{40\}$' && getline(line('.')+1) =~ '^type \%(commit\|tree\|blob\)$'
|
|
|
|
let ref = matchstr(getline('.'),'\x\{40\}')
|
|
|
|
let type = matchstr(getline(line('.')+1),'type \zs.*')
|
|
|
|
|
|
|
|
elseif getline('.') =~# '^\l\{3,8\} '.myhash.'$'
|
|
|
|
return ''
|
|
|
|
|
|
|
|
elseif getline('.') =~# '^\l\{3,8\} \x\{40\}\>'
|
|
|
|
let ref = matchstr(getline('.'),'\x\{40\}')
|
|
|
|
echoerr "warning: unknown context ".matchstr(getline('.'),'^\l*')
|
|
|
|
|
|
|
|
elseif getline('.') =~# '^[+-]\{3\} [ab/]'
|
|
|
|
let ref = getline('.')[4:]
|
|
|
|
|
2009-10-18 18:12:36 -04:00
|
|
|
elseif getline('.') =~# '^rename from '
|
|
|
|
let ref = 'a/'.getline('.')[12:]
|
|
|
|
elseif getline('.') =~# '^rename to '
|
|
|
|
let ref = 'b/'.getline('.')[10:]
|
|
|
|
|
|
|
|
elseif getline('.') =~# '^diff --git \%(a/.*\|/dev/null\) \%(b/.*\|/dev/null\)'
|
|
|
|
let dref = matchstr(getline('.'),'\Cdiff --git \zs\%(a/.*\|/dev/null\)\ze \%(b/.*\|/dev/null\)')
|
|
|
|
let ref = matchstr(getline('.'),'\Cdiff --git \%(a/.*\|/dev/null\) \zs\%(b/.*\|/dev/null\)')
|
|
|
|
|
2009-10-10 19:47:14 -04:00
|
|
|
elseif line('$') == 1 && getline('.') =~ '^\x\{40\}$'
|
|
|
|
let ref = getline('.')
|
|
|
|
else
|
|
|
|
let ref = ''
|
|
|
|
endif
|
|
|
|
|
2009-10-18 18:12:36 -04:00
|
|
|
if myhash ==# ''
|
|
|
|
let ref = s:sub(ref,'^a/','HEAD:')
|
|
|
|
let ref = s:sub(ref,'^b/',':0:')
|
|
|
|
if exists('dref')
|
|
|
|
let dref = s:sub(dref,'^a/','HEAD:')
|
|
|
|
endif
|
|
|
|
else
|
2009-10-10 19:47:14 -04:00
|
|
|
let ref = s:sub(ref,'^a/',myhash.'^:')
|
|
|
|
let ref = s:sub(ref,'^b/',myhash.':')
|
2009-10-18 18:12:36 -04:00
|
|
|
if exists('dref')
|
|
|
|
let dref = s:sub(dref,'^a/',myhash.'^:')
|
|
|
|
endif
|
2009-10-10 19:47:14 -04:00
|
|
|
endif
|
|
|
|
|
2010-01-09 17:35:27 -05:00
|
|
|
if ref ==# '/dev/null'
|
2009-10-10 19:47:14 -04:00
|
|
|
" Empty blob
|
|
|
|
let ref = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
|
|
|
|
endif
|
|
|
|
|
2009-10-18 18:12:36 -04:00
|
|
|
if exists('dref')
|
|
|
|
return s:Edit(a:mode,ref) . '|Gdiff '.s:fnameescape(dref)
|
|
|
|
elseif ref != ""
|
2009-10-10 19:47:14 -04:00
|
|
|
return s:Edit(a:mode,ref)
|
|
|
|
endif
|
|
|
|
|
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
catch /^fugitive:/
|
|
|
|
return 'echoerr v:errmsg'
|
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
2010-02-02 23:46:04 -05:00
|
|
|
" }}}1
|
|
|
|
" Statusline {{{1
|
|
|
|
|
|
|
|
function! s:repo_head_ref() dict abort
|
|
|
|
return readfile(s:repo().dir('HEAD'))[0]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call s:add_methods('repo',['head_ref'])
|
|
|
|
|
|
|
|
function! fugitive#statusline(...)
|
|
|
|
if !exists('b:git_dir')
|
|
|
|
return ''
|
|
|
|
endif
|
2010-02-10 10:07:56 -05:00
|
|
|
let status = ''
|
2010-02-02 23:46:04 -05:00
|
|
|
if s:buffer().commit() != ''
|
|
|
|
let status .= ':' . s:buffer().commit()[0:7]
|
|
|
|
endif
|
|
|
|
let head = s:repo().head_ref()
|
|
|
|
if head =~# '^ref: '
|
|
|
|
let status .= s:sub(head,'^ref: %(refs/%(heads/|remotes/|tags/)=)=','(').')'
|
|
|
|
elseif head =~# '^\x\{40\}$'
|
|
|
|
let status .= '('.head[0:7].')'
|
|
|
|
endif
|
2010-02-10 10:07:56 -05:00
|
|
|
if &statusline =~# '%[MRHWY]' && &statusline !~# '%[mrhwy]'
|
|
|
|
return ',GIT'.status
|
|
|
|
else
|
|
|
|
return '[Git'.status.']'
|
|
|
|
endif
|
2010-02-02 23:46:04 -05:00
|
|
|
endfunction
|
|
|
|
|
2009-10-10 19:47:14 -04:00
|
|
|
" }}}1
|
|
|
|
|
|
|
|
" vim:set ft=vim ts=8 sw=2 sts=2:
|