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