v2 / vlib / v / util / version / version.v
52 lines · 46 sloc · 1.59 KB · 0c3183c55b39534f9bb0d2f796bb575d39c9d229
Raw
1module version
2
3import os
4
5pub const v_version = '0.5.1'
6
7pub fn full_hash() string {
8 build_hash := vhash()
9
10 if vcurrent_hash() == '' || build_hash[..7] == vcurrent_hash() {
11 return build_hash
12 }
13 return '${build_hash}.${vcurrent_hash()}'
14}
15
16// full_v_version() returns the full version of the V compiler
17pub fn full_v_version(is_verbose bool) string {
18 if is_verbose {
19 return 'V ${v_version} ${full_hash()}'
20 }
21 return 'V ${v_version} ${vcurrent_hash()}'
22}
23
24// githash tries to find the current git commit hash for the specified
25// project path by parsing the relevant files in its `.git/` folder.
26pub fn githash(path string) !string {
27 // .git/HEAD
28 git_head_file := os.join_path(path, '.git', 'HEAD')
29 if !os.exists(git_head_file) {
30 return error('failed to find `${git_head_file}`')
31 }
32 // 'ref: refs/heads/master' ... the current branch name
33 head_content := os.read_file(git_head_file) or {
34 return error('failed to read `${git_head_file}`')
35 }
36 current_branch_hash := if head_content.starts_with('ref: ') {
37 rev_rel_path := head_content.replace('ref: ', '').trim_space()
38 rev_file := os.join_path(path, '.git', rev_rel_path)
39 // .git/refs/heads/master
40 if !os.exists(rev_file) {
41 return error('failed to find revision file `${rev_file}`')
42 }
43 // get the full commit hash contained in the ref heads file
44 os.read_file(rev_file) or { return error('failed to read revision file `${rev_file}`') }
45 } else {
46 head_content
47 }
48 desired_hash_length := 7
49 return current_branch_hash[0..desired_hash_length] or {
50 error('failed to limit hash `${current_branch_hash}` to ${desired_hash_length} characters')
51 }
52}
53