From be89cbf26f751c30833e6bea0a577791f94879d8 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Fri, 3 Nov 2023 19:25:46 +0100 Subject: [PATCH] vpm.tools: cleanup and make handling of the different VCS systems more robust (#19740) --- cmd/tools/vpm/common.v | 28 +++++++++------------ cmd/tools/vpm/install.v | 27 ++++++++++----------- cmd/tools/vpm/update.v | 12 ++++----- cmd/tools/vpm/vpm.v | 54 ++++++++++++++++++++--------------------- 4 files changed, 57 insertions(+), 64 deletions(-) diff --git a/cmd/tools/vpm/common.v b/cmd/tools/vpm/common.v index ac367383a1..9cb80baac3 100644 --- a/cmd/tools/vpm/common.v +++ b/cmd/tools/vpm/common.v @@ -36,12 +36,11 @@ fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModDateInfo { } final_module_path := valid_final_path_of_existing_module(result.name) or { return result } vcs := vcs_used_in_dir(final_module_path) or { return result } - is_hg := vcs[0] == 'hg' - vcs_cmd_steps := supported_vcs_outdated_steps[vcs[0]] + is_hg := vcs.cmd == 'hg' mut outputs := []string{} - for step in vcs_cmd_steps { + for step in vcs.outdated_steps { path_flag := if is_hg { '-R' } else { '-C' } - cmd := '${vcs[0]} ${path_flag} "${final_module_path}" ${step}' + cmd := '${vcs.cmd} ${path_flag} "${final_module_path}" ${step}' res := os.execute('${cmd}') if res.exit_code < 0 { verbose_println('Error command: ${cmd}') @@ -55,7 +54,7 @@ fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModDateInfo { } outputs << res.output } - // vcs[0] == 'git' + // vcs.cmd == 'git' if !is_hg && outputs[1] != outputs[2] { result.outdated = true } @@ -229,8 +228,8 @@ fn ensure_vmodules_dir_exist() { } } -fn ensure_vcs_is_installed(vcs string) ! { - cmd := '${vcs} ${supported_vcs_version_cmds[vcs]}' +fn ensure_vcs_is_installed(vcs &VCS) ! { + cmd := '${vcs.cmd} --version' verbose_println(' command: ${cmd}') res := os.execute(cmd) if res.exit_code != 0 { @@ -284,18 +283,13 @@ fn url_to_module_name(modulename string) string { return res } -fn vcs_used_in_dir(dir string) ?[]string { - mut vcs := []string{} - for repo_subfolder in supported_vcs_folders { - checked_folder := os.real_path(os.join_path(dir, repo_subfolder)) - if os.is_dir(checked_folder) { - vcs << repo_subfolder.replace('.', '') +fn vcs_used_in_dir(dir string) ?VCS { + for vcs in supported_vcs.values() { + if os.is_dir(os.real_path(os.join_path(dir, vcs.dir))) { + return vcs } } - if vcs.len == 0 { - return none - } - return vcs + return none } fn valid_final_path_of_existing_module(modulename string) ?string { diff --git a/cmd/tools/vpm/install.v b/cmd/tools/vpm/install.v index 1c724b9fc9..9572989561 100644 --- a/cmd/tools/vpm/install.v +++ b/cmd/tools/vpm/install.v @@ -79,13 +79,13 @@ fn vpm_install(requested_modules []string, opts []string) { vpm_install_from_vpm(vpm_modules) } if external_modules.len > 0 { - source := if '--hg' in opts { Source.hg } else { Source.git } - vpm_install_from_vcs(external_modules, source) + vcs := if '--hg' in opts { supported_vcs['hd'] } else { supported_vcs['git'] } + vpm_install_from_vcs(external_modules, vcs) } } -fn install_module(vcs string, name string, url string, final_module_path string) ! { - cmd := '${vcs} ${supported_vcs_install_cmds[vcs]} "${url}" "${final_module_path}"' +fn install_module(vcs &VCS, name string, url string, final_module_path string) ! { + cmd := '${vcs.cmd} ${vcs.install_arg} "${url}" "${final_module_path}"' verbose_println(' command: ${cmd}') println('Installing module "${name}" from "${url}" to "${final_module_path}" ...') res := os.execute(cmd) @@ -105,14 +105,14 @@ fn vpm_install_from_vpm(module_names []string) { eprintln(err) continue } - mut vcs := mod.vcs - if vcs == '' { - vcs = supported_vcs_systems[0] - } - if vcs !in supported_vcs_systems { - errors++ - eprintln('Skipping module "${name}", since it uses an unsupported VCS {${vcs}} .') - continue + vcs := if mod.vcs != '' { + supported_vcs[mod.vcs] or { + errors++ + eprintln('Skipping module "${name}", since it uses an unsupported VCS {${mod.vcs}} .') + continue + } + } else { + supported_vcs['git'] } ensure_vcs_is_installed(vcs) or { errors++ @@ -140,8 +140,7 @@ fn vpm_install_from_vpm(module_names []string) { } } -fn vpm_install_from_vcs(modules []string, vcs_key Source) { - vcs := vcs_key.str() +fn vpm_install_from_vcs(modules []string, vcs &VCS) { mut errors := 0 for raw_url in modules { url := urllib.parse(raw_url) or { diff --git a/cmd/tools/vpm/update.v b/cmd/tools/vpm/update.v index f709827278..223a11ed05 100644 --- a/cmd/tools/vpm/update.v +++ b/cmd/tools/vpm/update.v @@ -18,14 +18,14 @@ fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo { zname := url_to_module_name(result.name) result.final_path = valid_final_path_of_existing_module(result.name) or { return result } println('Updating module "${zname}" in "${result.final_path}" ...') - vcs := vcs_used_in_dir(result.final_path) or { return result }[0] + vcs := vcs_used_in_dir(result.final_path) or { return result } ensure_vcs_is_installed(vcs) or { result.has_err = true eprintln(err) return result } - path_flag := if vcs == 'hg' { '-R' } else { '-C' } - cmd := '${vcs} ${path_flag} "${result.final_path}" ${supported_vcs_update_cmds[vcs]}' + path_flag := if vcs.cmd == 'hg' { '-R' } else { '-C' } + cmd := '${vcs.cmd} ${path_flag} "${result.final_path}" ${vcs.update_arg}' verbose_println(' command: ${cmd}') res := os.execute('${cmd}') if res.exit_code != 0 { @@ -70,14 +70,14 @@ fn vpm_update_verbose(module_names []string) { zname := url_to_module_name(name) final_module_path := valid_final_path_of_existing_module(name) or { continue } println('Updating module "${zname}" in "${final_module_path}" ...') - vcs := vcs_used_in_dir(final_module_path) or { continue }[0] + vcs := vcs_used_in_dir(final_module_path) or { continue } ensure_vcs_is_installed(vcs) or { errors++ eprintln(err) continue } - path_flag := if vcs == 'hg' { '-R' } else { '-C' } - cmd := '${vcs} ${path_flag} "${final_module_path}" ${supported_vcs_update_cmds[vcs]}' + path_flag := if vcs.cmd == 'hg' { '-R' } else { '-C' } + cmd := '${vcs.cmd} ${path_flag} "${final_module_path}" ${vcs.update_arg}' verbose_println(' command: ${cmd}') res := os.execute('${cmd}') if res.exit_code != 0 { diff --git a/cmd/tools/vpm/vpm.v b/cmd/tools/vpm/vpm.v index f3a50c9cd2..ff4c274c3e 100644 --- a/cmd/tools/vpm/vpm.v +++ b/cmd/tools/vpm/vpm.v @@ -17,36 +17,36 @@ mut: vmodules_path string } -enum Source { - git - hg - vpm +struct VCS { + cmd string + dir string + update_arg string + install_arg string + outdated_steps []string } const ( - settings = &VpmSettings{} - default_vpm_server_urls = ['https://vpm.vlang.io', 'https://vpm.url4e.com'] - vpm_server_urls = rand.shuffle_clone(default_vpm_server_urls) or { [] } // ensure that all queries are distributed fairly - valid_vpm_commands = ['help', 'search', 'install', 'update', 'upgrade', 'outdated', - 'list', 'remove', 'show'] - excluded_dirs = ['cache', 'vlib'] - supported_vcs_systems = ['git', 'hg'] - supported_vcs_folders = ['.git', '.hg'] - supported_vcs_update_cmds = { - 'git': 'pull --recurse-submodules' // pulling with `--depth=1` leads to conflicts, when the upstream is more than 1 commit newer - 'hg': 'pull --update' - } - supported_vcs_install_cmds = { - 'git': 'clone --depth=1 --recursive --shallow-submodules' - 'hg': 'clone' - } - supported_vcs_outdated_steps = { - 'git': ['fetch', 'rev-parse @', 'rev-parse @{u}'] - 'hg': ['incoming'] - } - supported_vcs_version_cmds = { - 'git': 'version' - 'hg': 'version' + settings = &VpmSettings{} + default_vpm_server_urls = ['https://vpm.vlang.io', 'https://vpm.url4e.com'] + vpm_server_urls = rand.shuffle_clone(default_vpm_server_urls) or { [] } // ensure that all queries are distributed fairly + valid_vpm_commands = ['help', 'search', 'install', 'update', 'upgrade', 'outdated', 'list', + 'remove', 'show'] + excluded_dirs = ['cache', 'vlib'] + supported_vcs = { + 'git': VCS{ + cmd: 'git' + dir: '.git' + update_arg: 'pull --recurse-submodules' // pulling with `--depth=1` leads to conflicts, when the upstream is more than 1 commit newer + install_arg: 'clone --depth=1 --recursive --shallow-submodules' + outdated_steps: ['fetch', 'rev-parse @', 'rev-parse @{u}'] + } + 'hg': VCS{ + cmd: 'hg' + dir: '.hg' + update_arg: 'pull --update' + install_arg: 'clone' + outdated_steps: ['incoming'] + } } )