Skip to content

Commit

Permalink
Merge 35270ab into 3dce06a
Browse files Browse the repository at this point in the history
  • Loading branch information
htaunay committed Aug 12, 2015
2 parents 3dce06a + 35270ab commit dc5b58d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
34 changes: 28 additions & 6 deletions README.md
Expand Up @@ -2,20 +2,18 @@

> Get the most recent git tag of your repository using git-describe(1)

## Install

```sh
$ npm install --save git-latest-tag
```


## Usage

### Use a callback

```js
var getLatestTag = require('git-latest-tag');
var getLatestTag = require('git-latest-tag').getLatestTag;
var options = {
all: 'ok',
contains: true,
Expand All @@ -32,7 +30,7 @@ getLatestTag(options, function(err, tag) {
### Use as a readable stream

```js
var getLatestTag = require('git-latest-tag');
var getLatestTag = require('git-latest-tag').getLatestTag;
var options = {
all: 'ok',
contains: true,
Expand All @@ -44,6 +42,21 @@ getLatestTag(options)
.pipe(...);
```

### Use it synchronously

```js
var getLatestTagSync = require('git-latest-tag').getLatestTagSync;
var options = {
all: 'ok',
contains: true,
candidates: 10,
'commit-ish': 'HEAD'
};

var tag = getLatestTagSync(options);
console.log(tag);
//=> latestTag
```

## API

Expand All @@ -59,18 +72,27 @@ All options will be dash-cased for you.

Please check the available options at http://git-scm.com/docs/git-describe.

You can also define a specific Git repo other than the current path through the 'repoPath' option:

```js
// Options to get latest tag from current branch of a given repo
var options = {
tags: true,
abbrev: 0,
repoPath: '/path/to/repo'
};
```

*NOTE*: if a flag takes no value and the passed `options.value` is truthy, it will generate the flag only without any value. If it's falsy the flag will not be included.

If it's a `true`, it will suppress long format, only showing the closest tag in refs/tags namespace and will return an empty string if there is no tags but more than one commit (same as `{ tags: true, abbrev: 0 }`).

#### callback(err, tag)


## License

MIT © [Steve Mao](https://github.com/stevemao)


[npm-image]: https://badge.fury.io/js/git-latest-tag.svg
[npm-url]: https://npmjs.org/package/git-latest-tag
[travis-image]: https://travis-ci.org/stevemao/git-latest-tag.svg?branch=master
Expand Down
29 changes: 27 additions & 2 deletions index.js
@@ -1,5 +1,6 @@
'use strict';
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
var decamelize = require('decamelize');
var _ = require('lodash');

Expand Down Expand Up @@ -50,6 +51,18 @@ function getCmd(opts) {
return cmd;
}

function getExecOptions(opts) {

var execOpts = {};

if (opts instanceof Object && "repoPath" in opts) {
execOpts.cwd = opts.repoPath;
delete opts.repoPath;
}

return execOpts;
}

function getLatestTag(opts, cb) {
if (typeof opts === 'function') {
cb = opts;
Expand All @@ -58,9 +71,10 @@ function getLatestTag(opts, cb) {
cb = cb || function() {};
}

var execOpts = getExecOptions(opts);
var cmd = getCmd(opts);

return exec(cmd, function(err, stdout) {
return exec(cmd, execOpts, function(err, stdout) {
if (err) {
cb(err);
} else {
Expand All @@ -69,4 +83,15 @@ function getLatestTag(opts, cb) {
}).stdout;
}

module.exports = getLatestTag;
function getLatestTagSync(opts) {

if(opts === null) opts = {};

var execOpts = getExecOptions(opts);
var cmd = getCmd(opts);
var stdout = execSync(cmd, execOpts);

return String(stdout).trim();
}

module.exports = { getLatestTag: getLatestTag, getLatestTagSync: getLatestTagSync };
37 changes: 27 additions & 10 deletions test.js
@@ -1,11 +1,13 @@
/*global it */
'use strict';
var assert = require('assert');
var through = require('through2');
var rewire = require('rewire');
var gitLatestTag = rewire('./');
var assert = require('assert');
var through = require('through2');
var rewire = require('rewire');

var getCmd = gitLatestTag.__get__('getCmd');
var gitLatestTagModule = rewire('./');
var getCmd = gitLatestTagModule.__get__('getCmd');
var gitLatestTag = gitLatestTagModule.getLatestTag;
var gitLatestTagSync = gitLatestTagModule.getLatestTagSync;

it('without options', function() {
var cmd = getCmd();
Expand Down Expand Up @@ -67,13 +69,28 @@ it('should callback with options', function(done) {
});
});

it('should callback without options', function(done) {
it('should\'t work without options', function(done) {
gitLatestTag(function(err, tag) {
if (err) {
assert(err);
return done();
}
assert.equal(tag.indexOf('v'), 0);
done();
});
});

it('should work syncronously with true flag', function() {
var tag = gitLatestTagSync(true);
assert.equal(tag.indexOf('v'), 0);
});

it('should work syncronously with custom repo path', function() {
var tag = gitLatestTagSync({abbrev: 0, tags: true, repoPath: "."});
assert.equal(tag.indexOf('v'), 0);
});

it('shouldn\'t work syncronously with wrong custom repo path', function() {
assert.throws(
function() {
var tag = gitLatestTagSync({abbrev: 0, tags: true, repoPath: "./somewhere"});
},
Error
);
});

0 comments on commit dc5b58d

Please sign in to comment.