blob: 6977ea24ad11cc3fe999e8a1a59862783bb14285 [file] [log] [blame]
John Cater27444122018-02-27 12:29:09 -08001#!/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
18set -eu
19
20USAGE='bazel-bisect.sh GOOD_COMMIT BAD_COMMIT [<bazel arguments>...]'
21DESCRIPTION='
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
26function 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.
33BAZEL_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.
36BAZEL_DIR=${BAZEL_DIR:-$HOME/os-bazel-bisect}
37# Bazel to use to build local bazel binaries.
38BAZEL_BINARY=${BAZEL_BINARY:-$(which bazel)}
John Cater6376dac2019-11-18 10:41:45 -080039# Whether to run 'bazel clean' between invocations.
40# Set BAZEL_BISECT_WITH_CLEAN=y to clean build artifacts between runs.
41BAZEL_BISECT_WITH_CLEAN=${BAZEL_BISECT_WITH_CLEAN:-n}
John Cater27444122018-02-27 12:29:09 -080042
43# Collect the arguments.
44if [ "$#" -lt 3 ]; then
45 usage
46 exit 1
47fi
48
49# Collect the arguments.
50#
51GOOD_COMMIT="$1"
52shift
53BAD_COMMIT="$1"
54shift
55BAZEL_ARGUMENTS="$@"
56
57echo "Bisecting bazel from good $GOOD_COMMIT to bad $BAD_COMMIT: bazel $BAZEL_ARGUMENTS"
58
59# Check out and update Bazel.
60if [ ! -d "$BAZEL_DIR" ]; then
61 git clone "$BAZEL_REPO" "$BAZEL_DIR"
62fi
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.
71WORKING_DIR="$PWD"
72(
73 BISECT_SCRIPT=/tmp/bisect.sh
74 TMP_BIN=/tmp/bazel.bisect
75
76 cat >$BISECT_SCRIPT <<EOF
77#!/bin/bash
78set -eu
John Cater6376dac2019-11-18 10:41:45 -080079"$BAZEL_BINARY" build //src:bazel >/dev/null 2>&1 || exit 1
jcater45f016d2019-05-06 10:10:50 -070080cp -f bazel-bin/src/bazel "$TMP_BIN"
John Cater27444122018-02-27 12:29:09 -080081cd "$WORKING_DIR"
jcater45f016d2019-05-06 10:10:50 -070082# Call the newly built Bazel. If it fails, save the exit code but don't stop
83# the script.
84result=0
John Cater6376dac2019-11-18 10:41:45 -080085if [[ "${BAZEL_BISECT_WITH_CLEAN}" = "y" ]]; then
86 "$TMP_BIN" clean || true
87fi
88"$TMP_BIN" $BAZEL_ARGUMENTS || result=\$? && true
jcater45f016d2019-05-06 10:10:50 -070089"$TMP_BIN" shutdown || true
90exit \$result
John Cater27444122018-02-27 12:29:09 -080091EOF
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)