|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 5 | +SCRIPT="$ROOT/scripts/ensure_history.sh" |
| 6 | +TMP_DIR="$(mktemp -d)" |
| 7 | +trap 'rm -rf "$TMP_DIR"' EXIT |
| 8 | + |
| 9 | +init_remote_repo() { |
| 10 | + local name="$1" |
| 11 | + INIT_REMOTE="$TMP_DIR/$name-remote.git" |
| 12 | + INIT_WORK="$TMP_DIR/$name-work" |
| 13 | + |
| 14 | + git init --bare --initial-branch=main "$INIT_REMOTE" >/dev/null |
| 15 | + git clone "$INIT_REMOTE" "$INIT_WORK" >/dev/null |
| 16 | + git -C "$INIT_WORK" config user.email "test@example.com" |
| 17 | + git -C "$INIT_WORK" config user.name "Test" |
| 18 | + git -C "$INIT_WORK" branch -M main |
| 19 | +} |
| 20 | + |
| 21 | +run_history_check() { |
| 22 | + local repo="$1" |
| 23 | + local base_ref="$2" |
| 24 | + local output_file="$3" |
| 25 | + |
| 26 | + ( |
| 27 | + cd "$repo" |
| 28 | + BASE_REF="$base_ref" GITHUB_OUTPUT="$output_file" bash "$SCRIPT" |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +test_raw_sha_fetch_stays_shallow() { |
| 33 | + init_remote_repo raw-sha |
| 34 | + local remote="$INIT_REMOTE" |
| 35 | + local work="$INIT_WORK" |
| 36 | + local clone="$TMP_DIR/raw-sha-clone" |
| 37 | + local output_file="$TMP_DIR/raw-sha-output.txt" |
| 38 | + local base_sha |
| 39 | + |
| 40 | + printf 'one\n' > "$work/file.txt" |
| 41 | + git -C "$work" add file.txt |
| 42 | + git -C "$work" commit -m "one" >/dev/null |
| 43 | + base_sha="$(git -C "$work" rev-parse HEAD)" |
| 44 | + |
| 45 | + printf 'two\n' > "$work/file.txt" |
| 46 | + git -C "$work" add file.txt |
| 47 | + git -C "$work" commit -m "two" >/dev/null |
| 48 | + git -C "$work" push -u origin main >/dev/null |
| 49 | + |
| 50 | + git clone --depth 1 --branch main "file://$remote" "$clone" >/dev/null |
| 51 | + |
| 52 | + if git -C "$clone" rev-parse --verify "$base_sha^{commit}" >/dev/null 2>&1; then |
| 53 | + echo "raw SHA unexpectedly present before targeted fetch" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + |
| 57 | + run_history_check "$clone" "$base_sha" "$output_file" |
| 58 | + |
| 59 | + git -C "$clone" rev-parse --verify "$base_sha^{commit}" >/dev/null |
| 60 | + [[ -f "$(git -C "$clone" rev-parse --git-path shallow)" ]] |
| 61 | + grep -qx 'shallow=true' "$output_file" |
| 62 | +} |
| 63 | + |
| 64 | +test_branch_ref_falls_back_to_full_history() { |
| 65 | + init_remote_repo branch-ref |
| 66 | + local remote="$INIT_REMOTE" |
| 67 | + local work="$INIT_WORK" |
| 68 | + local clone="$TMP_DIR/branch-ref-clone" |
| 69 | + local output_file="$TMP_DIR/branch-ref-output.txt" |
| 70 | + |
| 71 | + printf 'base\n' > "$work/file.txt" |
| 72 | + git -C "$work" add file.txt |
| 73 | + git -C "$work" commit -m "base" >/dev/null |
| 74 | + git -C "$work" push -u origin main >/dev/null |
| 75 | + |
| 76 | + git -C "$work" checkout -b feature >/dev/null |
| 77 | + printf 'feature\n' > "$work/feature.txt" |
| 78 | + git -C "$work" add feature.txt |
| 79 | + git -C "$work" commit -m "feature" >/dev/null |
| 80 | + git -C "$work" push -u origin feature >/dev/null |
| 81 | + |
| 82 | + git -C "$work" checkout main >/dev/null |
| 83 | + printf 'main\n' > "$work/main.txt" |
| 84 | + git -C "$work" add main.txt |
| 85 | + git -C "$work" commit -m "main advance" >/dev/null |
| 86 | + git -C "$work" push >/dev/null |
| 87 | + |
| 88 | + git clone --depth 1 --branch feature "file://$remote" "$clone" >/dev/null |
| 89 | + |
| 90 | + if git -C "$clone" rev-parse --verify "origin/main^{commit}" >/dev/null 2>&1; then |
| 91 | + echo "origin/main unexpectedly present before branch fetch" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + run_history_check "$clone" "origin/main" "$output_file" |
| 96 | + |
| 97 | + git -C "$clone" rev-parse --verify "origin/main^{commit}" >/dev/null |
| 98 | + git -C "$clone" merge-base HEAD origin/main >/dev/null |
| 99 | + [[ ! -f "$(git -C "$clone" rev-parse --git-path shallow)" ]] |
| 100 | + grep -qx 'shallow=true' "$output_file" |
| 101 | +} |
| 102 | + |
| 103 | +test_raw_sha_fetch_stays_shallow |
| 104 | +test_branch_ref_falls_back_to_full_history |
| 105 | + |
| 106 | +echo "ensure_history tests passed" |
0 commit comments