Skip to content

Commit ccd9195

Browse files
committed
compare-screenshots: inherit page path from first argument
When the first argument specifies a page path (either via URL or worktree:/path syntax), the second argument will automatically use the same page path if none is specified. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 44cfaee commit ccd9195

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

script/compare-screenshots.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const { spawn, execSync } = require('child_process');
3333
const fs = require('fs');
3434
const path = require('path');
3535

36+
let lastPagePath;
37+
3638
/**
3739
* Parse a worktree argument to extract worktree path, commit, and page path.
3840
*
@@ -46,23 +48,38 @@ const path = require('path');
4648
* /path/to/worktree:/docs/git -> { worktreePath: '/path/to/worktree', commit: undefined, pagePath: 'docs/git' }
4749
* .@main:/about -> { worktreePath: '.', commit: 'main', pagePath: 'about' }
4850
*
51+
* If no page path is specified, inherits the page path from the previous call.
52+
*
4953
* Returns false if the argument is a URL or not a valid worktree.
5054
*/
5155
function getWorktreeInfo(arg) {
52-
if (arg.startsWith('http://') || arg.startsWith('https://')) return false;
56+
if (arg.startsWith('http://') || arg.startsWith('https://')) {
57+
// Extract path from URL for inheritance
58+
try {
59+
lastPagePath = new URL(arg).pathname.replace(/^\/+/, '');
60+
} catch {
61+
}
62+
return false;
63+
}
5364
// Allow @commit as shorthand for .@commit (current directory)
5465
if (arg.startsWith('@')) arg = '.' + arg;
5566
const colonIndex = arg.indexOf(':');
5667
const beforeColon = colonIndex === -1 ? arg : arg.slice(0, colonIndex);
57-
const pagePath = colonIndex === -1 ? '' : arg.slice(colonIndex + 1).replace(/^\/+/, '');
68+
let pagePath = colonIndex === -1 ? undefined : arg.slice(colonIndex + 1).replace(/^\/+/, '');
5869
const atIndex = beforeColon.indexOf('@');
5970
const worktreePath = atIndex === -1 ? beforeColon : beforeColon.slice(0, atIndex);
6071
let commit = atIndex === -1 ? undefined : beforeColon.slice(atIndex + 1);
6172
// Allow @{u} as shorthand for @@{u} since refs can't start with {
6273
if (commit && commit.startsWith('{')) commit = '@' + commit;
74+
// Inherit page path from previous call if not specified
75+
if (pagePath === undefined && lastPagePath !== undefined) {
76+
pagePath = lastPagePath;
77+
} else if (pagePath !== undefined) {
78+
lastPagePath = pagePath;
79+
}
6380
try {
6481
if (fs.statSync(path.join(worktreePath, 'hugo.yml')).isFile()) {
65-
return { worktreePath, commit, pagePath };
82+
return { worktreePath, commit, pagePath: pagePath || '' };
6683
}
6784
} catch {
6885
}

0 commit comments

Comments
 (0)