()
| 262 | # |
| 263 | # Entrypoint |
| 264 | def main(): |
| 265 | DEFAULT_REFSPEC = 'origin/main' |
| 266 | |
| 267 | parser = argparse.ArgumentParser(description='''Usage: python ./scripts/check_code_format.py |
| 268 | - Reqires python3 and clang-format |
| 269 | - Run script in repo root |
| 270 | - May produce inaccurate clang-format results if local branch is not rebased on the TARGET_REFSPEC |
| 271 | ''', formatter_class=RawDescriptionHelpFormatter) |
| 272 | parser.add_argument('--target-refspec', metavar='TARGET_REFSPEC', type=str, dest='target_refspec', help = 'Refspec to ' |
| 273 | + 'diff against (default is origin/main)', default=DEFAULT_REFSPEC) |
| 274 | parser.add_argument('--base-refspec', metavar='BASE_REFSPEC', type=str, dest='base_refspec', help = 'Base refspec to ' |
| 275 | + ' compare (default is HEAD)', default='HEAD') |
| 276 | parser.add_argument('--fetch-main', dest='fetch_main', action='store_true', help='Fetch the main branch first.' |
| 277 | + ' Useful with --target-refspec=FETCH_HEAD to compare against what is currently on main') |
| 278 | args = parser.parse_args() |
| 279 | |
| 280 | if os.path.isfile('check_code_format.py'): |
| 281 | os.chdir('..') |
| 282 | |
| 283 | target_refspec = args.target_refspec |
| 284 | base_refspec = args.base_refspec |
| 285 | |
| 286 | if args.fetch_main: |
| 287 | print('Fetching main branch...') |
| 288 | subprocess.check_call(['git', 'fetch', 'https://github.com/KhronosGroup/Vulkan-ValidationLayers.git', 'main']) |
| 289 | |
| 290 | # Check if this is a merge commit |
| 291 | commit_parents = check_output(['git', 'rev-list', '--parents', '-n', '1', 'HEAD']) |
| 292 | if len(commit_parents.split(b' ')) > 2: |
| 293 | # If this is a merge commit, this is a PR being built, and has been merged into main for testing. |
| 294 | # The first parent (HEAD^) is going to be main, the second parent (HEAD^2) is going to be the PR commit. |
| 295 | # TODO (ncesario) We should *ONLY* get here when on github CI, building a PR. Should probably print a |
| 296 | # warning if this happens locally. |
| 297 | target_refspec = 'HEAD^' |
| 298 | base_refspec = 'HEAD^2' |
| 299 | |
| 300 | orig_branch = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode('utf-8').splitlines()[0] |
| 301 | if orig_branch == 'HEAD': |
| 302 | orig_branch = check_output(['git', 'rev-parse', 'HEAD']).decode('utf-8').splitlines()[0] |
| 303 | |
| 304 | commits = check_output(['git', 'log', '--format=%h', f'{base_refspec}...{target_refspec}']).split(b'\n') |
| 305 | commits.reverse() |
| 306 | |
| 307 | # Run code format check on each commit in a PR so that we ensure that each commit is correct. |
| 308 | failure = 0 |
| 309 | for c in commits: |
| 310 | if len(c) == 0: |
| 311 | continue |
| 312 | |
| 313 | commit = c.decode('utf-8') |
| 314 | diff_range = f'{commit}^...{commit}' |
| 315 | |
| 316 | commit_message = check_output(['git', 'log', '--pretty="%h %s"', diff_range]).decode('utf-8') |
| 317 | CPrint('CONTENT', "\nChecking commit: " + commit_message) |
| 318 | |
| 319 | subprocess.run(['git', 'checkout', '-q', commit]) |
| 320 | |
| 321 | # Get list of files involved in this commit |
no test coverage detected