|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | +# -e: exit immediately if any command exits with non-zero |
| 4 | +# -u: treat unset variables as errors |
| 5 | +# -o pipefail: fail if any command in a pipeline fails |
| 6 | + |
| 7 | +# Usage: ./.devcontainer/setup-local-store.sh REPO_URL REF [REPO_URL REF …] |
| 8 | +# REPO_URL: Git repository HTTPS or SSH URL |
| 9 | +# REF: Either a commit SHA (7–40 hex chars), a PR number (digits), or a branch name |
| 10 | +if [ $# -lt 2 ] || (( $# % 2 )); then |
| 11 | + echo "Usage: $0 REPO_URL REF [REPO_URL REF …]" >&2 |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +# Define workspace directories under current working directory |
| 16 | +ROOT=$(pwd) |
| 17 | +WORKDIR="$ROOT/.local-store" # all cloned repos go here |
| 18 | +CACHE="$WORKDIR/.cache" # store last-built SHAs here |
| 19 | +mkdir -p "$WORKDIR" "$CACHE" # ensure directories exist |
| 20 | + |
| 21 | +# Process each pair of arguments: repository URL + reference |
| 22 | +while (( $# )); do |
| 23 | + REPO=$1; REF=$2; shift 2 |
| 24 | + |
| 25 | + # Derive short name from repo URL (strip trailing '.git') |
| 26 | + NAME=$(basename "$REPO" .git) |
| 27 | + REPO_DIR="$WORKDIR/$NAME" |
| 28 | + |
| 29 | + # Clone the repo if not already cloned |
| 30 | + mkdir -p "$REPO_DIR" |
| 31 | + [ -d "$REPO_DIR/.git" ] || git clone "$REPO" "$REPO_DIR" |
| 32 | + |
| 33 | + # Enter repository directory quietly |
| 34 | + pushd "$REPO_DIR" >/dev/null |
| 35 | + |
| 36 | + # Fetch all remote updates and remove deleted refs |
| 37 | + git fetch origin --prune |
| 38 | + |
| 39 | + # Initialize flag for detached-SHA workflow |
| 40 | + DETACHED= |
| 41 | + |
| 42 | + # Case A: REF is a full (7–40 char) hex commit SHA |
| 43 | + if [[ $REF =~ ^[0-9a-f]{7,40}$ ]]; then |
| 44 | + # commit SHA |
| 45 | + BRANCH="FETCH_HEAD" |
| 46 | + SHA="$REF" |
| 47 | + DETACHED=1 |
| 48 | + # explicitly fetch that commit |
| 49 | + git fetch --no-tags --depth=1 origin "$SHA" |
| 50 | + |
| 51 | + # Case B: REF is all digits → treat as GitHub pull request number |
| 52 | + elif [[ $REF =~ ^[0-9]+$ ]]; then |
| 53 | + REMOTE_REF="pull/$REF/head" |
| 54 | + BRANCH="pr-$REF" |
| 55 | + |
| 56 | + # Case C: otherwise, REF is a branch name under refs/heads |
| 57 | + else |
| 58 | + REMOTE_REF="refs/heads/$REF" |
| 59 | + # sanitize branch name (slashes → dashes) |
| 60 | + BRANCH=${REF//\//-} |
| 61 | + fi |
| 62 | + |
| 63 | + # If not in detached-SHA mode, fetch the specific remote ref into our origin namespace |
| 64 | + if [[ -z ${DETACHED:-} ]]; then |
| 65 | + git fetch origin "$REMOTE_REF:refs/remotes/origin/$BRANCH" --force |
| 66 | + # Resolve the commit SHA on origin/<branch> |
| 67 | + SHA=$(git rev-parse "origin/$BRANCH") |
| 68 | + fi |
| 69 | + |
| 70 | + # Prepare cache filename for this repository (single file per repo) |
| 71 | + CACHE_FILE="$CACHE/$NAME.sha" |
| 72 | + |
| 73 | + # If last-built SHA matches current SHA, skip the build |
| 74 | + if [[ -f $CACHE_FILE && $(<"$CACHE_FILE") == "$SHA" ]]; then |
| 75 | + echo "$NAME/$BRANCH @ $SHA — up to date, skipping build" |
| 76 | + else |
| 77 | + # Otherwise, reset, clean, and check out the required commit/branch |
| 78 | + if [[ -n ${DETACHED:-} ]]; then |
| 79 | + # Detached-SHA: reset to the SHA, remove untracked files, then check out the SHA |
| 80 | + git reset --hard FETCH_HEAD |
| 81 | + git clean -fd |
| 82 | + git -c advice.detachedHead=false checkout FETCH_HEAD |
| 83 | + else |
| 84 | + # Branch or PR: reset to origin/<branch>, remove untracked files, recreate local branch |
| 85 | + git reset --hard "origin/$BRANCH" |
| 86 | + git clean -fd |
| 87 | + git checkout -B "$BRANCH" "origin/$BRANCH" |
| 88 | + fi |
| 89 | + |
| 90 | + # Update cache and invoke build steps |
| 91 | + echo "$SHA" >"$CACHE_FILE" |
| 92 | + echo "Building $NAME/$BRANCH @ $SHA" |
| 93 | + pnpm install |
| 94 | + pnpm build |
| 95 | + fi |
| 96 | + |
| 97 | + # Return to the root directory before next iteration |
| 98 | + popd >/dev/null |
| 99 | +done |
0 commit comments