@@ -17,7 +17,7 @@ pub fn vhash() string {
17
17
18
18
pub fn full_hash () string {
19
19
build_hash := vhash ()
20
- current_hash := githash ( false )
20
+ current_hash := @VCURRENTHASH
21
21
if build_hash == current_hash {
22
22
return build_hash
23
23
}
@@ -29,52 +29,35 @@ pub fn full_v_version(is_verbose bool) string {
29
29
if is_verbose {
30
30
return 'V ${version.v_version} ${full_hash()} '
31
31
}
32
- hash := githash (false )
33
- return 'V ${version.v_version} ${hash} '
32
+ return 'V ${version.v_version} ${@VCURRENTHASH} '
34
33
}
35
34
36
- // githash(x) returns the current git commit hash.
37
- // When x is false, it is very fast - it just returns a predefined C constant.
38
- // When x is true, it tries to get the current commit hash, by parsing the
39
- // relevant files in the .git/ folder, or if that is not possible
40
- // for example when using a V from a V binary release, that does not have .git/
41
- // defaults to getting the predefined C constant again.
42
- // Note: githash(true) must be called only when v detects that it builds itself.
43
- // For all other programs, githash(false) should be used.
44
- pub fn githash (should_get_from_filesystem bool ) string {
45
- for {
46
- // The `for` construct here is used as a goto substitute.
47
- // The code in this function will break out of the `for`
48
- // if it detects an error and can not continue.
49
- if should_get_from_filesystem {
50
- vexe := os.getenv ('VEXE' )
51
- vroot := os.dir (vexe)
52
- // .git/HEAD
53
- git_head_file := os.join_path (vroot, '.git' , 'HEAD' )
54
- if ! os.exists (git_head_file) {
55
- break
56
- }
57
- // 'ref: refs/heads/master' ... the current branch name
58
- head_content := os.read_file (git_head_file) or { break }
59
- mut current_branch_hash := head_content
60
- if head_content.starts_with ('ref: ' ) {
61
- gcbranch_rel_path := head_content.replace ('ref: ' , '' ).trim_space ()
62
- gcbranch_file := os.join_path (vroot, '.git' , gcbranch_rel_path)
63
- // .git/refs/heads/master
64
- if ! os.exists (gcbranch_file) {
65
- break
66
- }
67
- // get the full commit hash contained in the ref heads file
68
- branch_hash := os.read_file (gcbranch_file) or { break }
69
- current_branch_hash = branch_hash
70
- }
71
- desired_hash_length := 7
72
- if current_branch_hash.len > desired_hash_length {
73
- return current_branch_hash[0 ..desired_hash_length]
74
- }
35
+ // githash tries to find the current git commit hash for the specified
36
+ // project path by parsing the relevant files in its `.git/` folder.
37
+ pub fn githash (path string ) ! string {
38
+ // .git/HEAD
39
+ git_head_file := os.join_path (path, '.git' , 'HEAD' )
40
+ if ! os.exists (git_head_file) {
41
+ return error ('failed to find `${git_head_file} `' )
42
+ }
43
+ // 'ref: refs/heads/master' ... the current branch name
44
+ head_content := os.read_file (git_head_file) or {
45
+ return error ('failed to read `${git_head_file} `' )
46
+ }
47
+ current_branch_hash := if head_content.starts_with ('ref: ' ) {
48
+ rev_rel_path := head_content.replace ('ref: ' , '' ).trim_space ()
49
+ rev_file := os.join_path (path, '.git' , rev_rel_path)
50
+ // .git/refs/heads/master
51
+ if ! os.exists (rev_file) {
52
+ return error ('failed to find revision file `${rev_file} `' )
75
53
}
76
- break
54
+ // get the full commit hash contained in the ref heads file
55
+ os.read_file (rev_file) or { return error ('failed to read revision file `${rev_file} `' ) }
56
+ } else {
57
+ head_content
58
+ }
59
+ desired_hash_length := 7
60
+ return current_branch_hash[0 ..desired_hash_length] or {
61
+ error ('failed to limit hash `${current_branch_hash} ` to ${desired_hash_length} characters' )
77
62
}
78
-
79
- return @VCURRENTHASH
80
63
}
0 commit comments