1#!/bin/bash -ex
2#
3# Copyright 2023 Google Inc. 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# Update between branches, to a new major version.
18
19cd `dirname ${BASH_SOURCE[0]}`/..
20
21if [ -z ${1+x} ]; then
22    echo "Usage: update-major-version.sh <upstream tag>"
23    exit 1
24fi
25NEW_VERSION=$1
26NEW_VERSION_SHORT=${NEW_VERSION#upstream-}
27CURRENT_VERSION=upstream-$(grep 'version:' METADATA | grep -E -o 'v[0-9][^"]+')
28CURRENT_HEAD=$(git rev-parse HEAD)
29echo "Updating from $CURRENT_VERSION to $NEW_VERSION"
30
31ADDED_FILES=$(git diff --name-only --diff-filter=A $CURRENT_VERSION HEAD | sort)
32CHANGED_FILES=$(git diff --name-only --diff-filter=a $CURRENT_VERSION HEAD | sort)
33
34PATCHFILE=$(mktemp --suffix=.patch)
35trap "rm -f $PATCHFILE" EXIT
36
37git diff --patch $CURRENT_VERSION HEAD -- $CHANGED_FILES > $PATCHFILE
38
39git merge --no-commit -s ours $NEW_VERSION
40git restore --staged --worktree --source=$NEW_VERSION -- .
41git restore --staged --worktree --source=$CURRENT_HEAD -- $ADDED_FILES
42patch -p1 < $PATCHFILE
43git add $CHANGED_FILES
44sed -i -e "s/\(version: \"\)v[0-9][^\"]*/\1$NEW_VERSION_SHORT/" METADATA
45git add METADATA
46
47NEW_ADDED_FILES=$(git diff --name-only --diff-filter=A --staged $NEW_VERSION | sort)
48if [ "$ADDED_FILES" != "$NEW_ADDED_FILES" ] ; then
49    echo "Added files don't match"
50    exit 1
51fi
52
53NEW_CHANGED_FILES=$(git diff --name-only --diff-filter=a --staged $NEW_VERSION | sort)
54if [ "$CHANGED_FILES" != "$NEW_CHANGED_FILES" ] ; then
55    echo "Changed files don't match"
56    exit 1
57fi
58