Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·232 lines (208 loc) · 6.77 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·232 lines (208 loc) · 6.77 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
#!/usr/bin/env bash
#
# Install git-wrapper
#
# Installs:
# - Wrapper script to ~/.local/bin/git (or $GIT_WRAPPER_BIN)
# - Plugin directories to ~/.git.d/ (or $GIT_WRAPPER_CONFIG)
#
# Options:
# --bin-dir DIR Install wrapper to DIR (default: ~/.local/bin)
# --config-dir DIR Install plugins to DIR (default: ~/.git.d)
# --with-plugins Copy sample plugins to config directory
# --no-config Skip setting git config for scriptDir
# --dry-run Show what would be done without doing it
# --uninstall Remove installation (same as running uninstall.sh)
# -h, --help Show this help message
#
set -euo pipefail
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Installation defaults
BIN_DIR="${GIT_WRAPPER_BIN:-${HOME}/.local/bin}"
CONFIG_DIR="${GIT_WRAPPER_CONFIG:-${HOME}/.git.d}"
# Options
WITH_PLUGINS=false
SET_CONFIG=true
DRY_RUN=false
# Colors
C_GREEN=$'\033[32m'
C_YELLOW=$'\033[33m'
C_CYAN=$'\033[36m'
C_BOLD=$'\033[1m'
C_RESET=$'\033[0m'
usage() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Install git-wrapper.
Options:
--bin-dir DIR Install wrapper to DIR (default: ~/.local/bin)
--config-dir DIR Install plugins to DIR (default: ~/.git.d)
--with-plugins Copy sample plugins to config directory
--no-config Skip setting git config for scriptDir
--dry-run Show what would be done without doing it
--uninstall Remove installation (same as running uninstall.sh)
-h, --help Show this help message
Environment variables:
GIT_WRAPPER_BIN Override default bin directory
GIT_WRAPPER_CONFIG Override default config directory
Examples:
$(basename "$0") # Standard install
$(basename "$0") --with-plugins # Install with sample plugins
$(basename "$0") --dry-run # Preview installation
EOF
}
log() {
echo "${C_GREEN}==>${C_RESET} ${C_BOLD}$*${C_RESET}"
}
log_action() {
echo " ${C_CYAN}$1${C_RESET} $2"
}
warn() {
echo "${C_YELLOW}Warning:${C_RESET} $*" >&2
}
run() {
if ${DRY_RUN}; then
echo " ${C_YELLOW}[dry-run]${C_RESET} $*"
else
"$@"
fi
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--bin-dir)
BIN_DIR="$2"
shift 2
;;
--bin-dir=*)
BIN_DIR="${1#*=}"
shift
;;
--config-dir)
CONFIG_DIR="$2"
shift 2
;;
--config-dir=*)
CONFIG_DIR="${1#*=}"
shift
;;
--with-plugins)
WITH_PLUGINS=true
shift
;;
--no-config)
SET_CONFIG=false
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--uninstall)
exec "${SCRIPT_DIR}/uninstall.sh" "$@"
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 1
;;
esac
done
# Expand paths
BIN_DIR="${BIN_DIR/#\~/$HOME}"
CONFIG_DIR="${CONFIG_DIR/#\~/$HOME}"
echo "${C_BOLD}git-wrapper installer${C_RESET}"
echo ""
# Check if wrapper already exists
if [[ -e "${BIN_DIR}/git" ]]; then
if [[ -L "${BIN_DIR}/git" ]]; then
existing_target=$(readlink -f "${BIN_DIR}/git")
if [[ "${existing_target}" == "${SCRIPT_DIR}/bin/git" ]]; then
log "Wrapper already installed at ${BIN_DIR}/git"
else
warn "Existing symlink at ${BIN_DIR}/git points to: ${existing_target}"
warn "Remove it first or use a different --bin-dir"
exit 1
fi
else
warn "File already exists at ${BIN_DIR}/git (not a symlink)"
warn "Remove it first or use a different --bin-dir"
exit 1
fi
else
# Install wrapper
log "Installing wrapper"
log_action "create" "${BIN_DIR}/"
run mkdir -p "${BIN_DIR}"
log_action "symlink" "${BIN_DIR}/git -> ${SCRIPT_DIR}/bin/git"
run ln -sf "${SCRIPT_DIR}/bin/git" "${BIN_DIR}/git"
fi
# Create config directory structure
log "Setting up config directory"
log_action "create" "${CONFIG_DIR}/pre-process.d/"
run mkdir -p "${CONFIG_DIR}/pre-process.d"
log_action "create" "${CONFIG_DIR}/post-process.d/"
run mkdir -p "${CONFIG_DIR}/post-process.d"
# Copy plugins if requested
if ${WITH_PLUGINS}; then
log "Installing sample plugins"
for plugin in "${SCRIPT_DIR}/plugins/pre-process.d/"*.sh; do
[[ -f "${plugin}" ]] || continue
plugin_name=$(basename "${plugin}")
log_action "copy" "pre-process.d/${plugin_name}"
run cp "${plugin}" "${CONFIG_DIR}/pre-process.d/"
run chmod +x "${CONFIG_DIR}/pre-process.d/${plugin_name}"
done
for plugin in "${SCRIPT_DIR}/plugins/post-process.d/"*.sh; do
[[ -f "${plugin}" ]] || continue
plugin_name=$(basename "${plugin}")
log_action "copy" "post-process.d/${plugin_name}"
run cp "${plugin}" "${CONFIG_DIR}/post-process.d/"
run chmod +x "${CONFIG_DIR}/post-process.d/${plugin_name}"
done
fi
# Set git config
if ${SET_CONFIG}; then
log "Configuring git"
log_action "config" "wrapper.scriptDir = ${CONFIG_DIR}"
if ! ${DRY_RUN}; then
git config --global wrapper.scriptDir "${CONFIG_DIR}"
else
echo " ${C_YELLOW}[dry-run]${C_RESET} git config --global wrapper.scriptDir '${CONFIG_DIR}'"
fi
fi
echo ""
log "Installation complete!"
echo ""
# Check if BIN_DIR is in PATH
if ! ${DRY_RUN}; then
if [[ ":${PATH}:" == *":${BIN_DIR}:"* ]]; then
# Check if it comes before /usr/bin
__bin_pos=$(echo "${PATH}" | tr ':' '\n' | grep -n "^${BIN_DIR}$" | cut -d: -f1 | head -1)
__usr_pos=$(echo "${PATH}" | tr ':' '\n' | grep -n "^/usr/bin$" | cut -d: -f1 | head -1)
if [[ -n "${__bin_pos}" && -n "${__usr_pos}" && ${__bin_pos} -lt ${__usr_pos} ]]; then
echo "${C_GREEN}✓${C_RESET} ${C_CYAN}${BIN_DIR}${C_RESET} is in PATH (before /usr/bin)"
elif [[ -n "${__bin_pos}" ]]; then
warn "${BIN_DIR} is in PATH but AFTER /usr/bin"
echo " Move it earlier in PATH to use the wrapper by default."
fi
else
warn "${BIN_DIR} is not in your PATH"
echo " Add it to your shell config (e.g., ~/.bashrc):"
echo " ${C_CYAN}export PATH=\"${BIN_DIR}:\${PATH}\"${C_RESET}"
fi
echo ""
fi
echo "To add plugins, place executable scripts in:"
echo " ${C_CYAN}${CONFIG_DIR}/pre-process.d/${C_RESET} (run before git commands)"
echo " ${C_CYAN}${CONFIG_DIR}/post-process.d/${C_RESET} (run after git commands)"
echo ""
if ! ${WITH_PLUGINS}; then
echo "Run with ${C_CYAN}--with-plugins${C_RESET} to install sample plugins."
echo ""
fi