diff --git a/publish/src/core/change-detection.ts b/publish/src/core/change-detection.ts index 2dacde0..eae6a34 100644 --- a/publish/src/core/change-detection.ts +++ b/publish/src/core/change-detection.ts @@ -4,6 +4,10 @@ function escapeRegex(s: string): string { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') } +function stripParentSegments(path: string): string { + return path.replace(/^(\.\.\/)+/, '') +} + export function filterPostDirs(lines: string[], contentRoot: string): string[] { const root = contentRoot.replace(/\/$/, '') const prefix = root + '/' @@ -49,7 +53,8 @@ export interface DiffArgs { export async function changedPostDirs(args: DiffArgs): Promise { const runner = args.runner ?? defaultRunner const stdout = await runner(['diff', '--name-only', `${args.from}..${args.to}`]) - return filterPostDirs(stdout.split('\n'), args.contentRoot) + const normalizedRoot = stripParentSegments(args.contentRoot) + return filterPostDirs(stdout.split('\n'), normalizedRoot) } export async function allPostDirs(contentRoot: string): Promise { diff --git a/publish/tests/change-detection_test.ts b/publish/tests/change-detection_test.ts index 300287c..5a153f4 100644 --- a/publish/tests/change-detection_test.ts +++ b/publish/tests/change-detection_test.ts @@ -85,6 +85,30 @@ Deno.test('filterPostDirs: _drafts unter sprach-ebene wird ignoriert', () => { assertEquals(filterPostDirs(lines, 'content/posts'), ['content/posts/de/real']) }) +Deno.test('changedPostDirs: normalisiert contentRoot mit ../-präfix', async () => { + const runner: GitRunner = () => + Promise.resolve('content/posts/de/alpha/index.md\ncontent/posts/en/beta/index.md\n') + const dirs = await changedPostDirs({ + from: 'HEAD~1', + to: 'HEAD', + contentRoot: '../content/posts', + runner, + }) + assertEquals(dirs.sort(), ['content/posts/de/alpha', 'content/posts/en/beta']) +}) + +Deno.test('changedPostDirs: normalisiert contentRoot mit mehrfachem ../-präfix', async () => { + const runner: GitRunner = () => + Promise.resolve('content/posts/de/alpha/index.md\n') + const dirs = await changedPostDirs({ + from: 'HEAD~1', + to: 'HEAD', + contentRoot: '../../content/posts', + runner, + }) + assertEquals(dirs, ['content/posts/de/alpha']) +}) + Deno.test('allPostDirs: findet posts in sprach-unterordnern', async () => { const tmp = await Deno.makeTempDir() try {