[ZSH] Hook improvements

This commit is contained in:
Fabian Ising
2026-07-27 10:49:25 +02:00
committed by Fabian Ising
parent c9137564a1
commit 01222ba1da
+18 -10
View File
@@ -31,6 +31,9 @@ fi
# Per-branch, so `server` on each device fast-forwards to origin/server. # Per-branch, so `server` on each device fast-forwards to origin/server.
update_dotfiles() { update_dotfiles() {
local repo="$HOME/dotfiles" local repo="$HOME/dotfiles"
local cache_dir="${ZDOTDIR:-$HOME}/.cache/zsh"
local notice_file="$cache_dir/dotfiles-update-notice"
local notice_tmp="$notice_file.$$.tmp"
# Bound the network call so a stalled fetch can't wedge this backgrounded # Bound the network call so a stalled fetch can't wedge this backgrounded
# (&!) job into a days-long orphan holding unreaped children. `timeout` is # (&!) job into a days-long orphan holding unreaped children. `timeout` is
# absent on macOS, so only use it when present. Disable ssh multiplexing so # absent on macOS, so only use it when present. Disable ssh multiplexing so
@@ -40,8 +43,11 @@ update_dotfiles() {
$TO git -C "$repo" fetch --quiet </dev/null 2>/dev/null || return $TO git -C "$repo" fetch --quiet </dev/null 2>/dev/null || return
git -C "$repo" rev-parse --abbrev-ref '@{u}' >/dev/null 2>&1 || return # no upstream git -C "$repo" rev-parse --abbrev-ref '@{u}' >/dev/null 2>&1 || return # no upstream
git -C "$repo" merge-base --is-ancestor '@{u}' HEAD 2>/dev/null && return # already current git -C "$repo" merge-base --is-ancestor '@{u}' HEAD 2>/dev/null && return # already current
git -C "$repo" merge --ff-only --quiet '@{u}' 2>/dev/null \ git -C "$repo" merge --ff-only --quiet '@{u}' 2>/dev/null && return
|| print -u2 "⚠ dotfiles: $(git -C "$repo" symbolic-ref --short HEAD) can't fast-forward to @{u} — run: git -C ~/dotfiles pull" local notice="⚠ dotfiles: $(git -C "$repo" symbolic-ref --short HEAD) can't fast-forward to @{u} — run: git -C ~/dotfiles pull"
mkdir -p "$cache_dir" || return
print -r -- "$notice" >| "$notice_tmp" || return
mv -f "$notice_tmp" "$notice_file"
} }
update_dotfiles &! update_dotfiles &!
@@ -93,17 +99,19 @@ check_tools_repo() {
} }
check_tools_repo &! check_tools_repo &!
show_tools_repo_notice() { show_update_notices() {
local cache_dir="${ZDOTDIR:-$HOME}/.cache/zsh" local cache_dir="${ZDOTDIR:-$HOME}/.cache/zsh"
local notice_file="$cache_dir/tools-update-notice" local notice_file claimed_notice
local claimed_notice="$notice_file.$$.show" for notice_file in "$cache_dir"/{dotfiles,tools}-update-notice; do
[[ -s "$notice_file" ]] || return [[ -s "$notice_file" ]] || continue
mv "$notice_file" "$claimed_notice" 2>/dev/null || return claimed_notice="$notice_file.$$.show"
command cat "$claimed_notice" mv "$notice_file" "$claimed_notice" 2>/dev/null || continue
command rm -f "$claimed_notice" command cat "$claimed_notice"
command rm -f "$claimed_notice"
done
} }
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
add-zsh-hook precmd show_tools_repo_notice add-zsh-hook precmd show_update_notices
# Load Antidote # Load Antidote
mkdir -p ${ZDOTDIR:-~}/.cache/zsh mkdir -p ${ZDOTDIR:-~}/.cache/zsh