| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # check-patch.py: run checkpatch.pl across all commits in a branch |
| 4 | # |
| 5 | # Copyright (C) 2020 Red Hat, Inc. |
| 6 | # |
| 7 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 8 | |
| 9 | import os |
| 10 | import os.path |
| 11 | import sys |
| 12 | import subprocess |
| 13 | |
| 14 | namespace = "qemu-project" |
| 15 | if len(sys.argv) >= 2: |
| 16 | namespace = sys.argv[1] |
| 17 | |
| 18 | cwd = os.getcwd() |
| 19 | reponame = os.path.basename(cwd) |
| 20 | repourl = "https://gitlab.com/%s/%s.git" % (namespace, reponame) |
| 21 | |
| 22 | print(f"adding upstream git repo @ {repourl}") |
| 23 | # GitLab CI environment does not give us any direct info about the |
| 24 | # base for the user's branch. We thus need to figure out a common |
| 25 | # ancestor between the user's branch and current git master. |
| 26 | subprocess.check_call(["git", "remote", "add", "check-patch", repourl]) |
| 27 | subprocess.check_call(["git", "fetch", "--refetch", "check-patch", "master"]) |
| 28 | |
| 29 | ancestor = subprocess.check_output(["git", "merge-base", |
| 30 | "check-patch/master", "HEAD"], |
| 31 | universal_newlines=True) |
| 32 | |
| 33 | ancestor = ancestor.strip() |
| 34 | |
| 35 | log = subprocess.check_output(["git", "log", "--format=%H %s", |
| 36 | ancestor + "..."], |
| 37 | universal_newlines=True) |
| 38 | |
| 39 | subprocess.check_call(["git", "remote", "rm", "check-patch"]) |
| 40 | |
| 41 | if log == "": |
| 42 | print("\nNo commits since %s, skipping checks\n" % ancestor) |
| 43 | sys.exit(0) |
| 44 | |
| 45 | errors = False |
| 46 | |
| 47 | print("\nChecking all commits since %s...\n" % ancestor, flush=True) |
| 48 | |
| 49 | ret = subprocess.run(["scripts/checkpatch.pl", "--terse", ancestor + "..."]) |
| 50 | |
| 51 | if ret.returncode != 0: |
| 52 | print(" ❌ FAIL one or more commits failed scripts/checkpatch.pl") |
| 53 | sys.exit(1) |
| 54 | |
| 55 | sys.exit(0) |