Skip to content

Commit

Permalink
Added an Antora extension file to enable using Git worktrees. Added a…
Browse files Browse the repository at this point in the history
… build script that runs the necessary command line option to enable the extension. (#80)
  • Loading branch information
stoobie authored Oct 19, 2022
1 parent f6fd2c2 commit 34d09bc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions build_local_site.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx antora --extension ./linked-worktree-as-content-source.js playbook.yml
45 changes: 45 additions & 0 deletions linked-worktree-as-content-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

/* Copyright (c) 2022 OpenDevise, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License Version 2.0. If a copy of this license was not distributed
* with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
const { promises: fsp } = require('fs')
const ospath = require('path')

/**
* Rewrites local content sources to support the use of linked worktrees.
*
* @author Dan Allen <dan@opendevise.com>
*/
module.exports.register = function () {
this.once('playbookBuilt', async ({ playbook }) => {
const expandPath = this.require('@antora/expand-path-helper')
for (const contentSource of playbook.content.sources) {
const { url, branches } = contentSource
if (url.charAt() !== '.') continue
const absdir = expandPath(url, { dot: playbook.dir })
const gitfile = ospath.join(absdir, '.git')
if (await fsp.stat(gitfile).then((stat) => !stat.isDirectory(), () => false)) {
const worktreeGitdir = await fsp.readFile(gitfile, 'utf8')
.then((contents) => contents.trimRight().substr(8))
const worktreeBranch = await fsp.readFile(ospath.join(worktreeGitdir, 'HEAD'), 'utf8')
.then((contents) => contents.trimRight().replace(/^ref: (?:refs\/heads\/)?/, ''))
const reldir = ospath.relative(
playbook.dir,
await fsp.readFile(ospath.join(worktreeGitdir, 'commondir'), 'utf8')
.then((contents) => {
const gitdir = ospath.join(worktreeGitdir, contents.trimRight())
return ospath.basename(gitdir) === '.git' ? ospath.dirname(gitdir) : gitdir
})
)
contentSource.url = reldir ? `.${ospath.sep}${reldir}` : '.'
if (!branches) continue
contentSource.branches = (branches.constructor === Array ? branches : [branches])
.map((pattern) => pattern.replaceAll('HEAD', worktreeBranch))
}
}
})
}

0 comments on commit 34d09bc

Please sign in to comment.