function branch(dir) { var gitDir = _getGitDirectory(dir); var head = fs.readFileSync(path.resolve(gitDir, 'HEAD'), 'utf8'); var b = head.match(RE_BRANCH); if (b) { return b[1]; } return 'Detached: ' + head.trim(); }
...
console.log(git.short());
// 75bf4ee
console.log(git.long());
// 75bf4eea9aa1a7fd6505d0d0aa43105feafa92ef
console.log(git.branch());
// master
```
You can also run these examples via: `npm run examples`
## Install
...
function count() { return parseInt(_command('git', ['rev-list', '--all', '--count']), 10); }
...
return the result of `git rev-parse HEAD`; optional `filePath` parameter can be used to run the command against a repo outside the
current working directory
#### `git.branch([filePath])` → <String>
return the current branch; optional `filePath` parameter can be used to run the command against a repo outside the current working
directory
#### `git.count()` → <Number>
return the count of commits across all branches; this method will fail if the `git` command is not found in `PATH`
#### `git.date()` → <Date>
returns the date of the current commit; this method will fail if the `git` command is not found in `PATH`
...
function date() { return new Date(_command('git', ['log', '--no-color', '-n', '1', '--pretty=format:"%ad"'])); }
...
return the current branch; optional `filePath` parameter can be used to run the command against a repo outside the current working
directory
#### `git.count()` → <Number>
return the count of commits across all branches; this method will fail if the `git` command is not found in `PATH`
#### `git.date()` → <Date>
returns the date of the current commit; this method will fail if the `git` command is not found in `PATH`
#### `git.isTagDirty()` → <Boolean>
returns true if the current tag is dirty; this method will fail if the `git` command is not found in `PATH`
...
function isTagDirty() { try { _command('git', ['describe', '--exact-match', '--tags']); } catch (e) { if (e.message.indexOf('no tag exactly matches')) { return true; } throw e; } return false; }
...
return the count of commits across all branches; this method will fail if the `git` command is not found in `PATH`
#### `git.date()` → <Date>
returns the date of the current commit; this method will fail if the `git` command is not found in `PATH`
#### `git.isTagDirty()` → <Boolean>
returns true if the current tag is dirty; this method will fail if the `git` command is not found in `PATH`
#### `git.message()` → <String>
return the current commit message; this method will fail if the `git` command is not found in `PATH`
...
function log() { throw new Error('not implemented'); }
...
## Example
```js
var git = require('git-rev-sync');
console.log(git.short());
// 75bf4ee
console.log(git.long());
// 75bf4eea9aa1a7fd6505d0d0aa43105feafa92ef
console.log(git.branch());
// master
...
function long(dir) { var b = branch(dir); if (/Detached: /.test(b)) { return b.substr(11); } var gitDir = _getGitDirectory(dir); var refsFilePath = path.resolve(gitDir, 'refs', 'heads', b); var ref; if (fs.existsSync(refsFilePath)) { ref = fs.readFileSync(refsFilePath, 'utf8'); } else { // If there isn't an entry in /refs/heads for this branch, it may be that // the ref is stored in the packfile (.git/packed-refs). Fall back to // looking up the hash here. var refToFind = ['refs', 'heads', b].join('/'); var packfileContents = fs.readFileSync(path.resolve(gitDir, 'packed-refs'), 'utf8'); var packfileRegex = new RegExp('(.*) ' + escapeStringRegexp(refToFind)); ref = packfileRegex.exec(packfileContents)[1]; } return ref.trim(); }
...
```js
var git = require('git-rev-sync');
console.log(git.short());
// 75bf4ee
console.log(git.long());
// 75bf4eea9aa1a7fd6505d0d0aa43105feafa92ef
console.log(git.branch());
// master
```
You can also run these examples via: `npm run examples`
...
function message() { return _command('git', ['log', '-1', '--pretty=%B']); }
...
returns the date of the current commit; this method will fail if the `git` command is not found in `PATH`
#### `git.isTagDirty()` → <Boolean>
returns true if the current tag is dirty; this method will fail if the `git` command is not found in `PATH`
#### `git.message()` → <String>
return the current commit message; this method will fail if the `git` command is not found in `PATH`
#### `git.tag([markDirty])` → <String>
return the current tag and mark as dirty if markDirty is truthful; this method will fail if the `git` command is not found in `PATH
`
...
function short(dir) { return long(dir).substr(0, 7); }
...
## Example
```js
var git = require('git-rev-sync');
console.log(git.short());
// 75bf4ee
console.log(git.long());
// 75bf4eea9aa1a7fd6505d0d0aa43105feafa92ef
console.log(git.branch());
// master
...
function tag(markDirty) { if (markDirty) { return _command('git', ['describe', '--always', '--tag', '--dirty', '--abbrev=0']); } return _command('git', ['describe', '--always', '--tag', '--abbrev=0']); }
...
returns true if the current tag is dirty; this method will fail if the `git` command is not found in `PATH`
#### `git.message()` → <String>
return the current commit message; this method will fail if the `git` command is not found in `PATH`
#### `git.tag([markDirty])` → <String>
return the current tag and mark as dirty if markDirty is truthful; this method will fail if the `git` command is not found in `PATH
`
## License
[MIT](https://github.com/kurttheviking/git-rev-sync/blob/master/LICENSE)
...