Skip to content

Commit

Permalink
fix: read last update from inner git repositories (#6592)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs authored Feb 3, 2022
1 parent 8535154 commit b03431f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 16 additions & 3 deletions packages/docusaurus-plugin-content-docs/src/lastUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import shell from 'shelljs';
import logger from '@docusaurus/logger';
import path from 'path';

type FileLastUpdateData = {timestamp?: number; author?: string};

Expand Down Expand Up @@ -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}`,
Expand Down

0 comments on commit b03431f

Please sign in to comment.