-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabgit-shell-integration.sh
More file actions
265 lines (233 loc) · 8.97 KB
/
labgit-shell-integration.sh
File metadata and controls
265 lines (233 loc) · 8.97 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env bash
# labgit-shell-integration.sh
# Source this in your ~/.zshrc or ~/.bashrc:
#
# source /opt/labgit/labgit-shell-integration.sh
#
# Provides: git identify, git whoami, git logout, git renew, git users,
# git add-user, git remove-user
#
# Enforcement is handled by git hooks (pre-commit, pre-push, post-commit),
# not by wrapping the git binary. This means IDEs work naturally.
LABGIT_DIR="${LABGIT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)}"
export LABGIT_CONF="${LABGIT_CONF:-${LABGIT_DIR}/labgit-users.conf}"
LABGIT_SESSION_FILE="${HOME}/.labgit-session"
LABGIT_TIMEOUT="${LABGIT_TIMEOUT:-28800}"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
_labgit_format_duration() {
local secs=$1
local hours=$(( secs / 3600 ))
local mins=$(( (secs % 3600) / 60 ))
if (( hours > 0 )); then
echo "${hours}h ${mins}m"
else
echo "${mins}m"
fi
}
_labgit_session_age() {
if [[ ! -f "$LABGIT_SESSION_FILE" ]]; then
echo "999999"
return
fi
local now file_mod
now=$(date +%s)
if stat --version &>/dev/null 2>&1; then
file_mod=$(stat -c %Y "$LABGIT_SESSION_FILE" 2>/dev/null) || { echo "999999"; return; }
else
file_mod=$(stat -f %m "$LABGIT_SESSION_FILE" 2>/dev/null) || { echo "999999"; return; }
fi
echo $(( now - file_mod ))
}
_labgit_session_expired() {
local age
age=$(_labgit_session_age)
(( age > LABGIT_TIMEOUT ))
}
_labgit_session_remaining() {
local age remaining
age=$(_labgit_session_age)
remaining=$(( LABGIT_TIMEOUT - age ))
if (( remaining < 0 )); then
echo "0"
else
echo "$remaining"
fi
}
_labgit_write_session() {
cat > "$LABGIT_SESSION_FILE" <<EOF
export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME}"
export GIT_AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL}"
export GIT_COMMITTER_NAME="${GIT_COMMITTER_NAME}"
export GIT_COMMITTER_EMAIL="${GIT_COMMITTER_EMAIL}"
export LABGIT_USER="${LABGIT_USER}"
EOF
if [[ -n "${GIT_SSH_COMMAND:-}" ]]; then
echo "export GIT_SSH_COMMAND=\"${GIT_SSH_COMMAND}\"" >> "$LABGIT_SESSION_FILE"
fi
chmod 600 "$LABGIT_SESSION_FILE"
}
_labgit_clear_session() {
rm -f "$LABGIT_SESSION_FILE"
}
_labgit_list_users() {
if [[ ! -f "$LABGIT_CONF" ]]; then
echo "No user config found: ${LABGIT_CONF}"
return 1
fi
echo "Registered lab users:"
echo ""
while IFS='|' read -r uname fullname email sshkey; do
[[ "$uname" =~ ^#.*$ || -z "$uname" ]] && continue
printf " \033[0;36m%-12s\033[0m %s <%s>" "$uname" "$fullname" "$email"
if [[ -n "${sshkey:-}" ]]; then
printf " [SSH: %s]" "$sshkey"
fi
echo ""
done < "$LABGIT_CONF"
}
# ---------------------------------------------------------------------------
# Restore session on shell startup
# ---------------------------------------------------------------------------
if [[ -z "${LABGIT_USER:-}" && -f "$LABGIT_SESSION_FILE" && -O "$LABGIT_SESSION_FILE" ]]; then
if _labgit_session_expired; then
echo "labgit: Session expired (timeout: $(_labgit_format_duration "$LABGIT_TIMEOUT")). Run 'git identify <username>'."
rm -f "$LABGIT_SESSION_FILE"
else
source "$LABGIT_SESSION_FILE"
if [[ -n "${LABGIT_USER:-}" ]]; then
echo "labgit: Restored session for ${GIT_AUTHOR_NAME} <${GIT_AUTHOR_EMAIL}> ($(_labgit_format_duration "$(_labgit_session_remaining)") remaining)"
fi
fi
fi
# ---------------------------------------------------------------------------
# Unset global git identity to prevent fallback
# ---------------------------------------------------------------------------
_labgit_real_git="$(command -v git 2>/dev/null || echo /usr/bin/git)"
if "$_labgit_real_git" config --global user.name &>/dev/null; then
"$_labgit_real_git" config --global --unset user.name 2>/dev/null || true
fi
if "$_labgit_real_git" config --global user.email &>/dev/null; then
"$_labgit_real_git" config --global --unset user.email 2>/dev/null || true
fi
# ---------------------------------------------------------------------------
# Git subcommand extensions (via git aliases would also work, but a function
# gives us env var control)
# ---------------------------------------------------------------------------
git() {
local cmd="${1:-}"
case "$cmd" in
identify)
shift
local username="${1:-}"
if [[ -z "$username" ]]; then
echo "Usage: git identify <username>"
echo ""
_labgit_list_users
return 1
fi
# Look up user in config
if [[ ! -f "$LABGIT_CONF" ]]; then
echo "ERROR: No user config: ${LABGIT_CONF}"
return 1
fi
local entry
entry=$(grep -i "^${username}|" "$LABGIT_CONF" 2>/dev/null | head -1)
if [[ -z "$entry" ]]; then
echo "ERROR: Unknown user '${username}'."
echo ""
_labgit_list_users
return 1
fi
IFS='|' read -r _uname fullname email sshkey <<< "$entry"
export GIT_AUTHOR_NAME="$fullname"
export GIT_AUTHOR_EMAIL="$email"
export GIT_COMMITTER_NAME="$fullname"
export GIT_COMMITTER_EMAIL="$email"
export LABGIT_USER="$_uname"
if [[ -n "${sshkey:-}" ]]; then
export GIT_SSH_COMMAND="ssh -i ${sshkey}"
fi
_labgit_write_session
echo -e "\033[0;32m\033[1mlabgit:\033[0m Identified as: \033[1m${fullname}\033[0m <${email}>"
if [[ -n "${sshkey:-}" ]]; then
echo -e " SSH key: ${sshkey}"
fi
echo -e " Expires in $(_labgit_format_duration "$LABGIT_TIMEOUT")"
;;
whoami)
if [[ -n "${LABGIT_USER:-}" ]]; then
echo -e "\033[0;32m\033[1mlabgit:\033[0m ${GIT_AUTHOR_NAME} <${GIT_AUTHOR_EMAIL}> (user: ${LABGIT_USER})"
if [[ -f "$LABGIT_SESSION_FILE" ]]; then
echo -e " Expires in $(_labgit_format_duration "$(_labgit_session_remaining)")"
fi
else
echo "Not identified. Run 'git identify <username>'."
echo ""
_labgit_list_users
return 1
fi
;;
logout|unidentify)
_labgit_clear_session
unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
unset GIT_SSH_COMMAND LABGIT_USER
echo -e "\033[0;32m\033[1mlabgit:\033[0m Identity cleared."
;;
renew)
if [[ -z "${LABGIT_USER:-}" ]]; then
echo "No active session to renew. Run 'git identify <username>'."
return 1
fi
_labgit_write_session
echo -e "\033[0;32m\033[1mlabgit:\033[0m Session renewed. Expires in $(_labgit_format_duration "$LABGIT_TIMEOUT")."
;;
users)
_labgit_list_users
;;
add-user)
shift
local au_name="${1:-}" au_full="${2:-}" au_email="${3:-}" au_ssh="${4:-}"
if [[ -z "$au_name" || -z "$au_full" || -z "$au_email" ]]; then
echo "Usage: git add-user <username> \"Full Name\" <email> [ssh_key_path]"
echo ""
echo "Example:"
echo " git add-user barnaby \"Barnaby Norris\" barnaby@example.com ~/.ssh/id_ed25519_barnaby"
return 1
fi
if [[ ! -f "$LABGIT_CONF" ]]; then
cat > "$LABGIT_CONF" <<'HEADER'
# labgit user configuration
# Format: username|Full Name|email|ssh_key_path (ssh_key_path is optional)
HEADER
fi
if grep -qi "^${au_name}|" "$LABGIT_CONF" 2>/dev/null; then
echo "ERROR: User '${au_name}' already exists."
return 1
fi
echo "${au_name}|${au_full}|${au_email}|${au_ssh}" >> "$LABGIT_CONF"
echo -e "\033[0;32m\033[1mlabgit:\033[0m Added user: ${au_name} (${au_full} <${au_email}>)"
;;
remove-user)
shift
local ru_name="${1:-}"
if [[ -z "$ru_name" ]]; then
echo "Usage: git remove-user <username>"
return 1
fi
if ! grep -qi "^${ru_name}|" "$LABGIT_CONF" 2>/dev/null; then
echo "ERROR: User '${ru_name}' not found."
return 1
fi
grep -vi "^${ru_name}|" "$LABGIT_CONF" > "${LABGIT_CONF}.tmp"
mv "${LABGIT_CONF}.tmp" "$LABGIT_CONF"
echo -e "\033[0;32m\033[1mlabgit:\033[0m Removed user: ${ru_name}"
;;
*)
# Everything else: pass through to real git
"$_labgit_real_git" "$@"
;;
esac
}