Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions example/nested.sh
Original file line number Diff line number Diff line change
@@ -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

11 changes: 11 additions & 0 deletions example/outside.js
Original file line number Diff line number Diff line change
@@ -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)

})

21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 + ']'))
Expand Down