A Simple Solution

Sometimes, or may be always, it is best to just seek the simpler or even simplest solution.For some time now, I have been plagued by a problem. Plagued is quite appropriate, because this problem was very annoying. I won’t get into the details it is a programming things and will be a bit boring. But, essentially, I had a problem where every time I started a new web application project using JavaScript libraries. I had this fatal error.

The root cause will be boring, so I will put that last if you want to be tortured. But for my rather annoying problem, I tried to code around it. And since it would happen every time I created a new project. Well, I had to dig up the code and run it to fix the problem. This problem is months old for me, may be even a year.

Finally, today I found a very elegant solution. A setting I can put in a file, and I own’t have to deal with it every again for any project. All I had to do was add ‘safecrlf=false’. That was it. I had tried a bunch of other suggestions from time to time and none of them worked.

Lesson, keep it simple. Writing about it now will be the last time I would have to think about how annoying it was.

The root cause for the problem is that developers who are working on windows, would submit files with CRLF ending. CRLF mean ‘carriage-return & line-feed’. Why is that a problem? Well, back in the days of typewriters. Once could do a ‘carriage-return’, which would do two things. Rotate the drum up enough to move to the next line. Here next line really means a new line, and of course slide it over so you can start typing at the beginning of the new line. You could also do a ‘line-feed’, which would just rotate the drum enough for a new line, but it wouldn’t return it to the beginning. This might not seems like something useful. But imagine that you wanted to just travel down the middle of  page after moving the the desired column. Then all you really wanted was ‘line-feed’. Of course you could turn the drum manually too. But never, all that, that is why there is both ‘carriage-return’ and ‘line-feed’.

Ok, so 99.99% of the people or companies who developed an operating system, used ‘carriage-return’ to mean ‘go to the beginning of a new line’. Microsoft, in their infinite wisdom, use not ‘carriage-return’ or ‘line-feed’, but both.  Usually, interchanging documents between systems is not a problem.  A mild annoyance to some non-Windows developers as their text documents have a funny extra character, the ‘line-feed’, at the end of each line. For Windows users, lines won’t wrap as they should, since their system is expecting two characters. But over time, these things became less of a problem, as editors just easily code round the problem.

But code is still being development and submitted or save with either CR or CRLF. Now we are at my problem. JavaScript libraries are distributed in text format. JavaScript doesn’t have a compiled for, not yet anyway. And my version control software, had a setting that said, “don’t save file with CRLF on this system”. I was the idiot who set that. So when I try to version control several dozen libraries, many of which had a line or two edited on a Windows system. The whole versioning things would grind to a halt.

At first, being the programmer I am, I coded around the problem. Something like this, but in Bash:

while (failing to commit); run dos-to-unix util on failed filed; repeat

I would have to run the code from every project. The code did a lot more than the one liner I showed above. It had to, (a) tried to commit a file, (b) see if it failed, (c) extract the file name form the error message, (d) strip out the extra LF character, (e) try to commit again. Repeat until all the files can be checked in without complaints from Git. Phew.

Leave a Reply