├── .pre-commit-config.yaml ├── .pre-commit-hooks.yaml ├── README.md ├── LICENSE └── getshas.sh /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/shellcheck-py/shellcheck-py 3 | rev: v0.11.0.1 4 | hooks: 5 | - id: shellcheck 6 | -------------------------------------------------------------------------------- /.pre-commit-hooks.yaml: -------------------------------------------------------------------------------- 1 | - id: buildifier 2 | name: buildifier 3 | description: Format starlark code with buildifier 4 | entry: buildifier-wrapper.sh fix -mode=fix -lint=fix 5 | files: '^(.*/)?(.*\.bzl|.*\.sky|.*\.bazel|BUILD|.*\.BUILD|BUILD\..*\.oss|WORKSPACE|WORKSPACE\.bzlmod|WORKSPACE\.oss|WORKSPACE\..*\.oss)$' 6 | language: script 7 | - id: buildifier-lint 8 | name: buildifier-lint 9 | description: Lint starlark code with buildifier 10 | entry: buildifier-wrapper.sh lint -mode=diff -lint=warn 11 | files: '^(.*/)?(.*\.bzl|.*\.sky|.*\.bazel|BUILD|.*\.BUILD|BUILD\..*\.oss|WORKSPACE|WORKSPACE\.bzlmod|WORKSPACE\.oss|WORKSPACE\..*\.oss)$' 12 | language: script 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pre-commit-buildifier 2 | 3 | This is a hook for [pre-commit][pc] and [buildifier][buildifier] that 4 | doesn't require it to be installed ahead of time and easily lets you pin 5 | to a specific version. 6 | 7 | ## Usage 8 | 9 | ```yaml 10 | - repo: https://github.com/keith/pre-commit-buildifier 11 | rev: TAG OR SHA 12 | hooks: 13 | - id: buildifier 14 | - id: buildifier-lint 15 | ``` 16 | 17 | This repo provides multiple hooks because some buildifier rules cannot 18 | be autofixed. The `buildifier` hook fixes everything that can while the 19 | `buildifier-lint` hook prints unfixable warnings. If you use both of 20 | them you should use them in that order so you don't end up in duplicate 21 | warnings. 22 | 23 | If you'd like to pass custom flags to buildifier (as well as the default 24 | mode configurations) you can use pre-commit's `args`: 25 | 26 | ```yaml 27 | - id: buildifier 28 | args: [custom, flags] 29 | ``` 30 | 31 | [buildifier]: https://github.com/bazelbuild/buildtools/tree/master/buildifier 32 | [pc]: https://pre-commit.com 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Keith Smiley (http://keith.so) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the 'Software'), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /getshas.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | getsha() { 6 | if command -v sha256sum >/dev/null 2>&1; then 7 | sha256sum "$1" 8 | else 9 | shasum -a 256 "$1" 10 | fi 11 | } 12 | 13 | readonly version="$1" 14 | echo "readonly version=$version" 15 | 16 | # List of operating systems and architectures 17 | for os in darwin linux windows 18 | do 19 | for arch in amd64 arm64 20 | do 21 | # Skip arm64 for windows since there is no build for it 22 | if [[ $os == windows && $arch == arm64 ]]; then 23 | continue 24 | fi 25 | 26 | # Special handling for Windows to include ".exe" extension 27 | if [[ $os == windows ]]; then 28 | filename="buildifier-$os-$arch.exe" 29 | else 30 | filename="buildifier-$os-$arch" 31 | fi 32 | 33 | url="https://github.com/bazelbuild/buildtools/releases/download/$version/$filename" 34 | bin=$(mktemp) 35 | if ! curl --fail --silent -L "$url" -o "$bin"; then 36 | echo "error: failed to download $url, is the version correct?" 37 | exit 1 38 | fi 39 | 40 | sha=$(getsha "$bin" | cut -d ' ' -f 1) 41 | echo "# shellcheck disable=SC2034" 42 | echo "readonly ${os}_${arch}_sha=$sha" 43 | 44 | rm -f "$bin" 45 | done 46 | done 47 | --------------------------------------------------------------------------------