From 1c4e4d93c78bb067ecc46a5ad4c138d254e80f77 Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 26 Jul 2014 14:15:58 -0700 Subject: [PATCH 1/3] whitespace tweak --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index fc3d578..b81c9a1 100755 --- a/index.js +++ b/index.js @@ -10,16 +10,16 @@ module.exports = { short : function (cb) { _command('git rev-parse --short HEAD', cb) } - , long : function (cb) { + , long : function (cb) { _command('git rev-parse HEAD', cb) } - , branch : function (cb) { + , branch : function (cb) { _command('git rev-parse --abbrev-ref HEAD', cb) } - , tag : function (cb) { + , tag : function (cb) { _command('git describe --always --tag --abbrev=0', cb) } - , log : function (cb) { + , log : function (cb) { _command('git log --no-color --pretty=format:\'[ "%H", "%s", "%cr", "%an" ],\' --abbrev-commit', function (str) { str = str.substr(0, str.length-1) cb(JSON.parse('[' + str + ']')) From d58577e5fb4f4b3c069bbe8acf8466188900d368 Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 26 Jul 2014 14:16:18 -0700 Subject: [PATCH 2/3] ability to change current workig directory This module offers a nice way to get current hash of an npm project that is currently in the git repo's root. Some hosting providers run and serve npm projects from the repo root, but in MS's Azure, the git `repository` and the `wwwroot` directories are sibling repos. During deployment phase, the files are deployed to `wwwroot`. This change makes it possible for an npm project running in Azure (or any provider where the npm project is run or deployed in a different location from the git repo root) to re-use this module to easily grab the last hash. --- index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b81c9a1..241eae5 100755 --- a/index.js +++ b/index.js @@ -1,13 +1,20 @@ var exec = require('child_process').exec function _command (cmd, cb) { - exec(cmd, { cwd: __dirname }, function (err, stdout, stderr) { + exec(cmd, { cwd: cwd( ) }, function (err, stdout, stderr) { cb(stdout.split('\n').join('')) }) } -module.exports = { - short : function (cb) { +function cwd (_) { + if (_) { cwd.directory = _; } + return cwd.directory; +} +cwd.directory = __dirname; + +module.exports = { + cwd: cwd + , short : function (cb) { _command('git rev-parse --short HEAD', cb) } , long : function (cb) { From cb4a1955728e3534805d3ffcbf17077b87c829cd Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 26 Jul 2014 14:20:41 -0700 Subject: [PATCH 3/3] Add a nice little way to test "outside" repos This is a quick hack to test the ability to read a git repo that may reside outside the current project's root. --- example/nested.sh | 16 ++++++++++++++++ example/outside.js | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100755 example/nested.sh create mode 100644 example/outside.js diff --git a/example/nested.sh b/example/nested.sh new file mode 100755 index 0000000..fa2b5b3 --- /dev/null +++ b/example/nested.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +pwd +export ORIGINAL=$(pwd) +TESTDIR=/tmp/outside-git-rev +mkdir -p $TESTDIR +cp $ORIGINAL/index.js $TESTDIR/index.js +cp $ORIGINAL/example/outside.js $TESTDIR/outside.js +( + cd $TESTDIR + echo "inside" $TESTDIR + pwd + ORIGINAL=$ORIGINAL node outside.js +) +rm -Rfv $TESTDIR + diff --git a/example/outside.js b/example/outside.js new file mode 100644 index 0000000..efa0ec5 --- /dev/null +++ b/example/outside.js @@ -0,0 +1,11 @@ + +var git = require('./index'); +var cwd = process.env.ORIGINAL; +console.log('dir', __dirname); +console.log('original', cwd); +git.cwd(cwd); +git.short(function (str) { + console.log('short', str) + +}) +