diff --git a/cmd/tools/vpm/vcs.v b/cmd/tools/vpm/vcs.v index 1117dad880..d2e32e33a8 100644 --- a/cmd/tools/vpm/vcs.v +++ b/cmd/tools/vpm/vcs.v @@ -26,7 +26,7 @@ const vcs_info = init_vcs_info() or { } fn init_vcs_info() !map[VCS]VCSInfo { - git_installed_raw_ver := os.execute_opt('git --version')!.output.all_after_last(' ').all_before('.windows').trim_space() + git_installed_raw_ver := parse_git_version(os.execute_opt('git --version')!.output) or { '' } git_installed_ver := semver.from(git_installed_raw_ver)! git_submod_filter_ver := semver.from('2.36.0')! mut git_install_cmd := 'clone --depth=1 --recursive --shallow-submodules --filter=blob:none' @@ -89,3 +89,17 @@ fn vcs_from_str(str string) ?VCS { else { none } } } + +// parse_git_version retrieves only the stable version part of the output of `git version`. +// For example: parse_git_version('git version 2.39.3')! will return just '2.39.3'. +pub fn parse_git_version(version string) !string { + git_version_start := 'git version ' + // The output from `git version` varies, depending on how git was compiled. Here are some examples: + // `git version 2.44.0` when compiled from source, or from brew on macos. + // `git version 2.39.3 (Apple Git-146)` on macos with XCode's cli tools. + // `git version 2.44.0.windows.1` on windows's Git Bash shell. + if !version.starts_with(git_version_start) { + return error('should start with `${git_version_start}`') + } + return version.all_after(git_version_start).all_before(' ').all_before('.windows').trim_space() +} diff --git a/cmd/tools/vpm/vcs_test.v b/cmd/tools/vpm/vcs_test.v new file mode 100644 index 0000000000..107d99b95c --- /dev/null +++ b/cmd/tools/vpm/vcs_test.v @@ -0,0 +1,10 @@ +module main + +fn test_parse_git_version() { + if _ := parse_git_version('abcd') { + assert false + } + assert parse_git_version('git version 2.44.0.windows.1')! == '2.44.0' + assert parse_git_version('git version 2.34.0')! == '2.34.0' + assert parse_git_version('git version 2.39.3 (Apple Git-146)')! == '2.39.3' +}