-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_auto_pull.sh
More file actions
executable file
·207 lines (168 loc) · 4.65 KB
/
Copy pathgit_auto_pull.sh
File metadata and controls
executable file
·207 lines (168 loc) · 4.65 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
: <<'EOF'
Runs every 30 minutes via launchd (com.pythoninthegrass.git-auto-pull.plist) on macOS,
or via systemd (git-auto-pull.timer/.service) on Linux.
For every repo under ~/git with a commit or fetch in the last $DAYS days:
- registers it into git's global maintenance config (add-only; never
unregisters pre-existing entries)
- performs a guarded `git pull --ff-only`, skipping (and logging) any
repo that is dirty, detached, or has no upstream
Never merges, never rebases, never touches uncommitted work.
EOF
set -u
ROOT="${GIT_AUTO_PULL_ROOT:-$HOME/git}"
DAYS="${GIT_AUTO_PULL_DAYS:-15}"
if [ -n "${GIT_AUTO_PULL_LOG_DIR:-}" ]; then
LOG_DIR="$GIT_AUTO_PULL_LOG_DIR"
else
case "$(uname -s)" in
Darwin)
LOG_DIR="$HOME/Library/Logs"
;;
*)
LOG_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/git-auto-pull"
;;
esac
fi
mkdir -p "$LOG_DIR"
LOG="$LOG_DIR/git-auto-pull.log"
LOCK="${TMPDIR:-/tmp}/git-auto-pull.lock"
log() {
printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >>"$LOG"
}
setup_env() {
export PATH="/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/bin:/usr/local/bin:/usr/bin:/bin:$HOME/.local/bin:$PATH"
export GIT_TERMINAL_PROMPT=0
export GIT_SSH_COMMAND="ssh -o BatchMode=yes -o ConnectTimeout=10"
}
# Portable stat: BSD (macOS) uses -f, GNU (Linux) uses -c.
stat_size() {
stat -f %z "$1" 2>/dev/null || stat -c %s "$1" 2>/dev/null || printf '0\n'
}
stat_mtime() {
stat -f %m "$1" 2>/dev/null || stat -c %Y "$1" 2>/dev/null || printf '0\n'
}
rotate_log() {
# Trim the log if it's grown past ~1 MB, keeping the tail.
if [ -f "$LOG" ] && [ "$(stat_size "$LOG")" -gt 1048576 ]; then
tail -n 2000 "$LOG" >"${LOG}.tmp" && mv "${LOG}.tmp" "$LOG"
fi
}
acquire_lock() {
# mkdir-based lock: no-op if a previous run is still in flight.
if ! mkdir "$LOCK" 2>/dev/null; then
log "skip: already running (lock held: $LOCK)"
exit 0
fi
trap 'rmdir "$LOCK" 2>/dev/null' EXIT
}
# Latest activity epoch for a repo: newest of committerdate across all refs, # or FETCH_HEAD mtime.
repo_latest_epoch() {
repo="$1"
last_commit_epoch=$(git -C "$repo" for-each-ref --sort=-committerdate \
--format='%(committerdate:unix)' refs/ 2>/dev/null | head -1)
last_commit_epoch="${last_commit_epoch:-0}"
fetch_head_epoch=0
if [ -f "$repo/.git/FETCH_HEAD" ]; then
fetch_head_epoch=$(stat_mtime "$repo/.git/FETCH_HEAD")
fi
latest_epoch=$last_commit_epoch
if [ "$fetch_head_epoch" -gt "$latest_epoch" ]; then
latest_epoch=$fetch_head_epoch
fi
printf '%s\n' "$latest_epoch"
}
is_active() {
repo="$1"
cutoff_epoch="$2"
latest_epoch="$(repo_latest_epoch "$repo")"
[ "$latest_epoch" -ge "$cutoff_epoch" ]
}
# Add-only registration into git's global maintenance config.
register_repo() {
repo="$1"
name="$2"
if git config --global --get-all maintenance.repo 2>/dev/null | grep -Fxq "$repo"; then
return 0
fi
if git -C "$repo" maintenance register 2>>"$LOG"; then
log "$name: registered for git maintenance"
else
log "$name: error registering for git maintenance"
fi
}
# Classifies a repo's pull-readiness as one of: dirty, detached, no-upstream, clean.
repo_status() {
repo="$1"
if [ -n "$(git -C "$repo" status --porcelain 2>/dev/null)" ]; then
printf 'dirty\n'
return
fi
if ! git -C "$repo" symbolic-ref -q HEAD >/dev/null; then
printf 'detached\n'
return
fi
if ! git -C "$repo" rev-parse --abbrev-ref --symbolic-full-name '@{u}' >/dev/null 2>&1; then
printf 'no-upstream\n'
return
fi
printf 'clean\n'
}
do_pull() {
repo="$1"
name="$2"
branch="$(git -C "$repo" symbolic-ref --short HEAD)"
before="$(git -C "$repo" rev-parse HEAD)"
if pull_output=$(git -C "$repo" pull --ff-only --no-rebase 2>&1); then
after="$(git -C "$repo" rev-parse HEAD)"
if [ "$before" = "$after" ]; then
log "$name: already-current ($branch)"
else
log "$name: updated $before -> $after ($branch)"
fi
else
log "$name: error:${pull_output//$'\n'/ | }"
fi
}
# Guarded ff-only pull, dispatched on repo_status().
pull_repo() {
repo="$1"
name="$2"
case "$(repo_status "$repo")" in
dirty)
log "$name: skipped:dirty"
;;
detached)
log "$name: skipped:detached-head"
;;
no-upstream)
log "$name: skipped:no-upstream"
;;
clean)
do_pull "$repo" "$name"
;;
esac
}
process_repo() {
dir="$1"
cutoff_epoch="$2"
repo="${dir%/}"
[ -d "$repo/.git" ] || return
name="$(basename "$repo")"
is_active "$repo" "$cutoff_epoch" || return
register_repo "$repo" "$name"
pull_repo "$repo" "$name"
}
main() {
setup_env
rotate_log
acquire_lock
log "=== run start ==="
cutoff_epoch=$(( $(date +%s) - DAYS * 86400 ))
shopt -s nullglob
for dir in "$ROOT"/*/; do
process_repo "$dir" "$cutoff_epoch"
done
log "=== run end ==="
}
main "$@"