Skip to content

Commit

Permalink
fix: determine git root correctly from sub directories
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl authored and commitlint committed Aug 19, 2017
1 parent 8bfa0cb commit 6bc2052
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
9 changes: 2 additions & 7 deletions @commitlint/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ test('should work with husky commitmsg hook', async () => {
await rm([HUSKY])();
});

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

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

try {
await git(['commit', '-m', '"chore: this should work"'], {cwd})();
} catch (err) {
console.log(err.stderr);
throw err;
}
await git(['commit', '-m', '"chore: this should work"'], {cwd})();

await rm([upper])();
});
2 changes: 1 addition & 1 deletion @commitlint/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@
"chalk": "^2.0.1",
"conventional-changelog-angular": "^1.3.3",
"conventional-commits-parser": "^1.3.0",
"find-up": "^2.1.0",
"franc": "^2.0.0",
"git-raw-commits": "^1.1.2",
"git-toplevel": "^1.1.1",
"import-from": "^2.1.0",
"lodash": "^4.17.4",
"mz": "^2.6.0",
Expand Down
34 changes: 28 additions & 6 deletions @commitlint/core/src/read.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {join} from 'path';
import path from 'path';
import exists from 'path-exists';
import up from 'find-up';
import gitRawCommits from 'git-raw-commits';
import gitToplevel from 'git-toplevel';
import {readFile} from 'mz/fs';

export default getCommitMessages;
Expand Down Expand Up @@ -45,16 +45,38 @@ function getHistoryCommits(options) {
// Check if the current repository is shallow
// () => Promise<Boolean>
async function isShallow() {
const top = await gitToplevel();
const shallow = join(top, '.git/shallow');
const top = await toplevel();

if (typeof top !== 'string') {
throw new TypeError(`Could not find git root - is this a git repository?`);
}

const shallow = path.join(top, '.git/shallow');
return exists(shallow);
}

// Get recently edited commit message
// () => Promise<Array<String>>
async function getEditCommit() {
const top = await gitToplevel();
const editFilePath = join(top, '.git/COMMIT_EDITMSG');
const top = await toplevel();

if (typeof top !== 'string') {
throw new TypeError(`Could not find git root - is this a git repository?`);
}

const editFilePath = path.join(top, '.git/COMMIT_EDITMSG');
const editFile = await readFile(editFilePath);
return [`${editFile.toString('utf-8')}\n`];
}

// Find the next git root
// (start: string) => Promise<string | null>
async function toplevel(cwd = process.cwd()) {
const found = await up('.git', {cwd});

if (typeof found !== 'string') {
return found;
}

return path.join(found, '..');
}

0 comments on commit 6bc2052

Please sign in to comment.