Igor Opaniuk | 1c93c2b | 2016-11-03 13:27:28 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | DIR="${BASH_SOURCE%/*}" |
| 4 | |
Markus S. Wamser | 33977c0 | 2018-10-17 16:30:06 +0200 | [diff] [blame] | 5 | # if no CHECKPATCH is explicitly given by the environment, try to |
| 6 | # locate checkpatch.pl: first take the one from the path, then check |
| 7 | # for a local copy of the linux headers, finally try sources downloaded |
| 8 | # with OP-TEE (for QEMU) |
| 9 | if [ -z "$CHECKPATCH" ]; then |
| 10 | CHECKPATCH=$(command -v checkpatch.pl) |
| 11 | fi |
| 12 | if [ -z "$CHECKPATCH" ]; then |
| 13 | CHECKPATCH=$(find /usr/src/linux-headers* -name checkpatch.pl -print -quit) |
| 14 | fi |
| 15 | if [ -z "$CHECKPATCH" ]; then |
| 16 | CHECKPATCH=$(find "$PWD/../linux" -name checkpatch.pl -print -quit) |
| 17 | fi |
| 18 | |
Igor Opaniuk | 1c93c2b | 2016-11-03 13:27:28 +0200 | [diff] [blame] | 19 | source "$DIR/checkpatch_inc.sh" |
| 20 | |
| 21 | hash $CHECKPATCH 2>/dev/null || |
| 22 | { echo >&2 "Could not find checkpatch.pl, aborting"; exit 1; } |
| 23 | |
Andrew Mustea | bac7974 | 2023-01-19 13:33:34 -0800 | [diff] [blame] | 24 | help() { |
| 25 | cat <<-EOF |
| 26 | Usage: |
| 27 | checkpatch.sh [--working] |
| 28 | checkpatch.sh <COMMIT>... |
| 29 | checkpatch.sh <SELECTION>... |
| 30 | checkpatch.sh --diff <COMMIT> <COMMIT> |
| 31 | checkpatch.sh --cached |
| 32 | checkpatch.sh --help |
| 33 | |
| 34 | Args: |
| 35 | <COMMIT> Any commit or any number of commits. |
| 36 | <SELECTION> Any number of Git Revision Selections. (requires git v2.19) |
| 37 | https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection |
| 38 | |
| 39 | Options: |
| 40 | --working Check the working area [Default]. |
| 41 | --cached Check the staging area. |
| 42 | --diff <commit1> <commit2> Check the diff between commit1 and commit2. |
| 43 | --help Print this help message. |
| 44 | |
| 45 | Examples: |
| 46 | checkpatch.sh commit1 commit2 commit3 Check commit1, commit2, and commit3. |
| 47 | |
| 48 | checkpatch.sh HEAD~5 Check the commit 5 revisions before |
| 49 | the current HEAD. |
| 50 | |
| 51 | checkpatch.sh commit1..^commit2 Check each commit from commit1 to |
| 52 | commit2 inclusively. |
| 53 | (requires git v2.19) |
| 54 | |
| 55 | checkpatch.sh HEAD~5..HEAD~1 Check each commit from HEAD~5 to |
| 56 | HEAD~1 exclusively, aka not including |
| 57 | HEAD~1. (requires git v2.19) |
| 58 | |
| 59 | checkpatch.sh commit1...tags/tag1 Check each commit that exists |
| 60 | exclusively within the history of |
| 61 | only one of each given revision. |
| 62 | (requires git v2.19) |
| 63 | |
| 64 | checkpatch.sh HEAD~10-5 Check 5 commits moving forward in |
| 65 | history starting from HEAD~10. |
| 66 | (requires git v2.19) |
| 67 | |
| 68 | checkpatch.sh branch1 tags/tag1 Check the HEAD of branch1 and the |
| 69 | HEAD of tag1. (requires git v2.19) |
| 70 | EOF |
Igor Opaniuk | 1c93c2b | 2016-11-03 13:27:28 +0200 | [diff] [blame] | 71 | exit 1 |
| 72 | } |
| 73 | |
| 74 | op=${1:---working} |
| 75 | case "$op" in |
| 76 | --cached) |
| 77 | echo "Checking staging area: " |
| 78 | checkstaging |
| 79 | ;; |
| 80 | --diff) |
| 81 | echo "Checking diff (diff $1...$2)" |
| 82 | checkdiff "$2" "$3" |
| 83 | ;; |
| 84 | --working) |
| 85 | echo "Checking working area: " |
| 86 | checkworking |
| 87 | ;; |
| 88 | --help|-h) |
Andrew Mustea | bac7974 | 2023-01-19 13:33:34 -0800 | [diff] [blame] | 89 | help |
Igor Opaniuk | 1c93c2b | 2016-11-03 13:27:28 +0200 | [diff] [blame] | 90 | ;; |
| 91 | *) |
Jerome Forissier | 849b17b | 2017-02-22 18:11:36 +0100 | [diff] [blame] | 92 | echo "Checking commit(s):" |
Andrew Mustea | 7757121 | 2022-12-21 16:49:48 -0800 | [diff] [blame] | 93 | read -r MAJOR MINOR < <(git --version | awk -F '[. ]' '{print $3, $4}') |
| 94 | if (( MAJOR < 2 )) || (( MAJOR == 2 && MINOR < 19 )); then |
| 95 | for c in "$@"; do checkpatch "$c"; done |
| 96 | else |
| 97 | for arg in "$@"; do |
| 98 | # parse the argument into a git object or list of git objects |
| 99 | object="$(git rev-parse "${arg}")" || continue |
| 100 | # run checkpatch if the parsed argument represents a single commit hash |
| 101 | if git cat-file -e "${object}" 2>/dev/null; then |
| 102 | checkpatch "${object}" |
| 103 | else |
| 104 | # expand the object list and run checkpatch on each commit id |
| 105 | commits="$(echo "${object}" | git rev-list --stdin)" |
| 106 | for c in ${commits}; do checkpatch "$c"; done |
| 107 | fi |
| 108 | done |
| 109 | fi |
| 110 | ;; |
Igor Opaniuk | 1c93c2b | 2016-11-03 13:27:28 +0200 | [diff] [blame] | 111 | |
| 112 | esac |