Skip to content

Commit 35bc0a2

Browse files
authored
Add a timeout to git fetch in precommit hook (#3237)
1 parent 0924fa7 commit 35bc0a2

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

tools/gitHooks/precommit.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ $ git stash -u
3636
$ git stash pop
3737
`;
3838

39+
const FETCH_TIMEOUT = 10 * 1000; // 10 seconds
40+
41+
function timeoutPromise(millis) {
42+
return new Promise((resolve, reject) => {
43+
setTimeout(reject, millis);
44+
});
45+
}
46+
3947
(async () => {
4048
try {
4149
const hasDiff = !!(await git.diff());
@@ -50,7 +58,10 @@ $ git stash pop
5058
' Fetching latest version of master branch.'
5159
).start();
5260
try {
53-
await git.fetch('origin', 'master');
61+
await Promise.race([
62+
git.fetch('origin', 'master'),
63+
timeoutPromise(FETCH_TIMEOUT)
64+
]);
5465
fetchSpinner.stopAndPersist({
5566
symbol: '✅'
5667
});
@@ -59,7 +70,7 @@ $ git stash pop
5970
symbol: '⚠️'
6071
});
6172
console.warn(
62-
chalk`\n{yellow} Unable to fetch latest version of master, diff may be stale.`
73+
chalk`\n{yellow WARNING: Unable to fetch latest version of master, diff may be stale.}`
6374
);
6475
}
6576

0 commit comments

Comments
 (0)