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) + +}) + diff --git a/index.js b/index.js index fc3d578..241eae5 100755 --- a/index.js +++ b/index.js @@ -1,25 +1,32 @@ 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) { + , 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 + ']'))