|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Unit test for src/scripts/git_ungraft.sh. |
| 4 | +# |
| 5 | +# Builds a self-contained shallow clone from a local bare repo (no |
| 6 | +# network) and exercises three behaviors: |
| 7 | +# 1. Before parents are fetched: no candidates, shallow file unchanged |
| 8 | +# (mtime preserved). |
| 9 | +# 2. --dry-run: prints "Would ungraft <sha>" lines without modifying |
| 10 | +# .git/shallow. |
| 11 | +# 3. Default (non-dry) run: rewrites .git/shallow, removing the |
| 12 | +# entries whose parents are now locally present. |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 17 | +UNGRAFT="${REPO_ROOT}/src/scripts/git_ungraft.sh" |
| 18 | + |
| 19 | +TMP="$(mktemp -d)" |
| 20 | +trap 'rm -rf "${TMP}"' EXIT |
| 21 | + |
| 22 | +REMOTE="${TMP}/remote.git" |
| 23 | +SRC="${TMP}/src" |
| 24 | +CLIENT="${TMP}/client" |
| 25 | + |
| 26 | +# --- build a deterministic 20-commit linear history in a local bare repo --- |
| 27 | +# Enough commits that a small `--deepen` leaves the file non-empty so |
| 28 | +# cases 2 and 3 have something to assert against. |
| 29 | +git init --quiet --bare "${REMOTE}" |
| 30 | +git init --quiet -b main "${SRC}" |
| 31 | +( |
| 32 | + cd "${SRC}" |
| 33 | + for i in $(seq 1 20); do |
| 34 | + git -c user.email=t@t -c user.name=t commit --quiet --allow-empty -m "c${i}" |
| 35 | + done |
| 36 | + git push --quiet "${REMOTE}" main |
| 37 | +) |
| 38 | + |
| 39 | +# --- shallow clone at depth=1 --- |
| 40 | +git clone --quiet --depth=1 "file://${REMOTE}" "${CLIENT}" |
| 41 | +cd "${CLIENT}" |
| 42 | + |
| 43 | +shallow_file="${CLIENT}/.git/shallow" |
| 44 | +if [ ! -f "${shallow_file}" ]; then |
| 45 | + echo "FAIL: expected .git/shallow to exist after --depth=1 clone" |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | +shallow_before="$(cat "${shallow_file}")" |
| 49 | + |
| 50 | +# --- case 1: no candidates, no rewrite, mtime preserved --- |
| 51 | +# Parents of the shallow-boundary commit are NOT present locally, so |
| 52 | +# no entry is ungraftable. The script must not rewrite the file. |
| 53 | +mtime_before="$(stat -f %m "${shallow_file}" 2>/dev/null || stat -c %Y "${shallow_file}")" |
| 54 | +output="$(bash "${UNGRAFT}")" |
| 55 | +mtime_after="$(stat -f %m "${shallow_file}" 2>/dev/null || stat -c %Y "${shallow_file}")" |
| 56 | +if [ "${output}" != "No candidate commits to ungraft" ]; then |
| 57 | + echo "FAIL (case 1): expected 'No candidate commits to ungraft'" |
| 58 | + echo " got: ${output}" |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | +if [ "${mtime_before}" != "${mtime_after}" ]; then |
| 62 | + echo "FAIL (case 1): .git/shallow mtime changed on no-op run" |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | +if [ "$(cat "${shallow_file}")" != "${shallow_before}" ]; then |
| 66 | + echo "FAIL (case 1): .git/shallow contents changed on no-op run" |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | +echo "PASS (case 1): no candidates, shallow file untouched" |
| 70 | + |
| 71 | +# --- fetch HEAD's parent at depth=1 so HEAD's parents are locally present --- |
| 72 | +# This mirrors what src/scripts/git_fetch_parents.sh does in production: |
| 73 | +# the parent SHA is both present locally AND added to .git/shallow, |
| 74 | +# which makes HEAD (still in .git/shallow) ungraftable. |
| 75 | +head_sha="$(git rev-parse HEAD)" |
| 76 | +head_parent="$(git cat-file -p "${head_sha}" \ |
| 77 | + | awk '/^$/ { exit } $1 == "parent" { print $2 }' | head -n1)" |
| 78 | +if [ -z "${head_parent}" ]; then |
| 79 | + echo "FAIL: could not determine HEAD's parent SHA" |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | +git fetch --quiet --update-shallow --depth=1 origin "${head_parent}:__parent__" |
| 83 | +if [ ! -f "${shallow_file}" ]; then |
| 84 | + echo "FAIL: expected .git/shallow to remain after parent fetch" |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +# Pick a shallow entry whose parents are now all locally present. |
| 89 | +ungraftable="" |
| 90 | +while IFS= read -r sha || [ -n "${sha}" ]; do |
| 91 | + [ -z "${sha}" ] && continue |
| 92 | + all_present=1 |
| 93 | + parents="$(git cat-file -p "${sha}" | awk '/^$/ { exit } $1 == "parent" { print $2 }')" |
| 94 | + [ -z "${parents}" ] && continue # root commit — skip |
| 95 | + for p in ${parents}; do |
| 96 | + git cat-file -e "${p}^{commit}" 2>/dev/null || { all_present=0; break; } |
| 97 | + done |
| 98 | + if [ "${all_present}" -eq 1 ]; then |
| 99 | + ungraftable="${sha}" |
| 100 | + break |
| 101 | + fi |
| 102 | +done < "${shallow_file}" |
| 103 | +if [ -z "${ungraftable}" ]; then |
| 104 | + echo "FAIL: no shallow entry with locally-present parents after parent fetch" |
| 105 | + echo "--- .git/shallow ---" |
| 106 | + cat "${shallow_file}" |
| 107 | + echo "--------------------" |
| 108 | + exit 1 |
| 109 | +fi |
| 110 | +echo "Ungraftable candidate: ${ungraftable}" |
| 111 | + |
| 112 | +# --- case 2: --dry-run prints candidates without rewriting --- |
| 113 | +mtime_before="$(stat -f %m "${shallow_file}" 2>/dev/null || stat -c %Y "${shallow_file}")" |
| 114 | +contents_before="$(cat "${shallow_file}")" |
| 115 | +dry_output="$(bash "${UNGRAFT}" --dry-run)" |
| 116 | +mtime_after="$(stat -f %m "${shallow_file}" 2>/dev/null || stat -c %Y "${shallow_file}")" |
| 117 | +if ! grep -qxF "Would ungraft ${ungraftable}" <<< "${dry_output}"; then |
| 118 | + echo "FAIL (case 2): expected 'Would ungraft ${ungraftable}' in dry-run output" |
| 119 | + echo "--- dry-run output ---" |
| 120 | + echo "${dry_output}" |
| 121 | + echo "----------------------" |
| 122 | + exit 1 |
| 123 | +fi |
| 124 | +if [ "${mtime_before}" != "${mtime_after}" ]; then |
| 125 | + echo "FAIL (case 2): --dry-run changed .git/shallow mtime" |
| 126 | + exit 1 |
| 127 | +fi |
| 128 | +if [ "$(cat "${shallow_file}")" != "${contents_before}" ]; then |
| 129 | + echo "FAIL (case 2): --dry-run changed .git/shallow contents" |
| 130 | + exit 1 |
| 131 | +fi |
| 132 | +echo "PASS (case 2): --dry-run printed candidate and left shallow file untouched" |
| 133 | + |
| 134 | +# --- case 3: real run removes the ungraftable entry --- |
| 135 | +real_output="$(bash "${UNGRAFT}")" |
| 136 | +if ! grep -qxF "Ungrafted ${ungraftable}" <<< "${real_output}"; then |
| 137 | + echo "FAIL (case 3): expected 'Ungrafted ${ungraftable}' in output" |
| 138 | + echo "--- output ---" |
| 139 | + echo "${real_output}" |
| 140 | + echo "--------------" |
| 141 | + exit 1 |
| 142 | +fi |
| 143 | +if grep -qxF "${ungraftable}" "${shallow_file}"; then |
| 144 | + echo "FAIL (case 3): ${ungraftable} still present in .git/shallow after ungraft" |
| 145 | + exit 1 |
| 146 | +fi |
| 147 | +echo "PASS (case 3): ungraftable entry removed from .git/shallow" |
| 148 | + |
| 149 | +echo "PASS: test_git_ungraft.sh" |
0 commit comments