Skip to content

Commit 0ae8fb6

Browse files
committed
feat(zsh): add cai
1 parent 599c031 commit 0ae8fb6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

config/zsh/.config/functions.zsh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,38 @@ docker-f() {
265265
echo "Running: $cmd"
266266
eval "$cmd"
267267
}
268+
269+
function git-trim-eof-newlines() {
270+
local all_files=false
271+
if [[ "$1" == "-a" || "$1" == "--all" ]]; then
272+
all_files=true
273+
fi
274+
275+
local files
276+
if [[ "$all_files" == true ]]; then
277+
files=(${(f)"$(git ls-files)"})
278+
else
279+
files=(${(f)"$(git diff --name-only && git diff --cached --name-only | sort -u)"})
280+
fi
281+
282+
if [[ -z "$files" ]]; then
283+
if [[ "$all_files" == true ]]; then
284+
echo "No files managed by git found."
285+
else
286+
echo "No changed files found."
287+
fi
288+
return 0
289+
fi
290+
291+
for file in $files; do
292+
if [[ -f "$file" ]]; then
293+
if file "$file" | grep -q "text"; then
294+
perl -i -0777 -pe 's/\n+\z/\n/' "$file"
295+
echo "Trimmed EOF: $file"
296+
fi
297+
fi
298+
done
299+
300+
echo "Done."
301+
}
302+
alias cai='git-trim-eof-newlines'

0 commit comments

Comments
 (0)