blob: 57b8bd23b863b8c5970ca289bbcd9c411b831553 [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)}
39
40# Collect the arguments.
41if [ "$#" -lt 3 ]; then
42 usage
43 exit 1
44fi
45
46# Collect the arguments.
47#
48GOOD_COMMIT="$1"
49shift
50BAD_COMMIT="$1"
51shift
52BAZEL_ARGUMENTS="$@"
53
54echo "Bisecting bazel from good $GOOD_COMMIT to bad $BAD_COMMIT: bazel $BAZEL_ARGUMENTS"
55
56# Check out and update Bazel.
57if [ ! -d "$BAZEL_DIR" ]; then
58 git clone "$BAZEL_REPO" "$BAZEL_DIR"
59fi
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.
68WORKING_DIR="$PWD"
69(
70 BISECT_SCRIPT=/tmp/bisect.sh
71 TMP_BIN=/tmp/bazel.bisect
72
73 cat >$BISECT_SCRIPT <<EOF
74#!/bin/bash
75set -eu
76"$BAZEL_BINARY" build //src:bazel || exit 1
77cp -f bazel-bin/src/bazel $TMP_BIN
78cd "$WORKING_DIR"
79exec "$TMP_BIN" $BAZEL_ARGUMENTS
80EOF
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)