@@ -25,51 +25,65 @@ def git_commit(dir):
2525
2626
2727def parse_git_version (version ):
28- # Parse, e.g.: wasi-sdk-21 -0-g317548590b40+m
28+ # Parse, e.g.: wasi-sdk-$version -0-g317548590b40+m
2929 parts = version .replace ('+' , '-' ).split ('-' )
3030 assert parts .pop (0 ) == 'wasi'
3131 assert parts .pop (0 ) == 'sdk'
3232
33- major , minor = parts .pop (0 ), parts .pop (0 )
34- git = None
3533 dirty = False
36-
37- if parts :
38- # Check: git|dirty.
39- next = parts .pop (0 )
40- if next == 'm' :
41- dirty = True
42- elif minor != '0' :
43- git = next [1 :]
44-
45- # Check: dirty.
46- if parts :
47- assert parts .pop (0 ) == 'm' , f'expected dirty flag: +m'
48- dirty = True
49-
50- assert not parts , f'unexpected suffixes: { parts } '
51- return major , minor , git , dirty
34+ git = parts .pop ()
35+ assert git is not None
36+ # If git printed `+m` at the end then we're dirty and the next-to-last part
37+ # is now the git commit
38+ if git == 'm' :
39+ dirty = True
40+ git = parts .pop ()
41+
42+ # If the git commit doesn't start with `g` then that's not actually a git
43+ # commit. Technically this should look for `GIT_REF_LEN` hexadecimal digits,
44+ # but that's left for later.
45+ if not git .startswith ('g' ):
46+ parts .append (git )
47+ git = None
48+
49+ # The next part in the back is the number of commits since the tag, and the
50+ # first part in the front is the major version of wasi-sdk itself.
51+ commits_since_tag = parts .pop ()
52+ major_version = parts .pop (0 )
53+
54+ # If there are 0 commits since the last tag then forcibly drop the git part
55+ # as that produces a nice clean version for tagged commits.
56+ if commits_since_tag == '0' :
57+ git = None
58+
59+ # Pretend the commits since the tag is a minor version number, and then
60+ # add in any other words after the tag to the prerelease part, if any.
61+ ret = f'{ major_version } .{ commits_since_tag } '
62+ if len (parts ) > 0 :
63+ ret += '-' + '-' .join (parts )
64+
65+ # Throw on git/dirty info
66+ if git :
67+ ret += git
68+ if dirty :
69+ ret += '+m'
70+ return ret
5271
5372
5473# Some inline tests to check Git version parsing:
55- assert parse_git_version (
56- 'wasi-sdk-21-1-g317548590b40+m' ) == ('21' , '1' , '317548590b40' , True )
57- assert parse_git_version ('wasi-sdk-21-2+m' ) == ('21' , '2' , None , True )
58- assert parse_git_version (
59- 'wasi-sdk-23-0-g317548590b40' ) == ('23' , '0' , None , False )
60-
74+ assert parse_git_version ('wasi-sdk-21-1-g317548590b40+m' ) == '21.1g317548590b40+m'
75+ assert parse_git_version ('wasi-sdk-21-2+m' ) == '21.2+m'
76+ assert parse_git_version ('wasi-sdk-23-0-g317548590b40' ) == '23.0'
77+ assert parse_git_version ('wasi-sdk-23-tag-0-g317548590b40' ) == '23.0-tag'
78+ assert parse_git_version ('wasi-sdk-23-some-tag-0-g317548590b40' ) == '23.0-some-tag'
79+ assert parse_git_version ('wasi-sdk-23-some.tag-0-g317548590b40' ) == '23.0-some.tag'
80+ assert parse_git_version ('wasi-sdk-23-tag.rc1-1-g100' ) == '23.1-tag.rc1g100'
6181
6282def git_version ():
6383 version = exec (['git' , 'describe' , '--long' , '--candidates=999' ,
6484 '--match=wasi-sdk-*' , '--dirty=+m' , f'--abbrev={ GIT_REF_LEN } ' ],
6585 os .path .dirname (sys .argv [0 ]))
66- major , minor , git , dirty = parse_git_version (version )
67- version = f'{ major } .{ minor } '
68- if git :
69- version += f'g{ git } '
70- if dirty :
71- version += '+m'
72- return version
86+ return parse_git_version (version )
7387
7488
7589def parse_cmake_set (line ):
0 commit comments