2
2
3
3
import sys
4
4
import argparse
5
+ import fnmatch
5
6
from typing import Optional , Dict
6
7
7
8
import mistune
8
9
import CppHeaderParser
9
10
import semver
10
11
import git
11
12
12
-
13
13
parser = argparse .ArgumentParser (description = '''Check that the versioning of the repo has been correctly changed.
14
14
Intended to run in a PR CI''' )
15
15
parser .add_argument ('branchspec' , default = 'origin/develop' , nargs = '?' ,
@@ -34,6 +34,45 @@ def __str__(self):
34
34
return 'V' + super (SemVerWithVPrefix , self ).__str__ ()
35
35
36
36
37
+ def only_changed_non_fw_files (refspec : str ) -> bool :
38
+ print ('Checking if the files changed impact the firmware...' )
39
+ repo = git .Repo ('.' )
40
+ index = repo .index
41
+ origin_develop_commit = repo .commit (refspec )
42
+
43
+ # index vs working copy (unstaged files)
44
+ changed_but_not_staged = [item .a_path for item in index .diff (None )]
45
+ # index vs current HEAD tree (staged files)
46
+ changed_and_staged = [item .a_path for item in index .diff ('HEAD' )]
47
+ # origin vs current HEAD tree (files changed in the branch)
48
+ branch_files_changed = [item .a_path for item in origin_develop_commit .diff ('HEAD' )]
49
+
50
+ print (f'Unstaged changes: { changed_but_not_staged } ' )
51
+ print (f'Staged changes: { changed_and_staged } ' )
52
+ print (f'Changed in branch: { branch_files_changed } ' )
53
+
54
+ all_changed_filenames = changed_but_not_staged + changed_and_staged + branch_files_changed
55
+ print (f'Total changed files: { all_changed_filenames } ' )
56
+
57
+ non_fw_globs = [
58
+ # non-build Python scripts (pre/post Platformio scripts still affect the build process)
59
+ 'matrix_build*.py' , 'version_check.py' , 'scripts/MeadeCommandParser.py' ,
60
+ # prose text files
61
+ '*.md' , 'LICENSE' , '.mailmap' ,
62
+ # build system
63
+ '.github/*.yml' , 'requirements_*.txt' ,
64
+ # misc tooling files
65
+ '.gitignore' , '.git-blame-ignore-revs' ,
66
+ ]
67
+ for changed_filename in all_changed_filenames :
68
+ if not any (fnmatch .fnmatch (changed_filename , non_fw_glob ) for non_fw_glob in non_fw_globs ):
69
+ # If the changed file isn't a non-FW file (==> is a FW file)
70
+ # then we need to do the full version check
71
+ print (f"Changed file { changed_filename } will affect firmware ({ non_fw_globs } )" )
72
+ return False
73
+ return True
74
+
75
+
37
76
def get_header_defines (header_contents : str ) -> Dict [str , str ]:
38
77
defines_detail = CppHeaderParser .CppHeader (header_contents , argType = 'string' ).defines_detail
39
78
return dict (d ['value' ].split (' ' , maxsplit = 1 ) for d in defines_detail )
@@ -91,6 +130,9 @@ def get_previous_header_version(version_header_path: str, refspec: str) -> semve
91
130
92
131
93
132
def main ():
133
+ if only_changed_non_fw_files (args .branchspec ):
134
+ print ('No FW files changed, not checking version.' )
135
+ return
94
136
version_header_path = './Version.h'
95
137
# Get the current version from the header
96
138
print (f'Checking current header version from { version_header_path } ' )
0 commit comments