diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts index d90d3b2189..6e90dc54d6 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts @@ -55,7 +55,7 @@ describe('lastUpdate', () => { await expect(getFileLastUpdate(nonExistingFilePath)).resolves.toBeNull(); expect(consoleMock).toHaveBeenCalledTimes(1); expect(consoleMock).toHaveBeenLastCalledWith( - expect.stringMatching(/with exit code 128/), + expect.stringMatching(/because the file does not exist./), ); await expect(getFileLastUpdate(null)).resolves.toBeNull(); await expect(getFileLastUpdate(undefined)).resolves.toBeNull(); diff --git a/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts b/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts index 87dde70cf0..9fdf2ecba7 100644 --- a/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts +++ b/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts @@ -7,6 +7,7 @@ import shell from 'shelljs'; import logger from '@docusaurus/logger'; +import path from 'path'; type FileLastUpdateData = {timestamp?: number; author?: string}; @@ -43,9 +44,21 @@ export async function getFileLastUpdate( return null; } - const result = shell.exec(`git log -1 --format=%ct,%an "${filePath}"`, { - silent: true, - }); + if (!shell.test('-f', filePath)) { + throw new Error( + `Retrieval of git history failed at "${filePath}" because the file does not exist.`, + ); + } + + const fileBasename = path.basename(filePath); + const fileDirname = path.dirname(filePath); + const result = shell.exec( + `git log --max-count=1 --format=%ct,%an -- "${fileBasename}"`, + { + cwd: fileDirname, // this is needed: https://github.com/facebook/docusaurus/pull/5048 + silent: true, + }, + ); if (result.code !== 0) { throw new Error( `Retrieval of git history failed at "${filePath}" with exit code ${result.code}: ${result.stderr}`,