xref: /optee_os/scripts/checkpatch.sh (revision 775712134e8170753e6c6b0c8ae0a62109ddc1c5)
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
24usage() {
25  SCR=$(basename "$0")
26  echo "Usage: $SCR [--working]                 Check working area"
27  echo "       $SCR <commit>...                 Check specific commits,
28                                                symbolic names, and/or revision
29                                                selections"
30  echo "       $SCR --diff <commit1> <commit2>  Check diff commit1...commit2"
31  echo "       $SCR --cached                    Check staging area"
32  echo "       $SCR --help                      This help"
33  exit 1
34}
35
36op=${1:---working}
37case "$op" in
38	--cached)
39		echo "Checking staging area:  "
40		checkstaging
41		;;
42	--diff)
43		echo "Checking diff (diff $1...$2)"
44		checkdiff "$2" "$3"
45		;;
46	--working)
47		echo "Checking working area:  "
48		checkworking
49		;;
50	--help|-h)
51		usage
52		;;
53	*)
54		echo "Checking commit(s):"
55    read -r MAJOR MINOR < <(git --version | awk -F '[. ]' '{print $3, $4}')
56    if (( MAJOR < 2 )) || (( MAJOR == 2 && MINOR < 19 )); then
57      for c in "$@"; do checkpatch "$c"; done
58    else
59      for arg in "$@"; do
60        # parse the argument into a git object or list of git objects
61        object="$(git rev-parse "${arg}")" || continue
62        # run checkpatch if the parsed argument represents a single commit hash
63        if git cat-file -e "${object}" 2>/dev/null; then
64          checkpatch "${object}"
65        else
66          # expand the object list and run checkpatch on each commit id
67          commits="$(echo "${object}" | git rev-list --stdin)"
68          for c in ${commits}; do checkpatch "$c"; done
69        fi
70      done
71    fi
72    ;;
73
74esac
75