P i
P i

Reputation: 30684

Add missing trailing newline to all files in Git repo

Many files in my Git repo are missing a trailing newline:

GitHub notice "No newline at end of file"

For consistency, and to remove git-related visual clutter, I would like to automatically add it where it is missing.

Upvotes: 2

Views: 1017

Answers (1)

P i
P i

Reputation: 30684

#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
GREY='\033[0;30m'
RESET='\033[0m' # No Color

#files=$(find ./files -type f)
files=$(git ls-files)

for file in $files; do
    if [[ $(tail -c 1 "$file" | xxd -p) != 0a ]]; then
        echo -en "Fix ${RED}$file${RESET}? (y/n) "
        read -n 1  answer
        echo
        if [[ $answer = 'y' ]]; then
            echo -e "FIXING ${GREEN}$file${RESET}"
            echo >> $file
        else
            echo "Skipping $file"
        fi
    else
        echo -e "${GREY}$file has newline${RESET}"
    fi
done

Upvotes: 8

Related Questions