From cc7c8009a1dd1720e2d7d36c2b075d1b56f11f87 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Wed, 15 Jul 2020 21:55:07 +0200 Subject: [PATCH] vpm: outdated command (#5833) --- cmd/tools/vpm.v | 58 ++++++++++++++++++++++++++++++++++++++++-- cmd/v/help/default.txt | 1 + cmd/v/v.v | 2 +- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/cmd/tools/vpm.v b/cmd/tools/vpm.v index 8c4d2639f4..acec3e7c32 100644 --- a/cmd/tools/vpm.v +++ b/cmd/tools/vpm.v @@ -9,7 +9,7 @@ import v.vmod const ( default_vpm_server_urls = ['https://vpm.best', 'https://vpm.vlang.io'] - valid_vpm_commands = ['help', 'search', 'install', 'update', 'remove'] + valid_vpm_commands = ['help', 'search', 'install', 'update', 'outdated', 'remove'] excluded_dirs = ['cache', 'vlib'] supported_vcs_systems = ['git', 'hg'] supported_vcs_folders = ['.git', '.hg'] @@ -21,6 +21,9 @@ const ( 'git': 'git clone --depth=1' 'hg': 'hg clone' } + supported_vcs_outdate_cmds = { + 'git': ['git rev-parse @{u}', 'git rev-parse @'] + } ) struct Mod { @@ -73,6 +76,9 @@ fn main() { 'update' { vpm_update(module_names) } + 'outdated' { + vpm_outdated() + } 'remove' { vpm_remove(module_names) } @@ -120,7 +126,6 @@ fn vpm_search(keywords []string) { break } } - if index == 0 { println('No module(s) found for "$joined"') } else { @@ -229,6 +234,55 @@ fn vpm_update(m []string) { } } +fn vpm_outdated() { + module_names := get_installed_modules() + mut errors := 0 + mut outdated := []string{} + for name in module_names { + final_module_path := valid_final_path_of_existing_module(name) or { + continue + } + os.chdir(final_module_path) + vcs := vcs_used_in_dir(final_module_path) or { + continue + } + if vcs[0] != 'git' { + println('Listing outdated modules is not supported with VCS {$vcs}.') + exit(1) + } + local_vcs_cmd := supported_vcs_outdate_cmds[vcs[0]][0] + local_res := os.exec(local_vcs_cmd) or { + errors++ + println('Could not get local commit sha of "$name".') + verbose_println('Error command: $local_vcs_cmd') + verbose_println('Error details:\n$err') + continue + } + upstream_vcs_cmd := supported_vcs_outdate_cmds[vcs[0]][1] + upstream_res := os.exec(upstream_vcs_cmd) or { + errors++ + println('Could not read upstream commit sha of "$name".') + verbose_println('Error command: $upstream_vcs_cmd') + verbose_println('Error details:\n$err') + continue + } + if local_res.output != upstream_res.output { + outdated << name + } + } + if outdated.len > 0 { + println('Outdated modules:') + for m in outdated { + println(' $m') + } + } else { + println('Modules are up to date.') + } + if errors > 0 { + exit(1) + } +} + fn vpm_remove(module_names []string) { if settings.is_help { vhelp.show_topic('remove') diff --git a/cmd/v/help/default.txt b/cmd/v/help/default.txt index 02b73e891b..d8ea51512b 100644 --- a/cmd/v/help/default.txt +++ b/cmd/v/help/default.txt @@ -31,6 +31,7 @@ V supports the following commands: remove Remove a module that was installed from VPM. search Search for a module from VPM. update Update an installed module from VPM. + outdated List installed modules that need updates. * Others: build Build a V code in the provided path (the default, so you can skip the word `build`). translate Translate C code to V (coming soon in 0.3). diff --git a/cmd/v/v.v b/cmd/v/v.v index 6565d80a6d..13122a2064 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -70,7 +70,7 @@ fn main() { println('Translating C to V will be available in V 0.3') return } - 'search', 'install', 'update', 'remove' { + 'search', 'install', 'update', 'outdated', 'remove' { util.launch_tool(prefs.is_verbose, 'vpm', os.args[1..]) return }