From cbf96cc01a429143c9a135ae572fec5ffaad8c40 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Wed, 20 Jun 2018 18:01:27 -0400 Subject: [PATCH] Clean up repetition in URL parsing --- autoload/fugitive.vim | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index 3b3db38..ba86ce4 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -427,7 +427,7 @@ call s:add_methods('repo',['keywordprg']) " Section: Buffer -function! s:UrlSplit(path) abort +function! s:DirCommitFile(path) abort let vals = matchlist(s:shellslash(a:path), '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\w\+\)\(/.*\)\=$') if empty(vals) return ['', '', ''] @@ -436,7 +436,7 @@ function! s:UrlSplit(path) abort endfunction function! fugitive#Filename(url) abort - let [dir, rev, file] = s:UrlSplit(a:url) + let [dir, commit, file] = s:DirCommitFile(a:url) if len(dir) return s:PlatformSlash(FugitiveTreeForGitDir(dir) . file) elseif a:url =~# '^[\\/]\|^\a:[\\/]' @@ -1729,8 +1729,8 @@ endfunction 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) + let my_score = get(scores, ':'.self.commit(), 0) + let their_score = get(scores, ':'.substitute(a:commit, '^:', '', ''), 0) if my_score || their_score return my_score < their_score ? -1 : my_score != their_score elseif self.commit() ==# a:commit @@ -1800,13 +1800,12 @@ function! s:Diff(vert,keepfocus,...) abort endif try let spec = s:repo().translate(file) - let commit = matchstr(spec,'\C[^:/]\%(//\|::\)\zs\x\+') let restore = s:diff_restore() if exists('+cursorbind') setlocal cursorbind endif let w:fugitive_diff_restore = restore - if s:buffer().compare_age(commit) < 0 + if s:buffer().compare_age(s:DirCommitFile(spec)[1]) < 0 execute 'rightbelow '.vert.'diffsplit '.s:fnameescape(spec) else execute 'leftabove '.vert.'diffsplit '.s:fnameescape(spec) @@ -2220,8 +2219,10 @@ function! s:Browse(bang,line1,count,...) abort let full = s:repo().translate(expanded) let commit = '' if full =~? '^fugitive:' - let commit = matchstr(full,':\%(//\)\=.*\%(//\|::\)\zs\w\w\+') - let path = matchstr(full,':\%(//\)\=.*\%(//\|::\)\w\+\zs/.*') + let [dir, commit, path] = s:DirCommitFile(full) + if commit =~# '^:\=\d$' + let commit = '' + endif if commit =~ '..' let type = s:repo().git_chomp('cat-file','-t',commit.s:sub(path,'^/',':')) let branch = matchstr(expanded, '^[^:]*') @@ -2566,8 +2567,9 @@ endfunction function! fugitive#FileRead() abort try - let repo = s:repo(FugitiveExtractGitDir(expand(''))) - let path = s:sub(s:sub(matchstr(expand(''),'fugitive:\%(//\)\=.\{-\}\%(//\|::\)\zs.*'),'/',':'),'^\d:',':&') + let [dir, commit, file] = s:DirCommitFile(expand('')) + let repo = s:repo(dir) + let path = commit . substitute(file, '^/', ':', '') let hash = repo.rev_parse(path) if path =~ '^:' let type = 'blob' @@ -2605,15 +2607,15 @@ endfunction function! fugitive#BufWriteIndex() abort let tmp = tempname() try - let path = matchstr(expand(''),'//\d/\zs.*') - let stage = matchstr(expand(''),'//\zs\d') + let [dir, commit, file] = s:DirCommitFile(expand('')) + let path = file[1:-1] silent execute 'write !'.s:repo().git_command('hash-object','-w','--stdin').' > '.tmp let sha1 = readfile(tmp)[0] let old_mode = matchstr(s:repo().git_chomp('ls-files','--stage',path),'^\d\+') if old_mode == '' let old_mode = executable(s:repo().tree(path)) ? '100755' : '100644' endif - let info = old_mode.' '.sha1.' '.stage."\t".path + let info = old_mode.' '.sha1.' '.commit[-1:-1]."\t".path call writefile([info],tmp) if s:winshell() let error = system('type '.s:gsub(tmp,'/','\\').'|'.s:repo().git_command('update-index','--index-info'))