@@ -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 {