Permalink
Browse files

refactor system & execute.

  • Loading branch information...
1 parent 0cfb410 commit 89db8476e9f233bb267a2eea05405abac8a1b026 @hrsh7th committed Oct 5, 2012
View
@@ -22,15 +22,26 @@ function! vcs#detect(args)
endfunction
function! vcs#vcs(command, args)
+ " detect vcs type.
let target = vcs#target(a:args)
let type = vcs#detect(target)
if type == ''
echoerr 'vcs can not detected: ' . target
return
endif
- return {'vcs#' . type . '#' . a:command . '#do'}(a:args)
+
+ " move current directory & do command.
+ let root = {'vcs#' . type . '#root#do'}(a:args)
+ let save = getcwd()
+
+ call vcs#execute(['cd', root])
+ let result = {'vcs#' . type . '#' . a:command . '#do'}(a:args)
+ call vcs#execute(['cd', save])
+
+ return result
endfunction
+" TODO: refactor.
function! vcs#target(args)
if type(a:args) == type([])
let args = a:args
@@ -54,39 +65,43 @@ function! vcs#target(args)
return target
endfunction
+function! vcs#execute(list)
+ execute join(a:list, ' ')
+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(...)
- return exists('g:loaded_vimproc') ? call('vimproc#system', a:000) : call('system', a:000)
+function! vcs#system(list)
+ return exists('g:loaded_vimproc') ? call('vimproc#system', [join(a:list, ' ')]) : call('system', [join(a:list, ' ')])
endfunction
function! vcs#diff_file_with_string(path, arg)
- exec 'tabedit ' . a:path
+ call vcs#execute(['tabedit', a:path])
diffthis
vnew
put!=a:arg.string
setlocal bufhidden=delete buftype=nofile nobuflisted noswapfile nomodifiable
- execute 'file ' . a:arg.name
+ call vcs#execute(['file', a:arg.name])
diffthis
endfunction
function! vcs#diff_string_with_string(arg1, arg2)
tabnew
put!=a:arg1.string
setlocal bufhidden=delete buftype=nofile nobuflisted noswapfile nomodifiable
- execute 'file ' . a:arg1.name
+ call vcs#execute(['file', a:arg1.name])
diffthis
vnew
put!=a:arg2.string
setlocal bufhidden=delete buftype=nofile nobuflisted noswapfile nomodifiable
- execute 'file ' . a:arg2.name
+ call vcs#execute(['file', a:arg2.name])
diffthis
endfunction
View
@@ -2,18 +2,12 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#add#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', files)
-
let files = type(a:args) == type([]) ? a:args : [a:args]
- let result = substitute(vcs#system(join([
+ return substitute(vcs#system([
\ 'git',
\ 'add',
\ join(vcs#escape(files), ' ')
- \ ], ' ')), '\r', '', 'g')
-
- exec 'cd ' . cwd
- return result
+ \ ]), '\r', '', 'g')
endfunction
let &cpo = s:save_cpo
View
@@ -2,20 +2,14 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#cat#do(args)
- let cwd = getcwd()
- exec 'cd '. vcs#vcs('root', [a:args])
-
let target = vcs#target(a:args)
let revision = len(a:args) == 2 ? a:args[1] : 'HEAD'
let root = vcs#vcs('root', [target])
- let result = substitute(vcs#system(join([
+ return substitute(vcs#system([
\ 'git',
\ 'show',
\ revision . ":" . vcs#escape(target[strlen(root) + 1:-1])
- \ ], ' ')), '\r', '', 'g')
-
- exec 'cd ' . cwd
- return result
+ \ ]), '\r', '', 'g')
endfunction
let &cpo = s:save_cpo
@@ -2,28 +2,22 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#changeset#do(args)
- let cwd = getcwd()
- exec 'cd '. vcs#vcs('root', [a:args])
-
let target = vcs#target(a:args)
let revision = len(a:args) == 2 ? a:args[1] : 'HEAD'
let str = s:system(target, revision)
let list = s:str2list(str)
- let result = s:parse(target, revision, list)
-
- exec 'cd ' . cwd
- return result
+ return s:parse(target, revision, list)
endfunction
function! s:system(target, revision)
- return vcs#system(join([
+ return vcs#system([
\ 'git',
\ 'log',
\ '--name-status',
\ '--pretty=format:"' . g:vcs#git#log_format . '"',
\ '-1',
\ a:revision,
- \ ], ' '))
+ \ ])
endfunction
function! s:str2list(str)
@@ -2,21 +2,16 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#commit#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', a:args)
-
let files = type(a:args) == type([]) ? a:args : [a:args]
" TODO: collect windows.
- exec join([
+ call vcs#execute([
\ '!',
\ 'export EDITOR=vim;',
\ 'git',
\ 'commit',
\ '--include',
\ join(vcs#escape(files), ' ')
- \ ], ' ')
-
- exec 'cd ' . cwd
+ \ ])
endfunction
let &cpo = s:save_cpo
@@ -2,20 +2,14 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#delete#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', a:args)
-
let files = type(a:args) == type([]) ? a:args : [a:args]
- let result = substitute(vcs#system(join([
+ return substitute(vcs#system([
\ 'git',
\ 'rm',
\ '-f',
\ '--cached',
\ join(vcs#escape(files), ' ')
- \ ], ' ')), '\r', '', 'g')
-
- exec 'cd ' . cwd
- return result
+ \ ]), '\r', '', 'g')
endfunction
let &cpo = s:save_cpo
View
@@ -2,25 +2,19 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#log#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', a:args)
-
let target = vcs#target(a:args)
let str = s:system(target)
let list = s:str2list(str)
- let result = s:parse(target, list)
-
- exec 'cd ' . cwd
- return result
+ return s:parse(target, list)
endfunction
function! s:system(target)
- return vcs#system(join([
+ return vcs#system([
\ 'git',
\ 'log',
\ '--pretty=format:"' . g:vcs#git#log_format . '"',
\ vcs#escape(a:target)
- \ ], ' '))
+ \ ])
endfunction
function! s:str2list(str)
@@ -2,30 +2,27 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#revert#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', a:args)
-
let files = type(a:args) == type([]) ? a:args : [a:args]
let result = ''
for file in files
let status = vcs#vcs('status', [file])
if status[0].status =~ 'A'
- let result = result . '\n' . substitute(vcs#system(join([
+ let result = result . '\n' . substitute(vcs#system([
\ 'git',
\ 'reset',
\ vcs#escape(file),
- \ ], ' ')), '\r', '', 'g')
+ \ ]), '\r', '', 'g')
continue
endif
if status[0].status =~ 'D'
- let result = result . '\n' . substitute(vcs#system(join([
+ let result = result . '\n' . substitute(vcs#system([
\ 'git',
\ 'reset',
\ 'HEAD',
\ vcs#escape(file),
- \ ], ' ')), '\r', '', 'g')
+ \ ]), '\r', '', 'g')
if !filereadable(file)
- let result = result . '\n' . substitute(vcs#system(join([
+ let result = result . '\n' . substitute(vcs#system([
\ 'git',
\ 'checkout',
\ vcs#escape(file),
@@ -34,16 +31,14 @@ function! vcs#git#revert#do(args)
continue
endif
if status[0].status =~ 'M'
- let result = result . '\n' . substitute(vcs#system(join([
+ let result = result . '\n' . substitute(vcs#system([
\ 'git',
\ 'checkout',
\ vcs#escape(file),
- \ ], ' ')), '\r', '', 'g')
+ \ ]), '\r', '', 'g')
continue
endif
endfor
-
- exec 'cd ' . cwd
return result
endfunction
@@ -2,26 +2,20 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#git#status#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', a:args)
-
let target = vcs#target(a:args)
let str = s:system(target)
let list = s:str2list(str)
let list = s:extract(list)
- let result = s:parse(target, list)
-
- exec 'cd ' . cwd
- return result
+ return s:parse(target, list)
endfunction
function! s:system(target)
- return vcs#system(join([
+ return vcs#system([
\ 'git',
\ 'status',
\ '--short',
\ vcs#escape(a:target)
- \ ], ' '))
+ \ ])
endfunction
function! s:str2list(str)
View
@@ -2,18 +2,12 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#svn#add#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', a:args)
-
let files = type(a:args) == type([]) ? a:args : [a:args]
- let result = substitute(vcs#system(join([
+ return substitute(vcs#system([
\ 'svn',
\ 'add',
\ join(vcs#escape(files), ' ')
- \ ], ' ')), '\r', '', 'g')
-
- exec 'cd ' . cwd
- return result
+ \ ]), '\r', '', 'g')
endfunction
let &cpo = s:save_cpo
View
@@ -2,21 +2,15 @@ let s:save_cpo = &cpo
set cpo&vim
function! vcs#svn#cat#do(args)
- let cwd = getcwd()
- exec 'cd ' . vcs#vcs('root', a:args)
-
let target = vcs#target(a:args)
let revision = len(a:args) == 2 ? a:args[1] : 'HEAD'
- let result = substitute(vcs#system(join([
+ return substitute(vcs#system([
\ 'svn',
\ 'cat',
\ '--revision',
\ revision,
\ vcs#escape(target)
- \ ], ' ')), '\r', '', 'g')
-
- exec 'cd ' . cwd
- return result
+ \ ]), '\r', '', 'g')
endfunction
let &cpo = s:save_cpo
Oops, something went wrong.

0 comments on commit 89db847

Please sign in to comment.