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)} |
John Cater | 6376dac | 2019-11-18 10:41:45 -0800 | [diff] [blame] | 39 | # Whether to run 'bazel clean' between invocations. |
| 40 | # Set BAZEL_BISECT_WITH_CLEAN=y to clean build artifacts between runs. |
| 41 | BAZEL_BISECT_WITH_CLEAN=${BAZEL_BISECT_WITH_CLEAN:-n} |
John Cater | 2744412 | 2018-02-27 12:29:09 -0800 | [diff] [blame] | 42 | |
| 43 | # Collect the arguments. |
| 44 | if [ "$#" -lt 3 ]; then |
| 45 | usage |
| 46 | exit 1 |
| 47 | fi |
| 48 | |
| 49 | # Collect the arguments. |
| 50 | # |
| 51 | GOOD_COMMIT="$1" |
| 52 | shift |
| 53 | BAD_COMMIT="$1" |
| 54 | shift |
| 55 | BAZEL_ARGUMENTS="$@" |
| 56 | |
| 57 | echo "Bisecting bazel from good $GOOD_COMMIT to bad $BAD_COMMIT: bazel $BAZEL_ARGUMENTS" |
| 58 | |
| 59 | # Check out and update Bazel. |
| 60 | if [ ! -d "$BAZEL_DIR" ]; then |
| 61 | git clone "$BAZEL_REPO" "$BAZEL_DIR" |
| 62 | fi |
| 63 | # Ensure the repository is up to date. |
| 64 | ( |
| 65 | cd "$BAZEL_DIR" |
| 66 | git fetch --tags |
| 67 | git checkout master |
| 68 | ) |
| 69 | |
| 70 | # Run the actual bisect. |
| 71 | WORKING_DIR="$PWD" |
| 72 | ( |
| 73 | BISECT_SCRIPT=/tmp/bisect.sh |
| 74 | TMP_BIN=/tmp/bazel.bisect |
| 75 | |
| 76 | cat >$BISECT_SCRIPT <<EOF |
| 77 | #!/bin/bash |
| 78 | set -eu |
John Cater | 6376dac | 2019-11-18 10:41:45 -0800 | [diff] [blame] | 79 | "$BAZEL_BINARY" build //src:bazel >/dev/null 2>&1 || exit 1 |
jcater | 45f016d | 2019-05-06 10:10:50 -0700 | [diff] [blame] | 80 | cp -f bazel-bin/src/bazel "$TMP_BIN" |
John Cater | 2744412 | 2018-02-27 12:29:09 -0800 | [diff] [blame] | 81 | cd "$WORKING_DIR" |
jcater | 45f016d | 2019-05-06 10:10:50 -0700 | [diff] [blame] | 82 | # Call the newly built Bazel. If it fails, save the exit code but don't stop |
| 83 | # the script. |
| 84 | result=0 |
John Cater | 6376dac | 2019-11-18 10:41:45 -0800 | [diff] [blame] | 85 | if [[ "${BAZEL_BISECT_WITH_CLEAN}" = "y" ]]; then |
| 86 | "$TMP_BIN" clean || true |
| 87 | fi |
| 88 | "$TMP_BIN" $BAZEL_ARGUMENTS || result=\$? && true |
jcater | 45f016d | 2019-05-06 10:10:50 -0700 | [diff] [blame] | 89 | "$TMP_BIN" shutdown || true |
| 90 | exit \$result |
John Cater | 2744412 | 2018-02-27 12:29:09 -0800 | [diff] [blame] | 91 | EOF |
| 92 | chmod +x "$BISECT_SCRIPT" |
| 93 | cd "$BAZEL_DIR" |
| 94 | git bisect start |
| 95 | git bisect good "$GOOD_COMMIT" |
| 96 | git bisect bad "$BAD_COMMIT" |
| 97 | result=0 |
| 98 | git bisect run "$BISECT_SCRIPT" || result=$? |
| 99 | git bisect reset |
| 100 | exit $result |
| 101 | ) |