Skip to content

Commit 157e14a

Browse files
committed
fix: warn about shallow clones
1 parent dcf2774 commit 157e14a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
},
6767
"repository": {
6868
"type": "git",
69-
"url": "git+https://github.com/marionebl/conventional-changelog-lint.git"
69+
"url": "https://github.com/marionebl/conventional-changelog-lint.git"
7070
},
7171
"bugs": {
7272
"url": "https://github.com/marionebl/conventional-changelog-lint/issues"

source/library/get-messages.js

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {join} from 'path';
2+
import exists from 'path-exists';
23
import gitRawCommits from 'git-raw-commits';
34
import gitToplevel from 'git-toplevel';
45
import {readFile} from 'mz/fs';
@@ -14,6 +15,10 @@ async function getCommitMessages(settings) {
1415
return getEditCommit();
1516
}
1617

18+
if (await isShallow()) {
19+
throw new Error('Could not get git history from shallow clone. Original issue: https://git.io/vyKMq\n Refer to https://git.io/vyKMv for details.');
20+
}
21+
1722
return await getHistoryCommits({from, to});
1823
}
1924

@@ -31,6 +36,14 @@ function getHistoryCommits(options) {
3136
});
3237
}
3338

39+
// Check if the current repository is shallow
40+
// () => Promise<Boolean>
41+
async function isShallow() {
42+
const top = await gitToplevel();
43+
const shallow = join(top, '.git/shallow');
44+
return await exists(shallow);
45+
}
46+
3447
// Get recently edited commit message
3548
// () => Promise<Array<String>>
3649
async function getEditCommit() {

test/integration/get-messages.js

+28
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import rimraf from 'rimraf';
1111
import expect from 'unexpected';
1212

1313
import getMessages from '../../source/library/get-messages';
14+
import pkg from '../../package';
1415

1516
const rm = denodeify(rimraf);
1617

@@ -60,6 +61,22 @@ test.serial('get edit commit message from git subdirectory', async () => {
6061
await cleanRepository(repo);
6162
});
6263

64+
test.serial('get history commit messages from shallow clone', async () => {
65+
const repo = await initRepository();
66+
67+
await writeFile('alpha.txt', 'alpha');
68+
await execa('git', ['add', 'alpha.txt']);
69+
await execa('git', ['commit', '-m', 'alpha']);
70+
71+
const clone = await cloneRepository(pkg.repository.url, repo, '--depth', '1');
72+
73+
const actual = async () => await getMessages({from: 'master'});
74+
expect(actual, 'to error with', /Could not get git history from shallow clone/);
75+
76+
await cleanRepository(clone);
77+
await cleanRepository(repo);
78+
});
79+
6380
async function initRepository() {
6481
const previous = process.cwd();
6582
const directory = join(tmpdir(), rand());
@@ -74,6 +91,17 @@ async function initRepository() {
7491
return {directory, previous};
7592
}
7693

94+
async function cloneRepository(source, context, ...args) {
95+
const directory = join(tmpdir(), rand());
96+
await execa('git', ['clone', ...args, source, directory]);
97+
process.chdir(directory);
98+
99+
await execa('git', ['config', 'user.email', '[email protected]']);
100+
await execa('git', ['config', 'user.name', 'ava']);
101+
102+
return {directory, previous: context.previous};
103+
}
104+
77105
async function cleanRepository(repo) {
78106
process.chdir(repo.previous);
79107

0 commit comments

Comments
 (0)