|
1 |
| -import {join} from 'path'; |
| 1 | +import path from 'path'; |
2 | 2 | import exists from 'path-exists';
|
| 3 | +import up from 'find-up'; |
3 | 4 | import gitRawCommits from 'git-raw-commits';
|
4 |
| -import gitToplevel from 'git-toplevel'; |
5 | 5 | import {readFile} from 'mz/fs';
|
6 | 6 |
|
7 | 7 | export default getCommitMessages;
|
@@ -45,16 +45,38 @@ function getHistoryCommits(options) {
|
45 | 45 | // Check if the current repository is shallow
|
46 | 46 | // () => Promise<Boolean>
|
47 | 47 | 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'); |
50 | 55 | return exists(shallow);
|
51 | 56 | }
|
52 | 57 |
|
53 | 58 | // Get recently edited commit message
|
54 | 59 | // () => Promise<Array<String>>
|
55 | 60 | 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'); |
58 | 68 | const editFile = await readFile(editFilePath);
|
59 | 69 | return [`${editFile.toString('utf-8')}\n`];
|
60 | 70 | }
|
| 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