File size: 2,172 Bytes
42c0d23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# Auto-commit + push loop for CausalGrok.
#
# Every INTERVAL seconds (default 900 = 15 min) this:
#   1. Stages safe paths (paper_figures/, experiments/*.py, scripts/*.sh,
#      *.md, plus mechinterp/*.json under experiments/runs/) — never
#      adds checkpoints (*.pt) or large per-run logs.
#   2. If there are staged changes, commits with a uniform message and
#      pushes to origin master.
#   3. Logs everything to logs/auto_commit_push.log.
#
# Usage:
#   nohup bash scripts/auto_commit_push.sh > /dev/null 2>&1 &
#   disown
#   tail -f logs/auto_commit_push.log
#
# Stop:
#   pkill -f "auto_commit_push.sh"

set +e   # tolerate transient git/network errors; loop continues

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

INTERVAL="${INTERVAL:-900}"   # 15 min default
LOG="logs/auto_commit_push.log"
mkdir -p logs

stamp() { date -u +"[%FT%TZ]"; }

echo "$(stamp) auto-pusher starting (interval ${INTERVAL}s, repo=$(pwd))" >> "$LOG"

while true; do
    # Stage all paths the user wants on GitHub.
    # `git add -A` would also add .pt files but they're now in .gitignore so
    # they're excluded automatically.
    git add -A 2>>"$LOG"

    # Belt-and-suspenders: explicitly drop any *.pt that might have slipped
    # through (e.g., if .gitignore is briefly missing the rule).
    git diff --cached --name-only | grep '\.pt$' | while read f; do
        git rm --cached -- "$f" 2>/dev/null
    done

    # If anything is staged, commit + push.
    if ! git diff --cached --quiet; then
        files=$(git diff --cached --name-only | wc -l | tr -d ' ')
        msg="auto: snapshot $(date -u +%FT%TZ) ($files file$([ "$files" = 1 ] || echo s))"
        echo "$(stamp) committing: $msg" >> "$LOG"
        git diff --cached --name-only | sed 's/^/    /' >> "$LOG"
        if git commit -m "$msg" >>"$LOG" 2>&1; then
            git push origin master >>"$LOG" 2>&1
            echo "$(stamp) pushed." >> "$LOG"
        else
            echo "$(stamp) commit failed; resetting index" >> "$LOG"
            git reset >>"$LOG" 2>&1
        fi
    else
        echo "$(stamp) nothing to commit" >> "$LOG"
    fi

    sleep "$INTERVAL"
done