-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabgit-install.sh
More file actions
88 lines (77 loc) · 2.63 KB
/
labgit-install.sh
File metadata and controls
88 lines (77 loc) · 2.63 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
#!/usr/bin/env bash
# labgit-install.sh
# Sets up labgit hooks and shell integration on a shared lab machine.
#
# Usage:
# ./labgit-install.sh # interactive
# ./labgit-install.sh --global # install hooks globally via core.hooksPath
# ./labgit-install.sh /path/to/repo # install hooks into a specific repo
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOOKS_DIR="${SCRIPT_DIR}/hooks"
GREEN='\033[0;32m'
BOLD='\033[1m'
RESET='\033[0m'
info() { echo -e "${GREEN}${BOLD}labgit:${RESET} $*"; }
install_hooks_to() {
local target="$1"
mkdir -p "$target"
for hook in pre-commit post-commit pre-push; do
cp "${HOOKS_DIR}/${hook}" "${target}/${hook}"
chmod +x "${target}/${hook}"
done
info "Hooks installed to ${target}"
}
# --- Parse arguments ---
if [[ "${1:-}" == "--global" ]]; then
# Global hooks via core.hooksPath
GLOBAL_HOOKS_DIR="${HOME}/.labgit/hooks"
install_hooks_to "$GLOBAL_HOOKS_DIR"
git config --global core.hooksPath "$GLOBAL_HOOKS_DIR"
info "Global core.hooksPath set to ${GLOBAL_HOOKS_DIR}"
info "All repos on this machine will now use labgit hooks."
echo ""
echo "Note: repos with their own .git/hooks will be overridden by the"
echo "global hooksPath. To exempt a repo, run inside it:"
echo " git config --unset core.hooksPath"
elif [[ -n "${1:-}" ]]; then
# Per-repo install
REPO_DIR="$1"
if [[ ! -d "${REPO_DIR}/.git" ]]; then
echo "ERROR: ${REPO_DIR} is not a git repository."
exit 1
fi
install_hooks_to "${REPO_DIR}/.git/hooks"
else
# Interactive
echo "labgit hook installer"
echo ""
echo "Options:"
echo " 1) Global -- all repos on this machine (sets core.hooksPath)"
echo " 2) Per-repo -- install into a specific repo"
echo ""
read -rp "Choice [1/2]: " choice
case "$choice" in
1)
GLOBAL_HOOKS_DIR="${HOME}/.labgit/hooks"
install_hooks_to "$GLOBAL_HOOKS_DIR"
git config --global core.hooksPath "$GLOBAL_HOOKS_DIR"
info "Global core.hooksPath set to ${GLOBAL_HOOKS_DIR}"
;;
2)
read -rp "Path to repo: " repo_path
if [[ ! -d "${repo_path}/.git" ]]; then
echo "ERROR: ${repo_path} is not a git repository."
exit 1
fi
install_hooks_to "${repo_path}/.git/hooks"
;;
*)
echo "Invalid choice."
exit 1
;;
esac
fi
echo ""
info "Done. Don't forget to source the shell integration in ~/.zshrc:"
echo " source ${SCRIPT_DIR}/labgit-shell-integration.sh"