-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathhooks.sh
More file actions
125 lines (99 loc) · 2.76 KB
/
hooks.sh
File metadata and controls
125 lines (99 loc) · 2.76 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
#!/usr/bin/env bash
# Hook execution system
# Run hooks for a specific phase
# Usage: run_hooks phase [env_vars...]
# Example: run_hooks postCreate REPO_ROOT="$root" WORKTREE_PATH="$path"
run_hooks() {
local phase="$1"
shift
# Get hooks from git config and .gtrconfig file
local hooks
hooks=$(cfg_get_all "gtr.hook.$phase" "hooks.$phase")
if [ -z "$hooks" ]; then
# No hooks configured for this phase
return 0
fi
log_step "Running $phase hooks..."
local hook_count=0
local failed=0
# Capture environment variable assignments in array to preserve quoting
local envs=("$@")
# Execute each hook in a subshell to isolate side effects
while IFS= read -r hook; do
[ -z "$hook" ] && continue
hook_count=$((hook_count + 1))
log_info "Hook $hook_count: $hook"
# Run hook in subshell with properly quoted environment exports
if (
# Export each KEY=VALUE exactly as passed, safely quoted
for kv in "${envs[@]}"; do
# shellcheck disable=SC2163
export "$kv"
done
# Execute the hook
eval "$hook"
); then
log_info "Hook $hook_count completed successfully"
else
local rc=$?
log_error "Hook $hook_count failed with exit code $rc"
failed=$((failed + 1))
fi
done <<EOF
$hooks
EOF
if [ "$failed" -gt 0 ]; then
log_warn "$failed hook(s) failed"
return 1
fi
return 0
}
# Run hooks in a specific directory
# Usage: run_hooks_in phase directory [env_vars...]
run_hooks_in() {
local phase="$1"
local directory="$2"
shift 2
local old_pwd
old_pwd=$(pwd)
if [ ! -d "$directory" ]; then
log_error "Directory does not exist: $directory"
return 1
fi
cd "$directory" || return 1
run_hooks "$phase" "$@"
local result=$?
cd "$old_pwd" || return 1
return $result
}
# Run hooks in current shell without subshell isolation
# Env vars set by hooks (e.g., source ./vars.sh) persist in the calling shell.
# IMPORTANT: Call from within a subshell to avoid polluting the main script.
# Usage: run_hooks_export phase [env_vars...]
# Example: ( cd "$dir" && run_hooks_export postCd REPO_ROOT="$root" )
run_hooks_export() {
local phase="$1"
shift
local hooks
hooks=$(cfg_get_all "gtr.hook.$phase" "hooks.$phase")
if [ -z "$hooks" ]; then
return 0
fi
log_step "Running $phase hooks..."
# Export env vars so hooks and child processes can see them
local kv
for kv in "$@"; do
# shellcheck disable=SC2163
export "$kv"
done
local hook_count=0
while IFS= read -r hook; do
[ -z "$hook" ] && continue
hook_count=$((hook_count + 1))
log_info "Hook $hook_count: $hook"
# eval directly (no subshell) so exports persist
eval "$hook" || log_warn "Hook $hook_count failed (continuing)"
done <<EOF
$hooks
EOF
}