Skip to content

Commit 77e65ac

Browse files
authored
tools.vpm: update vcs handling (#20090)
1 parent 700fbd7 commit 77e65ac

File tree

7 files changed

+82
-69
lines changed

7 files changed

+82
-69
lines changed

cmd/tools/vpm/common.v

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,24 @@ fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModuleDateInf
3939
}
4040
path := get_path_of_existing_module(result.name) or { return result }
4141
vcs := vcs_used_in_dir(path) or { return result }
42-
is_hg := vcs.cmd == 'hg'
42+
args := vcs_info[vcs].args
4343
mut outputs := []string{}
44-
for step in vcs.args.outdated {
45-
cmd := '${vcs.cmd} ${vcs.args.path} "${path}" ${step}'
44+
for step in args.outdated {
45+
cmd := [vcs.str(), args.path, os.quoted_path(path), step].join(' ')
4646
res := os.execute(cmd)
4747
if res.exit_code < 0 {
4848
verbose_println('Error command: ${cmd}')
4949
verbose_println('Error details:\n${res.output}')
5050
result.exec_err = true
5151
return result
5252
}
53-
if is_hg && res.exit_code == 1 {
53+
if vcs == .hg && res.exit_code == 1 {
5454
result.outdated = true
5555
return result
5656
}
5757
outputs << res.output
5858
}
59-
// vcs.cmd == 'git'
60-
if !is_hg && outputs[1] != outputs[2] {
59+
if vcs == .git && outputs[1] != outputs[2] {
6160
result.outdated = true
6261
}
6362
return result
@@ -217,10 +216,6 @@ fn get_path_of_existing_module(mod_name string) ?string {
217216
vpm_error('skipping `${path}`, since it is not a directory.')
218217
return none
219218
}
220-
vcs_used_in_dir(path) or {
221-
vpm_error('skipping `${path}`, since it uses an unsupported version control system.')
222-
return none
223-
}
224219
return path
225220
}
226221

@@ -252,12 +247,6 @@ fn ensure_vmodules_dir_exist() {
252247
}
253248
}
254249

255-
fn (vcs &VCS) is_executable() ! {
256-
os.find_abs_path_of_executable(vcs.cmd) or {
257-
return error('VPM needs `${vcs.cmd}` to be installed.')
258-
}
259-
}
260-
261250
fn increment_module_download_count(name string) ! {
262251
if settings.no_dl_count_increment {
263252
println('Skipping download count increment for `${name}`.')
@@ -296,15 +285,6 @@ fn resolve_dependencies(manifest ?vmod.Manifest, modules []string) {
296285
}
297286
}
298287

299-
fn vcs_used_in_dir(dir string) ?VCS {
300-
for vcs in supported_vcs.values() {
301-
if os.is_dir(os.real_path(os.join_path(dir, vcs.dir))) {
302-
return vcs
303-
}
304-
}
305-
return none
306-
}
307-
308288
fn verbose_println(msg string) {
309289
if settings.is_verbose {
310290
println(msg)

cmd/tools/vpm/install.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ fn (m Module) install() InstallResult {
121121
}
122122
}
123123
vcs := m.vcs or { settings.vcs }
124-
version_opt := if m.version != '' { ' ${vcs.args.version} ${m.version}' } else { '' }
125-
cmd := '${vcs.cmd} ${vcs.args.install}${version_opt} "${m.url}" "${m.install_path}"'
124+
args := vcs_info[vcs].args
125+
version_opt := if m.version != '' { '${args.version} ${m.version}' } else { '' }
126+
cmd := [vcs.str(), args.install, version_opt, m.url, os.quoted_path(m.install_path)].join(' ')
126127
vpm_log(@FILE_LINE, @FN, 'command: ${cmd}')
127128
println('Installing `${m.name}`...')
128129
verbose_println(' cloning from `${m.url}` to `${m.install_path_fmted}`')

cmd/tools/vpm/parse.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mut:
2828

2929
fn parse_query(query []string) []Module {
3030
mut p := Parser{
31-
is_git_setting: settings.vcs.cmd == 'git'
31+
is_git_setting: settings.vcs == .git
3232
}
3333
for m in query {
3434
p.parse_module(m)
@@ -96,20 +96,20 @@ fn (mut p Parser) parse_module(m string) {
9696
// Verify VCS.
9797
mut is_git_module := true
9898
vcs := if info.vcs != '' {
99-
info_vcs := supported_vcs[info.vcs] or {
99+
info_vcs := vcs_from_str(info.vcs) or {
100100
vpm_error('skipping `${info.name}`, since it uses an unsupported version control system `${info.vcs}`.')
101101
p.errors++
102102
return
103103
}
104-
is_git_module = info.vcs == 'git'
104+
is_git_module = info_vcs == .git
105105
if !is_git_module && version != '' {
106106
vpm_error('skipping `${info.name}`, version installs are currently only supported for projects using `git`.')
107107
p.errors++
108108
return
109109
}
110110
info_vcs
111111
} else {
112-
supported_vcs['git']
112+
VCS.git
113113
}
114114
vcs.is_executable() or {
115115
vpm_error(err.msg())

cmd/tools/vpm/settings.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn init_settings() VpmSettings {
3535
is_verbose: '-v' in opts || '--verbose' in opts
3636
is_force: '-f' in opts || '--force' in opts
3737
server_urls: cmdline.options(args, '--server-urls')
38-
vcs: supported_vcs[if '--hg' in opts { 'hg' } else { 'git' }]
38+
vcs: if '--hg' in opts { .hg } else { .git }
3939
vmodules_path: os.vmodules_dir()
4040
no_dl_count_increment: os.getenv('CI') != '' || (no_inc_env != '' && no_inc_env != '0')
4141
fail_on_prompt: os.getenv('VPM_FAIL_ON_PROMPT') != ''

cmd/tools/vpm/update.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &UpdateResult {
5454
vpm_error(err.msg())
5555
return &UpdateResult{}
5656
}
57-
cmd := '${vcs.cmd} ${vcs.args.path} "${install_path}" ${vcs.args.update}'
57+
args := vcs_info[vcs].args
58+
cmd := [vcs.str(), args.path, os.quoted_path(install_path), args.update].join(' ')
5859
vpm_log(@FILE_LINE, @FN, 'cmd: ${cmd}')
5960
println('Updating module `${name}` in `${fmt_mod_path(install_path)}`...')
6061
res := os.execute_opt(cmd) or {

cmd/tools/vpm/vcs.v

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module main
2+
3+
import os
4+
5+
// Supported version control system commands.
6+
enum VCS {
7+
git
8+
hg
9+
}
10+
11+
struct VCSInfo {
12+
dir string @[required]
13+
args struct {
14+
install string @[required]
15+
version string @[required] // flag to specify a version, added to install.
16+
path string @[required] // flag to specify a path. E.g., used to explicitly work on a path during multithreaded updating.
17+
update string @[required]
18+
outdated []string @[required]
19+
}
20+
}
21+
22+
const vcs_info = {
23+
VCS.git: VCSInfo{
24+
dir: '.git'
25+
args: struct {
26+
install: 'clone --depth=1 --recursive --shallow-submodules'
27+
version: '--single-branch -b'
28+
update: 'pull --recurse-submodules' // pulling with `--depth=1` leads to conflicts when the upstream has more than 1 new commits.
29+
path: '-C'
30+
outdated: ['fetch', 'rev-parse @', 'rev-parse @{u}']
31+
}
32+
}
33+
VCS.hg: VCSInfo{
34+
dir: '.hg'
35+
args: struct {
36+
install: 'clone'
37+
version: '' // not supported yet.
38+
update: 'pull --update'
39+
path: '-R'
40+
outdated: ['incoming']
41+
}
42+
}
43+
}
44+
45+
fn (vcs &VCS) is_executable() ! {
46+
cmd := vcs.str()
47+
os.find_abs_path_of_executable(cmd) or {
48+
return error('VPM requires that `${cmd}` is executable.')
49+
}
50+
}
51+
52+
fn vcs_used_in_dir(dir string) ?VCS {
53+
for vcs, info in vcs_info {
54+
if os.is_dir(os.real_path(os.join_path(dir, info.dir))) {
55+
return vcs
56+
}
57+
}
58+
return none
59+
}
60+
61+
fn vcs_from_str(str string) ?VCS {
62+
return match str {
63+
'git' { .git }
64+
'hg' { .hg }
65+
else { none }
66+
}
67+
}

cmd/tools/vpm/vpm.v

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,12 @@ import rand
99
import v.help
1010
import v.vmod
1111

12-
struct VCS {
13-
dir string @[required]
14-
cmd string @[required]
15-
args struct {
16-
install string @[required]
17-
version string @[required] // flag to specify a version, added to install.
18-
path string @[required] // flag to specify a path. E.g., used to explicitly work on a path during multithreaded updating.
19-
update string @[required]
20-
outdated []string @[required]
21-
}
22-
}
23-
2412
const settings = init_settings()
2513
const default_vpm_server_urls = ['https://vpm.vlang.io', 'https://vpm.url4e.com']
2614
const vpm_server_urls = rand.shuffle_clone(default_vpm_server_urls) or { [] } // ensure that all queries are distributed fairly
2715
const valid_vpm_commands = ['help', 'search', 'install', 'update', 'upgrade', 'outdated', 'list',
2816
'remove', 'show']
2917
const excluded_dirs = ['cache', 'vlib']
30-
const supported_vcs = {
31-
'git': VCS{
32-
dir: '.git'
33-
cmd: 'git'
34-
args: struct {
35-
install: 'clone --depth=1 --recursive --shallow-submodules'
36-
version: '--single-branch -b'
37-
update: 'pull --recurse-submodules' // pulling with `--depth=1` leads to conflicts when the upstream has more than 1 new commits.
38-
path: '-C'
39-
outdated: ['fetch', 'rev-parse @', 'rev-parse @{u}']
40-
}
41-
}
42-
'hg': VCS{
43-
dir: '.hg'
44-
cmd: 'hg'
45-
args: struct {
46-
install: 'clone'
47-
version: '' // not supported yet.
48-
update: 'pull --update'
49-
path: '-R'
50-
outdated: ['incoming']
51-
}
52-
}
53-
}
5418

5519
fn main() {
5620
// This tool is intended to be launched by the v frontend,

0 commit comments

Comments
 (0)