Skip to content

Commit 392222a

Browse files
committed
Initial commit
0 parents  commit 392222a

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# Compiled binary addons (http://nodejs.org/api/addons.html)
20+
build/Release
21+
22+
# Dependency directory
23+
# Deployed apps should consider commenting this line out:
24+
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
25+
node_modules

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- '0.12'
4+
- '0.10'
5+
- '0.8'
6+
- 'iojs'
7+
before_install:
8+
- npm install -g npm@~1.4.6

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# git-state
2+
3+
Get the current state of any git repository.
4+
5+
[![Build status](https://travis-ci.org/watson/git-state.svg?branch=master)](https://travis-ci.org/watson/git-state)
6+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
7+
8+
## Installation
9+
10+
```
11+
npm install git-state
12+
```
13+
14+
## Usage
15+
16+
```js
17+
var gitState = require('git-state')
18+
19+
var path = '/path/to/git/repo'
20+
21+
if (gitState.isGit(path)) {
22+
gitState.check(path, function (err, result) {
23+
if (err) throw err
24+
console.log(result) // => { branch: 'master',
25+
// ahead: 0,
26+
// dirty: 9,
27+
// untracked: 1,
28+
// issues: true }
29+
})
30+
}
31+
```
32+
33+
## API
34+
35+
#### `isGit(path)`
36+
37+
Returns either `true` or `false` if the given path contains a git
38+
repository.
39+
40+
#### `check(path, callback)`
41+
42+
Will check the state of the git repository at the given `path` and call
43+
the `callback`. The `callback` will be called with two arguments: An
44+
optional error object and a result object.
45+
46+
The result object contains the following properties:
47+
48+
- `branch` - The currently checked out branch
49+
- `ahead` - The amount of commits the current branch is ahead of the
50+
remote (may be `NaN` if there for instance is no remote)
51+
- `dirty` - The number of dirty files
52+
- `untracked` - The number of untracked files
53+
- `issues` - A generic boolean which is `true` if the repository is in a
54+
non-clean state (e.g. it's dirty, contains untracked files, is head of
55+
its remote or is currently not on master)
56+
57+
## License
58+
59+
MIT

index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
var os = require('os')
4+
var fs = require('fs')
5+
var path = require('path')
6+
var exec = require('child_process').exec
7+
var afterAll = require('after-all-results')
8+
9+
exports.isGit = function (dir) {
10+
return fs.existsSync(path.join(dir, '.git'))
11+
}
12+
13+
exports.check = function (repo, cb) {
14+
var next = afterAll(function (err, results) {
15+
if (err) return cb(err)
16+
17+
var branch = results[0]
18+
var ahead = results[1]
19+
var status = results[2]
20+
var issues = Boolean(branch !== 'master' ||
21+
ahead || Number.isNaN(ahead) ||
22+
status.dirty || status.untracked)
23+
24+
cb(null, {
25+
branch: branch,
26+
ahead: ahead,
27+
dirty: status.dirty,
28+
untracked: status.untracked,
29+
issues: issues
30+
})
31+
})
32+
33+
branch(repo, next())
34+
ahead(repo, next())
35+
status(repo, next())
36+
}
37+
38+
var status = function (repo, cb) {
39+
exec('git status -s', { cwd: repo }, function (err, stdout, stderr) {
40+
if (err) return cb(err)
41+
var status = { dirty: 0, untracked: 0 }
42+
stdout.trim().split(os.EOL).forEach(function (file) {
43+
if (file.substr(0, 2) === '??') status.untracked++
44+
else status.dirty++
45+
})
46+
cb(null, status)
47+
})
48+
}
49+
50+
var branch = function (repo, cb) {
51+
exec('cat .git/HEAD', { cwd: repo }, function (err, stdout, stderr) {
52+
if (err) return cb(err)
53+
stdout = stdout.trim()
54+
var branch = stdout.indexOf('ref:') === 0 ? stdout.substr(16) : stdout
55+
cb(null, branch)
56+
})
57+
}
58+
59+
var ahead = function (repo, cb) {
60+
exec('git rev-list HEAD --not --remotes', { cwd: repo }, function (err, stdout, stderr) {
61+
if (err) return cb(null, NaN) // depending on the state of the git repo, the command might return non-0 exit code
62+
stdout = stdout.trim()
63+
cb(null, !stdout ? 0 : parseInt(stdout.split(os.EOL).length, 10))
64+
})
65+
}

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "git-state",
3+
"version": "0.0.0",
4+
"description": "Get the current state of any git repository",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && tape test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/watson/git-state.git"
12+
},
13+
"dependencies": {
14+
"after-all-results": "^2.0.0"
15+
},
16+
"devDependencies": {
17+
"standard": "^4.3.1",
18+
"tape": "^4.0.0"
19+
},
20+
"keywords": [
21+
"git",
22+
"status",
23+
"state",
24+
"issues",
25+
"branch",
26+
"repo",
27+
"repository",
28+
"ahead",
29+
"dirty",
30+
"untracked"
31+
],
32+
"author": "Thomas Watson Steen <[email protected]> (https://twitter.com/wa7son)",
33+
"license": "MIT",
34+
"bugs": {
35+
"url": "https://github.com/watson/git-state/issues"
36+
},
37+
"homepage": "https://github.com/watson/git-state",
38+
"coordinates": [
39+
18.4785126,
40+
-69.954776
41+
]
42+
}

test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
var test = require('tape')
4+
var git = require('./')
5+
6+
test('#isGit()', function (t) {
7+
var dir = process.cwd()
8+
var result = git.isGit(dir)
9+
t.equal(result, true)
10+
t.end()
11+
})
12+
13+
test('#check()', function (t) {
14+
var dir = process.cwd()
15+
git.check(dir, function (err, result) {
16+
t.error(err)
17+
t.deepEqual(Object.keys(result), ['branch', 'ahead', 'dirty', 'untracked', 'issues'])
18+
t.equal(typeof result.branch, 'string')
19+
t.equal(typeof result.ahead, 'number')
20+
t.equal(typeof result.dirty, 'number')
21+
t.equal(typeof result.untracked, 'number')
22+
t.equal(typeof result.issues, 'boolean')
23+
t.end()
24+
})
25+
})

0 commit comments

Comments
 (0)