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