|
| 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 | +rust_dir="${repo_root}/client-sdk-rust" |
| 22 | +rust_repo="livekit/client-sdk-rust" |
| 23 | +rust_remote="https://github.com/${rust_repo}.git" |
| 24 | + |
| 25 | +usage() { |
| 26 | + cat <<'EOF' |
| 27 | +Usage: ./scripts/update-rust-sdk.sh [--hash HASH] |
| 28 | +
|
| 29 | +Update the client-sdk-rust submodule used by the C++ SDK. |
| 30 | +
|
| 31 | +Options: |
| 32 | + --hash HASH |
| 33 | + Use the specified Rust SDK commit. You can use a short or full SHA-1 |
| 34 | + hash. |
| 35 | + -h, --help |
| 36 | + Show this help and exit. |
| 37 | +
|
| 38 | +When --hash is omitted, the script uses the commit tagged by the most recent |
| 39 | +published, non-draft GitHub release in livekit/client-sdk-rust (requires gh). |
| 40 | +EOF |
| 41 | +} |
| 42 | + |
| 43 | +requested_hash="" |
| 44 | +hash_provided=false |
| 45 | + |
| 46 | +while (($#)); do |
| 47 | + case "$1" in |
| 48 | + --hash) |
| 49 | + shift |
| 50 | + if (($# == 0)); then |
| 51 | + echo "ERROR: --hash requires a value" >&2 |
| 52 | + exit 2 |
| 53 | + fi |
| 54 | + requested_hash="$1" |
| 55 | + hash_provided=true |
| 56 | + shift |
| 57 | + ;; |
| 58 | + --hash=*) |
| 59 | + requested_hash="${1#--hash=}" |
| 60 | + hash_provided=true |
| 61 | + shift |
| 62 | + ;; |
| 63 | + -h|--help) |
| 64 | + usage |
| 65 | + exit 0 |
| 66 | + ;; |
| 67 | + *) |
| 68 | + echo "ERROR: unknown argument: $1" >&2 |
| 69 | + usage >&2 |
| 70 | + exit 2 |
| 71 | + ;; |
| 72 | + esac |
| 73 | +done |
| 74 | + |
| 75 | +if [[ "$hash_provided" == true && ! "$requested_hash" =~ ^[0-9a-fA-F]{7,40}$ ]]; then |
| 76 | + echo "ERROR: --hash must be a 7- to 40-character hexadecimal Git SHA-1 hash" >&2 |
| 77 | + exit 2 |
| 78 | +fi |
| 79 | + |
| 80 | +if ! command -v git >/dev/null 2>&1; then |
| 81 | + echo "ERROR: git not found in PATH" >&2 |
| 82 | + exit 1 |
| 83 | +fi |
| 84 | + |
| 85 | +if ! git -C "$repo_root" rev-parse --show-toplevel >/dev/null 2>&1; then |
| 86 | + echo "ERROR: ${repo_root} is not a Git worktree" >&2 |
| 87 | + exit 1 |
| 88 | +fi |
| 89 | + |
| 90 | +if [[ ! -e "${rust_dir}/.git" ]]; then |
| 91 | + echo "==> Initializing client-sdk-rust" |
| 92 | + git -C "$repo_root" submodule update --init client-sdk-rust |
| 93 | +fi |
| 94 | + |
| 95 | +if ! git -C "$rust_dir" diff --quiet || |
| 96 | + ! git -C "$rust_dir" diff --cached --quiet; then |
| 97 | + echo "ERROR: client-sdk-rust has tracked changes; commit or stash them first" >&2 |
| 98 | + exit 1 |
| 99 | +fi |
| 100 | + |
| 101 | +fetch_ref="$requested_hash" |
| 102 | +release_tag="" |
| 103 | + |
| 104 | +if [[ "$hash_provided" == false ]]; then |
| 105 | + if ! command -v gh >/dev/null 2>&1; then |
| 106 | + echo "ERROR: gh not found in PATH; install GitHub CLI or provide --hash" >&2 |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + |
| 110 | + echo "==> Finding the most recent published Rust SDK release" |
| 111 | + release_tags="$( |
| 112 | + gh api --paginate "repos/${rust_repo}/releases?per_page=100" \ |
| 113 | + --jq '.[] | select(.draft == false) | .tag_name' |
| 114 | + )" |
| 115 | + release_tag="${release_tags%%$'\n'*}" |
| 116 | + if [[ -z "$release_tag" ]]; then |
| 117 | + echo "ERROR: no published release found for ${rust_repo}" >&2 |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + |
| 121 | + tag_refs="$( |
| 122 | + git ls-remote "$rust_remote" \ |
| 123 | + "refs/tags/${release_tag}" "refs/tags/${release_tag}^{}" |
| 124 | + )" |
| 125 | + while read -r tag_hash tag_ref; do |
| 126 | + if [[ "$tag_ref" == "refs/tags/${release_tag}^{}" ]]; then |
| 127 | + requested_hash="$tag_hash" |
| 128 | + break |
| 129 | + fi |
| 130 | + if [[ "$tag_ref" == "refs/tags/${release_tag}" ]]; then |
| 131 | + requested_hash="$tag_hash" |
| 132 | + fi |
| 133 | + done <<<"$tag_refs" |
| 134 | + |
| 135 | + if [[ -z "$requested_hash" ]]; then |
| 136 | + echo "ERROR: release tag '${release_tag}' was not found in ${rust_repo}" >&2 |
| 137 | + exit 1 |
| 138 | + fi |
| 139 | + fetch_ref="refs/tags/${release_tag}" |
| 140 | + echo "==> Latest release: ${release_tag} (${requested_hash})" |
| 141 | +else |
| 142 | + requested_hash="$(printf '%s' "$requested_hash" | tr '[:upper:]' '[:lower:]')" |
| 143 | + echo "==> Requested Rust SDK commit: ${requested_hash}" |
| 144 | +fi |
| 145 | + |
| 146 | +echo "==> Fetching client-sdk-rust" |
| 147 | +if [[ "$hash_provided" == false ]]; then |
| 148 | + git -C "$rust_dir" fetch --quiet origin "$fetch_ref" |
| 149 | +elif [[ ${#requested_hash} -eq 40 ]]; then |
| 150 | + if ! git -C "$rust_dir" fetch --quiet origin "$fetch_ref"; then |
| 151 | + git -C "$rust_dir" fetch --quiet origin |
| 152 | + fi |
| 153 | +else |
| 154 | + git -C "$rust_dir" fetch --quiet origin |
| 155 | +fi |
| 156 | + |
| 157 | +if ! resolved_hash="$(git -C "$rust_dir" rev-parse --verify "${requested_hash}^{commit}" 2>/dev/null)"; then |
| 158 | + echo "ERROR: '${requested_hash}' does not resolve to a client-sdk-rust commit" >&2 |
| 159 | + exit 1 |
| 160 | +fi |
| 161 | + |
| 162 | +current_hash="$(git -C "$rust_dir" rev-parse HEAD)" |
| 163 | +if [[ "$current_hash" == "$resolved_hash" ]]; then |
| 164 | + echo "==> client-sdk-rust is already at ${resolved_hash}" |
| 165 | +else |
| 166 | + git -C "$rust_dir" checkout --quiet --detach "$resolved_hash" |
| 167 | + echo "==> Updated client-sdk-rust: ${current_hash} -> ${resolved_hash}" |
| 168 | +fi |
| 169 | + |
| 170 | +echo "==> Synchronizing nested Rust SDK submodules" |
| 171 | +git -C "$rust_dir" submodule sync --recursive |
| 172 | +git -C "$rust_dir" submodule update --init --recursive --checkout |
| 173 | + |
| 174 | +echo "==> Done. Review and commit the client-sdk-rust gitlink change." |
0 commit comments