fr-research/commit_face_search_research_changes.sh
2026-11-12 10:12:20 +00:00

116 lines
4.3 KiB
Bash

#!/usr/bin/env bash
set -Eeuo pipefail
# Run this on the research server, preferably after activating face-search-ann.
PROJECT="/mnt/18T/fr_research/face-search-research-audit-v1"
CODE_DIR="$PROJECT/face_search_baseline"
IVF_FILE="$CODE_DIR/src/ivf_portability_evaluation.py"
COMMIT_MESSAGE="${COMMIT_MESSAGE:-Integrate audited face-search research changes through v1.14.1}"
fail() { printf 'ERROR: %s\n' "$*" >&2; exit 2; }
[[ -d "$PROJECT" ]] || fail "Project directory not found: $PROJECT (run this on the research server)."
[[ -d "$CODE_DIR" ]] || fail "Code directory not found: $CODE_DIR"
if TOP="$(git -C "$PROJECT" rev-parse --show-toplevel 2>/dev/null)"; then
:
elif TOP="$(git -C "$CODE_DIR" rev-parse --show-toplevel 2>/dev/null)"; then
:
else
fail "No Git repository found at the project root or face_search_baseline."
fi
TOP_REAL="$(realpath "$TOP")"
PROJECT_REAL="$(realpath "$PROJECT")"
printf 'Detected Git root: %s\n' "$TOP_REAL"
printf 'Project directory: %s\n' "$PROJECT_REAL"
# Allow the repository to be either the project itself or an ancestor directory.
# In the ancestor case, stage ONLY the project subtree, not unrelated repository files.
if [[ "$TOP_REAL" == "$PROJECT_REAL" ]]; then
STAGE_PATH="."
elif [[ "$PROJECT_REAL" == "$TOP_REAL/"* ]]; then
STAGE_PATH="${PROJECT_REAL#"$TOP_REAL"/}"
else
fail "The project directory is not inside the detected Git repository. No commit was made."
fi
printf 'Git path to stage: %s\n' "$STAGE_PATH"
[[ -f "$PROJECT/.gitignore" ]] || fail "Expected project .gitignore is missing; refusing to stage a potentially large dataset/index tree."
[[ -f "$IVF_FILE" ]] || fail "IVF portability source not found: $IVF_FILE"
# The previous IVF replication failed because GLOBAL and ROUTED worker artifact paths collided.
# Require the v1.14.1 hotfix before committing the current research state.
if ! grep -q 'def _worker_json_path' "$IVF_FILE" || \
! grep -q 'def _worker_per_query_path' "$IVF_FILE" || \
! grep -q 'def _load_per_query_materialized' "$IVF_FILE"; then
cat >&2 <<MSG
ERROR: The v1.14.1 IVF artifact-collision hotfix is not present.
Apply face-search-research-audit-v1-PATCH-v1.14.1.zip, run its focused test, then rerun this script.
No files have been staged or committed by this script.
MSG
exit 2
fi
cd "$TOP_REAL"
# A normal `git commit` includes every staged path. Stop if the index already contains
# staged changes outside this project, so unrelated work cannot slip into this commit.
if [[ "$STAGE_PATH" != "." ]]; then
outside_staged=""
while IFS= read -r path; do
[[ -z "$path" ]] && continue
case "$path" in
"$STAGE_PATH"|"$STAGE_PATH"/*) ;;
*) outside_staged+=" $path\n" ;;
esac
done < <(git diff --cached --name-only)
if [[ -n "$outside_staged" ]]; then
printf 'ERROR: There are already-staged paths outside the target project:\n%b' "$outside_staged" >&2
printf 'Unstage or commit those separately, then rerun. No commit was made.\n' >&2
exit 2
fi
fi
printf '\nCurrent repository status:\n'
git status --short --branch
printf '\nChecking whitespace errors in the project...\n'
git diff --check -- "$STAGE_PATH"
printf '\nRunning project tests...\n'
(
cd "$PROJECT"
python -m pytest -q face_search_baseline/tests
)
printf '\nStaging non-ignored changes from the project subtree only...\n'
git add -A -- "$STAGE_PATH"
git diff --cached --check
# Hard stop if data/model/index/bulk/secret artifacts slipped into the staged set.
unsafe="$(git diff --cached --name-only --diff-filter=ACMR | grep -Ei '(^|/)(\.env([^/]*)?|id_rsa|id_ed25519|credentials[^/]*|secrets?[^/]*)$|\.(faiss|parquet|npy|npz|bin|pt|pth|ckpt|safetensors|arrow|h5|pkl|jpg|jpeg|pdf|index)$|(^|/)state/' || true)"
if [[ -n "$unsafe" ]]; then
printf '\nRefusing to commit: possible data/model/secret artifacts are staged:\n%s\n' "$unsafe" >&2
printf 'Review with: git diff --cached --name-only\n' >&2
exit 2
fi
if git diff --cached --quiet; then
printf '\nNo staged changes found; nothing to commit.\n'
exit 0
fi
printf '\nStaged summary:\n'
git diff --cached --stat
printf '\nStaged paths:\n'
git diff --cached --name-only
printf '\nCreating local commit (this script does not push to any remote)...\n'
git commit -m "$COMMIT_MESSAGE"
printf '\nCommit created:\n'
git log -1 --oneline
printf '\nFinal status:\n'
git status --short --branch