@@ -15,6 +15,7 @@ set -euo pipefail
1515#
1616# Options:
1717# check={all,staged-changes,working-tree-changes,branch} # Check mode, default is 'working-tree-changes'
18+ # exclude=file1,file2 # Comma-separated file paths to skip
1819# dry_run=true # Do not check, run dry run only, default is 'false'
1920# BRANCH_NAME=other-branch-than-main # Branch to compare with, default is `origin/main`
2021# FORCE_USE_DOCKER=true # If set to true the command is run in a Docker container, default is 'false'
@@ -43,6 +44,7 @@ set -euo pipefail
4344function main() {
4445
4546 cd " $( git rev-parse --show-toplevel) "
47+ apply-key-value-args " $@ "
4648
4749 # shellcheck disable=SC2154
4850 is-arg-true " ${dry_run:- false} " && dry_run_opt=" --dry-run"
@@ -82,7 +84,7 @@ function run-editorconfig-natively() {
8284 local files=()
8385 while IFS= read -r -d ' ' file; do
8486 files+=(" $file " )
85- done < <( $filter )
87+ done < <( get-filtered-files )
8688
8789 # If no files found, exit successfully
8890 [[ ${# files[@]} -eq 0 ]] && return 0
@@ -107,7 +109,7 @@ function run-editorconfig-in-docker() {
107109 local files=()
108110 while IFS= read -r -d ' ' file; do
109111 files+=(" $file " )
110- done < <( $filter )
112+ done < <( get-filtered-files )
111113
112114 # We use /dev/null here as a backstop in case there are no files in the state
113115 # we choose. If the filter comes back empty, adding `/dev/null` onto it has
@@ -123,6 +125,31 @@ function run-editorconfig-in-docker() {
123125 ec --exclude ' .git/' $dry_run_opt " ${files[@]} "
124126}
125127
128+ # Run the selected filter and remove files listed in the optional `exclude` argument.
129+ function get-filtered-files() {
130+
131+ local excludes_csv=" ${exclude:- } "
132+
133+ if [[ -z " $excludes_csv " ]]; then
134+ $filter
135+ return 0
136+ fi
137+
138+ local excludes=()
139+ IFS=' ,' read -r -a excludes <<< " $excludes_csv"
140+
141+ while IFS= read -r -d ' ' file; do
142+ local skip=false
143+ for ex in " ${excludes[@]} " ; do
144+ ex=" ${ex# ${ex%% [![:space:]]* } } "
145+ ex=" ${ex% ${ex##* [![:space:]]} } "
146+ [[ " $file " == " $ex " ]] && skip=true && break
147+ done
148+
149+ [[ " $skip " == false ]] && printf ' %s\0' " $file "
150+ done < <( $filter )
151+ }
152+
126153# ==============================================================================
127154
128155function is-arg-true() {
@@ -134,6 +161,19 @@ function is-arg-true() {
134161 fi
135162}
136163
164+ # Parse arguments passed by pre-commit as KEY=VALUE and expose them as env vars.
165+ function apply-key-value-args() {
166+
167+ for arg in " $@ " ; do
168+ if [[ " $arg " =~ ^[A-Za-z_][A-Za-z0-9_]* = .+$ ]]; then
169+ export " $arg "
170+ else
171+ echo " Unknown argument format: $arg (expected key=value)" >&2
172+ exit 126
173+ fi
174+ done
175+ }
176+
137177# ==============================================================================
138178
139179is-arg-true " ${VERBOSE:- false} " && set -x
0 commit comments