1#!/usr/bin/env bash
2
3# Display commands being run
4set -x
5
6# Only run apidiff checks on go1.11 (we only need it once).
7# TODO(deklerk) We should pass an environment variable from kokoro to decide
8# this logic instead.
9if [[ `go version` != *"go1.11"* ]]; then
10    exit 0
11fi
12
13try3() { eval "$*" || eval "$*" || eval "$*"; }
14
15try3 go get -u golang.org/x/exp/cmd/apidiff
16
17# We compare against master@HEAD. This is unfortunate in some cases: if you're
18# working on an out-of-date branch, and master gets some new feature (that has
19# nothing to do with your work on your branch), you'll get an error message.
20# Thankfully the fix is quite simple: rebase your branch.
21git clone https://github.com/google/go-genproto /tmp/genproto
22
23V1_DIRS=`find . -type d -regex '.*v1$'`
24V1_SUBDIRS=`find . -type d -regex '.*v1\/.*'`
25for dir in $V1_DIRS $V1_SUBDIRS; do
26  # turns things like ./foo/bar into foo/bar
27  dir_without_junk=`echo $dir | sed -n "s#\(\.\/\)\(.*\)#\2#p"`
28  pkg="google.golang.org/genproto/$dir_without_junk"
29  echo "Testing $pkg"
30
31  cd /tmp/genproto
32  apidiff -w /tmp/pkg.master $pkg
33  cd - > /dev/null
34
35  # TODO(deklerk) there's probably a nicer way to do this that doesn't require
36  # two invocations
37  if ! apidiff -incompatible /tmp/pkg.master $pkg | (! read); then
38    apidiff -incompatible /tmp/pkg.master $pkg
39    exit 1
40  fi
41done
42