-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset.sh
More file actions
executable file
·97 lines (77 loc) · 2.41 KB
/
reset.sh
File metadata and controls
executable file
·97 lines (77 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
set -euo pipefail
BASELINE_TAG="baseline"
MAIN_BRANCH="main"
REMOTE="origin"
usage() {
cat <<EOF
Usage: ./reset.sh <command>
Commands:
snapshot Tag the current HEAD as the baseline state and push the tag to the remote.
restore Hard-reset main to the baseline tag, wipe all untracked files
(including node_modules), and force-push to the remote.
Examples:
./reset.sh snapshot # Run once to save the current state
./reset.sh restore # Run after each test to reset everything
EOF
}
snapshot() {
echo "==> Creating baseline snapshot..."
# Warn if working tree is dirty
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "WARNING: Working tree has uncommitted changes. Snapshotting HEAD anyway."
fi
local sha
sha=$(git rev-parse --short HEAD)
git tag -f "$BASELINE_TAG" HEAD
echo " Tagged HEAD ($sha) as '$BASELINE_TAG'"
git push "$REMOTE" "$BASELINE_TAG" --force
echo " Pushed '$BASELINE_TAG' tag to $REMOTE"
echo "==> Snapshot complete. Baseline is commit $sha."
}
restore() {
# Verify baseline tag exists
if ! git rev-parse "$BASELINE_TAG" >/dev/null 2>&1; then
echo "ERROR: No '$BASELINE_TAG' tag found. Run './reset.sh snapshot' first."
exit 1
fi
local baseline_sha
baseline_sha=$(git rev-parse --short "$BASELINE_TAG")
echo "==> Restoring to baseline ($baseline_sha)..."
# Ensure we're on main
echo " Checking out $MAIN_BRANCH..."
git checkout "$MAIN_BRANCH"
# Hard-reset to the baseline tag
echo " Resetting to '$BASELINE_TAG'..."
git reset --hard "$BASELINE_TAG"
# Remove all untracked files and directories, including node_modules
echo " Cleaning untracked files (including node_modules)..."
git clean -fdx
# Force-push main to match the restored state
echo " Force-pushing $MAIN_BRANCH to $REMOTE..."
git push "$REMOTE" "$MAIN_BRANCH" --force
echo "==> Restore complete. $MAIN_BRANCH is now at baseline ($baseline_sha)."
echo " Run 'npm install --legacy-peer-deps' to reinstall dependencies."
}
# --- Main ---
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
case "$1" in
snapshot)
snapshot
;;
restore)
restore
;;
-h|--help|help)
usage
;;
*)
echo "ERROR: Unknown command '$1'"
echo
usage
exit 1
;;
esac