Thought experiment: letting git normalize whitespace

I'm working across a lot of different repos these days, and while fixing a whitespace issue in one of my patches, I wondered if it would be possible to eliminate this altogether.

Working with git, we already have a version of what I want in terms of [core.autocrlf](https://help.github.com/articles/dealing-with-line-endings#platform-all).  You wrote this patch on Windows, I'm on OS X, and our server is Linux?  No problem, we'll just use Unix EOLs and have git automatically manage the line endings whenever we checkout or commit.  In other words, when I checkout the code locally, I get a version of it customized to my environment.  There's a canonical version that git will store, but every developer can have something different in their editor.

Now let's go one step further.  Imagine you need to add the following code, as I did today:

module.exports = function( req, res ) {  
  res.json({  
    http: "okay"  
  });  
};

How many arguments can you have about how to style that code?  Come on, what would you r- in this?  "Dave, no space between ( and req please;" "Dave, you need to indent with 4-spaces not 2;" "Dave, you should just put that res.json all on one line."

The truth is, there's no right answer here.  Of course there are famous examples of automatic semi-colon insertion causing havoc (return\n{ vs. return {), but let's stick with my example code for a minute.  Could we let git manage the whitespace here?  Could we, in other words, take that code above, and when I commit, actually store this:

module.exports=function(req,res){res.json({http:"okay"});};

Now imagine that I've got a .gitstyle file in my tree, or I've somehow specified config options globally, and I can tell git how to style this code again when I check it out.  Imagine you like 8-space indents and I like 2-spaces; imagine you're allergic to spaces around arguments, and I love them; imagine your OCD demands that all variable declarations line up on the equal sign, and I'm happy to let them wind their way down my editor like a raindrop on a window.  And, imagine we can both do what we like with the same code in the same tree!  Imagine there's no style guide, it's easy if you try...

BUT DAVE, THIS CAN'T WORK BECAUSE <strong-reason />!!!  Sure, there's all sorts of things to work out.  This is a thought experiment.  I'm supposed to be grading final assignments right now, and I needed to procrastinate.  But if this could work, it would be so, so useful!

Patches accepted (please use 2-space indents).

Show Comments