Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 33 additions & 11 deletions lib/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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})
Comment thread
osfameron marked this conversation as resolved.
Outdated
}

if (args.remote) {
Expand Down Expand Up @@ -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}`
}

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isMainGitDir only treats a repo as valid when .git is a directory. Linked worktrees typically have a .git file pointing at the shared gitdir, so this logic will fail to recognize the current worktree checkout and fall back to the remote URL (triggering an unnecessary clone and breaking the intended worktree support). Adjust the detection to treat a .git file as a valid worktree (or parse git rev-parse --git-dir / git worktree list --porcelain to locate the right working tree).

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intention is that the readdir checks every file, and returns the first one that does have a main git dir.

}
}

Comment thread
osfameron marked this conversation as resolved.
// 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())
Expand Down Expand Up @@ -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,
Expand All @@ -133,8 +157,6 @@ module.exports.register = function () {
Object.entries(updatedSources)
.map(urlMapper)))

console.dir(mappedSources, {depth: 5})

const startPage =
{ site:
{ startPage:
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/preview
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
osfameron marked this conversation as resolved.
export PREVIEW_BRANCH=$(git branch --show-current)
export PREVIEW_START_PATH=.${ANTORA#$TOPLEVEL}
export PREVIEW_CONFIG=${PREVIEW_CONFIG:-$PREVIEW_BRANCH}
Expand Down Expand Up @@ -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"
Expand Down
Loading