Skip to content

Commit b8d8bb5

Browse files
committed
Add commonGitDir and worktreeGitDir to returned object; add typings
1 parent 3bb5154 commit b8d8bb5

File tree

5 files changed

+161
-54
lines changed

5 files changed

+161
-54
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ info.committerDate // commit date for the current sha
2222
info.author // author for the current sha
2323
info.authorDate // authored date for the current sha
2424
info.commitMessage // commit message for the current sha
25+
info.root // root directory for the Git repo or submodule
26+
// (if in a worktree, this is the directory containing the original copy)
27+
info.commonGitDir // directory containing Git metadata for this repo or submodule
28+
// (if in a worktree, this is the primary Git directory for the repo)
29+
info.worktreeGitDir // if in a worktree, the directory containing Git metadata specific to
30+
// this worktree; otherwise, this is the same as `commonGitDir`.
2531
```
2632

2733
When called without any arguments, `git-repo-info` will automatically lookup upwards

index.d.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
declare function gitRepoInfo(): gitRepoInfo.GitRepoInfo;
2+
3+
declare namespace gitRepoInfo {
4+
export interface GitRepoInfo {
5+
/** The current branch */
6+
branch: string;
7+
/** SHA of the current commit */
8+
sha: string;
9+
/** The first 10 chars of the current SHA */
10+
abbreviatedSha: string;
11+
/** The tag for the current SHA (or `null` if no tag exists) */
12+
tag: string | null;
13+
/** Tag for the closest tagged ancestor (or `null` if no ancestor is tagged) */
14+
lastTag: string | null;
15+
/** The committer of the current SHA */
16+
committer: string;
17+
/** The commit date of the current SHA */
18+
committerDate: string;
19+
/** The author for the current SHA */
20+
author: string;
21+
/** The authored date for the current SHA */
22+
authorDate: string;
23+
/** The commit message for the current SHA */
24+
commitMessage: string;
25+
/**
26+
* The root directory for the Git repo or submodule.
27+
* If in a worktree, this is the directory containing the original copy, not the worktree.
28+
*/
29+
root: string;
30+
/**
31+
* The directory containing Git metadata for this repo or submodule.
32+
* If in a worktree, this is the primary Git directory for the repo, not the worktree-specific one.
33+
*/
34+
commonGitDir: string;
35+
/**
36+
* If in a worktree, the directory containing Git metadata specific to this worktree.
37+
* Otherwise, this is the same as `commonGitDir`.
38+
*/
39+
worktreeGitDir: string;
40+
}
41+
}
42+
43+
export = gitRepoInfo;

index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ function changeGitDir(newDirName) {
1212

1313
function findRepoHandleLinkedWorktree(gitPath) {
1414
var stat = fs.statSync(gitPath);
15+
var root = path.dirname(path.resolve(gitPath));
1516
if (stat.isDirectory()) {
1617
return {
1718
// for the base (non-linked) dir, there is no distinction between where we
1819
// find the HEAD file and where we find the rest of .git
1920
worktreeGitDir: gitPath,
2021
commonGitDir: gitPath,
22+
root: root,
2123
};
2224
} else {
2325
// We have a file that tells us where to find the worktree git dir. Once we
@@ -30,21 +32,22 @@ function findRepoHandleLinkedWorktree(gitPath) {
3032
var worktreeGitDir = path.resolve(absolutePath,worktreeGitDirUnresolved);
3133
var commonDirPath = path.join(worktreeGitDir, 'commondir');
3234
if (fs.existsSync(commonDirPath)) {
33-
// this directory contains a `commondir` file; we're within a linked
34-
// worktree
35+
// this directory contains a `commondir` file; we're in a linked worktree
3536

3637
var commonDirRelative = fs.readFileSync(commonDirPath).toString().replace(/\r?\n$/, '');
3738
var commonDir = path.resolve(path.join(worktreeGitDir, commonDirRelative));
3839

3940
return {
4041
worktreeGitDir: worktreeGitDir,
4142
commonGitDir: commonDir,
43+
root: path.dirname(commonDir),
4244
};
4345
} else {
4446
// there is no `commondir` file; we're in a submodule
4547
return {
4648
worktreeGitDir: worktreeGitDir,
4749
commonGitDir: worktreeGitDir,
50+
root: root,
4851
};
4952
}
5053
}
@@ -94,7 +97,7 @@ function getPackedRefsFile(gitPath) {
9497
}
9598

9699
function getLinesForRefPath(packedRefsFile, type, refPath) {
97-
return packedRefsFile.split('\n').reduce(function(acc, line, idx, arr) {
100+
return packedRefsFile.split(/\r?\n/).reduce(function(acc, line, idx, arr) {
98101
var targetLine = line.indexOf('^') > -1 ? arr[idx-1] : line;
99102
return doesLineMatchRefPath(type, line, refPath) ? acc.concat(targetLine) : acc;
100103
}, []);
@@ -132,7 +135,7 @@ function commitForTag(gitPath, tag) {
132135
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson
133136
// <[email protected]> 1429100021 -0400\n\nI am making an annotated tag.\n'
134137
if (objectContents.slice(0,3) === 'tag') {
135-
var sections = objectContents.split(/\0|\n/);
138+
var sections = objectContents.split(/\0|\r?\n/);
136139
var sha = sections[1].slice(7);
137140

138141
return sha;
@@ -214,16 +217,20 @@ module.exports = function(gitPath) {
214217
authorDate: null,
215218
commitMessage: null,
216219
root: null,
220+
commonGitDir: null,
221+
worktreeGitDir: null,
217222
lastTag: null,
218223
commitsSinceLastTag: 0,
219224
};
220225

221226
if (!gitPathInfo) { return result; }
222227

223228
try {
224-
result.root = path.resolve(gitPathInfo.commonGitDir, '..');
229+
result.root = gitPathInfo.root;
230+
result.commonGitDir = gitPathInfo.commonGitDir;
231+
result.worktreeGitDir = gitPathInfo.worktreeGitDir;
225232

226-
var headFilePath = path.join(gitPathInfo.worktreeGitDir, 'HEAD');
233+
var headFilePath = path.join(gitPathInfo.worktreeGitDir, 'HEAD');
227234

228235
if (fs.existsSync(headFilePath)) {
229236
var headFile = fs.readFileSync(headFilePath, {encoding: 'utf8'});
@@ -290,7 +297,7 @@ function getCommitData(gitPath, sha) {
290297
if (zlib.inflateSync && fs.existsSync(objectPath)) {
291298
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
292299

293-
return objectContents.split(/\0|\n/)
300+
return objectContents.split(/\0|\r?\n/)
294301
.filter(function(item) {
295302
return !!item;
296303
})
@@ -315,7 +322,7 @@ function getCommitData(gitPath, sha) {
315322
}
316323
break;
317324
case 'parent':
318-
data.parents = section.split('\n').map(p => p.split(' ')[1]);
325+
data.parents = section.split(/\r?\n/).map(p => p.split(' ')[1]);
319326
break;
320327
default:
321328
//should just be the commit message left

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
"version": "2.0.0",
44
"description": "Retrieve current sha and branch name from a git repo.",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"files": [
7-
"index.js"
8+
"index.js",
9+
"index.d.ts"
810
],
911
"scripts": {
1012
"test": "mocha tests",
@@ -25,8 +27,8 @@
2527
},
2628
"homepage": "https://github.com/rwjblue/git-repo-info",
2729
"devDependencies": {
28-
"mocha": "^1.21.4",
29-
"mocha-jshint": "^1.1.0"
30+
"mocha": "^4.1.0",
31+
"mocha-jshint": "^2.3.1"
3032
},
3133
"engines": {
3234
"node": ">= 4.0"

0 commit comments

Comments
 (0)