From 5bc007435ab44bef185c7e8ef1363447a311b679 Mon Sep 17 00:00:00 2001 From: Hakim Cassimally Date: Wed, 11 Mar 2026 09:18:10 +0000 Subject: [PATCH 1/3] DOC-14109 WIP worktree support This seems to work for me for simple cases now, but I feel like I'm missing: * checking if current working tree is a linked worktree. * the `worktrees: true` key --- lib/preview.js | 44 +++++++++++++++++++++++++++++++++----------- scripts/preview | 4 ++-- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/preview.js b/lib/preview.js index 3dab1edac1..c27fa8474a 100644 --- a/lib/preview.js +++ b/lib/preview.js @@ -10,7 +10,11 @@ const util = require('util') const exec = util.promisify(require('child_process').exec) module.exports.register = function () { - this.once('playbookBuilt', async function ({ playbook }) { + + this.once('playbookBuilt', rebuildPlaybook) + + async function rebuildPlaybook ({ playbook }) { + // deep-copy playbook to allow it to be updated const env = playbook.env playbook = JSON.parse(JSON.stringify(playbook)) @@ -37,7 +41,7 @@ module.exports.register = function () { const sources = getSources(playbook, additionalSources) const source = sources[args.repo] if (! source) { - throw notfound(args) + throw notfound({goat:1, ...args}) } if (args.remote) { @@ -76,16 +80,35 @@ module.exports.register = function () { } } - const mapLocalUrl = (repo, url) => (args.repoPath - .map(p => path.resolve(p, repo)) - .find(fs.existsSync) - || url) + const isMainGitDir = p => { + const pwg = `${p}/.git` + if (! fs.existsSync(pwg)) { return false } + return fs.statSync(pwg).isDirectory() + } + + function mapLocalUrl (repo, url) { + for (const repoPath of args.repoPath) { + const p = path.resolve(repoPath, repo) + if (fs.existsSync(p)) { + if (isMainGitDir(p)) { return p } + // otherwise check if this directory contains worktrees + const subdir = + fs.readdirSync(p).find( + n => isMainGitDir(`${p}/${n}`)) + if (subdir) { + return `${p}/${subdir}` + } + } + } + + // fallback to the original (remote) url + return url + } const basePath = path.resolve( mapLocalUrl(args.repo, 'UNEXPECTED'), args.startPath || source.start_path || '') const antoraYmlPath = `${basePath}/antora.yml` - console.log(antoraYmlPath) const readYaml = (path) => fs.existsSync(path) && yaml.parse(fs.readFileSync(path).toString()) @@ -118,6 +141,7 @@ module.exports.register = function () { const urlMapper = ([k,v]) => { if (! v.url) { throw notfound({...args, + goat: 2, repo: k, githubRemote: undefined, branch: undefined, @@ -133,8 +157,6 @@ module.exports.register = function () { Object.entries(updatedSources) .map(urlMapper))) - console.dir(mappedSources, {depth: 5}) - const startPage = { site: { startPage: @@ -152,11 +174,11 @@ module.exports.register = function () { playbook.asciidoc.attributes['page-watermark'] = `${date} ${args.repo} ${watermark_branch}` - // console.dir(playbook, {depth: 5}); process.exit(1) // reinflate .env before updating playbook.env = env + this.updateVariables({ playbook }) - }) + } } function notfound(args) { diff --git a/scripts/preview b/scripts/preview index 8d664ceaef..52b208a496 100755 --- a/scripts/preview +++ b/scripts/preview @@ -193,7 +193,7 @@ fi ANTORA=$(realpath $(dirname $ANTORA)) TOPLEVEL=$(git rev-parse --show-toplevel) -export PREVIEW_REPO=$(basename $TOPLEVEL) +export PREVIEW_REPO=$(gh repo view --json name --jq .name) export PREVIEW_BRANCH=$(git branch --show-current) export PREVIEW_START_PATH=.${ANTORA#$TOPLEVEL} export PREVIEW_CONFIG=${PREVIEW_CONFIG:-$PREVIEW_BRANCH} @@ -283,7 +283,7 @@ else cd $DOCS_SITE echo - ANTORA="npx antora --extension lib/preview.js antora-playbook.preview.yml --stacktrace --url '$(pwd)/preview'" + ANTORA="npx antora --extension ./lib/preview.js antora-playbook.preview.yml --stacktrace --url '$(pwd)/preview'" if [ -n "$DEBUG" ]; then echo "Running in debug mode" From 1d3c2fc7c4dcb74bc2f3cedb362c8d029f005fd4 Mon Sep 17 00:00:00 2001 From: Hakim Cassimally Date: Wed, 11 Mar 2026 11:23:30 +0000 Subject: [PATCH 2/3] degoat --- lib/preview.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/preview.js b/lib/preview.js index c27fa8474a..8f4a5cb26e 100644 --- a/lib/preview.js +++ b/lib/preview.js @@ -41,7 +41,7 @@ module.exports.register = function () { const sources = getSources(playbook, additionalSources) const source = sources[args.repo] if (! source) { - throw notfound({goat:1, ...args}) + throw notfound(args) } if (args.remote) { @@ -141,7 +141,6 @@ module.exports.register = function () { const urlMapper = ([k,v]) => { if (! v.url) { throw notfound({...args, - goat: 2, repo: k, githubRemote: undefined, branch: undefined, From a818b2768393736212c65e8aab73e373c504354f Mon Sep 17 00:00:00 2001 From: Hakim Cassimally Date: Mon, 16 Mar 2026 13:50:30 +0000 Subject: [PATCH 3/3] Search for worktree up to 2 levels deep. --- lib/preview.js | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/preview.js b/lib/preview.js index 8f4a5cb26e..a071d6dbb8 100644 --- a/lib/preview.js +++ b/lib/preview.js @@ -80,6 +80,34 @@ module.exports.register = function () { } } + function subdirs(p) { + return ( + fs.readdirSync(p) + .map(d => path.resolve(p, d)) + .filter(i => fs.statSync(i).isDirectory()) + ) + } + + function* iterate(p, level = 0) { + + if (! fs.existsSync(p)) return + + const stats = fs.statSync(p) + if (stats.isDirectory()) { + yield p + const s1 = subdirs(p) + for (const d of s1) { + yield d + } + for (const d of s1) { + const s2 = subdirs(d) + for (const d of s2) { + yield d + } + } + } + } + const isMainGitDir = p => { const pwg = `${p}/.git` if (! fs.existsSync(pwg)) { return false } @@ -89,16 +117,9 @@ module.exports.register = function () { function mapLocalUrl (repo, url) { for (const repoPath of args.repoPath) { const p = path.resolve(repoPath, repo) - if (fs.existsSync(p)) { - if (isMainGitDir(p)) { return p } - // otherwise check if this directory contains worktrees - const subdir = - fs.readdirSync(p).find( - n => isMainGitDir(`${p}/${n}`)) - if (subdir) { - return `${p}/${subdir}` - } - } + + const mainGitDir = iterate(p).find(isMainGitDir) + if (mainGitDir) { return mainGitDir } } // fallback to the original (remote) url