/
vcs_checkouts.vim
92 lines (87 loc) · 2.61 KB
/
vcs_checkouts.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
" this may be useful for other projects.
" Thus move checking .hg .svn etc into a different file
fun! vcs_checkouts#Update(dir)
let directory = a:dir
if isdirectory(directory.'/.git')
call s:exec_in_dir([{'d': directory, 'c': 'git pull'}])
return !v:shell_error
elseif isdirectory(directory.'/.svn')
call s:exec_in_dir([{'d': directory, 'c': 'svn update'}])
return !v:shell_error
elseif isdirectory(directory.'/.hg')
call s:exec_in_dir([
\ {'d': directory, 'c': 'hg pull'},
\ {'d': directory, 'c': 'hg update'}
\ ])
if v:shell_error
throw "updating ".a:dir." falied. Got exit code: ".v:shell_error
endif
else
" not knowing how to update a repo is not a failure
return 0
endif
endf
" repository = {'type': svn|hg|git, 'url': .. }
fun! vcs_checkouts#Checkout(targetDir, repository)
if a:repository['type'] == 'git'
exec '!'.scriptmanager_util#ShellDSL('git clone $ $p', a:repository['url'], a:targetDir)
if !isdirectory(a:targetDir)
throw "Failed checking out ".a:targetDir."!"
endif
elseif a:repository['type'] == 'hg'
let parent = fnamemodify(a:targetDir,':h')
exec '!'.scriptmanager_util#ShellDSL('hg clone $ $p', a:repository['url'], a:targetDir)
if !isdirectory(a:targetDir)
throw "Failed checking out ".a:targetDir."!"
endif
elseif a:repository['type'] == 'svn'
let parent = fnamemodify(a:targetDir,':h')
call s:exec_in_dir([{'d': parent, 'c': 'svn checkout '.s:shellescape(a:repository['url']).' '.s:shellescape(a:targetDir)}])
if !isdirectory(a:targetDir)
throw "Failed checking out ".a:targetDir."!"
endif
endif
endf
fun! s:exec_in_dir(cmds)
call vcs_checkouts#ExecIndir(a:cmds)
endf
fun! vcs_checkouts#ExecIndir(cmds) abort
if g:is_win
" set different lcd in extra buffer:
new
let lcd=""
for c in a:cmds
if has_key(c, "d")
" TODO quoting
exec "lcd ".c.d
endif
if has_key(c, "c")
exec '!'.c.c
endif
" break if one of the pased commands failes:
if v:shell_error != 0
throw "error executing ".c.c
endif
endfor
" should lcd withou args be used instead?
bw!
else
" execute command sequences on linux
let cmds_str = []
for c in a:cmds
if has_key(c,"d")
call add(cmds_str, "cd ".s:shellescape(c.d))
endif
if has_key(c,"c")
call add(cmds_str, c.c)
endif
endfor
exec '!'.join(cmds_str," && ")
if v:shell_error != 0
throw "error executing ".string(cmds_str)
endif
endif
endf
fun! s:shellescape(s)
return shellescape(a:s,1)
endf