Skip to content

Commit a3d30de

Browse files
cpojerFacebook Github Bot 8
authored andcommitted
Wrap git/hg detectors in try-catch. See #905.
Summary: In #905 it was reported that people without mercurial had some trouble with `jest -o`. I fixed that initially but on some system it seems like `childProcess.spawn` can throw. It doesn't seem to happen on OS X locally but based on what I read about `childProcess.spawn` I do believe it can throw. I think it is best to wrap it in a try-catch now – the worst thing that can happen is to report back as not a git or not an hg repo, which it won't be if the commands fail. I also took a note to write an integration test for this, now that we have integration tests. I will add a few in a follow up. Closes #941 Reviewed By: kentaromiura Differential Revision: D3229317 fb-gh-sync-id: 34bffb7f256924b9766f6b12ba9c81ac663d8eba fbshipit-source-id: 34bffb7f256924b9766f6b12ba9c81ac663d8eba
1 parent 8ea6115 commit a3d30de

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/lib/git.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ function findChangedFiles(cwd) {
3939

4040
function isGitRepository(cwd) {
4141
return new Promise(resolve => {
42-
let stdout = '';
43-
const options = ['rev-parse', '--show-toplevel'];
44-
const child = childProcess.spawn('git', options, {cwd});
45-
child.stdout.on('data', data => stdout += data);
46-
child.on('error', () => resolve(null));
47-
child.on('close', code => resolve(code === 0 ? stdout.trim() : null));
42+
try {
43+
let stdout = '';
44+
const options = ['rev-parse', '--show-toplevel'];
45+
const child = childProcess.spawn('git', options, {cwd});
46+
child.stdout.on('data', data => stdout += data);
47+
child.on('error', () => resolve(null));
48+
child.on('close', code => resolve(code === 0 ? stdout.trim() : null));
49+
} catch (e) {
50+
resolve(null);
51+
}
4852
});
4953
}
5054

src/lib/hg.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ function findChangedFiles(cwd) {
3939

4040
function isHGRepository(cwd) {
4141
return new Promise(resolve => {
42-
let stdout = '';
43-
const child = childProcess.spawn('hg', ['root'], {cwd});
44-
child.stdout.on('data', data => stdout += data);
45-
child.on('error', () => resolve(null));
46-
child.on('close', code => resolve(code === 0 ? stdout.trim() : null));
42+
try {
43+
let stdout = '';
44+
const child = childProcess.spawn('hg', ['root'], {cwd});
45+
child.stdout.on('data', data => stdout += data);
46+
child.on('error', () => resolve(null));
47+
child.on('close', code => resolve(code === 0 ? stdout.trim() : null));
48+
} catch (e) {
49+
resolve(null);
50+
}
4751
});
4852
}
4953

0 commit comments

Comments
 (0)