Files
fish-config/functions/dng2avif.fish
T
rootiest 3414f81cb6 feat(tests): add shadow-classification lint; fix real cp/mv/less bugs
New Phase 1b in tests/run-tests.fish: catches a bare C1-shadowed-command
call in a functions/*.fish body with no matching uses-shadow(name) or
self-limiting(name) in that function's own CLASSIFICATION header. This
is exactly the check discussed after the rm and cd audits -- runtime
auto-unwrapping isn't viable in fish (there's no hook finer than
shadowing itself, and rewriting behavior invisibly at runtime is its
own footgun); a static lint using the CLASSIFICATION tag as the
declared-intentional marker is. Scoped to functions/*.fish only: the
one-function-per-file convention there makes body extraction exact
with no block-depth parser needed.

Added a new self-limiting(name) tag to the schema for the case a bare
call is safe not because the caller did anything, but because the
shadow's own logic already neutralizes the override: rm's and mkdir's
flag checks (verified precisely -- rm falls back to command rm for any
flag except a bare -r/-R/--recursive alone, which still routes to
trash; mkdir falls back to command mkdir -p for any flag, no
exception), and grep/fgrep/egrep/dir/vdir/cat's own tty auto-detection
(--color=auto, and bat's default color behavior -- verified
byte-identical to stock cat when piped, since bat also auto-disables
highlighting on a non-terminal). Explicit and durable rather than a
silent lint exemption: if a shadow's bypass condition is ever
weakened, every self-limiting site is one grep away instead of
silently wrong.

Running the first draft of the lint surfaced three more real bugs,
none previously audited:

- config-help.fish's --man pager path checks `type -q less` (proving
  it wants the real less binary specifically, for less-only -R/+N
  flag syntax) then called it bare, routing through our own
  $PAGER -> ov -> less -> more -> cat fallback chain instead -- which
  could hand those less-specific flags to a completely different
  program. Now command less.
- _fish_deps_install.fish and _fish_deps_update.fish's binary-upgrade
  paths cp a freshly downloaded binary over an already-installed one
  with no existence guard -- the update flow's target is guaranteed to
  already exist. Our cp shadow forces -i unconditionally (a plain
  alias, not flag-aware like rm's), so this would hang waiting on a
  confirmation prompt in any non-interactive run. Now command cp.
  Same two files' lazydocker install path piped curl output into bare
  bash, invoking our shell-switch wrapper instead of a plain
  subshell. Now command bash.
- agents-init.fish's AGENTS.md/CLAUDE.md relocation calls mv bare in
  four places; each is already guarded by a preceding test -f check on
  the destination, so the -i alias was unlikely to ever fire in
  practice, but explicit command mv removes the reliance on that guard
  entirely rather than leaving it as the only thing standing between a
  file move and an unattended hang.

The remaining ~65 flagged call sites across ~24 files were reviewed
individually and tagged self-limiting(rm)/self-limiting(mkdir)
(verified flagged with -f/-rf or -p) and self-limiting(grep)/
self-limiting(cat) (verified piped, captured, or -q/-c; none display
color to a human), plus uses-shadow(ls) for two existence-check-only
calls (cffetch.fish, ffetch.fish) whose output is redirected to
/dev/null.
2026-09-21 21:26:55 -04:00

130 lines
4.3 KiB
Fish

# Copyright (C) 2026 Rootiest
# SPDX-License-Identifier: AGPL-3.0-or-later
# CATEGORY
# 13-media-and-utilities
#
# CLASSIFICATION
# self-limiting(rm)
#
# SYNOPSIS
# dng2avif [-h] [-i <file>] [-o <file>] [-q <n>] [-s <n>] [input.dng]
#
# DESCRIPTION
# Converts a DNG raw image to a 10-bit HDR AVIF using a three-step pipeline:
# develop with ImageMagick, encode with ffmpeg+avifenc, sync metadata with
# exiftool. Requires magick, ffmpeg, avifenc, and exiftool.
#
# ARGUMENTS
# -i, --input FILE Input DNG file
# -o, --output FILE Output AVIF file (defaults to input basename)
# -q, --quality N Encoding quality 0-100 (default: 92)
# -s, --speed N Encoder speed 0-10 (default: 3, 0 = slowest)
# -h, --help Show help message
#
# EXIT STATUS
# 0 Conversion complete
# 1 File not found, missing dependency, or encode step failed
#
# EXAMPLE
# dng2avif photo.dng
# dng2avif -q 85 -s 5 -i shot.dng -o out.avif
function dng2avif --description 'Convert DNG raw to 10-bit HDR AVIF'
set -l options (fish_opt -s h -l help)
set -a options (fish_opt -s i -l input -r)
set -a options (fish_opt -s o -l output -r)
set -a options (fish_opt -s q -l quality -r)
set -a options (fish_opt -s s -l speed -r)
argparse $options -- $argv
or return
# Help Screen
if set -q _flag_help; or test (count $argv) -eq 0 -a -z "$_flag_input"
__fish_palette
echo "$c_head""Usage:$c_reset $c_cmd""dng2avif$c_reset $c_flag""[options]$c_reset $c_arg""[input.dng]$c_reset"
echo ""
echo "$c_head""Options:$c_reset"
echo " $c_flag-i$c_reset, $c_flag--input$c_reset $c_arg""FILE$c_reset Input DNG file"
echo " $c_flag-o$c_reset, $c_flag--output$c_reset $c_arg""FILE$c_reset Output AVIF file (defaults to input name)"
echo " $c_flag-q$c_reset, $c_flag--quality$c_reset $c_arg""N$c_reset Encoding quality 0-100 (default: 92)"
echo " $c_flag-s$c_reset, $c_flag--speed$c_reset $c_arg""N$c_reset Encoder speed 0-10 (default: 3, 0=slowest)"
echo " $c_flag-h$c_reset, $c_flag--help$c_reset Show this help message"
return 0
end
# Handle Input Source
set -l input ""
if set -q _flag_input
set input $_flag_input
else
set input $argv[1]
end
if not test -f "$input"
echo (set_color red)"Error: File '$input' not found."(set_color normal)
return 1
end
# Configuration
set -l quality 92
if set -q _flag_quality
set quality $_flag_quality
end
set -l speed 3
if set -q _flag_speed
set speed $_flag_speed
end
set -l basename (string replace -r '\.dng$' '' $input)
set -l output "$basename.avif"
if set -q _flag_output
set output $_flag_output
end
set -l temp_pnm "$basename"_temp.pnm
# Dependency check
for cmd in magick ffmpeg avifenc exiftool
if not type -q $cmd
echo (set_color red)"Error: $cmd is not installed."(set_color normal)
return 1
end
end
# Step 1: Develop
echo -n (set_color blue)"Step 1/3: Developing... "(set_color normal)
magick "$input" -depth 16 pnm:"$temp_pnm" &>/dev/null
if test $status -ne 0
echo (set_color red)"FAILED"
return 1
end
echo (set_color green)"DONE"(set_color normal)
# Step 2: Encode
echo -n (set_color blue)"Step 2/3: Encoding (Q:$quality, S:$speed)... "(set_color normal)
ffmpeg -i "$temp_pnm" -vf "format=yuv444p10le" -f yuv4mpegpipe -strict -1 - 2>/dev/null | avifenc -q $quality -s $speed --stdin -d 10 -y 444 --cicp 12/1/1 -r f -o "$output" &>/dev/null
if test $status -ne 0
echo (set_color red)"FAILED"
rm -f "$temp_pnm"
return 1
end
echo (set_color green)"DONE"(set_color normal)
# Step 3: Metadata
echo -n (set_color blue)"Step 3/3: Syncing Metadata... "(set_color normal)
exiftool -tagsFromFile "$input" -all:all "$output" -overwrite_original &>/dev/null
if test $status -ne 0
echo (set_color red)"FAILED"
else
echo (set_color green)"DONE"(set_color normal)
end
# Final Cleanup
test -f "$temp_pnm"; and rm -f "$temp_pnm"
set -l size (stat -c '%s' "$output" | numfmt --to=iec)
echo (set_color yellow)"Complete: $output ($size)"(set_color normal)
end