Skip to content
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

fix(content-docs): read last update from inner git repositories #6592

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
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
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