Browse files

refactor vcs#target & vcs#system.

  • Loading branch information...
1 parent 8dff805 commit c3d45ca5a65362beeb8da8cde9d4d1ab968e0108 @hrsh7th committed Oct 6, 2012
View
46 autoload/vcs.vim
@@ -21,6 +21,8 @@ function! vcs#detect(args)
return ''
endfunction
+
+" TODO: redesign architecture.
function! vcs#vcs(command, args)
" detect vcs type.
let target = vcs#target(a:args)
@@ -31,7 +33,7 @@ function! vcs#vcs(command, args)
endif
" move current directory & do command.
- let root = {'vcs#' . type . '#root#do'}(a:args)
+ let root = {'vcs#' . type . '#root#do'}([target])
let save = getcwd()
call vcs#execute(['cd', root])
@@ -45,43 +47,41 @@ function! vcs#vcs(command, args)
return result
endfunction
-" TODO: refactor.
function! vcs#target(args)
- if type(a:args) == type([])
- let args = a:args
- if len(args) == 0
- let args = [expand('%')]
-
- let filetype = getbufvar(bufnr('%'), '&filetype')
- if filetype == 'vimshell'
- let args = [b:vimshell.current_dir]
- endif
- if filetype == 'vimfiler'
- let args = [b:vimfiler.current_dir]
- endif
- endif
- else
- let args = [a:args]
+ " target given.
+ if type('') == type(a:args) || (type([]) == type(a:args) && len(a:args))
+ return fnamemodify(type([]) == type(a:args) ? a:args[0] : a:args, ':p')
+ endif
+
+ " target auto detect.
+ let filetype = getbufvar(bufnr('%'), '&filetype')
+ if filetype == 'vimshell'
+ let target = b:vimshell.current_dir
+ endif
+ if filetype == 'vimfiler'
+ let target = b:vimfiler.current_dir
endif
- let _ = type(args) == type([]) ? args[0] : args
- let target = type(_) == type([]) ? _[0] : _
- let target = fnamemodify(target, ':p')
- return target
+ return fnamemodify(exists('target') ? target : expand('%'), ':p')
endfunction
function! vcs#execute(list)
execute join(a:list, ' ')
endfunction
+function! vcs#system(list)
+ let result = exists('g:loaded_vimproc') ? call('vimproc#system', [join(a:list, ' ')]) : call('system', [join(a:list, ' ')])
+ return vcs#trim_cr(result)
+endfunction
+
function! vcs#escape(files)
if type(a:files) == type([])
return map(a:files, "escape(substitute(v:val, '\\', '/', 'g'), ' ')")
endif
return escape(substitute(a:files, '\\', '/', 'g'), ' ')
endfunction
-function! vcs#system(list)
- return exists('g:loaded_vimproc') ? call('vimproc#system', [join(a:list, ' ')]) : call('system', [join(a:list, ' ')])
+function! vcs#trim_cr(string)
+ return substitute(a:string, '\r', '', 'g')
endfunction
function! vcs#diff_file_with_string(path, arg)
View
4 autoload/vcs/git/add.vim
@@ -3,11 +3,11 @@ set cpo&vim
function! vcs#git#add#do(args)
let files = type(a:args) == type([]) ? a:args : [a:args]
- return substitute(vcs#system([
+ return vcs#system([
\ 'git',
\ 'add',
\ join(vcs#escape(files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo
View
5 autoload/vcs/git/cat.vim
@@ -7,9 +7,10 @@ function! vcs#git#cat#do(args)
let root = vcs#vcs('root', [target])
return substitute(vcs#system([
\ 'git',
- \ 'show',
+ \ 'cat-file',
+ \ '-p',
\ revision . ":" . vcs#escape(target[strlen(root) + 1:-1])
- \ ]), '\r', '', 'g')
+ \ ]), '\n$', '', 'g')
endfunction
let &cpo = s:save_cpo
View
4 autoload/vcs/git/commit.vim
@@ -32,14 +32,14 @@ function! s:commit()
if yesno == 'y'
call vcs#execute(['cd', root])
try
- let lines = substitute(vcs#system([
+ let lines = vcs#system([
\ 'git',
\ 'commit',
\ '--include',
\ '-F',
\ '.git/COMMIT_EDITMSG',
\ join(vcs#escape(s:files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
echomsg ' '
for line in split(lines, "\n")
View
4 autoload/vcs/git/delete.vim
@@ -3,13 +3,13 @@ set cpo&vim
function! vcs#git#delete#do(args)
let files = type(a:args) == type([]) ? a:args : [a:args]
- return substitute(vcs#system([
+ return vcs#system([
\ 'git',
\ 'rm',
\ '-f',
\ '--cached',
\ join(vcs#escape(files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo
View
16 autoload/vcs/git/revert.vim
@@ -7,35 +7,35 @@ function! vcs#git#revert#do(args)
for file in files
let status = vcs#vcs('status', [file])
if status[0].status =~ 'A'
- let result = result . '\n' . substitute(vcs#system([
+ let result = result . vcs#system([
\ 'git',
\ 'reset',
\ vcs#escape(file),
- \ ]), '\r', '', 'g')
+ \ ])
continue
endif
if status[0].status =~ 'D'
- let result = result . '\n' . substitute(vcs#system([
+ let result = result . vcs#system([
\ 'git',
\ 'reset',
\ 'HEAD',
\ vcs#escape(file),
- \ ]), '\r', '', 'g')
+ \ ])
if !filereadable(file)
- let result = result . '\n' . substitute(vcs#system([
+ let result = result . vcs#system([
\ 'git',
\ 'checkout',
\ vcs#escape(file),
- \ ])), '\r', '', 'g')
+ \ ]))
endif
continue
endif
if status[0].status =~ 'M'
- let result = result . '\n' . substitute(vcs#system([
+ let result = result . vcs#system([
\ 'git',
\ 'checkout',
\ vcs#escape(file),
- \ ]), '\r', '', 'g')
+ \ ])
continue
endif
endfor
View
4 autoload/vcs/svn/add.vim
@@ -3,11 +3,11 @@ set cpo&vim
function! vcs#svn#add#do(args)
let files = type(a:args) == type([]) ? a:args : [a:args]
- return substitute(vcs#system([
+ return vcs#system([
\ 'svn',
\ 'add',
\ join(vcs#escape(files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo
View
4 autoload/vcs/svn/cat.vim
@@ -4,13 +4,13 @@ set cpo&vim
function! vcs#svn#cat#do(args)
let target = vcs#target(a:args)
let revision = len(a:args) == 2 ? a:args[1] : 'HEAD'
- return substitute(vcs#system([
+ return vcs#system([
\ 'svn',
\ 'cat',
\ '--revision',
\ revision,
\ vcs#escape(target)
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo
View
4 autoload/vcs/svn/commit.vim
@@ -31,13 +31,13 @@ function! s:commit()
let yesno = input('commit? (y/n): ')
if yesno == 'y'
try
- let lines = substitute(vcs#system([
+ let lines = vcs#system([
\ 'svn',
\ 'commit',
\ '-F',
\ g:vcs#svn#commit_msgfile,
\ join(vcs#escape(s:files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
echomsg ' '
for line in split(lines, "\n")
View
4 autoload/vcs/svn/delete.vim
@@ -3,11 +3,11 @@ set cpo&vim
function! vcs#svn#delete#do(args)
let files = type(a:args) == type([]) ? a:args : [a:args]
- return substitute(vcs#system([
+ return vcs#system([
\ 'svn',
\ 'delete',
\ join(vcs#escape(files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo
View
4 autoload/vcs/svn/resolve.vim
@@ -4,13 +4,13 @@ set cpo&vim
function! vcs#svn#resolve#do(args)
let files = type(a:args) == type([]) ? a:args : [a:args]
let accept = input('accept? ', '')
- return substitute(vcs#system([
+ return vcs#system([
\ 'svn',
\ 'resolve',
\ '--accept',
\ accept,
\ join(vcs#escape(files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo
View
4 autoload/vcs/svn/resolved.vim
@@ -3,11 +3,11 @@ set cpo&vim
function! vcs#svn#resolved#do(args)
let files = type(a:args) == type([]) ? a:args : [a:args]
- return substitute(vcs#system([
+ return vcs#system([
\ 'svn',
\ 'resolved',
\ join(vcs#escape(files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo
View
4 autoload/vcs/svn/revert.vim
@@ -3,11 +3,11 @@ set cpo&vim
function! vcs#svn#revert#do(args)
let files = type(a:args) == type([]) ? a:args : [a:args]
- return substitute(vcs#system([
+ return vcs#system([
\ 'svn',
\ 'revert',
\ join(vcs#escape(files), ' ')
- \ ]), '\r', '', 'g')
+ \ ])
endfunction
let &cpo = s:save_cpo

0 comments on commit c3d45ca

Please sign in to comment.