Skip to content

Add a timeout to git fetch in precommit hook #3237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions tools/gitHooks/precommit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ $ git stash -u
$ git stash pop
`;

const FETCH_TIMEOUT = 10 * 1000; // 10 seconds

function timeoutPromise(millis) {
return new Promise((resolve, reject) => {
setTimeout(reject, millis);
});
}

(async () => {
try {
const hasDiff = !!(await git.diff());
Expand All @@ -50,7 +58,10 @@ $ git stash pop
' Fetching latest version of master branch.'
).start();
try {
await git.fetch('origin', 'master');
await Promise.race([
git.fetch('origin', 'master'),
timeoutPromise(FETCH_TIMEOUT)
]);
fetchSpinner.stopAndPersist({
symbol: '✅'
});
Expand All @@ -59,7 +70,7 @@ $ git stash pop
symbol: '⚠️'
});
console.warn(
chalk`\n{yellow} Unable to fetch latest version of master, diff may be stale.`
chalk`\n{yellow WARNING: Unable to fetch latest version of master, diff may be stale.}`
);
}

Expand Down