feat: add gitignore-scrub to catch tracked files newly matched by .gitignore #172

Merged
rootiest merged 3 commits from feat/gitignore-scrub into main 2026-09-23 20:04:09 +00:00
Showing only changes of commit 7e3d48ddac - Show all commits
+47 -9
View File
@@ -11,7 +11,7 @@
# blocking-prompt # blocking-prompt
# #
# SYNOPSIS # SYNOPSIS
# gitignore-scrub [-h] [-w] # gitignore-scrub [-h] [-r] [-w | -f | -i]
# #
# DESCRIPTION # DESCRIPTION
# Finds files that are tracked by git but now match a .gitignore pattern # 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 # default interactive mode, prompts once for all matches and runs
# git rm --cached on confirmation; a decline is remembered per-path in the # 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 # 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 # asked about again. -w/--warn is read-only: prints a Warning line per
# and makes no changes — meant for non-interactive callers such as a git # match and makes no changes, meant for non-interactive callers such as a
# hook. Both modes honor the skip list. Silently does nothing on a repo # git hook. -f/--force skips the prompt and untracks every match
# with more tracked files than $GITIGNORE_SCRUB_LIMIT (default 5000), to # immediately. -i/--individual prompts once per file instead of once for
# avoid adding latency to huge repos. # 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 # ARGUMENTS
# -h, --help Show help message # -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 # -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 # EXIT STATUS
# 0 Clean, or (interactive) prompt handled, or not a git repository is # 0 Clean, or a prompt/force run was handled
# never reached without erroring first
# 1 Not a git repository, or (-w only) unconfirmed matches were found # 1 Not a git repository, or (-w only) unconfirmed matches were found
# #
# EXAMPLE # EXAMPLE
# gitignore-scrub # gitignore-scrub
# gitignore-scrub --warn # 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' 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 or return 1
if set -q _flag_help if set -q _flag_help
@@ -47,11 +56,17 @@ function gitignore-scrub --description 'Find and optionally untrack files newly
echo "" echo ""
echo "$c_head""Flags:$c_reset" echo "$c_head""Flags:$c_reset"
echo " $c_flag-h, --help$c_reset Show this help message" 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-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 ""
echo "$c_head""Examples:$c_reset" 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$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 --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 return 0
end 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 -l offenders (git ls-files -ci --exclude-standard)
set -q offenders[1]; or return 0 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 skip_list (git config --local --get-all gitignore-scrub.skip 2>/dev/null)
set -l pending set -l pending
for f in $offenders for f in $offenders
@@ -89,6 +108,25 @@ function gitignore-scrub --description 'Find and optionally untrack files newly
return 1 return 1
end 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 set_color yellow
echo (count $pending)" tracked file(s) now match .gitignore:"(set_color normal) echo (count $pending)" tracked file(s) now match .gitignore:"(set_color normal)
for f in $pending for f in $pending