1#!/usr/bin/env bash
2# Copyright 2020 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -o errexit
17set -o nounset
18set -o xtrace
19set -o pipefail
20
21# Update the gh-pages branch. Note that `cargo doc` is **not deterministic** so
22# this should only be done when there is a real change.
23readonly RUST_BRANCH=${1:-main}
24readonly RUST_GH_BRANCH=gh-pages
25
26if [ -z "${FORCE+x}" ]; then
27  readonly PREV_COMMIT=$(git log --oneline -n 1 ${RUST_GH_BRANCH} | sed 's/.*branch at \([0-9a-f]*\)/\1/')
28  readonly CHANGES=$(git diff "${PREV_COMMIT}..${RUST_BRANCH}" | grep -e '[+-]//[/!]')
29
30  if [ -z "${CHANGES}" ]; then
31    echo "No doc comment changes found in ${PREV_COMMIT}..${RUST_BRANCH} subdir rust/"
32    exit 0
33  fi
34fi
35
36git switch "${RUST_BRANCH}"
37readonly RUST_BRANCH_SHA1=$(git rev-parse --short HEAD)
38readonly RUST_BRANCH_SUBJECT=$(git log -n 1 --format=format:%s)
39readonly COMMIT_MESSAGE=$(cat <<-END
40Update Rust docs to ${RUST_BRANCH} branch at ${RUST_BRANCH_SHA1}
41
42Auto-generated from commit ${RUST_BRANCH_SHA1} ("${RUST_BRANCH_SUBJECT}").
43END
44)
45
46readonly TGZ_FILE="/tmp/coset-doc-${RUST_BRANCH_SHA1}.tgz"
47# Build Cargo docs and save them off outside the repo
48(
49    rm -rf target/doc
50    cargo doc --no-deps
51    cargo deadlinks
52    cd target/doc || exit
53    tar czf "${TGZ_FILE}" ./*
54)
55
56# Shift to ${RUST_GH_BRANCH} branch and replace contents of (just) ./rust/
57git switch ${RUST_GH_BRANCH}
58
59readonly DOC_DIR=rust
60rm -rf ${DOC_DIR}
61mkdir ${DOC_DIR}
62(
63    cd "${DOC_DIR}" || exit
64    tar xzf "${TGZ_FILE}"
65)
66
67# Commit any differences
68git add "${DOC_DIR}"
69git commit --message="${COMMIT_MESSAGE}"
70git switch "${RUST_BRANCH}"
71