cainus/node-coveralls — pull request #25 by tdd (2013-11-08 18:55:46+00:00)
Title: Fix direct dev use on a local Git repo + improve Git metadata fetching

Hey!

I've been using node-coveralls on a project of mine ([Moments-Away](https://tdd.github.io/moments-away/), directly on my devbox (not in Travis, etc.) and noticed it utterly failed to detect the current Git config (the host project's).

Looking at the code, I figured this was due to its **never** detecting local Git branch/commit in such situations. It would try and grab those from various environment situations, mostly CI, but nothing in a direct use on a regular dev box.

So first, I [committed a fix](https://github.com/tdd/node-coveralls/commit/335918f28d6cbbac2efbc25990e97549dbd96aee) for proper dev-local detection of the current branch and its commit ID. For simplicity and perf reasons, this is done by hand, automatically detecting the relevant `.git` dir and checking out its `HEAD` contents and, if a symbolic head ref, looking into the matching `refs/heads/…` file.

Then I noticed the existing `fetchGitData.js` file ignored the passed branch entirely, attempting to detect it based on the commit SHA in `git.head.id`. The detection actually failed, as it assumed the first listed branch would be the current one, which isn't true (Git uses alphabetical order). Another fumble in remotes detection led to empty/undefined/null extraneous remote defintions.

I used a [second commit](https://github.com/tdd/node-coveralls/commit/36c62aff7424b12ca9e84370afc65ecfbe73e632) to fix the detection code by looking for the asterisk-marked branch, which is guaranteed to be the current one, and to clean up remotes detection. Plus, pre-provided branch (e.g. through local Git detection earlier on) will not be ignored, if passed: detection will only happen at this stage if the branch wasn't explicitly pre-provided. I also took that opportunity to restructure the code a bit to reduce nesting and remove obsolete arguments, etc.

Finally, I was fairly bugged with the way ref commit metadata were obtained. Relying on multiple git log calls was quite heavy-handed, and the code for it was also unduly verbose and convoluted. I proposed a [final commit](https://github.com/tdd/node-coveralls/commit/a49eaa7f533bb7c3338d7864e3d118d43ede861e) that speeds the whole thing up considerably by relying on just two Git plumbing commands: `rev-parse` (for initial Git context verification, which might be considered superfluous anyway) and `cat-file` for displaying all metadata for the ref commit in one shot.

I also straightened up and slimmed down the way these metadata are obtained, and reduced nesting by relying on hoisted function declarations, which makes the whole thing easier to understand as it's now split into smaller, well-defined functions.

I confess I did not take the time to complement the tests to detect these failures before putting the fixes in, but I did verify the existing test suite kept running just fine!

I hope this meets with your approval, and thanks for node-coveralls!