|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright 2026 LiveKit |
| 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 | +set -euo pipefail |
| 18 | + |
| 19 | +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
| 20 | +repo_root="$(cd "${script_dir}/.." && pwd -P)" |
| 21 | +examples_dir="${repo_root}/cpp-example-collection" |
| 22 | +examples_repo="livekit-examples/cpp-example-collection" |
| 23 | +examples_remote="https://github.com/${examples_repo}.git" |
| 24 | +default_branch="main" |
| 25 | + |
| 26 | +usage() { |
| 27 | + cat <<'EOF' |
| 28 | +Usage: ./scripts/update-cpp-example-collection.sh [--hash HASH] |
| 29 | +
|
| 30 | +Update the cpp-example-collection submodule used by the C++ SDK. |
| 31 | +
|
| 32 | +Options: |
| 33 | + --hash HASH |
| 34 | + Use the specified examples commit. You can use a short or full SHA-1 |
| 35 | + hash. |
| 36 | + -h, --help |
| 37 | + Show this help and exit. |
| 38 | +
|
| 39 | +When --hash is omitted, the script uses the most recent commit on the main |
| 40 | +branch of livekit-examples/cpp-example-collection. |
| 41 | +EOF |
| 42 | +} |
| 43 | + |
| 44 | +requested_hash="" |
| 45 | +hash_provided=false |
| 46 | + |
| 47 | +while (($#)); do |
| 48 | + case "$1" in |
| 49 | + --hash) |
| 50 | + shift |
| 51 | + if (($# == 0)); then |
| 52 | + echo "ERROR: --hash requires a value" >&2 |
| 53 | + exit 2 |
| 54 | + fi |
| 55 | + requested_hash="$1" |
| 56 | + hash_provided=true |
| 57 | + shift |
| 58 | + ;; |
| 59 | + --hash=*) |
| 60 | + requested_hash="${1#--hash=}" |
| 61 | + hash_provided=true |
| 62 | + shift |
| 63 | + ;; |
| 64 | + -h|--help) |
| 65 | + usage |
| 66 | + exit 0 |
| 67 | + ;; |
| 68 | + *) |
| 69 | + echo "ERROR: unknown argument: $1" >&2 |
| 70 | + usage >&2 |
| 71 | + exit 2 |
| 72 | + ;; |
| 73 | + esac |
| 74 | +done |
| 75 | + |
| 76 | +if [[ "$hash_provided" == true && ! "$requested_hash" =~ ^[0-9a-fA-F]{7,40}$ ]]; then |
| 77 | + echo "ERROR: --hash must be a 7- to 40-character hexadecimal Git SHA-1 hash" >&2 |
| 78 | + exit 2 |
| 79 | +fi |
| 80 | + |
| 81 | +if ! command -v git >/dev/null 2>&1; then |
| 82 | + echo "ERROR: git not found in PATH" >&2 |
| 83 | + exit 1 |
| 84 | +fi |
| 85 | + |
| 86 | +if ! git -C "$repo_root" rev-parse --show-toplevel >/dev/null 2>&1; then |
| 87 | + echo "ERROR: ${repo_root} is not a Git worktree" >&2 |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +if [[ ! -e "${examples_dir}/.git" ]]; then |
| 92 | + echo "==> Initializing cpp-example-collection" |
| 93 | + git -C "$repo_root" submodule update --init cpp-example-collection |
| 94 | +fi |
| 95 | + |
| 96 | +if ! git -C "$examples_dir" diff --quiet || |
| 97 | + ! git -C "$examples_dir" diff --cached --quiet; then |
| 98 | + echo "ERROR: cpp-example-collection has tracked changes; commit or stash them first" >&2 |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +fetch_ref="$requested_hash" |
| 103 | + |
| 104 | +if [[ "$hash_provided" == false ]]; then |
| 105 | + echo "==> Finding the most recent cpp-example-collection commit" |
| 106 | + branch_ref="refs/heads/${default_branch}" |
| 107 | + remote_ref="$(git ls-remote "$examples_remote" "$branch_ref")" |
| 108 | + read -r requested_hash resolved_ref <<<"$remote_ref" |
| 109 | + if [[ -z "$requested_hash" || "$resolved_ref" != "$branch_ref" ]]; then |
| 110 | + echo "ERROR: branch '${default_branch}' was not found in ${examples_repo}" >&2 |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + fetch_ref="$branch_ref" |
| 114 | + echo "==> Latest ${default_branch} commit: ${requested_hash}" |
| 115 | +else |
| 116 | + requested_hash="$(printf '%s' "$requested_hash" | tr '[:upper:]' '[:lower:]')" |
| 117 | + echo "==> Requested examples commit: ${requested_hash}" |
| 118 | +fi |
| 119 | + |
| 120 | +echo "==> Fetching cpp-example-collection" |
| 121 | +if [[ "$hash_provided" == false ]]; then |
| 122 | + git -C "$examples_dir" fetch --quiet origin "$fetch_ref" |
| 123 | +elif [[ ${#requested_hash} -eq 40 ]]; then |
| 124 | + if ! git -C "$examples_dir" fetch --quiet origin "$fetch_ref"; then |
| 125 | + git -C "$examples_dir" fetch --quiet origin |
| 126 | + fi |
| 127 | +else |
| 128 | + git -C "$examples_dir" fetch --quiet origin |
| 129 | +fi |
| 130 | + |
| 131 | +if ! resolved_hash="$(git -C "$examples_dir" rev-parse --verify "${requested_hash}^{commit}" 2>/dev/null)"; then |
| 132 | + echo "ERROR: '${requested_hash}' does not resolve to a cpp-example-collection commit" >&2 |
| 133 | + exit 1 |
| 134 | +fi |
| 135 | + |
| 136 | +current_hash="$(git -C "$examples_dir" rev-parse HEAD)" |
| 137 | +if [[ "$current_hash" == "$resolved_hash" ]]; then |
| 138 | + echo "==> cpp-example-collection is already at ${resolved_hash}" |
| 139 | +else |
| 140 | + git -C "$examples_dir" checkout --quiet --detach "$resolved_hash" |
| 141 | + echo "==> Updated cpp-example-collection: ${current_hash} -> ${resolved_hash}" |
| 142 | +fi |
| 143 | + |
| 144 | +echo "==> Synchronizing nested example submodules" |
| 145 | +git -C "$examples_dir" submodule sync --recursive |
| 146 | +git -C "$examples_dir" submodule update --init --recursive |
| 147 | + |
| 148 | +echo "==> Done. Review and commit the cpp-example-collection gitlink change." |
0 commit comments