xref: /qemu/.gitlab-ci.d/check-patch.py (revision 7025111a)
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
9import os
10import os.path
11import sys
12import subprocess
13
14namespace = "qemu-project"
15if len(sys.argv) >= 2:
16    namespace = sys.argv[1]
17
18cwd = os.getcwd()
19reponame = os.path.basename(cwd)
20repourl = "https://gitlab.com/%s/%s.git" % (namespace, reponame)
21
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.
25subprocess.check_call(["git", "remote", "add", "check-patch", repourl])
26subprocess.check_call(["git", "fetch", "check-patch", "master"],
27                      stdout=subprocess.DEVNULL,
28                      stderr=subprocess.DEVNULL)
29
30ancestor = subprocess.check_output(["git", "merge-base",
31                                    "check-patch/master", "HEAD"],
32                                   universal_newlines=True)
33
34ancestor = ancestor.strip()
35
36log = subprocess.check_output(["git", "log", "--format=%H %s",
37                               ancestor + "..."],
38                              universal_newlines=True)
39
40subprocess.check_call(["git", "remote", "rm", "check-patch"])
41
42if log == "":
43    print("\nNo commits since %s, skipping checks\n" % ancestor)
44    sys.exit(0)
45
46errors = False
47
48print("\nChecking all commits since %s...\n" % ancestor, flush=True)
49
50ret = subprocess.run(["scripts/checkpatch.pl", "--terse", ancestor + "..."])
51
52if ret.returncode != 0:
53    print("    ❌ FAIL one or more commits failed scripts/checkpatch.pl")
54    sys.exit(1)
55
56sys.exit(0)
57