Skip to content

Commit c45a62b

Browse files
committed
fix: determine git toplevel before reading .git/COMMIT_EDITMSG
test: set git user and email test: ensure git ident setting do not bleed test: rm temporary dir from outside to ensure windows compat
1 parent c517652 commit c45a62b

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

source/library/get-messages.js

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
// core modules
2-
import {
3-
readFile as readFileNodeback
4-
} from 'fs';
5-
6-
import denodeify from 'denodeify';
1+
import {join} from 'path';
72
import gitRawCommits from 'git-raw-commits';
3+
import gitToplevel from 'git-toplevel';
4+
import {readFile} from 'mz/fs';
85

9-
const readFile = denodeify(readFileNodeback);
6+
export default getCommitMessages;
107

118
// Get commit messages
12-
function getCommits(options) {
9+
// Object => Promise<Array<String>>
10+
async function getCommitMessages(settings) {
11+
const {from, to, edit} = settings;
12+
13+
if (edit) {
14+
return getEditCommit();
15+
}
16+
17+
return await getHistoryCommits({from, to});
18+
}
19+
20+
// Get commit messages from history
21+
// Object => Promise<Array<String>>
22+
function getHistoryCommits(options) {
1323
return new Promise((resolve, reject) => {
1424
const data = [];
1525
gitRawCommits(options)
@@ -21,17 +31,11 @@ function getCommits(options) {
2131
});
2232
}
2333

24-
// Get commit messages
25-
export default async settings => {
26-
const {from, to, edit} = settings;
27-
28-
if (edit) {
29-
const editFile = await readFile(`.git/COMMIT_EDITMSG`);
30-
return [`${editFile.toString('utf-8')}\n`];
31-
}
32-
33-
return await getCommits({
34-
from,
35-
to
36-
});
37-
};
34+
// Get recently edited commit message
35+
// () => Promise<Array<String>>
36+
async function getEditCommit() {
37+
const top = await gitToplevel();
38+
const editFilePath = join(top, '.git/COMMIT_EDITMSG');
39+
const editFile = await readFile(editFilePath);
40+
return [`${editFile.toString('utf-8')}\n`];
41+
}

test/integration/get-messages.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {join} from 'path';
55
import test from 'ava';
66
import denodeify from 'denodeify';
77
import execa from 'execa';
8-
import {writeFile} from 'mz/fs';
8+
import {mkdir, writeFile} from 'mz/fs';
99
import exists from 'path-exists';
1010
import rimraf from 'rimraf';
1111
import expect from 'unexpected';
@@ -44,19 +44,42 @@ test.serial('get history commit messages', async () => {
4444
await cleanRepository(repo);
4545
});
4646

47+
test.serial('get edit commit message from git subdirectory', async () => {
48+
const repo = await initRepository();
49+
50+
await mkdir('beta');
51+
await writeFile('beta/beta.txt', 'beta');
52+
process.chdir('beta');
53+
await execa('git', ['add', '.']);
54+
await execa('git', ['commit', '-m', 'beta']);
55+
56+
const expected = ['beta\n\n'];
57+
const actual = await getMessages({edit: true});
58+
expect(actual, 'to equal', expected);
59+
60+
await cleanRepository(repo);
61+
});
62+
4763
async function initRepository() {
4864
const previous = process.cwd();
4965
const directory = join(tmpdir(), rand());
66+
5067
await execa('git', ['init', directory]);
68+
5169
process.chdir(directory);
70+
71+
await execa('git', ['config', 'user.email', '[email protected]']);
72+
await execa('git', ['config', 'user.name', 'ava']);
73+
5274
return {directory, previous};
5375
}
5476

5577
async function cleanRepository(repo) {
78+
process.chdir(repo.previous);
79+
5680
if (await exists(repo.directory)) {
5781
await rm(repo.directory);
5882
}
59-
process.chdir(repo.previous);
6083
}
6184

6285
function rand() {

0 commit comments

Comments
 (0)