-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·433 lines (371 loc) · 14 KB
/
update.sh
File metadata and controls
executable file
·433 lines (371 loc) · 14 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env bash
# This file is auto-generated by scripts/build.sh
# DO NOT EDIT - modify scripts/src/*.src.sh instead and run 'make buildscripts'
set -e
# shellcheck disable=SC1091
# ── Inlined content from common.sh ──────────────────────────────────────────
# Color definitions - shared across all scripts
GRAY='\033[90m'
GREEN='\033[32m'
YELLOW='\033[33m'
RED='\033[31m'
BLUE='\033[34m'
NC='\033[0m' # No Color
# Basic logging functions
log() {
echo -e "${GRAY}git-undo:${NC} $1"
}
log_info() {
echo -e "${BLUE}[INFO]${NC} $*"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $*"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $*"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $*"
}
# _get_script_dir determines the directory of this file (common.sh) in a POSIX-portable way
#
# Works when:
# • the script is executed directly (`bash install.sh`)
# • the script is sourced from another Bash script
# • the outer shell is zsh (she-bang ensures Bash runs underneath)
_get_script_dir() {
# $1 = one element of ${BASH_SOURCE[@]} or the zsh %x expansion
local src="$1"
while [ -h "$src" ]; do # Resolve symlinks if any
local dir
dir="$(cd -P -- "$(dirname -- "$src")" && pwd)"
src="$(readlink "$src")"
[[ $src != /* ]] && src="$dir/$src"
done
# physical directory of the file itself
local dir
dir=$(cd -P -- "$(dirname -- "$src")" && pwd)
# Since common.sh is now in scripts/src/, we need to go up one level to get scripts/
# Remove /src suffix if present, otherwise assume we're already in scripts/
if [[ "$dir" == */src ]]; then
printf '%s' "${dir%/src}"
else
printf '%s' "$dir"
fi
}
if [[ -n "${BASH_SOURCE[0]:-}" ]]; then # Bash (she-bang path)
SCRIPT_DIR="$(_get_script_dir "${BASH_SOURCE[0]}")"
else # POSIX sh execution
SCRIPT_DIR="$(_get_script_dir "$0")"
fi
unset -f _get_script_dir
echo "SCRIPT DIR $SCRIPT_DIR"
# Coloring helpers
# shellcheck disable=SC1091
# Git-undo specific configuration
UNDO_BIN_NAME="git-undo"
BACK_BIN_NAME="git-back"
BIN_DIR=$(go env GOBIN 2>/dev/null || true)
[[ -z "$BIN_DIR" ]] && BIN_DIR="$(go env GOPATH)/bin"
export UNDO_BIN_PATH="$BIN_DIR/$UNDO_BIN_NAME"
export BACK_BIN_PATH="$BIN_DIR/$BACK_BIN_NAME"
# Legacy variable for backward compatibility
export BIN_PATH="$UNDO_BIN_PATH"
CFG_DIR="$HOME/.config/git-undo"
export BASH_HOOK="$CFG_DIR/git-undo-hook.bash"
export ZSH_HOOK="$CFG_DIR/git-undo-hook.zsh"
GIT_HOOKS_DIR="$CFG_DIR/hooks"
DISPATCHER_FILE="$GIT_HOOKS_DIR/git-hooks.sh"
DISPATCHER_SRC="$SCRIPT_DIR/scripts/git-undo-git-hook.sh"
REPO_OWNER="amberpixels"
REPO_NAME="git-undo"
export GITHUB_REPO_URL="github.com/$REPO_OWNER/$REPO_NAME"
GITHUB_API_URL="https://api.github.com/repos/$REPO_OWNER/$REPO_NAME"
export INSTALL_URL="https://raw.githubusercontent.com/$REPO_OWNER/$REPO_NAME/main/install.sh"
detect_shell() {
# Method 1: Check $SHELL environment variable (most reliable for login shell)
if [[ -n "$SHELL" ]]; then
case "$SHELL" in
*zsh*)
echo "zsh"
return
;;
*bash*)
echo "bash"
return
;;
esac
fi
# Method 2: Check shell-specific version variables
if [[ -n "$ZSH_VERSION" ]]; then
echo "zsh"
return
elif [[ -n "$BASH_VERSION" ]]; then
echo "bash"
return
fi
# If all methods fail
echo "unknown"
}
get_latest_version() {
local latest_release
if command -v curl >/dev/null 2>&1; then
latest_release=$(curl -s "$GITHUB_API_URL/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
elif command -v wget >/dev/null 2>&1; then
latest_release=$(wget -qO- "$GITHUB_API_URL/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
else
echo "error: curl or wget required for version check" >&2
return 1
fi
if [[ -z "$latest_release" || "$latest_release" == "null" ]]; then
echo "error: failed to fetch latest version" >&2
return 1
fi
echo "$latest_release"
}
version_compare() {
local version1="$1"
local version2="$2"
# Remove 'v' prefix if present
version1=${version1#v}
version2=${version2#v}
# Extract base version (everything before the first dash)
local base1
local base2
base1=$(echo "$version1" | cut -d'-' -f1)
base2=$(echo "$version2" | cut -d'-' -f1)
# Convert base versions to comparable format (e.g., 1.2.3 -> 001002003)
local v1
local v2
v1=$(echo "$base1" | awk -F. '{ printf("%03d%03d%03d\n", $1, $2, $3); }')
v2=$(echo "$base2" | awk -F. '{ printf("%03d%03d%03d\n", $1, $2, $3); }')
# Compare base versions first
if [[ "$v1" < "$v2" ]]; then
echo "older"
elif [[ "$v1" > "$v2" ]]; then
echo "newer"
else
# Base versions are the same, check for development version indicators
# If one has additional info (date, commit, branch) and the other doesn't,
# the one with additional info is newer
if [[ "$version1" == "$base1" && "$version2" != "$base2" ]]; then
# version1 is base tag, version2 is development version
echo "older"
elif [[ "$version1" != "$base1" && "$version2" == "$base2" ]]; then
# version1 is development version, version2 is base tag
echo "newer"
else
# Both are either base tags or both are development versions
echo "same"
fi
fi
}
install_dispatcher_into() {
local target="$1"
# Validate target directory
if [[ -z "$target" ]]; then
log_error "Target directory not specified"
return 1
fi
log "Installing git hooks into: $target"
# Create target directory if it doesn't exist
if ! mkdir -p "$target" 2>/dev/null; then
log_error "Failed to create hooks directory: $target"
return 1
fi
# 1) Install the dispatcher script only if target is our managed hooks directory
if [[ "$target" == "$GIT_HOOKS_DIR" ]]; then
log "Installing dispatcher script to: $DISPATCHER_FILE"
# Debug: Check if source file exists
if [[ ! -f "$DISPATCHER_SRC" ]]; then
log_error "Source dispatcher script not found: $DISPATCHER_SRC"
log_error "DISPATCHER_SRC variable: '$DISPATCHER_SRC'"
log_error "Contents of script directory:"
ls -la "$(dirname "$DISPATCHER_SRC")" 2>/dev/null || log_error "Cannot list script directory"
return 1
fi
log "Source file exists: $DISPATCHER_SRC"
log "Source file permissions: $(ls -l "$DISPATCHER_SRC")"
# Ensure the hooks directory exists
if ! mkdir -p "$GIT_HOOKS_DIR" 2>/dev/null; then
log_error "Failed to create git hooks directory: $GIT_HOOKS_DIR"
return 1
fi
log "Target directory exists: $GIT_HOOKS_DIR"
log "Target directory permissions: $(ls -ld "$GIT_HOOKS_DIR")"
# Try using cp instead of install command for better compatibility
if cp "$DISPATCHER_SRC" "$DISPATCHER_FILE" 2>/dev/null; then
chmod 755 "$DISPATCHER_FILE" 2>/dev/null || {
log_error "Failed to set permissions on dispatcher script"
return 1
}
log "Dispatcher copied and permissions set successfully"
else
log_error "Failed to copy dispatcher script from $DISPATCHER_SRC to $DISPATCHER_FILE"
log_error "Let's try to understand why:"
# More detailed debugging
log_error "Source file readable? $(test -r "$DISPATCHER_SRC" && echo "YES" || echo "NO")"
log_error "Target directory writable? $(test -w "$GIT_HOOKS_DIR" && echo "YES" || echo "NO")"
log_error "Disk space available? $(df -h "$GIT_HOOKS_DIR" | tail -1)"
return 1
fi
fi
# 2) Wire up post-commit & post-merge hooks
for hook in post-commit post-merge; do
local hook_file="$target/$hook"
log "Processing hook: $hook_file"
if [[ -f "$hook_file" && ! -L "$hook_file" ]]; then
# Existing regular file - append our hook call if not already present
log "Found existing hook file, checking if git-undo is already integrated"
if ! grep -q 'git-undo --hook' "$hook_file" 2>/dev/null; then
log "Adding git-undo integration to existing hook"
{
echo ""
echo "# git-undo integration"
echo "GIT_UNDO_INTERNAL_HOOK=1 git-undo --hook=\"$hook\""
} >> "$hook_file"
# Ensure it's executable
chmod +x "$hook_file" 2>/dev/null || {
log_error "Failed to make hook executable: $hook_file"
return 1
}
log "Successfully integrated with existing $hook hook"
else
log "git-undo already integrated in $hook hook"
fi
elif [[ -L "$hook_file" ]]; then
# It's a symlink - check if it points to our dispatcher
local link_target
link_target=$(readlink "$hook_file" 2>/dev/null || echo "")
if [[ "$link_target" != "$DISPATCHER_FILE" ]]; then
log_warning "Hook $hook_file is a symlink to $link_target, not our dispatcher"
log "This hook may not work with git-undo"
else
log "Hook $hook_file already points to our dispatcher"
fi
else
# No hook exists yet - create one
log "Creating new $hook hook"
# Try to create a symlink first (preferred method)
if ln -sf "$DISPATCHER_FILE" "$hook_file" 2>/dev/null; then
log "Created symlink: $hook_file -> $DISPATCHER_FILE"
else
# Fallback for filesystems that don't support symlinks
log "Symlink failed, creating standalone hook script"
cat > "$hook_file" << EOF
# git-undo hook - auto-generated
set -e
GIT_UNDO_INTERNAL_HOOK=1 git-undo --hook="$hook"
EOF
chmod +x "$hook_file" 2>/dev/null || {
log_error "Failed to make hook executable: $hook_file"
return 1
}
log "Created standalone hook: $hook_file"
fi
fi
done
log "Hook installation completed for: $target"
return 0
}
# ── End of inlined content ──────────────────────────────────────────────────
main() {
log "Checking for updates..."
# 1) Get current version from the binary itself
echo -en "${GRAY}git-undo:${NC} 1. Current version..."
local current_version
if ! current_version=$(git-undo version 2>/dev/null | awk '{print $2}'); then
echo -e " ${RED}FAILED${NC}"
log "Could not determine current version. Is git-undo installed?"
exit 1
fi
if [[ -z "$current_version" || "$current_version" == "unknown" ]]; then
echo -e " ${YELLOW}UNKNOWN${NC}"
log_warning "No version information found. Reinstall git-undo manually."
exit 1
else
echo -e " ${BLUE}$current_version${NC}"
fi
# 2) Get latest release version
echo -en "${GRAY}git-undo:${NC} 2. Checking latest release..."
local latest_version
if ! latest_version=$(get_latest_version); then
echo -e " ${RED}FAILED${NC}"
log "Failed to check latest version. Check your internet connection."
exit 1
fi
echo -e " ${BLUE}$latest_version${NC}"
# 3) Compare versions
echo -en "${GRAY}git-undo:${NC} 3. Comparing releases..."
local comparison
comparison=$(version_compare "$current_version" "$latest_version")
case "$comparison" in
"same")
echo -e " ${GREEN}UP TO DATE${NC}"
log "You're already running the latest release (${BLUE}$current_version${NC})"
exit 0
;;
"newer")
echo -e " ${YELLOW}NEWER${NC}"
log "You're running a newer release than available (${BLUE}$current_version${NC} > ${BLUE}$latest_version${NC})"
exit 0
;;
"older")
echo -e " ${YELLOW}UPDATE AVAILABLE${NC}"
;;
esac
# 4) Ask for confirmation
echo -e ""
echo -e "Update available: ${BLUE}$current_version${NC} → ${GREEN}$latest_version${NC}"
echo -en "Do you want to update? [Y/n]: "
read -r response
case "$response" in
[nN]|[nN][oO])
log "Update cancelled."
exit 0
;;
*)
;;
esac
# 5) Download and run new installer
echo -en "${GRAY}git-undo:${NC} 4. Downloading latest installer..."
local temp_installer
temp_installer=$(mktemp)
if command -v curl >/dev/null 2>&1; then
if curl -sL "$INSTALL_URL" -o "$temp_installer"; then
echo -e " ${GREEN}OK${NC}"
else
echo -e " ${RED}FAILED${NC}"
rm -f "$temp_installer"
exit 1
fi
elif command -v wget >/dev/null 2>&1; then
if wget -qO "$temp_installer" "$INSTALL_URL"; then
echo -e " ${GREEN}OK${NC}"
else
echo -e " ${RED}FAILED${NC}"
rm -f "$temp_installer"
exit 1
fi
else
echo -e " ${RED}FAILED${NC}"
log "curl or wget required for update"
exit 1
fi
# 6) Run the installer
echo -e ""
log "Running installer..."
chmod +x "$temp_installer"
"$temp_installer"
local install_status=$?
rm -f "$temp_installer"
if [[ $install_status -eq 0 ]]; then
log "${GREEN}Update completed successfully!${NC}"
log "Updated to version ${GREEN}$latest_version${NC}"
else
log "${RED}Update failed.${NC}"
exit 1
fi
}
# Run main function
main "$@"