check-patch.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. #
  4. # check-patch.py: run checkpatch.pl across all commits in a branch
  5. #
  6. # Based on qemu/.gitlab-ci.d/check-patch.py
  7. #
  8. # Copyright (C) 2020 Red Hat, Inc.
  9. # Copyright (C) 2022 Collabora Ltd.
  10. import os
  11. import os.path
  12. import sys
  13. import subprocess
  14. repourl = "https://gitlab.freedesktop.org/%s.git" % os.environ["CI_MERGE_REQUEST_PROJECT_PATH"]
  15. # GitLab CI environment does not give us any direct info about the
  16. # base for the user's branch. We thus need to figure out a common
  17. # ancestor between the user's branch and current git master.
  18. os.environ["GIT_DEPTH"] = "1000"
  19. subprocess.call(["git", "remote", "remove", "check-patch"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  20. subprocess.check_call(["git", "remote", "add", "check-patch", repourl])
  21. subprocess.check_call(["git", "fetch", "check-patch", os.environ["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"]],
  22. stdout=subprocess.DEVNULL,
  23. stderr=subprocess.DEVNULL)
  24. ancestor = subprocess.check_output(["git", "merge-base",
  25. "check-patch/%s" % os.environ["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"], "HEAD"],
  26. universal_newlines=True)
  27. ancestor = ancestor.strip()
  28. log = subprocess.check_output(["git", "log", "--format=%H %s",
  29. ancestor + "..."],
  30. universal_newlines=True)
  31. subprocess.check_call(["git", "remote", "rm", "check-patch"])
  32. if log == "":
  33. print("\nNo commits since %s, skipping checks\n" % ancestor)
  34. sys.exit(0)
  35. errors = False
  36. print("\nChecking all commits since %s...\n" % ancestor, flush=True)
  37. ret = subprocess.run(["scripts/checkpatch.pl",
  38. "--terse",
  39. "--types", os.environ["CHECKPATCH_TYPES"],
  40. "--git", ancestor + "..."])
  41. if ret.returncode != 0:
  42. print(" ❌ FAIL one or more commits failed scripts/checkpatch.pl")
  43. sys.exit(1)
  44. sys.exit(0)