From 0718c1a2a9a0f49da503f79d440f1941d3289172 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Wed, 17 Jul 2013 10:14:43 +0200 Subject: [PATCH] Add sy#repo.vim --- .gitignore | 1 + autoload/sy/repo.vim | 215 ++++++++++++++++++++++++++++++++++++++++++ plugin/signify.vim | 217 +------------------------------------------ 3 files changed, 219 insertions(+), 214 deletions(-) create mode 100644 autoload/sy/repo.vim diff --git a/.gitignore b/.gitignore index ed5901d..a0b475e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ !.gitignore !CONTRIBUTING.md !README.md +!autoload/sy/repo.vim !autoload/sy/utils.vim !doc/signify.txt !plugin/signify.vim diff --git a/autoload/sy/repo.vim b/autoload/sy/repo.vim new file mode 100644 index 0000000..f7b3d1a --- /dev/null +++ b/autoload/sy/repo.vim @@ -0,0 +1,215 @@ +if exists('b:autoloaded_sy_repo') + finish +endif +let b:autoloaded_sy_repo = 1 + +" Function: sy#repo#detect {{{1 +function! sy#repo#detect(path) abort + for type in get(g:, 'signify_vcs_list', [ 'git', 'hg', 'svn', 'darcs', 'bzr', 'fossil', 'cvs', 'rcs', 'accurev' ]) + let diff = sy#repo#get_diff_{type}(a:path) + if !empty(diff) + return [ diff, type ] + endif + endfor + + return [ '', '' ] +endfunction + +" Function: sy#repo#get_diff_git {{{1 +function! sy#repo#get_diff_git(path) abort + if executable('git') + let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && git diff --no-ext-diff -U0 -- '. sy#utils#escape(a:path)) + return v:shell_error ? '' : diff + endif +endfunction + +" Function: sy#repo#get_stat_git {{{1 +function! sy#repo#get_stat_git() abort + let s:stats = [] + let root = finddir('.git', fnamemodify(s:path, ':h') .';') + if empty(root) + echohl ErrorMsg | echomsg 'Cannot find the git root directory: '. s:path | echohl None + return + endif + let root = fnamemodify(root, ':h') + let output = system('cd '. sy#utils#escape(root) .' && git diff --numstat') + if v:shell_error + echohl ErrorMsg | echomsg "'git diff --numstat' failed" | echohl None + return + endif + for stat in split(output, '\n') + let tokens = matchlist(stat, '\v([0-9-]+)\t([0-9-]+)\t(.*)') + if empty(tokens) + echohl ErrorMsg | echomsg 'Cannot parse this line: '. stat | echohl None + elseif tokens[1] == '-' + continue + else + let path = root . sy#utils#separator() . tokens[3] + if !bufexists(path) + execute 'argadd '. path + endif + call add(s:stats, { 'bufnr': bufnr(path), 'text': tokens[1] .' additions, '. tokens[2] .' deletions', 'lnum': 1, 'col': 1 }) + endif + endfor + "call setqflist(stats) +endfunction + +" Function: sy#repo#get_diff_hg {{{1 +function! sy#repo#get_diff_hg(path) abort + if executable('hg') + let diff = system('hg diff --nodates -U0 -- '. sy#utils#escape(a:path)) + return v:shell_error ? '' : diff + endif +endfunction + +" Function: sy#repo#get_diff_svn {{{1 +function! sy#repo#get_diff_svn(path) abort + if executable('svn') + let diff = system('svn diff --diff-cmd '. s:difftool .' -x -U0 -- '. sy#utils#escape(a:path)) + return v:shell_error ? '' : diff + endif +endfunction + +" Function: sy#repo#get_diff_bzr {{{1 +function! sy#repo#get_diff_bzr(path) abort + if executable('bzr') + let diff = system('bzr diff --using '. s:difftool .' --diff-options=-U0 -- '. sy#utils#escape(a:path)) + return ((v:shell_error == 0) || (v:shell_error == 1) || (v:shell_error == 2)) ? diff : '' + endif +endfunction + +" Function: sy#repo#get_diff_darcs {{{1 +function! sy#repo#get_diff_darcs(path) abort + if executable('darcs') + let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && darcs diff --no-pause-for-gui --diff-command="'. s:difftool .' -U0 %1 %2" -- '. sy#utils#escape(a:path)) + return v:shell_error ? '' : diff + endif +endfunction + +" Function: sy#repo#get_diff_fossil {{{1 +function! sy#repo#get_diff_fossil(path) abort + if executable('fossil') + let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && fossil set diff-command "'. s:difftool .' -U 0" && fossil diff --unified -c 0 -- '. sy#utils#escape(a:path)) + return v:shell_error ? '' : diff + endif +endfunction + +" Function: sy#repo#get_diff_cvs {{{1 +function! sy#repo#get_diff_cvs(path) abort + if executable('cvs') + let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && cvs diff -U0 -- '. sy#utils#escape(fnamemodify(a:path, ':t'))) + return v:shell_error ? '' : diff + endif +endfunction + +" Function: sy#repo#get_diff_rcs {{{1 +function! sy#repo#get_diff_rcs(path) abort + if executable('rcs') + let diff = system('rcsdiff -U0 '. sy#utils#escape(a:path) .' 2>/dev/null') + return v:shell_error ? '' : diff + endif +endfunction + +" Function: sy#repo#get_diff_accurev {{{1 +function! sy#repo#get_diff_accurev(path) abort + if executable('accurev') + let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && accurev diff '. sy#utils#escape(fnamemodify(a:path, ':t')) . ' -- -U0') + return (v:shell_error != 1) ? '' : diff + endif +endfunction + +" Function: sy#repo#process_diff {{{1 +function! sy#repo#process_diff(path, diff) abort + " Determine where we have to put our signs. + for line in filter(split(a:diff, '\n'), 'v:val =~ "^@@ "') + let tokens = matchlist(line, '^@@ -\v(\d+),?(\d*) \+(\d+),?(\d*)') + + let [ old_line, old_count, new_line, new_count ] = [ str2nr(tokens[1]), empty(tokens[2]) ? 1 : str2nr(tokens[2]), str2nr(tokens[3]), empty(tokens[4]) ? 1 : str2nr(tokens[4]) ] + + let signs = [] + + " 2 lines added: + + " @@ -5,0 +6,2 @@ this is line 5 + " +this is line 5 + " +this is line 5 + + if (old_count == 0) && (new_count >= 1) + let offset = 0 + while offset < new_count + call add(signs, { 'type': 'SignifyAdd', 'lnum': new_line + offset, 'path': a:path }) + let offset += 1 + endwhile + + " 2 lines removed: + + " @@ -6,2 +5,0 @@ this is line 5 + " -this is line 6 + " -this is line 7 + + elseif (old_count >= 1) && (new_count == 0) + if new_line == 0 + call add(signs, { 'type': 'SignifyDeleteFirstLine', 'lnum': 1, 'path': a:path }) + else + call add(signs, { 'type': (old_count > 9) ? 'SignifyDeleteMore' : 'SignifyDelete'. old_count, 'lnum': new_line, 'path': a:path }) + endif + + " 2 lines changed: + + " @@ -5,2 +5,2 @@ this is line 4 + " -this is line 5 + " -this is line 6 + " +this os line 5 + " +this os line 6 + + elseif old_count == new_count + let offset = 0 + while offset < new_count + call add(signs, { 'type': 'SignifyChange', 'lnum': new_line + offset, 'path': a:path }) + let offset += 1 + endwhile + else + + " 2 lines changed; 2 lines deleted: + + " @@ -5,4 +5,2 @@ this is line 4 + " -this is line 5 + " -this is line 6 + " -this is line 7 + " -this is line 8 + " +this os line 5 + " +this os line 6 + + if old_count > new_count + let offset = 0 + while offset < (new_count - 1) + call add(signs, { 'type': 'SignifyChange', 'lnum': new_line + offset, 'path': a:path }) + let offset += 1 + endwhile + let deleted = old_count - new_count + call add(signs, { 'type': (deleted > 9) ? 'SignifyChangeDeleteMore' : 'SignifyChangeDelete'. deleted, 'lnum': new_line, 'path': a:path }) + + " lines changed and added: + + " @@ -5 +5,3 @@ this is line 4 + " -this is line 5 + " +this os line 5 + " +this is line 42 + " +this is line 666 + + else + let offset = 0 + while offset < old_count + call add(signs, { 'type': 'SignifyChange', 'lnum': new_line + offset, 'path': a:path }) + let offset += 1 + endwhile + while offset < new_count + call add(signs, { 'type': 'SignifyAdd', 'lnum': new_line + offset, 'path': a:path }) + let offset += 1 + endwhile + endif + endif + call s:sign_set(signs) + endfor +endfunction + diff --git a/plugin/signify.vim b/plugin/signify.vim index 3a6dc55..52a13e2 100644 --- a/plugin/signify.vim +++ b/plugin/signify.vim @@ -14,7 +14,6 @@ let s:other_signs_line_numbers = {} " overwrite non-signify signs by default let s:sign_overwrite = get(g:, 'signify_sign_overwrite', 1) -let s:vcs_list = get(g:, 'signify_vcs_list', [ 'git', 'hg', 'svn', 'darcs', 'bzr', 'fossil', 'cvs', 'rcs', 'accurev' ]) let s:id_start = 0x100 let s:id_top = s:id_start @@ -128,7 +127,7 @@ function! s:start(path) abort " new buffer.. add to list if !has_key(s:sy, a:path) - let [ diff, type ] = s:repo_detect(a:path) + let [ diff, type ] = sy#repo#detect(a:path) if empty(diff) return endif @@ -142,7 +141,7 @@ function! s:start(path) abort return " update signs else - let diff = s:repo_get_diff_{s:sy[a:path].type}(a:path) + let diff = sy#repo#get_diff_{s:sy[a:path].type}(a:path) if empty(diff) call s:sign_remove_all(a:path) return @@ -164,7 +163,7 @@ function! s:start(path) abort execute 'sign place 99999 line=1 name=SignifyPlaceholder file='. a:path call s:sign_remove_all(a:path) - call s:repo_process_diff(a:path, diff) + call sy#repo#process_diff(a:path, diff) sign unplace 99999 if !maparg('[c', 'n') @@ -252,216 +251,6 @@ function! s:sign_remove_all(path) abort let s:sy[a:path].hunks = [] endfunction -" Function: s:repo_detect {{{1 -function! s:repo_detect(path) abort - for type in s:vcs_list - let diff = s:repo_get_diff_{type}(a:path) - if !empty(diff) - return [ diff, type ] - endif - endfor - - return [ '', '' ] -endfunction - -" Function: s:repo_get_diff_git {{{1 -function! s:repo_get_diff_git(path) abort - if executable('git') - let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && git diff --no-ext-diff -U0 -- '. sy#utils#escape(a:path)) - return v:shell_error ? '' : diff - endif -endfunction - -" Function: s:repo_get_stat_git {{{1 -function! s:repo_get_stat_git() abort - let s:stats = [] - let root = finddir('.git', fnamemodify(s:path, ':h') .';') - if empty(root) - echohl ErrorMsg | echomsg 'Cannot find the git root directory: '. s:path | echohl None - return - endif - let root = fnamemodify(root, ':h') - let output = system('cd '. sy#utils#escape(root) .' && git diff --numstat') - if v:shell_error - echohl ErrorMsg | echomsg "'git diff --numstat' failed" | echohl None - return - endif - for stat in split(output, '\n') - let tokens = matchlist(stat, '\v([0-9-]+)\t([0-9-]+)\t(.*)') - if empty(tokens) - echohl ErrorMsg | echomsg 'Cannot parse this line: '. stat | echohl None - elseif tokens[1] == '-' - continue - else - let path = root . sy#utils#separator() . tokens[3] - if !bufexists(path) - execute 'argadd '. path - endif - call add(s:stats, { 'bufnr': bufnr(path), 'text': tokens[1] .' additions, '. tokens[2] .' deletions', 'lnum': 1, 'col': 1 }) - endif - endfor - "call setqflist(stats) -endfunction - -" Function: s:repo_get_diff_hg {{{1 -function! s:repo_get_diff_hg(path) abort - if executable('hg') - let diff = system('hg diff --nodates -U0 -- '. sy#utils#escape(a:path)) - return v:shell_error ? '' : diff - endif -endfunction - -" Function: s:repo_get_diff_svn {{{1 -function! s:repo_get_diff_svn(path) abort - if executable('svn') - let diff = system('svn diff --diff-cmd '. s:difftool .' -x -U0 -- '. sy#utils#escape(a:path)) - return v:shell_error ? '' : diff - endif -endfunction - -" Function: s:repo_get_diff_bzr {{{1 -function! s:repo_get_diff_bzr(path) abort - if executable('bzr') - let diff = system('bzr diff --using '. s:difftool .' --diff-options=-U0 -- '. sy#utils#escape(a:path)) - return ((v:shell_error == 0) || (v:shell_error == 1) || (v:shell_error == 2)) ? diff : '' - endif -endfunction - -" Function: s:repo_get_diff_darcs {{{1 -function! s:repo_get_diff_darcs(path) abort - if executable('darcs') - let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && darcs diff --no-pause-for-gui --diff-command="'. s:difftool .' -U0 %1 %2" -- '. sy#utils#escape(a:path)) - return v:shell_error ? '' : diff - endif -endfunction - -" Function: s:repo_get_diff_fossil {{{1 -function! s:repo_get_diff_fossil(path) abort - if executable('fossil') - let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && fossil set diff-command "'. s:difftool .' -U 0" && fossil diff --unified -c 0 -- '. sy#utils#escape(a:path)) - return v:shell_error ? '' : diff - endif -endfunction - -" Function: s:repo_get_diff_cvs {{{1 -function! s:repo_get_diff_cvs(path) abort - if executable('cvs') - let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && cvs diff -U0 -- '. sy#utils#escape(fnamemodify(a:path, ':t'))) - return v:shell_error ? '' : diff - endif -endfunction - -" Function: s:repo_get_diff_rcs {{{1 -function! s:repo_get_diff_rcs(path) abort - if executable('rcs') - let diff = system('rcsdiff -U0 '. sy#utils#escape(a:path) .' 2>/dev/null') - return v:shell_error ? '' : diff - endif -endfunction - -" Function: s:repo_get_diff_accurev {{{1 -function! s:repo_get_diff_accurev(path) abort - if executable('accurev') - let diff = system('cd '. sy#utils#escape(fnamemodify(a:path, ':h')) .' && accurev diff '. sy#utils#escape(fnamemodify(a:path, ':t')) . ' -- -U0') - return (v:shell_error != 1) ? '' : diff - endif -endfunction - -" Function: s:repo_process_diff {{{1 -function! s:repo_process_diff(path, diff) abort - " Determine where we have to put our signs. - for line in filter(split(a:diff, '\n'), 'v:val =~ "^@@ "') - let tokens = matchlist(line, '^@@ -\v(\d+),?(\d*) \+(\d+),?(\d*)') - - let [ old_line, old_count, new_line, new_count ] = [ str2nr(tokens[1]), empty(tokens[2]) ? 1 : str2nr(tokens[2]), str2nr(tokens[3]), empty(tokens[4]) ? 1 : str2nr(tokens[4]) ] - - let signs = [] - - " 2 lines added: - - " @@ -5,0 +6,2 @@ this is line 5 - " +this is line 5 - " +this is line 5 - - if (old_count == 0) && (new_count >= 1) - let offset = 0 - while offset < new_count - call add(signs, { 'type': 'SignifyAdd', 'lnum': new_line + offset, 'path': a:path }) - let offset += 1 - endwhile - - " 2 lines removed: - - " @@ -6,2 +5,0 @@ this is line 5 - " -this is line 6 - " -this is line 7 - - elseif (old_count >= 1) && (new_count == 0) - if new_line == 0 - call add(signs, { 'type': 'SignifyDeleteFirstLine', 'lnum': 1, 'path': a:path }) - else - call add(signs, { 'type': (old_count > 9) ? 'SignifyDeleteMore' : 'SignifyDelete'. old_count, 'lnum': new_line, 'path': a:path }) - endif - - " 2 lines changed: - - " @@ -5,2 +5,2 @@ this is line 4 - " -this is line 5 - " -this is line 6 - " +this os line 5 - " +this os line 6 - - elseif old_count == new_count - let offset = 0 - while offset < new_count - call add(signs, { 'type': 'SignifyChange', 'lnum': new_line + offset, 'path': a:path }) - let offset += 1 - endwhile - else - - " 2 lines changed; 2 lines deleted: - - " @@ -5,4 +5,2 @@ this is line 4 - " -this is line 5 - " -this is line 6 - " -this is line 7 - " -this is line 8 - " +this os line 5 - " +this os line 6 - - if old_count > new_count - let offset = 0 - while offset < (new_count - 1) - call add(signs, { 'type': 'SignifyChange', 'lnum': new_line + offset, 'path': a:path }) - let offset += 1 - endwhile - let deleted = old_count - new_count - call add(signs, { 'type': (deleted > 9) ? 'SignifyChangeDeleteMore' : 'SignifyChangeDelete'. deleted, 'lnum': new_line, 'path': a:path }) - - " lines changed and added: - - " @@ -5 +5,3 @@ this is line 4 - " -this is line 5 - " +this os line 5 - " +this is line 42 - " +this is line 666 - - else - let offset = 0 - while offset < old_count - call add(signs, { 'type': 'SignifyChange', 'lnum': new_line + offset, 'path': a:path }) - let offset += 1 - endwhile - while offset < new_count - call add(signs, { 'type': 'SignifyAdd', 'lnum': new_line + offset, 'path': a:path }) - let offset += 1 - endwhile - endif - endif - call s:sign_set(signs) - endfor -endfunction - " Function: s:highlight_setup {{{1 function! s:highlight_setup() abort highlight link SignifyLineAdd DiffAdd