tools: fix vpm on macos, when using the bundled git executable (#21292)

This commit is contained in:
Delyan Angelov 2024-04-16 14:19:12 +03:00 committed by GitHub
parent d83d5a9580
commit 9d117fc3a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -26,7 +26,7 @@ const vcs_info = init_vcs_info() or {
} }
fn init_vcs_info() !map[VCS]VCSInfo { 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_installed_ver := semver.from(git_installed_raw_ver)!
git_submod_filter_ver := semver.from('2.36.0')! git_submod_filter_ver := semver.from('2.36.0')!
mut git_install_cmd := 'clone --depth=1 --recursive --shallow-submodules --filter=blob:none' 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 } 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()
}

10
cmd/tools/vpm/vcs_test.v Normal file
View File

@ -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'
}