feat: add -r/--reset, -f/--force, -i/--individual to gitignore-scrub
CI / github-mirror (pull_request) Skipped
CI / test (pull_request) Successful in 2m15s
CI / build-docs (pull_request) Skipped

-r clears the repo's skip list first so declined files are reconsidered.
-f untracks every pending match immediately, no prompt. -i prompts once
per file instead of once for the whole group. -w, -f, -i are mutually
exclusive (argparse --exclusive), -r combines with any of them.
This commit is contained in:
2026-09-23 15:52:01 -04:00
parent 37fea155b3
commit 7e3d48ddac
+53 -15
View File
@@ -11,7 +11,7 @@
# blocking-prompt
#
# SYNOPSIS
# gitignore-scrub [-h] [-w]
# gitignore-scrub [-h] [-r] [-w | -f | -i]
#
# DESCRIPTION
# Finds files that are tracked by git but now match a .gitignore pattern
@@ -19,26 +19,35 @@
# default interactive mode, prompts once for all matches and runs
# git rm --cached on confirmation; a decline is remembered per-path in the
# repo's local git config (gitignore-scrub.skip) so the same file is not
# asked about again. With -w/--warn, only prints a Warning line per match
# and makes no changes — meant for non-interactive callers such as a git
# hook. Both modes honor the skip list. Silently does nothing on a repo
# with more tracked files than $GITIGNORE_SCRUB_LIMIT (default 5000), to
# avoid adding latency to huge repos.
# asked about again. -w/--warn is read-only: prints a Warning line per
# match and makes no changes, meant for non-interactive callers such as a
# git hook. -f/--force skips the prompt and untracks every match
# immediately. -i/--individual prompts once per file instead of once for
# the whole group. -w, -f, and -i are mutually exclusive. -r/--reset
# clears the repo's skip list first (combinable with any mode), so
# previously declined files are reconsidered. Silently does nothing on a
# repo with more tracked files than $GITIGNORE_SCRUB_LIMIT (default
# 5000), to avoid adding latency to huge repos.
#
# ARGUMENTS
# -h, --help Show help message
# -w, --warn Read-only: print warnings instead of prompting, make no changes
# -h, --help Show help message
# -r, --reset Clear the remembered skip list before checking
# -w, --warn Read-only: print warnings instead of prompting, make no changes
# -f, --force Untrack every match immediately, no prompt
# -i, --individual Prompt once per file instead of once for the whole group
#
# EXIT STATUS
# 0 Clean, or (interactive) prompt handled, or not a git repository is
# never reached without erroring first
# 0 Clean, or a prompt/force run was handled
# 1 Not a git repository, or (-w only) unconfirmed matches were found
#
# EXAMPLE
# gitignore-scrub
# gitignore-scrub --warn
# gitignore-scrub --force
# gitignore-scrub --individual
# gitignore-scrub --reset
function gitignore-scrub --description 'Find and optionally untrack files newly matched by .gitignore'
argparse h/help w/warn -- $argv
argparse --exclusive w,f,i h/help r/reset w/warn f/force i/individual -- $argv
or return 1
if set -q _flag_help
@@ -46,12 +55,18 @@ function gitignore-scrub --description 'Find and optionally untrack files newly
echo "$c_head""Usage:$c_reset $c_cmd""gitignore-scrub$c_reset $c_arg""[FLAGS]$c_reset"
echo ""
echo "$c_head""Flags:$c_reset"
echo " $c_flag-h, --help$c_reset Show this help message"
echo " $c_flag-w, --warn$c_reset Read-only: print warnings instead of prompting"
echo " $c_flag-h, --help$c_reset Show this help message"
echo " $c_flag-r, --reset$c_reset Clear the remembered skip list before checking"
echo " $c_flag-w, --warn$c_reset Read-only: print warnings instead of prompting"
echo " $c_flag-f, --force$c_reset Untrack every match immediately, no prompt"
echo " $c_flag-i, --individual$c_reset Prompt once per file instead of once for the group"
echo ""
echo "$c_head""Examples:$c_reset"
echo " $c_cmd""gitignore-scrub$c_reset $c_dim""# Interactive: prompt to untrack matches$c_reset"
echo " $c_cmd""gitignore-scrub --warn$c_reset $c_dim""# Read-only: for use in a git hook$c_reset"
echo " $c_cmd""gitignore-scrub$c_reset $c_dim""# Interactive: prompt to untrack matches$c_reset"
echo " $c_cmd""gitignore-scrub --warn$c_reset $c_dim""# Read-only: for use in a git hook$c_reset"
echo " $c_cmd""gitignore-scrub --force$c_reset $c_dim""# Untrack every match, no prompt$c_reset"
echo " $c_cmd""gitignore-scrub --individual$c_reset $c_dim""# Prompt per file instead of as a group$c_reset"
echo " $c_cmd""gitignore-scrub --reset$c_reset $c_dim""# Reconsider previously declined files$c_reset"
return 0
end
@@ -72,6 +87,10 @@ function gitignore-scrub --description 'Find and optionally untrack files newly
set -l offenders (git ls-files -ci --exclude-standard)
set -q offenders[1]; or return 0
if set -q _flag_reset
git config --local --remove-section gitignore-scrub 2>/dev/null
end
set -l skip_list (git config --local --get-all gitignore-scrub.skip 2>/dev/null)
set -l pending
for f in $offenders
@@ -89,6 +108,25 @@ function gitignore-scrub --description 'Find and optionally untrack files newly
return 1
end
if set -q _flag_force
git rm --cached -- $pending >/dev/null
echo (set_color green)"✔"(set_color normal)" Untracked "(count $pending)" file(s)."
return 0
end
if set -q _flag_individual
for f in $pending
read -P "Remove '$f' from git tracking? [y/N] " confirm
if string match -qir '^y' -- "$confirm"
git rm --cached -- "$f" >/dev/null
echo (set_color green)"✔"(set_color normal)" Untracked $f"
else
git config --local --add gitignore-scrub.skip "$f"
end
end
return 0
end
set_color yellow
echo (count $pending)" tracked file(s) now match .gitignore:"(set_color normal)
for f in $pending