xref: /kvm-unit-tests/scripts/check-patch.py (revision 220ac1e0aaeaa86968de0573970cc0d3cfa2dbfb)
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# This file is taken from qemu.git 029e13a8a56a2 .gitlab-ci.d/check-patch.py
10# with minor changes to adjust namespace and checkpatch.pl invocation.
11
12import os
13import os.path
14import sys
15import subprocess
16
17namespace = "kvm-unit-tests"
18if len(sys.argv) >= 2:
19    namespace = sys.argv[1]
20
21cwd = os.getcwd()
22reponame = os.path.basename(cwd)
23repourl = "https://gitlab.com/%s/%s.git" % (namespace, reponame)
24
25# GitLab CI environment does not give us any direct info about the
26# base for the user's branch. We thus need to figure out a common
27# ancestor between the user's branch and current git master.
28subprocess.check_call(["git", "remote", "add", "check-patch", repourl])
29subprocess.check_call(["git", "fetch", "check-patch", "master"])
30
31#                      stdout=subprocess.DEVNULL,
32#                      stderr=subprocess.DEVNULL)
33
34ancestor = subprocess.check_output(["git", "merge-base",
35                                    "check-patch/master", "HEAD"],
36                                   universal_newlines=True)
37
38ancestor = ancestor.strip()
39
40log = subprocess.check_output(["git", "log", "--format=%H %s",
41                               ancestor + "..."],
42                              universal_newlines=True)
43
44subprocess.check_call(["git", "remote", "rm", "check-patch"])
45
46if log == "":
47    print("\nNo commits since %s, skipping checks\n" % ancestor)
48    sys.exit(0)
49
50errors = False
51
52print("\nChecking all commits since %s...\n" % ancestor, flush=True)
53
54ret = subprocess.run(["scripts/checkpatch.pl", "--terse", "--no-tree", "--git",
55                      ancestor + "..."])
56
57if ret.returncode != 0:
58    print("    ❌ FAIL one or more commits failed scripts/checkpatch.pl")
59    sys.exit(1)
60
61sys.exit(0)
62