xref: /openbmc/qemu/.gitlab-ci.d/check-patch.py (revision faf9828e2fbb62cb2edaeeaa48449193d952016f)
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é
221f475472SDaniel P. Berrangé# GitLab CI environment does not give us any direct info about the
231f475472SDaniel P. Berrangé# base for the user's branch. We thus need to figure out a common
241f475472SDaniel P. Berrangé# ancestor between the user's branch and current git master.
251f475472SDaniel P. Berrangésubprocess.check_call(["git", "remote", "add", "check-patch", repourl])
261f475472SDaniel P. Berrangésubprocess.check_call(["git", "fetch", "check-patch", "master"],
271f475472SDaniel P. Berrangé                      stdout=subprocess.DEVNULL,
281f475472SDaniel P. Berrangé                      stderr=subprocess.DEVNULL)
291f475472SDaniel P. Berrangé
301f475472SDaniel P. Berrangéancestor = subprocess.check_output(["git", "merge-base",
311f475472SDaniel P. Berrangé                                    "check-patch/master", "HEAD"],
321f475472SDaniel P. Berrangé                                   universal_newlines=True)
331f475472SDaniel P. Berrangé
341f475472SDaniel P. Berrangéancestor = ancestor.strip()
351f475472SDaniel P. Berrangé
36*faf9828eSDaniel P. Berrangélog = subprocess.check_output(["git", "log", "--format=%H %s",
37*faf9828eSDaniel P. Berrangé                               ancestor + "..."],
38*faf9828eSDaniel P. Berrangé                              universal_newlines=True)
39*faf9828eSDaniel P. Berrangé
401f475472SDaniel P. Berrangésubprocess.check_call(["git", "remote", "rm", "check-patch"])
411f475472SDaniel P. Berrangé
42*faf9828eSDaniel P. Berrangéif log == "":
43*faf9828eSDaniel P. Berrangé    print("\nNo commits since %s, skipping checks\n" % ancestor)
44*faf9828eSDaniel P. Berrangé    sys.exit(0)
45*faf9828eSDaniel P. Berrangé
461f475472SDaniel P. Berrangéerrors = False
471f475472SDaniel P. Berrangé
481f475472SDaniel P. Berrangéprint("\nChecking all commits since %s...\n" % ancestor)
491f475472SDaniel P. Berrangé
501f475472SDaniel P. Berrangéret = subprocess.run(["scripts/checkpatch.pl", ancestor + "..."])
511f475472SDaniel P. Berrangé
521f475472SDaniel P. Berrangéif ret.returncode != 0:
531f475472SDaniel P. Berrangé    print("    ❌ FAIL one or more commits failed scripts/checkpatch.pl")
541f475472SDaniel P. Berrangé    sys.exit(1)
551f475472SDaniel P. Berrangé
561f475472SDaniel P. Berrangésys.exit(0)
57