xonecas.io
8356
Wx

www-xonecas-io / commit c545336

site remote new hosts a repo you git push to over SSH; each push rebuilds the site (1042)

Co-Authored-By: Claude Opus 5.5

Sean Caetano Martin2026-11-08 c545336f9535d4cb670e5d3e0a2ab7d49162834b parent c96c23d

10 files changed

cmd/site/main.go+1−1
@@ -1,7 +1,7 @@
11 // Command site builds xonecas.io from bare git repos and serves it locally.
22 //
33 // site import [-sources sources.txt] [-store repos] [-jobs 8] [-meta data/meta.json]
4-// site remote [-sources sources.txt] [-store repos] [-meta data/meta.json] list | add <url or path> | rm <url or path>
4+// site remote [-sources sources.txt] [-store repos] [-meta data/meta.json] list | new <name> | add <url or path> | rm <url or path>
55 // site index [-sources sources.txt] [-store repos] [-overrides overrides.txt] [-meta data/meta.json] [-out data/projects.json]
66 // site build [-data data/projects.json] [-out public] [-tailwind bin/tailwindcss]
77 // site serve [-dir public] [-addr :8080]
cmd/site/remote.go+55−7
@@ -5,13 +5,18 @@ import (
55 "fmt"
66 "log"
77 "os"
8+ "path/filepath"
9+ "regexp"
10+ "slices"
11+ "strings"
812
13+ "xonecas.io/site/internal/site"
914 "xonecas.io/site/internal/store"
1015 )
1116
12-// remote lists the remotes in the sources file, or adds or removes one.
13-// add mirrors the new remote and refreshes the hosts' metadata, as import does; rm deletes its mirror.
14-// Either way, site index and site build then show the change.
17+// remote lists the remotes in the sources file, or adds, removes or creates one.
18+// add mirrors a remote and refreshes the hosts' metadata, as import does. new makes a repo the site
19+// hosts, to push to over SSH; each push rebuilds the site. rm deletes a mirror, but keeps a hosted repo.
1520 func remote(args []string, sources, root, meta string) error {
1621 urls, err := store.Sources(sources)
1722 if err != nil {
@@ -23,30 +28,73 @@ func remote(args []string, sources, root, meta string) error {
2328 }
2429 return nil
2530 }
26- if len(args) != 2 || (args[0] != "add" && args[0] != "rm") {
27- return errors.New("usage: site remote list | add <url or path> | rm <url or path>")
31+ if len(args) != 2 || !slices.Contains([]string{"add", "rm", "new"}, args[0]) {
32+ return errors.New("usage: site remote list | new <name> | add <url or path> | rm <url or path>")
33+ }
34+ if args[0] == "new" {
35+ return newRepo(args[1], urls, sources, root, meta)
2836 }
2937 url, err := store.Clean(args[1])
3038 if err != nil {
3139 return err
3240 }
41+ dir := store.Dir(root, url)
3342 if args[0] == "rm" {
3443 if err := store.RemoveSource(sources, url); err != nil {
3544 return err
3645 }
46+ if store.Hosted(dir) {
47+ log.Printf("removed %s; its repo stays at %s", url, dir)
48+ return nil
49+ }
3750 log.Printf("removed %s; site index and site build drop it", url)
38- return os.RemoveAll(store.Dir(root, url))
51+ return os.RemoveAll(dir)
3952 }
4053 if err := store.AddSource(sources, url); err != nil {
4154 return err
4255 }
4356 if err := importAll(root, []string{url}, append(urls, url), 1, meta); err != nil {
44- return errors.Join(err, store.RemoveSource(sources, url), os.RemoveAll(store.Dir(root, url)))
57+ return errors.Join(err, store.RemoveSource(sources, url), os.RemoveAll(dir))
4558 }
4659 log.Printf("added %s; site index and site build list it", url)
4760 return nil
4861 }
4962
63+// newRepo creates the hosted repo name in the store, lists it, and prints where to push.
64+// Its hook rebuilds the site in the background, one build at a time, so a push returns at once.
65+func newRepo(name string, urls []string, sources, root, meta string) error {
66+ if !regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`).MatchString(name) || strings.HasSuffix(name, ".git") {
67+ return fmt.Errorf("%q is not a repo name: lower case letters, digits, . _ and -", name)
68+ }
69+ for _, u := range urls {
70+ if strings.TrimSuffix(filepath.Base(u), ".git") == name {
71+ return fmt.Errorf("%s already names a project", u)
72+ }
73+ }
74+ url := site.BaseURL + "/" + name + ".git"
75+ dir, err := filepath.Abs(store.Dir(root, url))
76+ if err != nil {
77+ return err
78+ }
79+ exe, err := os.Executable()
80+ if err != nil {
81+ return err
82+ }
83+ wd, _ := os.Getwd()
84+ lock, _ := filepath.Abs(filepath.Join(root, ".build.lock"))
85+ build := fmt.Sprintf("%s index -sources %s -store %s -meta %s && %[1]s build", exe, sources, root, meta)
86+ hook := fmt.Sprintf("unset GIT_DIR\ncd '%s' && flock '%s' sh -c '%s' < /dev/null >> '%s' 2>&1 &", wd, lock, build, filepath.Join(dir, "build.log"))
87+ if err := store.Create(dir, hook); err != nil {
88+ return err
89+ }
90+ if err := store.AddSource(sources, url); err != nil {
91+ return errors.Join(err, os.RemoveAll(dir))
92+ }
93+ host, _ := os.Hostname()
94+ fmt.Printf("git remote add origin %s@%s:%s\ngit push -u origin main\n", os.Getenv("USER"), host, dir)
95+ return nil
96+}
97+
5098 // importAll mirrors the remotes in sync, then saves the hosts' metadata for all of them to meta.
5199 func importAll(root string, sync, all []string, jobs int, meta string) error {
52100 if failed := mirrorAll(root, sync, jobs); failed > 0 {
internal/site/build.go+2−2
@@ -36,8 +36,8 @@ var funcs = template.FuncMap{
3636 "inc": func(i int) int { return i + 1 },
3737 "maxDiff": func() int { return MaxDiffLines },
3838 "dec": func(i int) int { return i - 1 },
39- "web": func(url string) string { // "" for a local repo, which has no other home
40- if !strings.HasPrefix(url, "https://") {
39+ "web": func(url string) string { // "" for a repo with no other home: hosted here, or local
40+ if !strings.HasPrefix(url, "https://") || strings.HasPrefix(url, BaseURL+"/") {
4141 return ""
4242 }
4343 return strings.TrimSuffix(strings.TrimPrefix(url, "https://"), ".git")
internal/store/hosted.go+20−0
@@ -0,0 +1,20 @@
1+package store
2+
3+import (
4+ "os"
5+ "path/filepath"
6+)
7+
8+// Hosted reports whether dir is a repo the site hosts: pushed to over SSH, with no origin to mirror.
9+func Hosted(dir string) bool {
10+ _, err := git("--git-dir", dir, "config", "remote.origin.url")
11+ return err != nil
12+}
13+
14+// Create makes a bare repo at dir to push to, on branch main, whose post-receive hook runs the shell line hook.
15+func Create(dir, hook string) error {
16+ if _, err := git("init", "--bare", "--quiet", "--initial-branch=main", dir); err != nil {
17+ return err
18+ }
19+ return os.WriteFile(filepath.Join(dir, "hooks", "post-receive"), []byte("#!/bin/sh\n"+hook+"\n"), 0o755)
20+}
internal/store/sources.go+1−1
@@ -31,7 +31,7 @@ func Sources(path string) ([]string, error) {
3131 func Clean(src string) (string, error) {
3232 if rest, ok := strings.CutPrefix(src, "https://"); ok {
3333 host, path, _ := strings.Cut(strings.TrimSuffix(strings.TrimSuffix(rest, "/"), ".git"), "/")
34- if host == "" || !strings.Contains(path, "/") {
34+ if host == "" || path == "" {
3535 return "", fmt.Errorf("%s is not a clone URL like https://host/owner/name.git", src)
3636 }
3737 return "https://" + host + "/" + path + ".git", nil
internal/store/store.go+4−1
@@ -20,7 +20,7 @@ func Dir(root, url string) string {
2020 }
2121
2222 // Sync creates url's mirror under root if it is missing, then fetches into it.
23-// It returns "new", "updated" or "same".
23+// It returns "new", "updated", "same", or "hosted" for a repo the site hosts.
2424 func Sync(root, url string) (string, error) {
2525 dir := Dir(root, url)
2626 state := "updated"
@@ -37,6 +37,9 @@ func Sync(root, url string) (string, error) {
3737 }
3838 }
3939 }
40+ if state != "new" && Hosted(dir) {
41+ return "hosted", nil // pushes fill it, so there is nothing to fetch
42+ }
4043 before, _ := git("--git-dir", dir, "for-each-ref")
4144 if _, err := git("--git-dir", dir, "fetch", "--prune", "--quiet", "origin"); err != nil {
4245 return "", err
internal/store/store_test.go+12−1
@@ -60,7 +60,7 @@ func TestEditSources(t *testing.T) {
6060 for in, want := range map[string]string{
6161 "https://github.com/x/a": "https://github.com/x/a.git", "https://gitlab.com/g/sub/b.git/": "https://gitlab.com/g/sub/b.git",
6262 "https://codeberg.org/x/c.git": "https://codeberg.org/x/c.git", local + "/": local,
63- "git@github.com:x/a.git": "", "https://github.com/x": "", t.TempDir(): "",
63+ "https://xonecas.io/d": "https://xonecas.io/d.git", "git@github.com:x/a.git": "", "https://github.com/": "", t.TempDir(): "",
6464 } {
6565 if got, _ := Clean(in); got != want {
6666 t.Errorf("Clean(%q) = %q, want %q", in, got, want)
@@ -78,3 +78,14 @@ func TestEditSources(t *testing.T) {
7878 t.Errorf("sources file is %q", b)
7979 }
8080 }
81+
82+func TestHosted(t *testing.T) {
83+ root := t.TempDir()
84+ url := "https://xonecas.io/demo.git"
85+ if err := Create(Dir(root, url), "true"); err != nil {
86+ t.Fatal(err)
87+ }
88+ if state, err := Sync(root, url); state != "hosted" || err != nil {
89+ t.Errorf("Sync of a hosted repo = %q, %v", state, err)
90+ }
91+}
sources.txt+1−1
@@ -1,4 +1,4 @@
1-# Every repo the site imports, one per line: a public https clone URL, or the path of a local repo that lives only here. Codeberg is left out: it holds only copies. Edit with bin/site remote add|rm.
1+# Every repo the site imports, one per line: a public https clone URL, the path of a local repo, or https://xonecas.io/<name>.git for a repo hosted here (pushed to over SSH). Codeberg is left out: it holds only copies. Edit with bin/site remote new|add|rm.
22 https://github.com/sacenox/alfred.git
33 https://github.com/sacenox/awesome-ai-tools.git
44 https://github.com/sacenox/awesome-cli-apps.git
tickets/1042-how-do-we-add-new-remotes.md+16−1
@@ -5,7 +5,7 @@ type: feature
55 status: review
66 priority: p2
77 created: 2026-11-08T06:26:18Z
8-updated: 2026-11-08T07:04:04Z
8+updated: 2026-11-08T07:08:43Z
99 source: human
1010 moves: 2026-11-08T06:26:22Z inbox doing drag; 2026-11-08T06:27:05Z doing review agent; 2026-11-08T06:57:08Z review doing drag; 2026-11-08T07:00:33Z doing review agent; 2026-11-08T07:01:42Z review todo note; 2026-11-08T07:01:44Z todo doing drag; 2026-11-08T07:04:04Z doing review agent
1111 area: import
@@ -52,3 +52,18 @@ Can we make this more formal, like a cli with the server bin? We could build on
5252 Checked: go vet and go test (Clean cases for a local repo, codeberg, nested gitlab, ssh, a non-repo dir). End to end on scratch copies: remote add . mirrored this repo by path, index made it a shown project (45 commits; no README, so no description), build rendered its page with 'Hosted here', and rm removed the line and the mirror. A failed add on another host rolled back.
5353 Not done: this repo isn't added to the real sources.txt. That is 1041, still in inbox with its question about tickets/ being public.
5454 Found, filed and not fixed: every published <name>.git/config holds the build machine's mirror path (/home/sacenox/...), for GitHub projects too; see the new inbox bug.
55+
56+- 2026-11-08 07:08Z (agent): Done: you can make a repo on the site and git push to it over SSH. Each push rebuilds the site.
57+ make site && bin/site remote new my-thing (run in the site folder)
58+ prints: git remote add origin sacenox@vm:/home/sacenox/repos/www-xonecas-io/repos/xonecas.io/my-thing.git
59+ git push -u origin main
60+ How it works:
61+ - new creates a bare repo in the store at repos/xonecas.io/<name>.git and lists it in sources.txt as https://xonecas.io/<name>.git, its public clone URL. Import marks it 'hosted' and never fetches it, because pushes fill it.
62+ - Its post-receive hook runs 'site index && site build' in the background, under flock repos/.build.lock so two pushes never build at once, and logs to <repo>/build.log. The push returns straight away.
63+ - Pushing over SSH needs only sshd and git on the machine, which it already has. There is no extra server.
64+ - An empty repo (before its first push) is hidden, as before. The project page shows 'Hosted here', and xonecas.io/<name>.git clones it.
65+ - rm of a hosted repo drops its line but keeps the repo, because it is the only copy. new refuses a bad name or one that already names a project.
66+ Checked: go vet and go test (new TestHosted, plus a Clean case). In a scratch site folder: remote new demo, two pushes to it, then the hook's build log shows index '1 shown' and 'built'. The page has the README and 'Hosted here', and cloning public/demo.git gives both commits. rm kept the repo.
67+ Not checked: the push over real SSH. Adding a test key to ~/.ssh/authorized_keys was blocked, so I pushed by path, which runs the same receive-pack and hook. Over SSH, only the transport differs.
68+ Important: repos/ is git-ignored, and this VM gets recreated. A hosted repo on the VM lives only there, so it would be lost on reset. Run new on the machine that serves xonecas.io, and back up repos/xonecas.io/.
69+ 1043 (mirror path in published .git/config) also applies to hosted repos.
tickets/1043-published-clones-expose-the-build-machine-s.md+5−1
@@ -6,8 +6,12 @@ status: inbox
66 priority: p2
77 area: site
88 created: 2026-11-08T07:04:04Z
9-updated: 2026-11-08T07:04:04Z
9+updated: 2026-11-08T07:08:43Z
1010 source: agent
1111 ---
1212
1313 Seen while testing 1042: writeClone (internal/site/clone.go) runs git clone --bare from the mirror, so every public/<name>.git/config keeps [remote "origin"] url = <absolute path of the mirror under repos/>, e.g. /home/sacenox/repos/www-xonecas-io/repos/github.com/... Anyone can fetch https://xonecas.io/<name>.git/config and read it. Seen in a scratch build. I did not check the live site or the other files in the clone (packed-refs, FETCH_HEAD).
14+
15+## Notes
16+
17+- 2026-11-08 07:08Z (agent): Triage: I filed this while working 1042. bug, p2, area site: a path leak in public files, with no breakage. It also affects repos hosted with remote new (1042).