Skip to content

Commit 7720f6e

Browse files
Change version_check.py to exit early if the files changed do not affect the firmware
1 parent 366931e commit 7720f6e

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

version_check.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import sys
44
import argparse
5+
import fnmatch
56
from typing import Optional, Dict
67

78
import mistune
89
import CppHeaderParser
910
import semver
1011
import git
1112

12-
1313
parser = argparse.ArgumentParser(description='''Check that the versioning of the repo has been correctly changed.
1414
Intended to run in a PR CI''')
1515
parser.add_argument('branchspec', default='origin/develop', nargs='?',
@@ -34,6 +34,45 @@ def __str__(self):
3434
return 'V' + super(SemVerWithVPrefix, self).__str__()
3535

3636

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+
3776
def get_header_defines(header_contents: str) -> Dict[str, str]:
3877
defines_detail = CppHeaderParser.CppHeader(header_contents, argType='string').defines_detail
3978
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
91130

92131

93132
def main():
133+
if only_changed_non_fw_files(args.branchspec):
134+
print('No FW files changed, not checking version.')
135+
return
94136
version_header_path = './Version.h'
95137
# Get the current version from the header
96138
print(f'Checking current header version from {version_header_path}')

0 commit comments

Comments
 (0)