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 8b44ce8fd9..323ac109ec 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts @@ -31,15 +31,16 @@ describe('lastUpdate', () => { test('non-existing file', async () => { const consoleMock = jest.spyOn(console, 'error'); consoleMock.mockImplementation(); + const nonExistingFileName = '.nonExisting'; const nonExistingFilePath = path.join( __dirname, '__fixtures__', - '.nonExisting', + nonExistingFileName, ); expect(await getFileLastUpdate(nonExistingFilePath)).toBeNull(); expect(consoleMock).toHaveBeenCalledTimes(1); expect(consoleMock.mock.calls[0][0].message).toContain( - `Command failed with exit code 128: git log -1 --format=%ct, %an ${nonExistingFilePath}`, + `Command failed with exit code 128: git log -1 --format=%ct, %an ${nonExistingFileName}`, ); expect(await getFileLastUpdate(null)).toBeNull(); expect(await getFileLastUpdate(undefined)).toBeNull(); diff --git a/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts b/packages/docusaurus-plugin-content-docs/src/lastUpdate.ts index 2ce8e71ffb..a7d07e9f94 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 execa from 'execa'; +import path from 'path'; type FileLastUpdateData = {timestamp?: number; author?: string}; @@ -43,12 +44,15 @@ export async function getFileLastUpdate( return null; } - const {stdout} = await execa('git', [ - 'log', - '-1', - '--format=%ct, %an', - filePath, - ]); + const fileBasename = path.basename(filePath); + const fileDirname = path.dirname(filePath); + const {stdout} = await execa( + 'git', + ['log', '-1', '--format=%ct, %an', fileBasename], + { + cwd: fileDirname, + }, + ); return getTimestampAndAuthor(stdout); } catch (error) { console.error(error);