Skip to content

Commit f0213e9

Browse files
committed
Initial commit.
0 parents  commit f0213e9

File tree

10 files changed

+180
-0
lines changed

10 files changed

+180
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## git-repo-info
2+
3+
Retrieves repo information.

index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
var GIT_DIR = '.git';
7+
8+
function changeGitDir(newDirName) {
9+
GIT_DIR = newDirName;
10+
}
11+
12+
function findRepo(startingPath) {
13+
var gitPath, lastPath;
14+
var currentPath = startingPath;
15+
16+
if (!currentPath) { currentPath = process.cwd(); }
17+
18+
do {
19+
gitPath = path.join(currentPath, GIT_DIR);
20+
21+
if (fs.existsSync(gitPath)) {
22+
return gitPath;
23+
}
24+
25+
lastPath = currentPath;
26+
currentPath = path.resolve(currentPath, '..');
27+
} while (lastPath !== currentPath);
28+
29+
return null;
30+
}
31+
32+
module.exports = function(gitPath) {
33+
if (!gitPath) { gitPath = findRepo(); }
34+
35+
var headFilePath = path.join(gitPath, 'HEAD');
36+
37+
var result = {
38+
sha: null,
39+
abbreviatedSha: null,
40+
branch: null
41+
}
42+
43+
try {
44+
if (fs.existsSync(headFilePath)) {
45+
var branchSHA;
46+
var headFile = fs.readFileSync(headFilePath, {encoding: 'utf8'});
47+
var branchName = headFile.split('/').slice(-1)[0].trim();
48+
var refPath = headFile.split(' ')[1];
49+
50+
if (refPath) {
51+
var branchPath = path.join(gitPath, refPath.trim());
52+
53+
result.branch = branchName;
54+
result.sha = fs.readFileSync(branchPath, {encoding: 'utf8' }).trim();
55+
} else {
56+
result.sha = branchName;
57+
}
58+
59+
result.abbreviatedSha = result.sha.slice(0,10);
60+
}
61+
} catch (e) {
62+
// eat it
63+
}
64+
65+
return result;
66+
};
67+
68+
module.exports._findRepo = findRepo;
69+
module.exports._changeGitDir = changeGitDir;

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "git-repo-info",
3+
"version": "0.0.0",
4+
"description": "Retrieve current sha and branch name.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha tests"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/rwjblue/git-repo-info.git"
12+
},
13+
"keywords": [
14+
"git"
15+
],
16+
"author": "Robert Jackson",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/rwjblue/git-repo-info/issues"
20+
},
21+
"homepage": "https://github.com/rwjblue/git-repo-info",
22+
"devDependencies": {
23+
"mocha": "^1.21.4"
24+
}
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9dac893d5a83c02344d91e79dad8904889aeacb1

tests/fixtures/nested-repo/dot-git/.gitkeep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5359aabd3872d9ffd160712e9615c5592dfe6745

tests/fixtures/nested-repo/foo/bar/.gitkeep

Whitespace-only changes.

tests/index.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
var assert = require('assert');
4+
var path = require('path');
5+
var repoInfo = require('../index');
6+
7+
var root = process.cwd();
8+
var testFixturesPath = path.join(__dirname, 'fixtures');
9+
var gitDir = 'dot-git';
10+
11+
describe('git-repo-info', function() {
12+
before(function() {
13+
repoInfo._changeGitDir(gitDir);
14+
});
15+
16+
afterEach(function() {
17+
process.chdir(root);
18+
})
19+
20+
describe('repo lookup', function() {
21+
var repoRoot = path.join(testFixturesPath, 'nested-repo');
22+
23+
it('finds a repo in the current directory', function() {
24+
process.chdir(repoRoot);
25+
26+
var foundPath = repoInfo._findRepo(repoRoot);
27+
assert.equal(foundPath, path.join(repoRoot, gitDir));
28+
});
29+
30+
it('finds a repo in the parent directory', function() {
31+
process.chdir(path.join(repoRoot, 'foo'));
32+
33+
var foundPath = repoInfo._findRepo(repoRoot);
34+
assert.equal(foundPath, path.join(repoRoot, gitDir));
35+
});
36+
37+
it('finds a repo 2 levels up', function() {
38+
process.chdir(path.join(repoRoot, 'foo', 'bar'));
39+
40+
var foundPath = repoInfo._findRepo(repoRoot);
41+
assert.equal(foundPath, path.join(repoRoot, gitDir));
42+
});
43+
44+
it('finds a repo without an argument', function() {
45+
process.chdir(repoRoot);
46+
47+
var foundPath = repoInfo._findRepo();
48+
assert.equal(foundPath, path.join(repoRoot, gitDir));
49+
});
50+
});
51+
52+
describe('repoInfo', function() {
53+
it('returns an object with repo info', function() {
54+
var repoRoot = path.join(testFixturesPath, 'nested-repo');
55+
var result = repoInfo(path.join(repoRoot, gitDir))
56+
57+
var expected = {
58+
branch: 'master',
59+
sha: '5359aabd3872d9ffd160712e9615c5592dfe6745',
60+
abbreviatedSha: '5359aabd38'
61+
}
62+
63+
assert.deepEqual(result, expected);
64+
});
65+
66+
it('returns an object with repo info', function() {
67+
var repoRoot = path.join(testFixturesPath, 'detached-head');
68+
var result = repoInfo(path.join(repoRoot, gitDir))
69+
70+
var expected = {
71+
branch: null,
72+
sha: '9dac893d5a83c02344d91e79dad8904889aeacb1',
73+
abbreviatedSha: '9dac893d5a'
74+
}
75+
76+
assert.deepEqual(result, expected);
77+
});
78+
});
79+
});

0 commit comments

Comments
 (0)