Phase 5 part 1: read a site's section types and pages

Sites → Sections and pages reads the website's Hugo repository: each
section type with the README's description, the fields in use and the
layout's, the pages using it and a real example; the pages and their
sections; the latest commits. Working copies are now held by one job at a
time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 13:52:35 -07:00
co-authored by Claude Opus 5.5
parent 00e6deee00
commit 1152cd448b
15 changed files with 958 additions and 11 deletions
+8 -2
View File
@@ -9,8 +9,9 @@ import (
)
// Site makes a small Hugo site in a bare repository and returns its
// clone URL. Its titles are "Page title · Site".
func Site(t testing.TB) string {
// clone URL. Its titles are "Page title · Site". Extra files are added to
// (or replace) the site's own.
func Site(t testing.TB, extra ...map[string]string) string {
t.Helper()
dir := t.TempDir()
src := filepath.Join(dir, "src")
@@ -24,6 +25,11 @@ func Site(t testing.TB) string {
"content/blog/first-post/index.md": "+++\ntitle = \"First post\"\n+++\nHello.\n",
"content/about.md": "---\ntitle: About us\nurl: /company/\n---\nWho we are.\n",
}
for _, e := range extra {
for name, body := range e {
files[name] = body
}
}
for name, body := range files {
p := filepath.Join(src, name)
os.MkdirAll(filepath.Dir(p), 0o755)
+64
View File
@@ -17,6 +17,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/net/html"
@@ -31,6 +32,58 @@ type Repo struct {
Author string // "Name <email>" for commits
touched []string // files written outside content/, to commit
release func()
}
var (
locksMu sync.Mutex
locks = map[string]*sync.Mutex{}
)
// Acquire takes the working copy for one job: syncing resets it, so two
// jobs mustn't share it at once. Release gives it back.
func (r *Repo) Acquire() {
locksMu.Lock()
mu := locks[r.Dir]
if mu == nil {
mu = &sync.Mutex{}
locks[r.Dir] = mu
}
locksMu.Unlock()
mu.Lock()
r.release = mu.Unlock
}
// Release gives the working copy back; it's safe to call when not held.
func (r *Repo) Release() {
if r.release != nil {
r.release()
r.release = nil
}
}
// Commit is one commit on the published branch.
type Commit struct {
Hash, Author, Subject string
At time.Time
}
// Log is the latest commits on the published branch, newest first.
func (r *Repo) Log(ctx context.Context, n int) ([]Commit, error) {
out, err := r.git(ctx, "log", "-n", strconv.Itoa(n), "--format=%H%x1f%an%x1f%at%x1f%s", "origin/"+r.Branch)
if err != nil {
return nil, err
}
var cs []Commit
for _, l := range strings.Split(strings.TrimSpace(out), "\n") {
f := strings.Split(l, "\x1f")
if len(f) != 4 {
continue
}
sec, _ := strconv.ParseInt(f[2], 10, 64)
cs = append(cs, Commit{Hash: f[0], Author: f[1], At: time.Unix(sec, 0), Subject: f[3]})
}
return cs, nil
}
// git runs a git command in the working copy, with the token as an HTTP
@@ -167,6 +220,17 @@ func cleanPath(p string) string {
return p
}
// PathOf is the path a content file (relative to content/) is published
// at: its front matter's url, else its slug, else where it sits.
func (r *Repo) PathOf(rel string) string {
us := urlsFor(rel, filepath.Join(r.Dir, "content", rel))
fm := frontMatter(filepath.Join(r.Dir, "content", rel))
if fm["url"] != "" || fm["slug"] != "" {
return us[len(us)-1]
}
return us[0]
}
// urlsFor are the paths a content file may be published at: from where it
// sits (with and without a leading language folder), and from its front
// matter's url or slug.