1#!/bin/bash
2
3DIR="${BASH_SOURCE%/*}"
4
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)
9if [ -z "$CHECKPATCH" ]; then
10  CHECKPATCH=$(command -v checkpatch.pl)
11fi
12if [ -z "$CHECKPATCH" ]; then
13  CHECKPATCH=$(find /usr/src/linux-headers* -name checkpatch.pl -print -quit)
14fi
15if [ -z "$CHECKPATCH" ]; then
16  CHECKPATCH=$(find "$PWD/../linux" -name checkpatch.pl -print -quit)
17fi
18
19source "$DIR/checkpatch_inc.sh"
20
21hash $CHECKPATCH 2>/dev/null ||
22		{ echo >&2 "Could not find checkpatch.pl, aborting"; exit 1; }
23
24help() {
25  cat <<-EOF
26Usage:
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
34Args:
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
39Options:
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
45Examples:
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)
70EOF
71  exit 1
72}
73
74op=${1:---working}
75case "$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)
89		help
90		;;
91	*)
92		echo "Checking commit(s):"
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    ;;
111
112esac
113