Git Bash script to ensure LF before committing

I’m using Git for a big folder of my notes, which I prefer to keep in unix file-format.  Occasionally I’m adding in files that have CRLF, which I could let Git deal with, but I prefer to tidy them before committing, so I wrote a Git Bash one-liner to find them, and wrapped a script around it.

A simple example – a working directory with 6 files:

unixff0.txt  unixff1.txt  unixff2.txt  unixff3.txt  unixff4.txt  unixff5.txt

I initialise a fresh repository, and commit them. Then I make some changes (move unixff2.txt into new directory case2, delete 2 files, and add 3 new files with the dos file-format that I don’t want in my repository), and:

output of git status -u in a simple case

– that’s what I want to parse, grabbing the addresses for those Untracked files (and lazily catching the modified‘s too).

Identifying the uncommited files with CRLFs

1. list just the files, identified by the leading tab:

git status -u | grep "$(echo -ne \\t)"

2. also strip out the now un-needed starting tab:

git status -u | grep "$(echo -ne \\t)" | sed 's#\t##'

3. and strip out those files that ain’t there anymore:

git status -u | grep "$(echo -ne \\t)" | sed 's#\t##' | grep -v "deleted:"

4. also strip out the un-needed description, if it’s there:

git status -u | grep "$(echo -ne \\t)" | sed 's#\t\(modified:   \)*##' |
  grep -v "deleted:"

5. home in on those files that have MSWin line endings:

git status -u | grep "$(echo -ne \\t)" | sed 's#\t\(modified:   \)*##' |
  grep -v "deleted:" | xargs -i{} grep -Ul $'\015' {}

Script the conversions to unix file-format

I wrote a Bash script to feed this captured list of files in dos file-format to vim (part of Git Bash), and do the conversion with :argdo w ++ff=unix.


Share Button