dotfiles/private_dot_config/kitty/private_executable_repair-auto-themes.sh
Anton Volnuhin a84778a640 fix(kitty): read file permissions correctly where stat is GNU
`chezmoi apply` on Linux printed "could not regenerate kitty theme
files" every run. GNU stat reads -f as --file-system, so
`stat -f '%Lp' file` prints a block of filesystem info to stdout and
exits non-zero. The `|| stat -c '%a'` fallback then appended the real
answer to that block, and chmod rejected the result, failing the script
under set -e.

Replace the chain with a file_mode() helper in both scripts: try the GNU
form first, since BSD rejects -c cleanly without printing anything, and
accept the answer only if it is octal. Covered by a new case in
tests/test_theme_mode.sh using a stat stub that reproduces GNU's
behaviour; it fails against the old chain.
2026-08-06 02:39:49 +03:00

45 lines
1.4 KiB
Bash

#!/bin/sh
set -eu
# Permission bits of a file, or 600 if they cannot be read. GNU stat spells this
# `-c %a` while BSD/macOS spells it `-f %Lp`, and each mis-parses the other's
# form -- GNU treats `-f` as --file-system and prints a block of filesystem info
# before failing -- so try one at a time and only accept an octal answer.
file_mode() {
_m=$(stat -c '%a' "$1" 2>/dev/null) || _m=''
case "$_m" in
[0-7][0-7][0-7] | [0-7][0-7][0-7][0-7]) printf '%s\n' "$_m"; return 0 ;;
esac
_m=$(stat -f '%Lp' "$1" 2>/dev/null) || _m=''
case "$_m" in
[0-7][0-7][0-7] | [0-7][0-7][0-7][0-7]) printf '%s\n' "$_m"; return 0 ;;
esac
printf '600\n'
}
config_dir=${1:-"$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"}
for appearance in dark light no-preference; do
config_file="$config_dir/$appearance-theme.auto.conf"
[ -f "$config_file" ] || continue
if awk '
$1 == "macos_titlebar_color" {
count++
if ($2 == "background") correct++
}
END { exit !(count == 1 && correct == 1) }
' "$config_file"; then
continue
fi
tmp=$(mktemp "$config_file.tmp.XXXXXX")
trap 'rm -f "$tmp"' EXIT HUP INT TERM
awk '$1 != "macos_titlebar_color"' "$config_file" > "$tmp"
printf '\nmacos_titlebar_color background\n' >> "$tmp"
chmod "$(file_mode "$config_file")" "$tmp"
mv -f "$tmp" "$config_file"
trap - EXIT HUP INT TERM
done