John Cater | 2744412 | 2018-02-27 12:29:09 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2018 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | set -eu |
| 19 | |
| 20 | USAGE='bazel-bisect.sh GOOD_COMMIT BAD_COMMIT [<bazel arguments>...]' |
| 21 | DESCRIPTION=' |
| 22 | Downloads a fresh copy of the Bazel source and runs git bisect from the |
| 23 | specified commits, testing by running the given bazel command in the |
| 24 | current working directory.' |
| 25 | |
| 26 | function usage() { |
| 27 | echo "$USAGE" "$DESCRIPTION" >&2 |
| 28 | } |
| 29 | |
| 30 | # Configuration params. Export these in your bashrc to set personal defaults. |
| 31 | |
| 32 | # The source of Bazel code. |
| 33 | BAZEL_REPO=${BAZEL_REPO:-https://github.com/bazelbuild/bazel} |
| 34 | # Where to keep the Bazel repository. If you make changes here, be warned that |
| 35 | # this script may overwrite or lose them. |
| 36 | BAZEL_DIR=${BAZEL_DIR:-$HOME/os-bazel-bisect} |
| 37 | # Bazel to use to build local bazel binaries. |
| 38 | BAZEL_BINARY=${BAZEL_BINARY:-$(which bazel)} |
| 39 | |
| 40 | # Collect the arguments. |
| 41 | if [ "$#" -lt 3 ]; then |
| 42 | usage |
| 43 | exit 1 |
| 44 | fi |
| 45 | |
| 46 | # Collect the arguments. |
| 47 | # |
| 48 | GOOD_COMMIT="$1" |
| 49 | shift |
| 50 | BAD_COMMIT="$1" |
| 51 | shift |
| 52 | BAZEL_ARGUMENTS="$@" |
| 53 | |
| 54 | echo "Bisecting bazel from good $GOOD_COMMIT to bad $BAD_COMMIT: bazel $BAZEL_ARGUMENTS" |
| 55 | |
| 56 | # Check out and update Bazel. |
| 57 | if [ ! -d "$BAZEL_DIR" ]; then |
| 58 | git clone "$BAZEL_REPO" "$BAZEL_DIR" |
| 59 | fi |
| 60 | # Ensure the repository is up to date. |
| 61 | ( |
| 62 | cd "$BAZEL_DIR" |
| 63 | git fetch --tags |
| 64 | git checkout master |
| 65 | ) |
| 66 | |
| 67 | # Run the actual bisect. |
| 68 | WORKING_DIR="$PWD" |
| 69 | ( |
| 70 | BISECT_SCRIPT=/tmp/bisect.sh |
| 71 | TMP_BIN=/tmp/bazel.bisect |
| 72 | |
| 73 | cat >$BISECT_SCRIPT <<EOF |
| 74 | #!/bin/bash |
| 75 | set -eu |
| 76 | "$BAZEL_BINARY" build //src:bazel || exit 1 |
| 77 | cp -f bazel-bin/src/bazel $TMP_BIN |
| 78 | cd "$WORKING_DIR" |
| 79 | exec "$TMP_BIN" $BAZEL_ARGUMENTS |
| 80 | EOF |
| 81 | chmod +x "$BISECT_SCRIPT" |
| 82 | cd "$BAZEL_DIR" |
| 83 | git bisect start |
| 84 | git bisect good "$GOOD_COMMIT" |
| 85 | git bisect bad "$BAD_COMMIT" |
| 86 | result=0 |
| 87 | git bisect run "$BISECT_SCRIPT" || result=$? |
| 88 | git bisect reset |
| 89 | exit $result |
| 90 | ) |