diff --git a/.eslintignore b/.eslintignore index 8f2627076..2effa4b77 100644 --- a/.eslintignore +++ b/.eslintignore @@ -570,6 +570,7 @@ packages/default-plugins/build.js packages/default-plugins/buildDefaultPlugins.js packages/default-plugins/commands/buildAll.js packages/default-plugins/commands/editPatch.js +packages/default-plugins/utils/getCurrentCommitHash.js packages/default-plugins/utils/getPathToPatchFileFor.js packages/default-plugins/utils/readRepositoryJson.js packages/default-plugins/utils/waitForCliInput.js diff --git a/.gitignore b/.gitignore index 6a453a1c7..64f1ba0db 100644 --- a/.gitignore +++ b/.gitignore @@ -550,6 +550,7 @@ packages/default-plugins/build.js packages/default-plugins/buildDefaultPlugins.js packages/default-plugins/commands/buildAll.js packages/default-plugins/commands/editPatch.js +packages/default-plugins/utils/getCurrentCommitHash.js packages/default-plugins/utils/getPathToPatchFileFor.js packages/default-plugins/utils/readRepositoryJson.js packages/default-plugins/utils/waitForCliInput.js diff --git a/packages/default-plugins/buildDefaultPlugins.ts b/packages/default-plugins/buildDefaultPlugins.ts index c764ea393..44c21f384 100644 --- a/packages/default-plugins/buildDefaultPlugins.ts +++ b/packages/default-plugins/buildDefaultPlugins.ts @@ -10,6 +10,7 @@ import { glob } from 'glob'; import readRepositoryJson from './utils/readRepositoryJson'; import waitForCliInput from './utils/waitForCliInput'; import getPathToPatchFileFor from './utils/getPathToPatchFileFor'; +import getCurrentCommitHash from './utils/getCurrentCommitHash'; type BeforeEachInstallCallback = (buildDir: string, pluginName: string)=> Promise; @@ -41,20 +42,21 @@ const buildDefaultPlugins = async (outputParentDir: string|null, beforeInstall: } chdir(pluginDir); - const currentCommitHash = (await execCommand(['git', 'rev-parse', 'HEAD'])).trim(); const expectedCommitHash = repositoryData.commit; - if (currentCommitHash !== expectedCommitHash) { - logStatus(`Switching to commit ${expectedCommitHash}`); - await execCommand(['git', 'switch', repositoryData.branch]); + logStatus(`Switching to commit ${expectedCommitHash}`); + await execCommand(['git', 'switch', repositoryData.branch]); - try { - await execCommand(['git', 'checkout', expectedCommitHash]); - } catch (error) { - logStatus(`git checkout failed with error ${error}. Fetching...`); - await execCommand(['git', 'fetch']); - await execCommand(['git', 'checkout', expectedCommitHash]); - } + try { + await execCommand(['git', 'checkout', expectedCommitHash]); + } catch (error) { + logStatus(`git checkout failed with error ${error}. Fetching...`); + await execCommand(['git', 'fetch']); + await execCommand(['git', 'checkout', expectedCommitHash]); + } + + if (await getCurrentCommitHash() !== expectedCommitHash) { + throw new Error(`Unable to checkout commit ${expectedCommitHash}`); } logStatus('Copying repository files...'); diff --git a/packages/default-plugins/utils/getCurrentCommitHash.ts b/packages/default-plugins/utils/getCurrentCommitHash.ts new file mode 100644 index 000000000..8bd6d2071 --- /dev/null +++ b/packages/default-plugins/utils/getCurrentCommitHash.ts @@ -0,0 +1,7 @@ +import { execCommand } from '@joplin/utils'; + +const getCurrentCommitHash = async () => { + return (await execCommand(['git', 'rev-parse', '--verify', 'HEAD^{commit}'])).trim(); +}; + +export default getCurrentCommitHash;