Skip to content

Commit 6b1de02

Browse files
committed
Add partial submodules support
This adds support for retrieiving the commit, branch &c. for submodules. It does not compute the correct `root` information, but it's still an improvement over v1.2.0 (which did not throw but provided no information) and v1.3.0 (which threw). [Fix #27]
1 parent 5f58b81 commit 6b1de02

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

index.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,32 @@ function findRepoHandleLinkedWorktree(gitPath) {
2020
commonGitDir: gitPath,
2121
};
2222
} else {
23+
// We have a file that tells us where to find the worktree git dir. Once we
24+
// look there we'll know how to find the common git dir, depending on
25+
// whether it's a linked worktree git dir, or a submodule dir
26+
2327
var linkedGitDir = fs.readFileSync(gitPath).toString();
24-
var worktreeGitDir = /gitdir: (.*)/.exec(linkedGitDir)[1];
28+
var worktreeGitDirUnresolved = /gitdir: (.*)/.exec(linkedGitDir)[1];
29+
var worktreeGitDir = path.resolve(worktreeGitDirUnresolved);
2530
var commonDirPath = path.join(worktreeGitDir, 'commondir');
26-
var commonDirRelative = fs.readFileSync(commonDirPath).toString().replace(/\r?\n$/, '');
27-
var commonDir = path.resolve(path.join(worktreeGitDir, commonDirRelative));
31+
if (fs.existsSync(commonDirPath)) {
32+
// this directory contains a `commondir` file; we're within a linked
33+
// worktree
2834

29-
return {
30-
worktreeGitDir: path.resolve(worktreeGitDir),
31-
commonGitDir: commonDir,
32-
};
35+
var commonDirRelative = fs.readFileSync(commonDirPath).toString().replace(/\r?\n$/, '');
36+
var commonDir = path.resolve(path.join(worktreeGitDir, commonDirRelative));
37+
38+
return {
39+
worktreeGitDir: worktreeGitDir,
40+
commonGitDir: commonDir,
41+
};
42+
} else {
43+
// there is no `commondir` file; we're in a submodule
44+
return {
45+
worktreeGitDir: worktreeGitDir,
46+
commonGitDir: worktreeGitDir,
47+
};
48+
}
3349
}
3450
}
3551

tests/fixtures/submodule/dot-git/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9dac893d5a83c02344d91e79dad8904889aeacb1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
409372f3bd07c11bfacee3963f48571d675268d7
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gitdir: ../dot-git/modules/my-submodule

tests/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ describe('git-repo-info', function() {
8484
commonGitDir: path.join(testFixturesPath, 'linked-worktree', 'dot-git'),
8585
});
8686
});
87+
88+
it('finds a repo via a submodule', function() {
89+
process.chdir(path.join(testFixturesPath, 'submodule', 'my-submodule'));
90+
91+
var foundPathInfo = repoInfo._findRepo();
92+
assert.deepEqual(foundPathInfo, {
93+
worktreeGitDir: path.join(testFixturesPath, 'submodule', 'dot-git', 'modules', 'my-submodule'),
94+
commonGitDir: path.join(testFixturesPath, 'submodule', 'dot-git', 'modules', 'my-submodule'),
95+
});
96+
});
8797
});
8898

8999
describe('repoInfo', function() {
@@ -290,6 +300,29 @@ describe('git-repo-info', function() {
290300

291301
assert.deepEqual(result, expected);
292302
});
303+
304+
it('returns an object for repo info for submodules', function() {
305+
process.chdir(path.join(testFixturesPath, 'submodule', 'my-submodule'));
306+
var result = repoInfo();
307+
308+
var expected = {
309+
branch: null,
310+
sha: '409372f3bd07c11bfacee3963f48571d675268d7',
311+
abbreviatedSha: '409372f3bd',
312+
tag: null,
313+
committer: null,
314+
committerDate: null,
315+
author: null,
316+
authorDate: null,
317+
commitMessage: null,
318+
// This is a pretty meaningless "root" path. The other information is
319+
// correct, but we do not have full support for submodules at the
320+
// moment.
321+
root: path.join(testFixturesPath, 'submodule', 'dot-git', 'modules')
322+
};
323+
324+
assert.deepEqual(result, expected);
325+
});
293326
});
294327

295328
describe('repoInfo().root', function() {

0 commit comments

Comments
 (0)