Skip to content

Commit 6bc2052

Browse files
marioneblcommitlint
authored and
commitlint
committed
fix: determine git root correctly from sub directories
1 parent 8bfa0cb commit 6bc2052

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

@commitlint/cli/cli.test.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ test('should work with husky commitmsg hook', async () => {
108108
await rm([HUSKY])();
109109
});
110110

111-
test.failing('should work with husky commitmsg hook in sub packages', async () => {
111+
test('should work with husky commitmsg hook in sub packages', async () => {
112112
const cwd = HUSKY_INTEGRATION;
113113
const upper = path.dirname(HUSKY_INTEGRATION);
114114

@@ -123,12 +123,7 @@ test.failing('should work with husky commitmsg hook in sub packages', async () =
123123
await npm(['install', 'husky'], {cwd})();
124124
await git(['add', 'package.json'], {cwd})();
125125

126-
try {
127-
await git(['commit', '-m', '"chore: this should work"'], {cwd})();
128-
} catch (err) {
129-
console.log(err.stderr);
130-
throw err;
131-
}
126+
await git(['commit', '-m', '"chore: this should work"'], {cwd})();
132127

133128
await rm([upper])();
134129
});

@commitlint/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@
134134
"chalk": "^2.0.1",
135135
"conventional-changelog-angular": "^1.3.3",
136136
"conventional-commits-parser": "^1.3.0",
137+
"find-up": "^2.1.0",
137138
"franc": "^2.0.0",
138139
"git-raw-commits": "^1.1.2",
139-
"git-toplevel": "^1.1.1",
140140
"import-from": "^2.1.0",
141141
"lodash": "^4.17.4",
142142
"mz": "^2.6.0",

@commitlint/core/src/read.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {join} from 'path';
1+
import path from 'path';
22
import exists from 'path-exists';
3+
import up from 'find-up';
34
import gitRawCommits from 'git-raw-commits';
4-
import gitToplevel from 'git-toplevel';
55
import {readFile} from 'mz/fs';
66

77
export default getCommitMessages;
@@ -45,16 +45,38 @@ function getHistoryCommits(options) {
4545
// Check if the current repository is shallow
4646
// () => Promise<Boolean>
4747
async function isShallow() {
48-
const top = await gitToplevel();
49-
const shallow = join(top, '.git/shallow');
48+
const top = await toplevel();
49+
50+
if (typeof top !== 'string') {
51+
throw new TypeError(`Could not find git root - is this a git repository?`);
52+
}
53+
54+
const shallow = path.join(top, '.git/shallow');
5055
return exists(shallow);
5156
}
5257

5358
// Get recently edited commit message
5459
// () => Promise<Array<String>>
5560
async function getEditCommit() {
56-
const top = await gitToplevel();
57-
const editFilePath = join(top, '.git/COMMIT_EDITMSG');
61+
const top = await toplevel();
62+
63+
if (typeof top !== 'string') {
64+
throw new TypeError(`Could not find git root - is this a git repository?`);
65+
}
66+
67+
const editFilePath = path.join(top, '.git/COMMIT_EDITMSG');
5868
const editFile = await readFile(editFilePath);
5969
return [`${editFile.toString('utf-8')}\n`];
6070
}
71+
72+
// Find the next git root
73+
// (start: string) => Promise<string | null>
74+
async function toplevel(cwd = process.cwd()) {
75+
const found = await up('.git', {cwd});
76+
77+
if (typeof found !== 'string') {
78+
return found;
79+
}
80+
81+
return path.join(found, '..');
82+
}

0 commit comments

Comments
 (0)