lint-git-commit-check.py
1 #!/usr/bin/env python3 2 # 3 # Copyright (c) 2020-2022 The Bitcoin Core developers 4 # Distributed under the MIT software license, see the accompanying 5 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 # 7 # Linter to check that commit messages have a new line before the body 8 # or no body at all 9 10 import argparse 11 import os 12 import sys 13 14 from subprocess import check_output 15 16 17 def parse_args(): 18 """Parse command line arguments.""" 19 parser = argparse.ArgumentParser( 20 description=""" 21 Linter to check that commit messages have a new line before 22 the body or no body at all. 23 """, 24 epilog=f""" 25 You can manually set the commit-range with the COMMIT_RANGE 26 environment variable (e.g. "COMMIT_RANGE='HEAD~n..HEAD' 27 {sys.argv[0]}") for the last 'n' commits. 28 """) 29 return parser.parse_args() 30 31 32 def main(): 33 parse_args() 34 exit_code = 0 35 36 assert os.getenv("COMMIT_RANGE") # E.g. COMMIT_RANGE='HEAD~n..HEAD' 37 commit_range = os.getenv("COMMIT_RANGE") 38 39 commit_hashes = check_output(["git", "log", commit_range, "--format=%H"], text=True, encoding="utf8").splitlines() 40 41 for hash in commit_hashes: 42 commit_info = check_output(["git", "log", "--format=%B", "-n", "1", hash], text=True, encoding="utf8").splitlines() 43 if len(commit_info) >= 2: 44 if commit_info[1]: 45 print(f"The subject line of commit hash {hash} is followed by a non-empty line. Subject lines should always be followed by a blank line.") 46 exit_code = 1 47 48 sys.exit(exit_code) 49 50 51 if __name__ == "__main__": 52 main()