|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2017-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 | +# Check for new lines in diff that introduce trailing whitespace or |
| 8 | +# tab characters instead of spaces. |
| 9 | + |
| 10 | +# We can't run this check unless we know the commit range for the PR. |
| 11 | + |
| 12 | +import argparse |
| 13 | +import os |
| 14 | +import re |
| 15 | +import sys |
| 16 | + |
| 17 | +from subprocess import check_output |
| 18 | + |
| 19 | +EXCLUDED_DIRS = ["depends/patches/", |
| 20 | + "contrib/guix/patches/", |
| 21 | + "src/leveldb/", |
| 22 | + "src/crc32c/", |
| 23 | + "src/secp256k1/", |
| 24 | + "src/minisketch/", |
| 25 | + "src/univalue/", |
| 26 | + "doc/release-notes/", |
| 27 | + "src/qt/locale"] |
| 28 | + |
| 29 | +def parse_args(): |
| 30 | + """Parse command line arguments.""" |
| 31 | + parser = argparse.ArgumentParser( |
| 32 | + description=""" |
| 33 | + Check for new lines in diff that introduce trailing whitespace |
| 34 | + or tab characters instead of spaces in unstaged changes, the |
| 35 | + previous n commits, or a commit-range. |
| 36 | + """, |
| 37 | + epilog=f""" |
| 38 | + You can manually set the commit-range with the COMMIT_RANGE |
| 39 | + environment variable (e.g. "COMMIT_RANGE='47ba2c3...ee50c9e' |
| 40 | + {sys.argv[0]}"). Defaults to current merge base when neither |
| 41 | + prev-commits nor the environment variable is set. |
| 42 | + """) |
| 43 | + |
| 44 | + parser.add_argument("--prev-commits", "-p", required=False, help="The previous n commits to check") |
| 45 | + |
| 46 | + return parser.parse_args() |
| 47 | + |
| 48 | + |
| 49 | +def report_diff(selection): |
| 50 | + filename = "" |
| 51 | + seen = False |
| 52 | + seenln = False |
| 53 | + |
| 54 | + print("The following changes were suspected:") |
| 55 | + |
| 56 | + for line in selection: |
| 57 | + if re.match(r"^diff", line): |
| 58 | + filename = line |
| 59 | + seen = False |
| 60 | + elif re.match(r"^@@", line): |
| 61 | + linenumber = line |
| 62 | + seenln = False |
| 63 | + else: |
| 64 | + if not seen: |
| 65 | + # The first time a file is seen with trailing whitespace or a tab character, we print the |
| 66 | + # filename (preceded by a newline). |
| 67 | + print("") |
| 68 | + print(filename) |
| 69 | + seen = True |
| 70 | + if not seenln: |
| 71 | + print(linenumber) |
| 72 | + seenln = True |
| 73 | + print(line) |
| 74 | + |
| 75 | + |
| 76 | +def get_diff(commit_range, check_only_code): |
| 77 | + exclude_args = [":(exclude)" + dir for dir in EXCLUDED_DIRS] |
| 78 | + |
| 79 | + if check_only_code: |
| 80 | + what_files = ["*.cpp", "*.h", "*.md", "*.py", "*.sh"] |
| 81 | + else: |
| 82 | + what_files = ["."] |
| 83 | + |
| 84 | + diff = check_output(["git", "diff", "-U0", commit_range, "--"] + what_files + exclude_args, universal_newlines=True, encoding="utf8") |
| 85 | + |
| 86 | + return diff |
| 87 | + |
| 88 | + |
| 89 | +def main(): |
| 90 | + args = parse_args() |
| 91 | + |
| 92 | + if not os.getenv("COMMIT_RANGE"): |
| 93 | + if args.prev_commits: |
| 94 | + commit_range = "HEAD~" + args.prev_commits + "...HEAD" |
| 95 | + else: |
| 96 | + # This assumes that the target branch of the pull request will be master. |
| 97 | + merge_base = check_output(["git", "merge-base", "HEAD", "master"], universal_newlines=True, encoding="utf8").rstrip("\n") |
| 98 | + commit_range = merge_base + "..HEAD" |
| 99 | + else: |
| 100 | + commit_range = os.getenv("COMMIT_RANGE") |
| 101 | + |
| 102 | + whitespace_selection = [] |
| 103 | + tab_selection = [] |
| 104 | + |
| 105 | + # Check if trailing whitespace was found in the diff. |
| 106 | + for line in get_diff(commit_range, check_only_code=False).splitlines(): |
| 107 | + if re.match(r"^(diff --git|\@@|^\+.*\s+$)", line): |
| 108 | + whitespace_selection.append(line) |
| 109 | + |
| 110 | + whitespace_additions = [i for i in whitespace_selection if i.startswith("+")] |
| 111 | + |
| 112 | + # Check if tab characters were found in the diff. |
| 113 | + for line in get_diff(commit_range, check_only_code=True).splitlines(): |
| 114 | + if re.match(r"^(diff --git|\@@|^\+.*\t)", line): |
| 115 | + tab_selection.append(line) |
| 116 | + |
| 117 | + tab_additions = [i for i in tab_selection if i.startswith("+")] |
| 118 | + |
| 119 | + ret = 0 |
| 120 | + |
| 121 | + if len(whitespace_additions) > 0: |
| 122 | + print("This diff appears to have added new lines with trailing whitespace.") |
| 123 | + report_diff(whitespace_selection) |
| 124 | + ret = 1 |
| 125 | + |
| 126 | + if len(tab_additions) > 0: |
| 127 | + print("This diff appears to have added new lines with tab characters instead of spaces.") |
| 128 | + report_diff(tab_selection) |
| 129 | + ret = 1 |
| 130 | + |
| 131 | + sys.exit(ret) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
0 commit comments