162306a36Sopenharmony_ci#!/usr/bin/env python3
262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
362306a36Sopenharmony_ci#
462306a36Sopenharmony_ci# check-patch.py: run checkpatch.pl across all commits in a branch
562306a36Sopenharmony_ci#
662306a36Sopenharmony_ci# Based on qemu/.gitlab-ci.d/check-patch.py
762306a36Sopenharmony_ci#
862306a36Sopenharmony_ci# Copyright (C) 2020 Red Hat, Inc.
962306a36Sopenharmony_ci# Copyright (C) 2022 Collabora Ltd.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciimport os
1262306a36Sopenharmony_ciimport os.path
1362306a36Sopenharmony_ciimport sys
1462306a36Sopenharmony_ciimport subprocess
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cirepourl = "https://gitlab.freedesktop.org/%s.git" % os.environ["CI_MERGE_REQUEST_PROJECT_PATH"]
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci# GitLab CI environment does not give us any direct info about the
1962306a36Sopenharmony_ci# base for the user's branch. We thus need to figure out a common
2062306a36Sopenharmony_ci# ancestor between the user's branch and current git master.
2162306a36Sopenharmony_cios.environ["GIT_DEPTH"] = "1000"
2262306a36Sopenharmony_cisubprocess.call(["git", "remote", "remove", "check-patch"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
2362306a36Sopenharmony_cisubprocess.check_call(["git", "remote", "add", "check-patch", repourl])
2462306a36Sopenharmony_cisubprocess.check_call(["git", "fetch", "check-patch", os.environ["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"]],
2562306a36Sopenharmony_ci                      stdout=subprocess.DEVNULL,
2662306a36Sopenharmony_ci                      stderr=subprocess.DEVNULL)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciancestor = subprocess.check_output(["git", "merge-base",
2962306a36Sopenharmony_ci                                    "check-patch/%s" % os.environ["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"], "HEAD"],
3062306a36Sopenharmony_ci                                   universal_newlines=True)
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciancestor = ancestor.strip()
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cilog = subprocess.check_output(["git", "log", "--format=%H %s",
3562306a36Sopenharmony_ci                               ancestor + "..."],
3662306a36Sopenharmony_ci                              universal_newlines=True)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cisubprocess.check_call(["git", "remote", "rm", "check-patch"])
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciif log == "":
4162306a36Sopenharmony_ci    print("\nNo commits since %s, skipping checks\n" % ancestor)
4262306a36Sopenharmony_ci    sys.exit(0)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cierrors = False
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ciprint("\nChecking all commits since %s...\n" % ancestor, flush=True)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciret = subprocess.run(["scripts/checkpatch.pl",
4962306a36Sopenharmony_ci                      "--terse",
5062306a36Sopenharmony_ci                      "--types", os.environ["CHECKPATCH_TYPES"],
5162306a36Sopenharmony_ci                      "--git", ancestor + "..."])
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ciif ret.returncode != 0:
5462306a36Sopenharmony_ci    print("    ❌ FAIL one or more commits failed scripts/checkpatch.pl")
5562306a36Sopenharmony_ci    sys.exit(1)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cisys.exit(0)
58