Skip to content

Commit

Permalink
v: add @VMODHASH comptime variable to store the commit sha of a V m…
Browse files Browse the repository at this point in the history
…odule (#21091)
  • Loading branch information
ttytm committed Apr 5, 2024
1 parent 67db38a commit 254250d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/docs.md
Expand Up @@ -5732,6 +5732,8 @@ that are substituted at compile time:
recompiled on a different commit (after local modifications, or after
using git bisect etc).
- `@VMOD_FILE` => replaced with the contents of the nearest v.mod file (as a string).
- `@VMODHASH` => is replaced by the shortened commit hash, derived from the .git directory
next to the nearest v.mod file (as a string).
- `@VMODROOT` => will be substituted with the *folder*,
where the nearest v.mod file is (as a string).
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/checker/checker.v
Expand Up @@ -3518,6 +3518,19 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {
vmod_file_location := mcache.get_by_file(c.file.path)
node.val = os.dir(vmod_file_location.vmod_file)
}
.vmod_hash {
mut mcache := vmod.get_cache()
vmod_file_location := mcache.get_by_file(c.file.path)
if vmod_file_location.vmod_file.len == 0 {
c.error('@VMODHASH can only be used in projects that have a v.mod file',
node.pos)
}
hash := version.githash(os.dir(vmod_file_location.vmod_file)) or {
c.error(err.msg(), node.pos)
''
}
node.val = hash
}
.unknown {
c.error('unknown @ identifier: ${node.name}. Available identifiers: ${token.valid_at_tokens}',
node.pos)
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/comptime.v
Expand Up @@ -402,6 +402,7 @@ fn (mut p Parser) at() ast.AtExpr {
'@VEXE' { token.AtKind.vexe_path }
'@VEXEROOT' { token.AtKind.vexeroot_path }
'@VMODROOT' { token.AtKind.vmodroot_path }
'@VMODHASH' { token.AtKind.vmod_hash }
'@VROOT' { token.AtKind.vroot_path } // deprecated, use @VEXEROOT or @VMODROOT
else { token.AtKind.unknown }
}
Expand Down
5 changes: 3 additions & 2 deletions vlib/v/token/token.v
Expand Up @@ -176,6 +176,7 @@ pub enum AtKind {
v_current_hash
vmod_file
vmodroot_path
vmod_hash
vroot_path // obsolete
vexeroot_path
file_path_line_nr
Expand All @@ -187,8 +188,8 @@ pub const assign_tokens = [Kind.assign, .plus_assign, .minus_assign, .mult_assig
.unsigned_right_shift_assign]

pub const valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT',
'@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@FILE_LINE',
'@LOCATION']
'@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@VMODHASH',
'@FILE_LINE', '@LOCATION']

pub const token_str = build_token_str()

Expand Down
4 changes: 2 additions & 2 deletions vlib/v/util/version/version_test.v
Expand Up @@ -27,12 +27,12 @@ fn test_githash() {
os.write_file('v.mod', '')!
os.execute_opt('git add .')!
os.execute_opt('git commit -m "test1"')!
test_rev := os.execute_opt('git rev-parse --short HEAD')!.output.trim_space()
test_rev := os.execute_opt('git rev-parse --short=7 HEAD')!.output.trim_space()
assert test_rev == version.githash(git_proj_path)!
os.write_file('README.md', '')!
os.execute_opt('git add .')!
os.execute_opt('git commit -m "test2"')!
test_rev2 := os.execute_opt('git rev-parse --short HEAD')!.output.trim_space()
test_rev2 := os.execute_opt('git rev-parse --short=7 HEAD')!.output.trim_space()
assert test_rev2 != test_rev
assert test_rev2 == version.githash(git_proj_path)!
}

0 comments on commit 254250d

Please sign in to comment.