-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
60 lines (56 loc) · 1.84 KB
/
pre-commit
File metadata and controls
60 lines (56 loc) · 1.84 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
#!/usr/bin/env bash
# labgit pre-commit hook
# Blocks commits unless the user has identified via 'git identify'.
#
# Install globally: git config --global core.hooksPath /opt/labgit/hooks
# Install per-repo: cp this file .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
SESSION_FILE="${HOME}/.labgit-session"
LABGIT_TIMEOUT="${LABGIT_TIMEOUT:-28800}"
# --- Helper: check session file age ---
_labgit_session_valid() {
if [[ ! -f "$SESSION_FILE" ]]; then
return 1
fi
local now file_mod age
now=$(date +%s)
if stat --version &>/dev/null 2>&1; then
file_mod=$(stat -c %Y "$SESSION_FILE" 2>/dev/null) || return 1
else
file_mod=$(stat -f %m "$SESSION_FILE" 2>/dev/null) || return 1
fi
age=$(( now - file_mod ))
(( age <= LABGIT_TIMEOUT ))
}
# --- Check identity: env vars OR session file, but always check timeout ---
if _labgit_session_valid; then
# Session file exists and is within timeout
if [[ -n "${LABGIT_USER:-}" ]]; then
# Env vars set (terminal) -- good to go
exit 0
fi
# No env vars (IDE) -- source the session file
if [[ -O "$SESSION_FILE" ]]; then
source "$SESSION_FILE"
if [[ -n "${LABGIT_USER:-}" ]]; then
exit 0
fi
fi
else
# Session expired or missing -- clean up
rm -f "$SESSION_FILE" 2>/dev/null
fi
# --- No valid identity ---
echo ""
echo "BLOCKED: You must identify yourself before committing."
echo ""
echo " Run: git identify <username>"
echo ""
if [[ -f "${LABGIT_CONF:-/opt/labgit/labgit-users.conf}" ]]; then
echo "Registered users:"
while IFS='|' read -r uname fullname email sshkey; do
[[ "$uname" =~ ^#.*$ || -z "$uname" ]] && continue
printf " %-12s %s <%s>\n" "$uname" "$fullname" "$email"
done < "${LABGIT_CONF:-/opt/labgit/labgit-users.conf}"
echo ""
fi
exit 1