1#!/bin/bash
2# Copyright 2016 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -eu
17
18GIT_URL="git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git"
19CGIT_URL="https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain"
20
21usage() {
22  cat <<EOF
23Usage: $0 [linux version]
24
25Helper script to quickly update the bundled checkpatch.pl script.
26EOF
27
28  if [[ $# -ne 0 ]]; then
29    echo "ERROR: $*" 2>&1
30    exit 1
31  else
32    exit 0
33  fi
34}
35
36# Return the latest kernel release version.
37get_latest_version() {
38  local output
39
40  # Parse the git tag output to find the latest version.
41  # Example output:
42  # 0fc80a1fc05dd08762fda6e8efafbb54e88608f4        refs/tags/v4.5
43  # b562e44f507e863c6792946e4e1b1449fbbac85d        refs/tags/v4.5^{}
44  # b13adfcdf288a00a7e58556f326ecb56fbb53b92        refs/tags/v4.5-rc1
45
46  output=$(git ls-remote --tags "${GIT_URL}")
47  # Filter out rc/signed tags.
48  output=$(echo "${output}" | grep -v -e '-rc' -e '\^{}')
49  # The output might not be sorted based on versions, so do so ourselves and
50  # grab the last entry.
51  output=$(echo "${output}" | cut -d/ -f3 | sort -V | tail -1)
52  # Chop the leading "v" bit.
53  echo "${output#v}"
54}
55
56# Download checkpatch.pl & data for the specified kernel version.
57download() {
58  local version="$1"
59  local url
60
61  # First the main checkpatch.pl file.
62  url="${CGIT_URL}/scripts/checkpatch.pl?h=v${version}"
63  wget "${url}" -O checkpatch.pl
64  chmod a+rx checkpatch.pl
65
66  # Then any data it uses.
67  url="${CGIT_URL}/scripts/spelling.txt?h=v${version}"
68  wget "${url}" -O spelling.txt
69}
70
71main() {
72  local version=''
73  while [[ $# -gt 0 ]]; do
74    case $1 in
75    -h|--help) usage;;
76    -x) set -x;;
77    -*) usage "Unknown option: $1";;
78    *) version=${1#v};;
79    esac
80    shift
81  done
82
83  if [[ -z "${version}" ]]; then
84     printf 'Detecting latest kernel version ... '
85     version=$(get_latest_version)
86     echo "${version}"
87  fi
88
89  download "${version}"
90}
91main "$@"
92