Repository navigation
Expand file tree
/
Copy pathhelpers.sh
More file actions
555 lines (519 loc) · 22.1 KB
/
Copy pathhelpers.sh
File metadata and controls
555 lines (519 loc) · 22.1 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/usr/bin/env bash
#
# Shared helpers, sourced by install.sh, doctor.sh, snapshot-private.sh and
# tests/run.sh. The sourcing script owns `set -euo pipefail` and IFS and must
# set SCRIPT_DIR to the dotfiles clone. Written for macOS bash 3.2 and Linux.
ALREADY_INSTALLED_MSG="is already installed"
# ------------- Logging -------------
log(){ printf "\033[1;34m[dotfiles]\033[0m %s\n" "$*"; }
warn(){ printf "\033[1;33m[warn]\033[0m %s\n" "$*"; }
err(){ printf "\033[1;31m[err]\033[0m %s\n" "$*" >&2; }
section(){ printf "\n\033[1;32m== %s ==\033[0m\n" "$*"; }
# ------------- What gets deployed -------------
# "<path in repo>:<path in $HOME>": shared, read-only config, symlinked.
# The global ignore file is deployed as ~/.gitignore (core.excludesfile) so the
# repo's own .gitignore can stay repo-local.
# shellcheck disable=SC2034 # Read by install.sh and doctor.sh, which source this file.
LINKED_DOTFILES=(
".gitmessage:.gitmessage"
".gitignore_global:.gitignore"
".tmux.conf:.tmux.conf"
".spaceship.zsh:.spaceship.zsh"
)
# "<name>:<kind>": regular, untracked files in $HOME that load the shared file
# from the repo. Tool installers that append to ~/.zshrc or run
# `git config --global` write to these local files, never to the tracked ones.
# shellcheck disable=SC2034 # Read by install.sh and doctor.sh, which source this file.
WRAPPED_DOTFILES=(".zshrc:zsh" ".gitconfig:git")
# ------------- Platform and permissions -------------
is_macos(){ [[ "$(uname -s)" == Darwin ]]; }
# Put an installed Homebrew on PATH. A fresh shell may not have it yet.
init_homebrew(){
command -v brew >/dev/null 2>&1 && return 0
local candidate
for candidate in /opt/homebrew/bin/brew /usr/local/bin/brew /home/linuxbrew/.linuxbrew/bin/brew; do
if [[ -x "$candidate" ]]; then
eval "$("$candidate" shellenv)"
return 0
fi
done
return 1
}
require_homebrew(){
init_homebrew && return 0
err "Homebrew is required. Install it from https://brew.sh, then re-run:"
# shellcheck disable=SC2016 # Printed for the user to run, not expanded here.
err ' /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"'
return 1
}
# Octal mode of a file, on BSD (macOS) and GNU (Linux) stat.
file_mode(){ stat -c '%a' "$1" 2>/dev/null || stat -f '%Lp' "$1"; }
# secure_file <path> [mode]: tighten a credential-bearing file. Symlinks are left alone
# so the permissions of an unexpected target are never changed.
secure_file(){
local path="$1" mode="${2:-600}"
[[ -e "$path" || -L "$path" ]] || return 0
if [[ -L "$path" ]]; then
warn "$path is a symlink; not changing permissions (check that its target is mode $mode)"
return 0
fi
[[ "$(file_mode "$path")" == "$mode" ]] && return 0
chmod "$mode" "$path" || { err "Could not chmod $mode $path"; return 1; }
log "Set $path to mode $mode"
}
# ------------- Linking -------------
# safe_link <source> <target>: symlink target -> source without destroying anything.
# missing or dangling target: link it
# already linked: nothing
# symlink elsewhere (for example another clone): leave it unless DOTFILES_FORCE=1
# regular file or directory: back it up to <target>.bak.<timestamp> first
safe_link(){
local source_path="$1" target_path="$2" current backup
[[ -e "$source_path" ]] || { warn "Skipping missing $source_path"; return 0; }
if [[ -L "$target_path" ]]; then
current="$(readlink "$target_path")"
if [[ "$current" == "$source_path" ]]; then
log "$(basename "$target_path") already linked"
return 0
fi
if [[ -e "$target_path" && "${DOTFILES_FORCE:-}" != 1 ]]; then
warn "$target_path already links to $current; leaving it (DOTFILES_FORCE=1 to repoint)"
return 0
fi
elif [[ -e "$target_path" ]]; then
backup="${target_path}.bak.$(date +%Y%m%d%H%M%S)"
mv "$target_path" "$backup" || { err "Could not back up $target_path; leaving it"; return 1; }
log "Backed up $target_path -> $backup"
fi
mkdir -p "$(dirname "$target_path")"
ln -sfn "$source_path" "$target_path"
log "Linked $target_path -> $source_path"
}
# ------------- Local wrapper files (~/.zshrc, ~/.gitconfig) -------------
WRAPPER_BEGIN="# >>> dotfiles (managed) >>>"
WRAPPER_END="# <<< dotfiles (managed) <<<"
# wrapper_ref <kind> <absolute path>: how a wrapper refers to a repo file. Paths under
# $HOME stay relative to it so the wrapper survives a different home directory.
wrapper_ref(){
local kind="$1" path="$2"
case "$path" in
*\"*|*\$*|*\`*|*\\*) err "Unsupported character in path: $path"; return 1 ;;
esac
if [[ "$path" == "$HOME"/* ]]; then
if [[ "$kind" == zsh ]]; then
# shellcheck disable=SC2016 # $HOME must reach ~/.zshrc literally.
printf '$HOME/%s' "${path#"$HOME"/}"
else
# shellcheck disable=SC2088 # git expands the literal ~ in include paths.
printf '~/%s' "${path#"$HOME"/}"
fi
else
printf '%s' "$path"
fi
}
# managed_block <kind> <absolute path of the shared file>
managed_block(){
local kind="$1" ref
ref="$(wrapper_ref "$kind" "$2")" || return 1
printf '%s\n' "$WRAPPER_BEGIN"
case "$kind" in
zsh)
printf '# Shared config lives in the dotfiles repo; edit it there, not here.\n'
printf 'if [[ -r "%s" ]]; then source "%s"; else print -u2 "dotfiles: %s not found; run install.sh from your clone"; fi\n' "$ref" "$ref" "$ref"
;;
git)
# Quoted so a '#' or ';' in the clone path is not read as the start of a comment.
printf '[include]\n\tpath = "%s"\n' "$ref"
;;
esac
printf '%s\n' "$WRAPPER_END"
}
# True when the file has exactly one BEGIN and one END marker, in that order.
has_managed_block(){
local begin_line end_line
[[ "$(grep -cxF "$WRAPPER_BEGIN" "$1" || true)" == 1 && "$(grep -cxF "$WRAPPER_END" "$1" || true)" == 1 ]] || return 1
begin_line="$(grep -nxF "$WRAPPER_BEGIN" "$1" | cut -d: -f1)"
end_line="$(grep -nxF "$WRAPPER_END" "$1" | cut -d: -f1)"
(( begin_line < end_line ))
}
# managed_block_target <kind> <file>: the shared file that the managed block in <file> loads,
# with $HOME and ~ expanded. Prints nothing when the block or its path cannot be read.
managed_block_target(){
local kind="$1" file="$2" lead ref
# shellcheck disable=SC2016,SC2088 # The block stores a literal $HOME or ~, which is what is matched.
local home_var='$HOME/' tilde='~/'
case "$kind" in
zsh) lead='if [[ -r "' ;;
git) lead='path = "' ;;
*) return 0 ;;
esac
ref="$(LEAD="$lead" awk -v begin="$WRAPPER_BEGIN" -v end="$WRAPPER_END" '
$0 == begin { inside = 1; next }
$0 == end { inside = 0 }
inside && (i = index($0, ENVIRON["LEAD"])) {
rest = substr($0, i + length(ENVIRON["LEAD"]))
j = index(rest, "\"")
if (j > 0) { print substr(rest, 1, j - 1); exit }
}
' "$file")" || return 0
case "$ref" in
"$home_var"*) ref="$HOME/${ref#"$home_var"}" ;;
"$tilde"*) ref="$HOME/${ref#"$tilde"}" ;;
esac
printf '%s' "$ref"
}
# replace_managed_block <file> <block>: swap the block, keep everything else. Prints
# "unchanged" without touching the file when the block is already current (so a read-only file
# is fine); otherwise it keeps a timestamped backup first and prints "refreshed". The new
# content is built in a temporary file, given the original's mode, and renamed over the original
# only when complete, so a failing mktemp, awk, cp or chmod, or a full disk, leaves the file
# exactly as it was. Callers run this in a command substitution, where errexit does not apply:
# every step is checked here.
replace_managed_block(){
local file="$1" block="$2" tmp backup mode
tmp="$(mktemp "${file}.XXXXXX")" || { err "Could not create a temporary file next to $file; leaving it unchanged"; return 1; }
if ! BLOCK="$block" awk -v begin="$WRAPPER_BEGIN" -v end="$WRAPPER_END" '
$0 == begin { print ENVIRON["BLOCK"]; skipping = 1; next }
$0 == end { skipping = 0; next }
!skipping { print }
' "$file" > "$tmp"; then
rm -f "$tmp"
err "Could not build the updated $file; leaving it unchanged"
return 1
fi
if cmp -s "$tmp" "$file"; then
rm -f "$tmp"
printf 'unchanged'
return 0
fi
if ! mode="$(file_mode "$file")" || ! chmod "$mode" "$tmp"; then
rm -f "$tmp"
err "Could not copy the mode of $file to its update; leaving it unchanged"
return 1
fi
backup="${file}.bak.$(date +%Y%m%d%H%M%S)"
if ! cp -p "$file" "$backup"; then
rm -f "$tmp"
err "Could not back up $file; leaving it unchanged"
return 1
fi
if ! mv -f "$tmp" "$file"; then
rm -f "$tmp"
err "Could not replace $file (the backup is $backup)"
return 1
fi
printf 'refreshed'
}
write_wrapper(){
local target="$1" kind="$2" block="$3"
{
printf '%s\n' "$block"
if [[ "$kind" == zsh ]]; then
printf '\n# Machine-local config. Tool installers may append below; this file is not tracked.\n'
fi
} > "$target"
}
# A tracked file counts as clean only when git can prove it. Unknown means not clean.
repo_file_is_clean(){
local name="$1" changes
git -C "$SCRIPT_DIR" rev-parse --git-dir >/dev/null 2>&1 || return 1
changes="$(git --no-optional-locks -C "$SCRIPT_DIR" status --porcelain --untracked-files=no -- "$name")" || return 1
[[ -z "$changes" ]]
}
# wrap_local_file <name> <kind>: make ~/<name> a regular local file that loads $SCRIPT_DIR/<name>.
# missing or dangling symlink: create it
# managed block present: refresh the block, keep everything else
# legacy symlink into this clone: convert it, unless the clone has local edits to that file
# symlink anywhere else (for example another clone): leave it alone
# other regular file: zsh is backed up and replaced, git keeps its content below the block
wrap_local_file(){
local name="$1" kind="$2" target="$HOME/$1" shared="$SCRIPT_DIR/$1" block current backup result loaded
[[ -f "$shared" ]] || { warn "Skipping missing $shared"; return 0; }
block="$(managed_block "$kind" "$shared")" || return 1
if [[ -L "$target" && ! -e "$target" ]]; then
rm -f "$target"
write_wrapper "$target" "$kind" "$block" || { err "Could not write $target"; return 1; }
log "Replaced the dangling symlink $target with a local file"
return 0
fi
if [[ -L "$target" ]]; then
current="$(readlink "$target")"
if [[ "$current" != "$shared" ]]; then
warn "$target links to $current, not this clone; leaving it"
return 0
fi
if ! repo_file_is_clean "$name"; then
warn "$target links into this clone, which has local edits to $name."
warn " Move those edits to the local layer first, then: git -C '$SCRIPT_DIR' checkout -- $name && ./install.sh"
return 0
fi
rm -f "$target"
write_wrapper "$target" "$kind" "$block" || { err "Could not write $target"; return 1; }
log "Converted $target from a symlink into a local file"
return 0
fi
if [[ -e "$target" && ! -f "$target" ]]; then
warn "$target is not a regular file; leaving it"
return 0
fi
if [[ -f "$target" ]]; then
if has_managed_block "$target"; then
# A clone that was moved or deleted is followed. A second clone that still exists is not:
# repointing to a worktree that is deleted later would drop the whole configuration.
loaded="$(managed_block_target "$kind" "$target")" || loaded=""
if [[ -n "$loaded" && -f "$loaded" && ! "$loaded" -ef "$shared" && "${DOTFILES_FORCE:-}" != 1 ]]; then
warn "$target loads $loaded, another clone that still exists; leaving it (DOTFILES_FORCE=1 ./install.sh loads $shared instead)"
return 0
fi
result="$(replace_managed_block "$target" "$block")" || return 1
log "Managed block in $target is $result"
return 0
fi
if grep -qxF "$WRAPPER_BEGIN" "$target" || grep -qxF "$WRAPPER_END" "$target"; then
warn "$target has malformed managed-block markers; leaving it. Fix or remove them, then re-run install.sh"
return 0
fi
backup="${target}.bak.$(date +%Y%m%d%H%M%S)"
cp -p "$target" "$backup" || { err "Could not back up $target; leaving it unchanged"; return 1; }
if [[ "$kind" == git ]]; then
{ printf '%s\n' "$block"; cat "$backup"; } > "$target" || { err "Could not write $target (the original is kept at $backup)"; return 1; }
log "Added the managed block to $target (original kept at $backup)"
else
write_wrapper "$target" "$kind" "$block" || { err "Could not write $target (the original is kept at $backup)"; return 1; }
warn "Replaced $target (original kept at $backup); move anything you still need to ~/.zshrc.local"
fi
return 0
fi
write_wrapper "$target" "$kind" "$block" || { err "Could not write $target"; return 1; }
log "Created $target"
}
# seed_local_file <template> <target>: create a private (mode 600) local file from a template, once.
seed_local_file(){
local template="$1" target="$2"
[[ -f "$template" ]] || return 0
if [[ -e "$target" || -L "$target" ]]; then return 0; fi
install -m 600 "$template" "$target"
log "Created $target from $(basename "$template")"
}
# ------------- Credentials -------------
# Host comes from GOPROXY_NETRC_HOST (set in ~/.zshrc.local); no-op when unset.
# An existing valid entry is never replaced: the proxy needs a personal access token,
# which a different $GITHUB_TOKEN (for example a gh OAuth token) would break.
ensure_goproxy_netrc(){
local netrc_path="${HOME}/.netrc" proxy_host="${GOPROXY_NETRC_HOST:-}"
secure_file "$netrc_path" 600 || return 1
if [[ -z "$proxy_host" ]]; then
log "GOPROXY_NETRC_HOST not set; skipping goproxy netrc entry"
return 0
fi
if [[ -z "${GITHUB_TOKEN:-}" ]]; then
log "GITHUB_TOKEN not set; skipping goproxy netrc entry"
return 0
fi
if [[ -f "$netrc_path" ]] && grep -qF "$proxy_host" "$netrc_path"; then
log "goproxy netrc entry detected; skipping update"
return 0
fi
# secure_file leaves a symlink's target alone, so appending a token through it could put it
# in a file with looser permissions, or in a repository.
if [[ -L "$netrc_path" ]]; then
warn "$netrc_path is a symlink; not adding the goproxy entry (add it to the link target yourself)"
return 0
fi
(
umask 077
if [[ -s "$netrc_path" && -n "$(tail -c 1 "$netrc_path")" ]]; then printf '\n' >> "$netrc_path"; fi
printf 'machine %s login nobody password %s\n' "$proxy_host" "$GITHUB_TOKEN" >> "$netrc_path"
)
secure_file "$netrc_path" 600 || return 1
log "goproxy netrc entry configured"
}
# ------------- Packages -------------
macos_setup(){
log "*** macOS setup ***"
require_homebrew || return 1
# --no-upgrade keeps an install from bumping tools that are already present. A package that
# fails to install must not stop the links and wrappers, so it only warns.
brew bundle --no-upgrade --file="$SCRIPT_DIR/Brewfile" \
|| warn "brew bundle reported failures; re-run: brew bundle --no-upgrade --file='$SCRIPT_DIR/Brewfile'"
command -v copilot >/dev/null 2>&1 || brew install --cask copilot-cli \
|| warn "Could not install the Copilot CLI cask; install it yourself"
log "*** macOS setup done ***"
}
# Debian and Ubuntu name the bat binary batcat; expose it as bat.
link_batcat(){
command -v bat >/dev/null 2>&1 && return 0
command -v batcat >/dev/null 2>&1 || return 0
mkdir -p "$HOME/.local/bin"
[[ -e "$HOME/.local/bin/bat" || -L "$HOME/.local/bin/bat" ]] || ln -s "$(command -v batcat)" "$HOME/.local/bin/bat"
}
apt_setup(){
log "*** Linux (apt) setup ***"
command -v apt-get >/dev/null 2>&1 || { warn "apt-get not found; install tmux bat ripgrep neovim fzf jq shellcheck zsh unzip gh yourself"; return 0; }
local wanted=(tmux:tmux bat:bat rg:ripgrep nvim:neovim fzf:fzf jq:jq shellcheck:shellcheck zsh:zsh unzip:unzip)
local missing=() entry bin names
for entry in "${wanted[@]}"; do
bin="${entry%%:*}"
command -v "$bin" >/dev/null 2>&1 && continue
[[ "$bin" == bat ]] && command -v batcat >/dev/null 2>&1 && continue
missing+=("${entry#*:}")
done
if (( ${#missing[@]} > 0 )); then
names="$(IFS=' '; echo "${missing[*]}")"
log "Installing $names"
# A package problem must not stop the links, wrappers and hooks, so it only warns.
sudo apt-get update -y || warn "apt-get update failed; trying the install anyway"
sudo apt-get install -y "${missing[@]}" || warn "apt-get install failed; install $names yourself"
else
log "Base packages $ALREADY_INSTALLED_MSG"
fi
# The shared .gitconfig calls `gh auth git-credential`. gh is not in every apt release, so a
# failure only warns.
if ! command -v gh >/dev/null 2>&1; then
sudo apt-get install -y gh || warn "Could not install gh from apt; see https://cli.github.com (the git credential helper needs it)"
fi
link_batcat
log "*** Linux setup done ***"
}
# Credential file permissions are tightened first, on every platform.
setup(){
ensure_goproxy_netrc
case "$(uname -s)" in
Darwin) macos_setup ;;
Linux) apt_setup ;;
*) warn "Unsupported OS $(uname -s); skipping package installation" ;;
esac
}
# ------------- Private overlay -------------
# A private repo (default ~/.dotfiles-private) holds the non-secret local files that
# must not be public: its home/ tree mirrors $HOME and its Brewfile lists extra packages.
# Create it with snapshot-private.sh. Keys and tokens belong in a password manager.
private_overlay_dir(){ printf '%s' "${DOTFILES_PRIVATE:-$HOME/.dotfiles-private}"; }
# physical_path <path>: absolute path with symlinks resolved, even when the tail does not exist
# yet, so a destination can be compared with a repository by where it really is. Paths with a
# ".." component are refused instead of guessed at.
physical_path(){
local path="$1" tail="" dir
case "$path" in
/*) ;;
*) path="$PWD/$path" ;;
esac
case "/$path/" in
*/../*) err "Refusing a path with a '..' component: $1"; return 1 ;;
esac
while [[ "$path" != / && "$path" == */ ]]; do path="${path%/}"; done
while [[ ! -d "$path" ]]; do
tail="/${path##*/}$tail"
path="${path%/*}"
if [[ -z "$path" ]]; then path=/; fi
done
dir="$(cd "$path" && pwd -P)"
tail="${dir%/}$tail"
printf '%s' "${tail:-/}"
}
# symlink_below <base> <relative path>: prints the first component of base/relative that is a
# symlink (the last one included) and succeeds, or fails when there is none. Copying through a
# symlink would write outside the tree the caller means to write to.
symlink_below(){
local current="$1" rest="$2" part
while [[ -n "$rest" ]]; do
part="${rest%%/*}"
if [[ "$rest" == */* ]]; then rest="${rest#*/}"; else rest=""; fi
if [[ -z "$part" ]]; then continue; fi
current="$current/$part"
if [[ -L "$current" ]]; then printf '%s' "$current"; return 0; fi
done
return 1
}
# restore_private_overlay: copy files from the overlay's home/ tree into $HOME where the
# target does not exist yet, owner-only (600, or 700 for files that are executable in the
# overlay). Nothing is overwritten or symlinked, and a path behind a symlinked directory is
# skipped so a file cannot land somewhere the overlay did not intend.
restore_private_overlay(){
local overlay rel dest link count=0
overlay="$(private_overlay_dir)"
if [[ ! -d "$overlay/home" ]]; then
log "No private overlay at $overlay (optional, see README)"
return 0
fi
while IFS= read -r -d '' rel; do
rel="${rel#./}"
dest="$HOME/$rel"
if [[ -e "$dest" || -L "$dest" ]]; then
log "Keeping existing $dest"
continue
fi
if link="$(symlink_below "$HOME" "$rel")"; then
warn "Skipping $dest: $link is a symlink"
continue
fi
mkdir -p "$(dirname "$dest")"
cp -p "$overlay/home/$rel" "$dest"
if [[ -x "$overlay/home/$rel" ]]; then chmod 700 "$dest"; else chmod 600 "$dest"; fi
count=$((count + 1))
log "Restored $dest"
done < <(cd "$overlay/home" && find . -type f -print0)
if [[ -d "$HOME/.ssh" ]]; then secure_file "$HOME/.ssh" 700; fi
log "Restored $count file(s) from $overlay"
}
# install_overlay_packages: the overlay's Brewfile can be long and may hold a stale entry, so a
# failure only warns and never stops the installer.
install_overlay_packages(){
local overlay
overlay="$(private_overlay_dir)"
[[ -f "$overlay/Brewfile" ]] || return 0
section "Installing packages from the private overlay"
require_homebrew || return 0
brew bundle --no-upgrade --file="$overlay/Brewfile" \
|| warn "The overlay Brewfile reported failures; re-run: brew bundle --no-upgrade --file='$overlay/Brewfile'"
}
# ------------- Git hooks -------------
# enable_git_hooks: point this clone at the tracked .githooks. It never overrides an
# existing core.hooksPath or hides custom hooks, because core.hooksPath replaces .git/hooks.
enable_git_hooks(){
[[ -d "$SCRIPT_DIR/.githooks" ]] || return 0
git -C "$SCRIPT_DIR" rev-parse --git-dir >/dev/null 2>&1 || return 0
local current hooks_dir hook
current="$(git -C "$SCRIPT_DIR" config --get core.hooksPath || true)"
if [[ "$current" == .githooks ]]; then
log "Git hooks already enabled"
return 0
fi
if [[ -n "$current" ]]; then
warn "core.hooksPath is already $current; not enabling .githooks"
return 0
fi
hooks_dir="$(git -C "$SCRIPT_DIR" rev-parse --path-format=absolute --git-path hooks 2>/dev/null || true)"
if [[ -n "$hooks_dir" ]]; then
for hook in "$hooks_dir"/*; do
if [[ -f "$hook" && "$hook" != *.sample ]]; then
warn "Custom git hook $(basename "$hook") exists; not enabling .githooks (it would be bypassed)"
return 0
fi
done
fi
git -C "$SCRIPT_DIR" config --local core.hooksPath .githooks
log "Enabled git hooks from .githooks"
}
install_neovim(){
section "Configuring Neovim"
if ! command -v nvim >/dev/null 2>&1; then
warn "Neovim is not installed (see the package step above); skipping its config. Re-run install.sh after installing it."
return 0
fi
# A symlinked config directory is the user's own arrangement; writing into it could change
# another repository, and mkdir -p fails on a dangling link.
if [[ -L "$HOME/.config/nvim" ]]; then
log "$HOME/.config/nvim is a symlink; leaving the Neovim config alone"
return 0
fi
if [[ ! -f "$HOME/.config/nvim/init.lua" && ! -f "$HOME/.config/nvim/init.vim" ]]; then
mkdir -p "$HOME/.config/nvim"
cat > "$HOME/.config/nvim/init.lua" <<'EOF'
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.termguicolors = true
EOF
fi
log "Neovim configured"
}