Skip to content
Open
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
17 changes: 17 additions & 0 deletions packages/docusaurus-utils/src/vcs/__tests__/gitUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,23 @@ describe('VSC strategies', () => {
});
});

it('can read repo file info from a site-relative path', async () => {
const {vcs} = await initVsc();

// RouteMetadata.sourceFilePath is documented as relative to the site dir,
// and the sitemap plugin forwards it here unchanged. The map is keyed by
// absolute paths, so a relative path used to miss and silently return
// null, dropping <lastmod> from the sitemap.
await expect(vcs.getFileLastUpdateInfo('rootFile.md')).resolves.toEqual({
author: 'Seb',
timestamp: new Date('2020-06-19').getTime(),
});
await expect(vcs.getFileCreationInfo('rootFile.md')).resolves.toEqual({
author: 'Seb',
timestamp: new Date('2020-06-19').getTime(),
});
});

it('can read submodule file', async () => {
const {vcs, repoDir} = await initVsc();

Expand Down
16 changes: 14 additions & 2 deletions packages/docusaurus-utils/src/vcs/vcsGitEager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {resolve, basename} from 'node:path';
import {resolve, basename, isAbsolute} from 'node:path';
import logger, {PerfLogger} from '@docusaurus/logger';
import {
getGitAllRepoRoots,
Expand Down Expand Up @@ -82,11 +82,22 @@ async function initialize({

export function createVcsGitEagerConfig(): VcsConfig {
let initPromise: Promise<InitializeResult> | null = null;
let initSiteDir: string | null = null;

async function getGitFileInfo(filePath: string): Promise<GitFileInfo | null> {
const init = (await initPromise)!;
if (init.type === 'success') {
return init.filesMap.get(filePath) ?? null;
// The map is keyed by absolute paths, but callers may pass a path
// relative to the site dir. RouteMetadata.sourceFilePath in particular
// "is expected to be relative to the site directory", which is what the
// sitemap plugin forwards here. The ad-hoc strategy tolerates both
// because it shells out to `git log`, so a relative path silently
// returned null here instead.
const key =
isAbsolute(filePath) || !initSiteDir
? filePath
: resolve(initSiteDir, filePath);
return init.filesMap.get(key) ?? null;
} else if (init.reason === 'not-in-worktree') {
throw new Error(
`This Docusaurus site is outside any Git worktree.
Expand Down Expand Up @@ -114,6 +125,7 @@ Unable to read Git info for file ${logger.path(filePath)} `,
return;
}

initSiteDir = siteDir;
initPromise = PerfLogger.async('Git Eager VCS init', () =>
initialize({siteDir}),
);
Expand Down