xref: /openbmc/qemu/.gitlab-ci.d/check-patch.py (revision 4ab2bc6482bc883ab0834c2b6834468743d110b1)
11f475472SDaniel P. Berrangé#!/usr/bin/env python3
21f475472SDaniel P. Berrangé#
31f475472SDaniel P. Berrangé# check-patch.py: run checkpatch.pl across all commits in a branch
41f475472SDaniel P. Berrangé#
51f475472SDaniel P. Berrangé# Copyright (C) 2020 Red Hat, Inc.
61f475472SDaniel P. Berrangé#
71f475472SDaniel P. Berrangé# SPDX-License-Identifier: GPL-2.0-or-later
81f475472SDaniel P. Berrangé
91f475472SDaniel P. Berrangéimport os
101f475472SDaniel P. Berrangéimport os.path
111f475472SDaniel P. Berrangéimport sys
121f475472SDaniel P. Berrangéimport subprocess
131f475472SDaniel P. Berrangé
141f475472SDaniel P. Berrangénamespace = "qemu-project"
151f475472SDaniel P. Berrangéif len(sys.argv) >= 2:
161f475472SDaniel P. Berrangé    namespace = sys.argv[1]
171f475472SDaniel P. Berrangé
181f475472SDaniel P. Berrangécwd = os.getcwd()
191f475472SDaniel P. Berrangéreponame = os.path.basename(cwd)
201f475472SDaniel P. Berrangérepourl = "https://gitlab.com/%s/%s.git" % (namespace, reponame)
211f475472SDaniel P. Berrangé
22*4ab2bc64SAlex Bennéeprint(f"adding upstream git repo @ {repourl}")
231f475472SDaniel P. Berrangé# GitLab CI environment does not give us any direct info about the
241f475472SDaniel P. Berrangé# base for the user's branch. We thus need to figure out a common
251f475472SDaniel P. Berrangé# ancestor between the user's branch and current git master.
261f475472SDaniel P. Berrangésubprocess.check_call(["git", "remote", "add", "check-patch", repourl])
27*4ab2bc64SAlex Bennéesubprocess.check_call(["git", "fetch", "check-patch", "master"])
281f475472SDaniel P. Berrangé
291f475472SDaniel P. Berrangéancestor = subprocess.check_output(["git", "merge-base",
301f475472SDaniel P. Berrangé                                    "check-patch/master", "HEAD"],
311f475472SDaniel P. Berrangé                                   universal_newlines=True)
321f475472SDaniel P. Berrangé
331f475472SDaniel P. Berrangéancestor = ancestor.strip()
341f475472SDaniel P. Berrangé
35faf9828eSDaniel P. Berrangélog = subprocess.check_output(["git", "log", "--format=%H %s",
36faf9828eSDaniel P. Berrangé                               ancestor + "..."],
37faf9828eSDaniel P. Berrangé                              universal_newlines=True)
38faf9828eSDaniel P. Berrangé
391f475472SDaniel P. Berrangésubprocess.check_call(["git", "remote", "rm", "check-patch"])
401f475472SDaniel P. Berrangé
41faf9828eSDaniel P. Berrangéif log == "":
42faf9828eSDaniel P. Berrangé    print("\nNo commits since %s, skipping checks\n" % ancestor)
43faf9828eSDaniel P. Berrangé    sys.exit(0)
44faf9828eSDaniel P. Berrangé
451f475472SDaniel P. Berrangéerrors = False
461f475472SDaniel P. Berrangé
477025111aSAlex Bennéeprint("\nChecking all commits since %s...\n" % ancestor, flush=True)
481f475472SDaniel P. Berrangé
497025111aSAlex Bennéeret = subprocess.run(["scripts/checkpatch.pl", "--terse", ancestor + "..."])
501f475472SDaniel P. Berrangé
511f475472SDaniel P. Berrangéif ret.returncode != 0:
521f475472SDaniel P. Berrangé    print("    ❌ FAIL one or more commits failed scripts/checkpatch.pl")
531f475472SDaniel P. Berrangé    sys.exit(1)
541f475472SDaniel P. Berrangé
551f475472SDaniel P. Berrangésys.exit(0)
56