Tucker McKnight <tucker.mcknight@gmail.com> | Sun Aug 24 2025
wip: moving from separate site repo to plugin
-1
dist
node_modules
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
import fsImport from 'fs'
import util from 'util'
import childProcess from 'child_process'
import repos from './src/repos.ts'
import branches from './src/branches.ts'
import flatFiles from './src/flatFiles.ts'
import {getLocation} from './src/helpers.ts'
import repoOperations from './src/vcses/operations.ts'
const exec = util.promisify(childProcess.exec)
export default async (eleventyConfig, reposConfiguration) => {
// TODO: check if the render plugin is available and throw an error otherwise
// TODO: check if the highlight function is available
// TODO: throw an error if reposConfiguration is undefined
const reposData = await repos(reposConfiguration)
eleventyConfig.addFilter("getFileName", (filePath) => {
const pathParts = filePath.split("/")
return pathParts[pathParts.length - 1]
})
eleventyConfig.addFilter("getDirectoryContents", (repo, branch, dirPath) => {
return reposData[repo].branches[branch].files.filter(file => file.startsWith(dirPath) && file !== dirPath)
})
eleventyConfig.addFilter("getRelativePath", (currentDir, fullFilePath) => {
return fullFilePath.replace(`${currentDir}/`, "")
})
eleventyConfig.addFilter("lineNumbers", (code) => {
const numLines = code.split('\n').length
const lineNumbers = []
for (let i = 1; i <= numLines; i++) {
lineNumbers.push(i)
}
return lineNumbers
})
eleventyConfig.addFilter("highlightCode", (code, language) => {
return eleventyConfig.javascript.functions.highlight(language, code)
})
eleventyConfig.addFilter("languageExtension", (filename, repoName) => {
let extension = filename.split(".")
extension = extension[extension.length - 1]
const extensionsConfig = reposConfiguration.repos[repoName].languageExtensions
return extensionsConfig && extensionsConfig[extension] ? extensionsConfig[extension] : extension
})
eleventyConfig.addFilter("topLevelFilesOnly", (files, currentLevel) => {
const onlyUnique = (value, index, array) => {
return array.findIndex(test => test.name === value.name) === index;
}
const currentLevelDirLength = currentLevel.split('/').length
const topLevels: Array<string> = []
files.forEach((file) => {
if (file.startsWith(currentLevel)) {
const parts = file.split("/").filter(part => part !== ".")
topLevels.push(parts.slice(0, currentLevelDirLength).join('/'))
}
})
const withNameAndDirAttrs = topLevels.map((file) => {
// is a directory if the entire filename, plus a slash, is contained inside of any
// other file
const isDirectory: boolean = files.some((testFile) => {
return testFile.startsWith(file + '/') && (testFile !== file)
})
return {name: file.replace(currentLevel, ''), fullPath: file, isDirectory}
})
const sortedByDirectory = withNameAndDirAttrs.filter(onlyUnique).toSorted((a, b) => {
if (a.isDirectory && b.isDirectory) {
return 0
}
if (a.isDirectory && !b.isDirectory) {
return -1
}
return 1
})
return sortedByDirectory
})
eleventyConfig.addAsyncFilter("getFileLastTouchInfo", async (repo, branch, filename) => {
const ignoreExtensions = ['.png', '.jpg']
if (ignoreExtensions.some(extension => filename.endsWith(extension))) {
return ""
}
const config = reposConfiguration.repos[repo]
const location = getLocation(reposConfiguration, branch, repo)
return repoOperations[config._type].getFileLastTouchInfo(repo, branch, filename, location)
})
eleventyConfig.addAsyncFilter("isDirectory", async(filename, repoName, branchName) => {
const files = reposData[repoName].branches[branchName].files
const isDirectory = files.some((testFile) => {
return testFile.startsWith(filename + '/') && (testFile !== filename)
})
return isDirectory
})
eleventyConfig.addAsyncFilter("getFileContents", async (repo, branch, filename) => {
const location = getLocation(reposConfiguration, branch, repo)
let command = ''
const config = reposConfiguration.repos[repo]
if (config._type === "git") {
command = `git show ${branch}:${filename}`
}
else if (config._type === "darcs") {
command = `darcs show contents ${filename}`
}
const res = await exec(`(cd ${location} && ${command})`)
return res.stdout
})
eleventyConfig.addAsyncFilter("getReadMe", async (repoName, branchName) => {
const location = getLocation(reposConfiguration, branchName, repoName)
const config = reposConfiguration.repos[repoName]
let command = ''
if (config._type === "git") {
command = `git show ${branchName}:README.md`
}
else if (config._type === "darcs") {
command = `darcs show contents README.md`
}
try {
const res = await exec(`(cd ${location} && ${command})`)
return res.stdout
} catch {
return ""
}
})
eleventyConfig.addFilter("jsonStringify", data => JSON.stringify(data))
const topLayoutPartial = fsImport.readFileSync(`${__dirname}/partial_templates/main_top.njk`).toString()
const bottomLayoutPartial = fsImport.readFileSync(`${__dirname}/partial_templates/main_bottom.njk`).toString()
// INDEX.NJK
const indexTemplate = fsImport.readFileSync(`${__dirname}/templates/index.njk`).toString()
eleventyConfig.addTemplate(
'repos/index.njk',
topLayoutPartial + indexTemplate + bottomLayoutPartial,
{
permalink: "repos/index.html",
}
)
// BRANCHES.NJK
const branchesTemplate = fsImport.readFileSync(`${__dirname}/templates/branches.njk`).toString()
const branchesData = branches(reposData)
eleventyConfig.addTemplate(
'repos/branches.njk',
topLayoutPartial + branchesTemplate + bottomLayoutPartial,
{
pagination: {
data: "branches",
size: 1,
alias: "branchInfo",
},
branches: branchesData,
permalink: (data) => {
const repoName = data.branchInfo.repoName
const branchName = data.branchInfo.branchName
return `repos/${eleventyConfig.getFilter("slugify")(repoName)}/branches/${eleventyConfig.getFilter("slugify")(branchName)}/list/`
},
eleventyComputed: {
nav: {
repoName: (data) => data.branchInfo.repoName,
branchName: (data) => data.branchInfo.branchName,
}
},
navTab: "branches",
}
)
// FILE.NJK
const fileTemplate = fsImport.readFileSync(`${__dirname}/templates/file.njk`).toString()
const flatFilesData = flatFiles(reposData)
eleventyConfig.addTemplate(
'repos/file.njk',
topLayoutPartial + fileTemplate + bottomLayoutPartial,
{
pagination: {
data: "flatFiles",
size: 1,
alias: "fileInfo",
},
flatFiles: flatFilesData,
permalink: (data) => {
const repoName = data.fileInfo.repoName
const branchName = data.fileInfo.branchName
return `repos/${eleventyConfig.getFilter("slugify")(repoName)}/branches/${eleventyConfig.getFilter("slugify")(branchName)}/files/${eleventyConfig.getFilter("slugify")(data.fileInfo.file)}.html`
},
eleventyComputed: {
nav: {
repoName: (data) => data.fileInfo.repoName,
branchName: (data) => data.fileInfo.branchName,
}
},
navTab: "files",
}
)
// FILES.NJK
const filesTemplate = fsImport.readFileSync(`${__dirname}/templates/files.njk`).toString()
eleventyConfig.addTemplate(
'repos/files.njk',
topLayoutPartial + filesTemplate + bottomLayoutPartial,
{
pagination: {
data: "branches",
size: 1,
alias: "branchInfo",
},
branches: branchesData,
permalink: (data) => {
const repoName = data.branchInfo.repoName
const branchName = data.branchInfo.branchName
return `repos/${eleventyConfig.getFilter("slugify")(repoName)}/branches/${eleventyConfig.getFilter("slugify")(branchName)}/files/`
},
eleventyComputed: {
nav: {
repoName: (data) => data.branchInfo.repoName,
branchName: (data) => data.branchInfo.branchName,
}
},
navTab: "files",
}
)
// REPO.NJK
const repoTemplate = fsImport.readFileSync(`${__dirname}/templates/repo.njk`).toString()
eleventyConfig.addTemplate(
'repos/repo.njk',
topLayoutPartial + repoTemplate + bottomLayoutPartial,
{
pagination: {
data: "branches",
size: 1,
alias: "branch",
},
branches: branchesData,
permalink: (data) => {
const repoName = data.branch.repoName
const branchName = data.branch.branchName
return `repos/${eleventyConfig.getFilter("slugify")(repoName)}/branches/${eleventyConfig.getFilter("slugify")(branchName)}/`
},
eleventyComputed: {
nav: {
repoName: (data) => data.branch.repoName,
branchName: (data) => data.branch.branchName,
}
},
navTab: "landing",
}
)
eleventyConfig.addGlobalData("repos", reposData)
eleventyConfig.addGlobalData("reposConfig", reposConfiguration)
}
-1 0 1 2 3 4 5
#!/bin/bash
rm -R dist
mkdir dist
mkdir dist/templates
mkdir dist/partial_templates
cp templates/*.njk dist/templates
cp partial_templates/*.njk dist/partial_templates
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
"name": "eleventy-plugin",
"version": "1.0.0",
"main": "dist/main.js",
"scripts": {
"build": "./make.sh && npx tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@types/node": "^24.0.7",
"typescript": "^5.8.3"
},
"dependencies": {
"@11ty/eleventy": "^3.1.2",
"diff": "^8.0.2",
"lodash": "^4.17.21"
}
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
</div>
</div>
</div>
<script>
const toggleUnifiedMode = (e) => {
const diffs = document.getElementById('diffs')
const afterDiffs = document.querySelectorAll('.diff-right')
if (e.checked) {
diffs.classList.add("unified")
afterDiffs.forEach((elem) => {
elem.classList.remove('border-start', 'ps-2')
})
}
else {
diffs.classList.remove("unified")
afterDiffs.forEach((elem) => {
elem.classList.add('border-start', 'ps-2')
})
}
}
const selectBranch = (e) => {
const values = e.value.split(",")
window.location = `/repos/${values[0]}/branches/${values[1]}/${values[2]}`
}
</script>
<script src="/static/main.js"></script>
</body>
</html>
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
<!DOCTYPE html>
<html>
<head>
<title>{% if nav.title %}{{nav.title}}{% else %}Repositories{% endif %}</title>
<script>
window.jsVars = {};
{% if nav %}
window.jsVars['baseUrl'] = `{{ repoConfig.repos[nav.repoName].baseUrl | jsonStringify | safe }}`;
window.jsVars['nav'] = {{nav | jsonStringify | safe}};
window.jsVars['cloneDiv'] = `{%- if reposConfig.repos[nav.repoName]._type == "darcs" -%}{% set url = repos[nav.repoName].cloneUrl + (nav.branchName | slugify) %}
<label class="form-label">HTTPS URL</label>
<div class="input-group d-flex flex-nowrap">
<span class="clone overflow-hidden input-group-text">
{{ url }}
</span>
<button data-clone-url="{{url}}" class="btn btn-primary" id="clone-button">Copy</button>
</div>{%- elif reposConfig.repos[nav.repoName]._type == "git" -%}<label class="form-label">HTTPS URL</label>
<div class="input-group d-flex flex-nowrap">
<span class="clone overflow-hidden input-group-text">
{% set url = repos[nav.repoName].cloneUrl %}
{{ url }}
</span>
<button data-clone-url="{{url}}" class="btn btn-primary" id="clone-button">Copy</button>
</div>
{%- endif -%}`;
{% endif %}
</script>
<script src="/static/top.js"></script>
<link rel="stylesheet" id="prism-theme" type="text/css" href="/prism.css" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.5/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-SgOJa3DmI69IUzQ2PVdRZhwQ+dy64/BUtbMJw1MZ8t5HZApcHrRKUc4W0kG879m7" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.6/dist/js/bootstrap.bundle.min.js" integrity="sha384-j1CDi7MgGQ12Z7Qab0qlWQ/Qqz24Gc6BM0thvEMVjHnfYGF0rmFCozFSxQBxwHKO" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="/static/main.css" />
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container-lg">
<div class="row pt-5">
<div class="col">
<header>
<div class="row d-flex justify-content-between pb-3">
<div class="col-auto">
<nav class="fs-4">
<a href="/">Repositories</a>{% if nav.repoName %}<span class="text-secondary mx-2">></span><a href="/repos/{{nav.repoName | slugify}}/branches/{{reposConfig.repos[nav.repoName].defaultBranch | slugify}}">{{nav.repoName}}</a>{% endif %}
</nav>
</div>
<div class="col-auto d-flex align-items-center">
<div class="dropdown">
<button class="dropdown-toggle btn" id="dark-mode-switch" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-brightness-high"></i>
</button>
<ul class="dropdown-menu">
<li><button class="btn" data-theme-pref="light" onclick="toggleDarkMode(this)"><i class="bi bi-brightness-high mx-1"></i>Light</button></li>
<li><button class="btn" data-theme-pref="dark" onclick="toggleDarkMode(this)"><i class="bi bi-moon mx-1"></i>Dark</button></li>
<li><button class="btn" data-theme-pref="auto" onclick="toggleDarkMode(this)"><i class="bi bi-yin-yang mx-1"></i>Auto</button></li>
</ul>
</div>
</div>
</div>
{% if nav.repoName %}
<div class="row mb-4">
<div class="col-12 col-md order-2 order-md-1 pe-0">
<nav class="nav-tabs">
<ul class="nav">
<li class="nav-item">
<a class="nav-link {% if navTab == "landing" %}active{% endif %}" href="/repos/{{nav.repoName | slugify}}/branches/{{nav.branchName}}">Landing Page</a>
</li>
<li class="nav-item">
<a class="nav-link {% if navTab == "files" %}active{% endif %}" href="/repos/{{nav.repoName | slugify}}/branches/{{nav.branchName}}/files">Files</a>
</li>
<li class="nav-item">
<a class="nav-link {% if navTab == "patches" %}active{% endif %}" href="/repos/{{nav.repoName | slugify}}/branches/{{nav.branchName}}/patches/page1">Changes</a>
</li>
<li class="nav-item">
<a class="nav-link {% if navTab == "branches" %}active{% endif %}" href="/repos/{{nav.repoName | slugify}}/branches/{{nav.branchName | slugify}}/list">Branches</a>
</li>
</ul>
</nav>
</div>
<div class="col-12 col-md-auto order-1 order-md-2 pb-2 pb-md-0 mb-2 mb-md-0 border-bottom">
<div class="row">
<div class="col-auto px-2">
<button type="button" id="clone-popover-btn" class="btn btn-sm btn-primary" data-bs-toggle="popover" data-bs-placement="bottom">Clone <i class="bi bi-caret-down-fill"></i></button>
</div>
<div class="col-auto px-2">
<div class="input-group input-group-sm">
<span class="input-group-text">Branch</span>
<select class="form-select" onchange="selectBranch(this)" aria-label="Repository branch selector">
{% for branch in branches %}
{% if branch.repoName == nav.repoName %}
<option value="{{branch.repoName | slugify}},{{branch.branchName | slugify}},{{nav.path}}" {% if branch.branchName == nav.branchName %}selected{% endif %}>{{branch.branchName}}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
</div>
</div>
</div>
{% endif %}
</header>
</div>
</div>
</div>
<div class="container-{% if width == "full"%}fluid{% else %}lg{% endif %}">
<div class="row">
<div class="col">
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13
let cachedBranches = null
export default (repos) => {
if (cachedBranches !== null) { return cachedBranches }
cachedBranches = Object.keys(repos).flatMap((repoName) => {
return Object.keys(repos[repoName].branches).map((branchName) => {
return {
branchName,
repoName,
}
})
})
return cachedBranches
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/**
* The ReposConfiguration object contains information about your local repositories,
* like their name and location on your local filesystem. Add repositories to this
* configuration object to make a static site for them.
*
* This static site generator works with both git and darcs repositories. Because of the
* differences between these two version control systems, the configuration for them
* looks a little different. Both types of configuration objects can be nested underneath
* the {@link ReposConfiguration.repos} key.
*
* You will also need to set the {@link ReposConfiguration.baseUrl} to the URL of your
* live website.
* @example
* const config: ReposConfiguration = {
* repos: {
* "My Darcs Project": {
* _type: "darcs",
* baseUrl: "https://repos.tuckerm.us",
* defaultBranch: 'main',
* branches: {
* 'main': {
* location: "/home/alice/projects/my_darcs_project",
* description: "Main branch of this project."
* },
* 'drafts': {
* location: "/home/alice/projects/my_darcs_project_drafts/",
* description: "Some things that are a work in progress",
* },
* },
* languageExtensions: {
* "njk": "html",
* },
* },
* },
* }
*/
type ReposConfiguration = {
repos: {
/** An object containing the configuration for your repositories.
* Each key in this object is a repository name, and the value has several
* config options for that repository. The required config options describe
* the path to the repository and which branches should be pulled. See the specific
* definitions of {@link GitConfig} and {@link DarcsConfig} for more details
* about what goes in these configuration objects.
*/
[repoName: string]: GitConfig | DarcsConfig
},
/**
* The root URL where this website will be. E.g.: https://blog.example.com/repos.
* This URL will be used when a clone or pull command is being shown on your site.
* @example baseUrl: "https://repos.tuckerm.us"
*/
baseUrl: string,
}
type GitConfig = {
_type: "git",
/* The absolute path to the repository.
* @example location: "/home/alice/projects/git_repo"
*/
location: string,
description?: string,
defaultBranch: string,
branchesToPull: Array<string>,
languageExtensions?: {
[fileExtension: string]: string
}
}
/**
* A configuration object for a darcs repository.
*
* @example
* "My Darcs Project": {
* _type: "darcs",
* baseUrl: "https://repos.tuckerm.us",
* defaultBranch: 'main',
* branches: {
* 'main': {
* location: "/home/alice/projects/my_darcs_project",
* description: "Main branch of this project."
* },
* 'drafts': {
* location: "/home/alice/projects/my_darcs_project_drafts/",
* description: "Some things that are a work in progress",
* },
* },
* languageExtensions: {
* "njk": "html",
* },
* }
*/
type DarcsConfig = {
/** Must be set to `"darcs"`. */
_type: "darcs",
/**
* The name of the default branch. Should match one of the keys in {@link DarcsConfig.branches}.
* @example defaultBranch: "main"
* */
defaultBranch: string,
/** The branches of this repository to generate pages for. Since darcs doesn't have
* branches in the same way that other VCSs do, a "branch" in this case is an entirely
* separate repository. So, these "branches" are more like repos that are grouped under
* the same project name. That is why you need to specify a separate location for each
* branch.
* @example
* branches: {
* main: {
* location: "/home/alice/projects/my-project/main",
* description: "The main release branch of this project."
* },
* drafts: {
* location: "/home/alice/projects/my-project/drafts",
* description: "Undeployed works-in-progress for this project."
* }
* }
*/
branches: {
/**
* Each key in this object will be the name that is used for that branch.
*/
[branchName: string]: {
/**
* The absolute path to the repository for this branch.
* @example location: "/home/alice/projects/darcs_repo_main"
*/
location: string,
/**
* A description of this branch. You may want to clarify what this branch is used for here.
*/
description?: string,
}
},
/**
* If your repository has any uncommon file extensions that should be treated like a different
* type of file, list them here. If you include `{njk: "html"}` here, that will tell the
* syntax highlighter to highlight an `njk` file like an `html` file. The key is the file
* extension in your code, and the value is the file extension that the syntax highlighter
* will know about.
* @example
* languageExtensions: {
* njk: "html",
* jss: "js",
* madeupformat: "txt"
* }
*/
languageExtensions?: {
[fileExtension: string]: string
}
}
export {
ReposConfiguration,
GitConfig,
DarcsConfig,
}
-1 0 1
export type BranchInfo = {
files: Array<string>,
patches: Array<any>
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import repos from './repos.ts'
let cachedFlatFiles = null
export default (repos) => {
if (cachedFlatFiles !== null) { return cachedFlatFiles }
cachedFlatFiles = Object.keys(repos).flatMap((repoName) => {
return Object.keys(repos[repoName].branches).flatMap((branchName) => {
return repos[repoName].branches[branchName].files.map((file) => {
return {
file,
branchName,
repoName,
}
})
})
})
return cachedFlatFiles
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
import _ from 'lodash'
import * as Diff from 'diff'
type NavValues = {
repoName: string | ((data: any) => string),
branchName: string | ((data: any) => string),
path: string | ((data: any) => string),
title: string | ((data: any) => string),
}
type Hunk = {
file: string,
lineNumber: number,
previousText: string,
afterText: string,
}
type DiffInfo = {
file: string,
lineNumber: number,
previousText: string,
afterText: string,
}
const getGitDiffsFromPatchText = (patchText: string): Array<DiffInfo> => {
const lines = patchText.split("\n")
const hunks: Array<Hunk> = []
let previousHunk = -1
const filenameRegex = RegExp(/diff --git a\/(.*?) b\/(.*?)/)
const lineNumberRegex = RegExp(/@@ -(.*?)[,| ].*/)
let previousFilename = ''
let currentFilename = ''
let skipFourStartingAt = -1
lines.forEach((line, index) => {
if (line.startsWith("diff")) {
previousFilename = currentFilename
currentFilename = line.match(filenameRegex)[1]
if (previousHunk !== -1) {
skipFourStartingAt = index
}
}
if (line.startsWith("@@") || index == lines.length - 1) {
if (previousHunk === -1) {
previousHunk = index
return
}
let hunkEndIndex = index + 1
if (skipFourStartingAt !== -1 && skipFourStartingAt < hunkEndIndex) {
hunkEndIndex = skipFourStartingAt
skipFourStartingAt = -1
}
const lastHunk = lines.slice(previousHunk, hunkEndIndex)
let lastHunkBefore = lastHunk.filter(line => line.startsWith("-")).map(str => str.replace("-", "")).join("\n")
let lastHunkAfter = lastHunk.filter(line => line.startsWith("+")).map(str => str.replace("+", "")).join("\n")
lastHunkBefore = _.escape(lastHunkBefore)
lastHunkAfter = _.escape(lastHunkAfter)
const changeObject = Diff.diffWordsWithSpace(lastHunkBefore, lastHunkAfter)
let previousText = ""
let afterText = ""
changeObject.forEach((obj) => {
if (!obj.added && !obj.removed) {
previousText = previousText + obj.value
afterText = afterText + obj.value
}
if (obj.added) {
afterText = afterText + "<mark>" + obj.value + "</mark>"
}
if (obj.removed) {
previousText = previousText + "<mark>" + obj.value + "</mark>"
}
})
hunks.push({
file: previousFilename !== '' ? previousFilename : currentFilename,
lineNumber: parseInt(lines[previousHunk].match(lineNumberRegex)[1]),
previousText,
afterText,
})
previousFilename = ''
previousHunk = index
}
})
return hunks
}
/** @hidden */
const getDarcsDiffsFromPatchText = (patchText: string): Array<DiffInfo> => {
const lines = patchText.split("\n")
const hunks: Array<Hunk> = []
let previousHunk = -1
lines.forEach((line, index) => {
if (line.startsWith("hunk") || index === lines.length - 1) {
if (previousHunk === -1) {
previousHunk = index
return
}
// get diff from previous hunk to this next one
const lastHunk = lines.slice(previousHunk, index + 1) // slice is non-inclusive for the end argument
let lastHunkBefore = lastHunk.filter(line => line.startsWith("-")).map(str => str.replace("-", "")).join("\n")
let lastHunkAfter = lastHunk.filter(line => line.startsWith("+")).map(str => str.replace("+", "")).join("\n")
lastHunkBefore = _.escape(lastHunkBefore)
lastHunkAfter = _.escape(lastHunkAfter)
let filename = lines[previousHunk].replace("hunk ./", "")
const changeObject = Diff.diffWords(lastHunkBefore, lastHunkAfter)
let previousText = ""
let afterText = ""
changeObject.forEach((obj) => {
if (!obj.added && !obj.removed) {
previousText = previousText + obj.value
afterText = afterText + obj.value
}
if (obj.added) {
afterText = afterText + "<mark>" + obj.value + "</mark>"
}
if (obj.removed) {
previousText = previousText + "<mark>" + obj.value + "</mark>"
}
})
const regex = RegExp(/(.*) ([0-9]+)$/)
const matches = filename.match(regex)
const file = matches[1]
const lineNumber: number = parseInt(matches[2])
hunks.push({
file,
lineNumber,
previousText,
afterText,
})
previousHunk = index
}
})
return hunks
}
const getLocation = (reposConfig: any, branchName: string, repoName: string): string => {
const config = reposConfig.repos[repoName]
if (config._type === "darcs") {
return config.branches[branchName].location
}
else if (config._type === "git") {
return config.location
}
}
export {
NavValues,
getDarcsDiffsFromPatchText,
getGitDiffsFromPatchText,
getLocation,
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
import {
type DarcsConfig,
type GitConfig,
} from './configTypes.ts'
import {type BranchInfo} from './dataTypes.ts'
import repoOperations from './vcses/operations.ts'
import { getLocation} from './helpers.ts'
import repoHelpers from './vcses/helpers.ts'
type BranchInfoTuple = [string, BranchInfo]
type BranchObject = {
[key: string]: BranchInfo
}
type RepoObjectTuple = [string, BranchObject]
type RepoObject = {
[key: string]: {
branches: BranchObject,
cloneUrl: string,
}
}
const getBranchNames = (repoConfig: DarcsConfig | GitConfig): Array<string> => {
if (repoConfig._type === 'darcs') {
return Object.keys(repoConfig.branches)
}
else if (repoConfig._type === 'git') {
return repoConfig.branchesToPull
}
}
let cachedRepos = null
const repos: (reposConfig: any) => Promise<RepoObject> = async (reposConfig) => {
if (cachedRepos !== null) { return cachedRepos }
const repoNames = Object.keys(reposConfig.repos)
const reposTuples: RepoObjectTuple[] = await Promise.all(repoNames.map(async (repoName): Promise<RepoObjectTuple> => {
const vcs = reposConfig.repos[repoName]._type
const branchNames = getBranchNames(reposConfig.repos[repoName])
const branchTuples: BranchInfoTuple[] = await Promise.all(branchNames.map(async (branchName): Promise<BranchInfoTuple> => {
const repoLocation = getLocation(reposConfig, branchName, repoName)
const files = await repoOperations[vcs].getFileList(repoName, branchName, repoLocation)
const patches = await repoOperations[vcs].getBranchInfo(repoName, branchName, repoLocation)
return [branchName, {
files,
patches,
}]
}))
const branchesObject: BranchObject = {}
for (let branchTuple of branchTuples) {
branchesObject[branchTuple[0]] = branchTuple[1]
}
return [repoName, branchesObject]
}))
const reposObject: RepoObject = {}
for (let repoTuple of reposTuples) {
const repoName = repoTuple[0]
const repoType = reposConfig.repos[repoName]._type
reposObject[repoName] = {
branches: repoTuple[1],
cloneUrl: repoHelpers[repoType].cloneUrl(reposConfig.baseUrl, repoName)
}
}
cachedRepos = reposObject
return reposObject
}
export default repos
-1 0 1 2
export default {
cloneUrl: (baseUrl: string, repoName: string) => {
return `${baseUrl}/repos/${repoName.toLowerCase().replaceAll(" ", "-")}/branches/`
}
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
import util from 'util'
import childProcess from 'child_process'
const exec = util.promisify(childProcess.exec)
import {getDarcsDiffsFromPatchText} from '../../helpers.ts'
export const getFileList = async(repoName: string, branchName: string, repoLocation: string) => {
const command = "darcs show files"
const result = await exec(`(cd ${repoLocation} && ${command})`)
const files = result.stdout.split("\n").filter(item => item.length > 0 && item != ".")
return files
}
export const getBranchInfo = async (repoName: string, branchName: string, repoLocation: string) => {
const patches = new Map()
const totalPatchesCountRes = await exec(`(cd ${repoLocation} && darcs log --count)`)
const totalPatchesCount = parseInt(totalPatchesCountRes.stdout)
let hunkRegex = RegExp(/^ *hunk /)
// Get 100 patches at a time and parse those
for (let i = 1; i <= totalPatchesCount; i = i + 100) {
let patchesSubsetRes = await exec(`(cd ${repoLocation} && darcs log --index=${i}-${i+100} -v)`)
let patchesSubset = patchesSubsetRes.stdout.split("\n")
do {
const nextPatchStart = patchesSubset.findIndex((line, index) => {
return (index > 0 && line.startsWith("patch ")) || index === patchesSubset.length - 1
})
const isEndOfFile = nextPatchStart === patchesSubset.length - 1
const currentPatch = patchesSubset.slice(0, isEndOfFile ? nextPatchStart : nextPatchStart - 1)
patchesSubset = patchesSubset.slice(nextPatchStart)
const hash = currentPatch[0].replace("patch ", "").trim()
const author = currentPatch[1].replace("Author: ", "").trim()
const date = currentPatch[2].replace("Date: ", "").trim()
const name = currentPatch[3].replace(" * ", "").trim()
const diffStart = currentPatch.findIndex((line) => {
return line.match(hunkRegex)
})
const description = currentPatch.slice(5, diffStart).map(str => str.replace(" ", "")).join("\n").trim()
const diffs = getDarcsDiffsFromPatchText(currentPatch.slice(diffStart).map(str => str.trimStart()).join("\n"))
patches.set(hash, {
name,
description,
author,
date,
hash,
diffs,
})
} while (patchesSubset.length > 1)
}
return Array.from(patches.values())
}
export const getFileLastTouchInfo = async (repoName: string, branchName: string, filename: string, repoLocation: string) => {
const command = `darcs annotate --machine-readable ${filename}`
const res = await exec(`(cd ${repoLocation} && ${command})`)
const output = res.stdout
const outputLines = output.split("\n").map((line) => {
return line.split(' ')[0]
})
return outputLines.map((line) => {
return {sha: line, author: ''}
})
}
-1 0 1 2
export default {
cloneUrl: (baseUrl: string, repoName: string) => {
return `${baseUrl}/repos/${repoName.toLowerCase().replaceAll(" ", "-")}.git`
}
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
import util from 'util'
import childProcess from 'child_process'
const exec = util.promisify(childProcess.exec)
import { getGitDiffsFromPatchText} from '../../helpers.ts'
export const getFileList = async (repoName: string, branchName: string, repoLocation: string) => {
const command = `git ls-tree -r --name-only ${branchName}`
const result = await exec(`(cd ${repoLocation} && ${command})`)
let files = result.stdout.split("\n").filter(item => item.length > 0 && item != ".")
// TODO: this could be better. This is adding each sub-path of a file to a set, so that
// we don't wind up with repeats, and then converting that back into an array. E.g. if
// we have two files:
// - posts/blog/one.md
// - posts/blog/two.md
// this will add the following to the set:
// posts, posts/blog, posts/blog/one.md, posts, posts/blog, posts/blog/two.md
// The repeats will be omitted because it's a Set, and the resulting array will
// be [posts, posts/blog, posts/blog/one.md, posts/blog/two.md].
// This is because it's convenient to have the directories show up as their own "file"
// in the file list, even though git doesn't treat them that way.
const fileSet: Set<string> = new Set()
files.forEach((file) => {
const fileParts = file.split("/")
const allPathsInFile = fileParts.reduce((accumulator, currentValue, index) => {
// Skip the first iteration, we have already added it as the initialValue
if (index === 0) { return accumulator }
accumulator.push(accumulator[accumulator.length - 1] + '/' + currentValue)
return accumulator
}, [fileParts[0]])
allPathsInFile.forEach((path) => {
fileSet.add(path)
})
})
return Array.from(fileSet)
}
export const getBranchInfo = async (repoName: string, branchName: string, repoLocation: string) => {
const patches = new Map()
const totalPatchesCountRes = await exec(`(cd ${repoLocation} && git rev-list --count ${branchName})`)
const totalPatchesCount = parseInt(totalPatchesCountRes.stdout)
for (let i = 0; i < totalPatchesCount; i = i + 10) {
const gitLogSubsetRes = await exec(`(cd ${repoLocation} && git log ${branchName} -p -n 10 --skip ${i})`)
let gitLogSubset = gitLogSubsetRes.stdout.split("\n")
do {
const nextPatchStart = gitLogSubset.findIndex((line, index) => {
return (index > 0 && line.startsWith("commit ")) || index === gitLogSubset.length - 1
})
const isEndOfFile = nextPatchStart === gitLogSubset.length - 1
const currentPatch = gitLogSubset.slice(0, isEndOfFile ? nextPatchStart : nextPatchStart - 1)
gitLogSubset = gitLogSubset.slice(nextPatchStart)
const hash = currentPatch[0].replace("commit ", "").trim()
const author = currentPatch[1].replace("Author: ", "").trim()
const date = currentPatch[2].replace("Date: ", "").trim()
const diffStart = currentPatch.findIndex((line) => {
return line.startsWith("diff ")
})
// Git log is indent four spaces by default -- remove those.
const commitMessage = currentPatch.slice(4, diffStart).map(str => str.replace(" ", ""))
const name = commitMessage[0].trim()
const description = commitMessage.slice(1, commitMessage.length - 1).filter((line) => {
// git log --porcelain output adds these "Ignore-this:" lines and I'm not sure what they are
return !line.startsWith('Ignore-this: ')
}).join("\n").trim()
const diffs = getGitDiffsFromPatchText(currentPatch.slice(diffStart).join("\n"))
patches.set(hash, {
name,
description,
author,
date,
hash,
diffs,
})
} while (gitLogSubset.length > 1)
}
return Array.from(patches.values())
}
export const getFileLastTouchInfo = async (repo, branch, filename, repoLocation) => {
const regex = RegExp(".* [0-9]+ [0-9]+")
const command = `git blame --porcelain ${branch} ${filename}`
const res = await exec(`(cd ${repoLocation} && ${command})`)
const output = res.stdout
const outputLines = output.split("\n")
const initialValue: Array<Array<string>> = [[outputLines[0]]]
const chunked = outputLines.reduce((accumulator, currentLine, currentIndex) => {
// skip the first iteration since we already gave it initialValue
if (currentIndex == 0) { return accumulator }
if (currentLine.match(regex)) {
accumulator.push([currentLine])
return accumulator
}
else {
accumulator[accumulator.length - 1].push(currentLine)
return accumulator
}
}, initialValue)
let currentAuthor = ''
let authorsAndShasByLine = []
chunked.forEach((chunk) => {
let shaAndLineNumberParts = chunk[0].split(' ')
let line = parseInt(shaAndLineNumberParts[2])
let sha = shaAndLineNumberParts[0]
if (chunk[1] && chunk[1].startsWith('author ')) {
currentAuthor = chunk[1].replace('author ', '')
}
authorsAndShasByLine[line - 1] = {sha, author: currentAuthor}
})
return authorsAndShasByLine
}
-1 0 1 2 3 4 5
import gitHelpers from './git/helpers.ts'
import darcsHelpers from './darcs/helpers.ts'
export default {
git: gitHelpers,
darcs: darcsHelpers
}
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import {
getBranchInfo as getGitBranchInfo,
getFileList as getGitFileList,
getFileLastTouchInfo as getGitFileLastTouchInfo,
} from './git/operations.ts'
import {
getBranchInfo as getDarcsBranchInfo,
getFileList as getDarcsFileList,
getFileLastTouchInfo as getDarcsFileLastTouchInfo,
} from './darcs/operations.ts'
import {type BranchInfo} from '../dataTypes.ts'
type RepoOperationsType = {
[vcs: string]: {
getBranchInfo: (repoName: string, branchName: string, repoLocation: string) => Promise<BranchInfo['patches']>,
getFileList: (repoName: string, branchName: string, repoLocation: string) => Promise<BranchInfo['files']>,
getFileLastTouchInfo: (repoName: string, branchName: string, filename: string, repoLocation: string) => Promise<Array<{sha: string, author: string}>>,
}
}
const repoOperations: RepoOperationsType = {
git: {
getBranchInfo: getGitBranchInfo,
getFileList: getGitFileList,
getFileLastTouchInfo: getGitFileLastTouchInfo,
},
darcs: {
getBranchInfo: getDarcsBranchInfo,
getFileList: getDarcsFileList,
getFileLastTouchInfo: getDarcsFileLastTouchInfo,
}
}
export default repoOperations
-1 0 1 2 3 4 5
<ul>
{% for branch in branches %}
{% set description = reposConfig.repos[branch.repoName].branches[branch.branchName].description %}
{% if branch.repoName == branchInfo.repoName %}
<li><a href="/repos/{{branch.repoName | slugify}}/branches/{{branch.branchName | slugify}}">{{branch.branchName}}</a>{% if branch.branchName == branchInfo.branchName %} (current){% endif %}{% if description %} - {{ description }}{% endif %}</li>
{% endif %}
{% endfor %}
</ul>
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<h3>{{fileInfo.file | getFileName}}</h3>
{% if fileInfo.file | isDirectory(fileInfo.repoName, fileInfo.branchName) %}
<ul class="list-group">
{% set dirs = fileInfo.repoName | getDirectoryContents(fileInfo.branchName, fileInfo.file) | topLevelFilesOnly(fileInfo.file + '/') %}
{% for dir in dirs %}
<li class="list-group-item">
{% if dir.isDirectory %}
<i class="bi bi-folder-fill"></i>
{% else %}
<i class="bi bi-file-earmark"></i>
{% endif %}
<a href="/repos/{{fileInfo.repoName | slugify}}/branches/{{fileInfo.branchName | slugify}}/files/{{dir.fullPath | slugify}}.html">{{fileInfo.file | getRelativePath(dir.name)}}</a>
</li>
{% endfor %}
</ul>
{% else %}
<div class="row py-2">
<div class="col">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="showLastTouch">
<label class="form-check-label" for="showLastTouch">Show last line change</label>
</div>
</div>
</div>
<div class="row">
<div class="col-auto border-end">
{% set fileContents = fileInfo.repoName | getFileContents(fileInfo.branchName, fileInfo.file) %}
<code style="white-space: pre;"><pre class="language-text">
{%- for lineNumber in fileContents | lineNumbers -%}
{{ lineNumber }}
{% endfor -%}</pre></code>
</div>
<div id="annotations" class="col-auto d-none">
{% set annotations = fileInfo.repoName | getFileLastTouchInfo(fileInfo.branchName, fileInfo.file) %}
<code style="white-space: pre;"><pre class="language-text">
{%- for annotation in annotations -%}
<a href="/repos/{{fileInfo.repoName | slugify}}/branches/{{fileInfo.branchName | slugify}}/patches/{{annotation.sha}}">{{ (annotation.sha | truncate(6, true, '')) }}</a> {{ annotation.author }}
{% endfor -%}
</pre></code>
</div>
<div class="col overflow-scroll">
<code>
{{- fileContents | highlightCode((fileInfo.file | languageExtension(fileInfo.repoName))) | safe }}
</code>
</div>
</div>
{% endif %}
<script type="text/javascript">
const toggleLastTouch = (event) => {
const isOn = event.target.checked
const annotations = document.getElementById("annotations")
if (isOn) {
annotations.classList.remove("d-none")
} else {
annotations.classList.add("d-none")
}
}
document.getElementById("showLastTouch")?.addEventListener('click', toggleLastTouch)
</script>
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
<div class="row">
<div class="col">
<ul class="list-group">
{% set files = repos[branchInfo.repoName].branches[branchInfo.branchName].files | topLevelFilesOnly('') %}
{% for file in files %}
<li class="list-group-item">
{% if file.isDirectory %}
<i class="bi bi-folder-fill"></i>
{% else %}
<i class="bi bi-file-earmark"></i>
{% endif %}
<a href="/repos/{{branchInfo.repoName | slugify}}/branches/{{branchInfo.branchName | slugify}}/files/{{file.fullPath | slugify}}.html">{{file.name}}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
-1 0 1 2
<ul>
{% for repoName, options in repos %}
<li><a href="/repos/{{repoName | slugify}}/branches/{{reposConfig.repos[repoName].defaultBranch}}">{{repoName}}</a></li>
{% endfor %}
</ul>
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<div class="row">
<div class="col-md-8 col-sm-12 order-md-1 order-sm-2">
{{ branch.repoName | getReadMe(branch.branchName) | renderContent("md") | safe }}
</div>
<div class="col-md-4 col-sm-12 order-md-2 order-sm-1">
<div class="row">
<div class="col">
<div class="row align-items-center">
<div class="col-auto">
<h2 class="fs-6 my-0">Recent patches in {{branch.branchName}}</h2>
</div>
<div class="col-auto">
<a href="/repos/{{ branch.repoName | slugify }}/branches/{{ branch.branchName | slugify }}/patches.xml" class="initialism">RSS<i class="bi bi-rss-fill ms-2" style="color: orange;"></i></a>
</div>
</div>
{% for patch in repos[branch.repoName].branches[branch.branchName].patches | batch(3) | first %}
<div class="card mt-2 mb-4">
<div class="card-body">
<a href="/repos/{{branch.repoName | slugify}}/branches/{{branch.branchName | slugify}}/patches/{{patch.hash}}" class="text-primary d-inline-block card-title fs-5">{{patch.name}}</a>
<p class="card-subtitle fs-6 mb-2 text-body-secondary">{{patch.date}}</p>
<p class="card-subtitle fs-6 mb-2 text-body-secondary">{{patch.author}}</p>
<p class="card-text">{{patch.description | truncate(150)}}</p>
</div>
<div class="card-footer">
{% if reposConfig.repos[branch.repoName]._type == "darcs" %}
<button data-hash="{{patch.hash}}" data-vcs="darcs" class="copy-btn btn btn-sm btn-outline-primary ms-2">
<i class="bi-copy bi me-1"></i>darcs pull {{patch.hash | truncate(6, true, "")}}
</button>
{% elif reposConfig.repos[branch.repoName]._type == "git" %}
<button data-hash="{{patch.hash}}" data-vcs="git" class="copy-btn btn btn-sm btn-outline-primary">
{{patch.hash | truncate(8, true, "")}} <i class="bi-copy bi me-1"></i>
</button>
{% endif %}
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</div>
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"rewriteRelativeImportExtensions": true,
"noImplicitAny": false,
"module": "nodenext",
"target": "es2017",
"lib": ["es2023", "dom"],
"allowJs": true,
"outDir": "dist"
},
"include": ["**/*.ts"],
"exclude": [
"dist/**/*",
"ts/frontend/**/*",
],
}