Skip to content

Commit

Permalink
simplify utils.version.githash(), prep @VMODHASH implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 25, 2024
1 parent 6b23b1d commit 7fb7a48
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
4 changes: 2 additions & 2 deletions cmd/tools/vup.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ fn main() {
os.chdir(app.vroot)!
println('Updating V...')
app.update_from_master()
v_hash := version.githash(false)
current_hash := version.githash(true)
v_hash := @VCURRENTHASH
current_hash := version.githash(vroot, true)
// println(v_hash)
// println(current_hash)
if v_hash == current_hash && !app.skip_current {
Expand Down
6 changes: 5 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker {
pref: pref_
timers: util.new_timers(should_print: timers_should_print, label: 'checker')
match_exhaustive_cutoff_limit: pref_.checker_match_exhaustive_cutoff_limit
v_current_commit_hash: version.githash(pref_.building_v)
v_current_commit_hash: if pref_.building_v {
version.githash(os.dir(pref.vexe_path()), true)
} else {
@VCURRENTHASH
}
}
checker.comptime = &comptime.ComptimeInfo{
resolver: checker
Expand Down
61 changes: 25 additions & 36 deletions vlib/v/util/version/version.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn vhash() string {

pub fn full_hash() string {
build_hash := vhash()
current_hash := githash(false)
current_hash := @VCURRENTHASH
if build_hash == current_hash {
return build_hash
}
Expand All @@ -29,7 +29,7 @@ pub fn full_v_version(is_verbose bool) string {
if is_verbose {
return 'V ${version.v_version} ${full_hash()}'
}
hash := githash(false)
hash := @VCURRENTHASH
return 'V ${version.v_version} ${hash}'
}

Expand All @@ -41,40 +41,29 @@ pub fn full_v_version(is_verbose bool) string {
// defaults to getting the predefined C constant again.
// Note: githash(true) must be called only when v detects that it builds itself.
// For all other programs, githash(false) should be used.
pub fn githash(should_get_from_filesystem bool) string {
for {
// The `for` construct here is used as a goto substitute.
// The code in this function will break out of the `for`
// if it detects an error and can not continue.
if should_get_from_filesystem {
vexe := os.getenv('VEXE')
vroot := os.dir(vexe)
// .git/HEAD
git_head_file := os.join_path(vroot, '.git', 'HEAD')
if !os.exists(git_head_file) {
break
}
// 'ref: refs/heads/master' ... the current branch name
head_content := os.read_file(git_head_file) or { break }
mut current_branch_hash := head_content
if head_content.starts_with('ref: ') {
gcbranch_rel_path := head_content.replace('ref: ', '').trim_space()
gcbranch_file := os.join_path(vroot, '.git', gcbranch_rel_path)
// .git/refs/heads/master
if !os.exists(gcbranch_file) {
break
}
// get the full commit hash contained in the ref heads file
branch_hash := os.read_file(gcbranch_file) or { break }
current_branch_hash = branch_hash
}
desired_hash_length := 7
if current_branch_hash.len > desired_hash_length {
return current_branch_hash[0..desired_hash_length]
}
pub fn githash(mod_path string, is_vroot bool) string {
mut res := if is_vroot { @VCURRENTHASH } else { '' }
git_head_file := os.join_path(mod_path, '.git', 'HEAD')
if !os.exists(git_head_file) {
return res
}
// 'ref: refs/heads/master' ... the current branch name
head_content := os.read_file(git_head_file) or { return res }
mut current_branch_hash := head_content
if head_content.starts_with('ref: ') {
gcbranch_rel_path := head_content.replace('ref: ', '').trim_space()
gcbranch_file := os.join_path(mod_path, '.git', gcbranch_rel_path)
// .git/refs/heads/master
if !os.exists(gcbranch_file) {
return res
}
break
// get the full commit hash contained in the ref heads file
branch_hash := os.read_file(gcbranch_file) or { return res }
current_branch_hash = branch_hash
}

return @VCURRENTHASH
desired_hash_length := 7
if current_branch_hash.len > desired_hash_length {
return current_branch_hash[0..desired_hash_length]
}
return res
}

0 comments on commit 7fb7a48

Please sign in to comment.