@@ -6,6 +6,7 @@ import (
66 "log"
77 "os"
88 "os/exec"
9+ "path/filepath"
910 "slices"
1011 "strings"
1112 "syscall"
@@ -13,13 +14,22 @@ import (
1314 "xonecas.io/site/internal/store"
1415 )
1516
16-// shell runs one SSH command for the site, as the git user's login shell (site -c <command>).
17-// git push to <name>.git creates the repo on its first push, git clone and fetch read it,
18-// and remote manages the repos, as site remote does. Nothing else runs, so the user gets no shell.
19-func shell(cmd, sources, root, meta string) error {
17+// shell runs one SSH command that the login shell from make login-shell hands the site (site -c <command>).
18+// git push to <name>.git creates the repo on its first push, git clone and fetch read it, and remote
19+// manages the repos, as site remote does. A path with a / or one that exists in the user's home isn't
20+// the site's, so git gets it as sshd would have.
21+func shell(cmd string) error {
2022 args := strings.Fields(cmd)
2123 if len(args) == 2 && (args[0] == "git-receive-pack" || args[0] == "git-upload-pack") {
22- name := strings.TrimSuffix(strings.TrimLeft(strings.Trim(args[1], "'"), "/~"), ".git")
24+ path := strings.Trim(args[1], "'")
25+ if rel := strings.TrimPrefix(path, "~/"); strings.Contains(rel, "/") || exists(rel) {
26+ return execGit(args[0], path)
27+ }
28+ if err := chdirSite(); err != nil {
29+ return err
30+ }
31+ const sources, root, meta = "sources.txt", "repos", "data/meta.json"
32+ name := strings.TrimSuffix(strings.TrimPrefix(path, "~/"), ".git")
2333 url := hostedURL(name)
2434 urls, err := store.Sources(sources)
2535 if err != nil {
@@ -32,16 +42,35 @@ func shell(cmd, sources, root, meta string) error {
3242 if err := newRepo(name, urls, sources, root, meta); err != nil {
3343 return err
3444 }
35- log.Printf("created %s; each push rebuilds the site", url)
45+ log.Printf("created %s; each push updates the site", url)
3646 }
37- git, err := exec.LookPath("git")
38- if err != nil {
47+ return execGit(args[0], store.Dir(root, url))
48+ }
49+ if len(args) > 0 && args[0] == "remote" {
50+ if err := chdirSite(); err != nil {
3951 return err
4052 }
41- return syscall.Exec(git, []string{"git", strings.TrimPrefix(args[0], "git-"), store.Dir(root, url)}, os.Environ())
53+ return remote(args[1:], "sources.txt", "repos", "data/meta.json")
4254 }
43- if len(args) > 0 && args[0] == "remote" {
44- return remote(args[1:], sources, root, meta)
55+ return errors.New("usage: git push <user>@<host>:<name>.git, git clone it, or ssh <user>@<host> remote list | new <name> | add <path or url> | rm <path or url>")
56+}
57+
58+func exists(path string) bool { _, err := os.Stat(path); return err == nil }
59+
60+// chdirSite moves to the site folder, bin/site's parent.
61+func chdirSite() error {
62+ exe, err := os.Executable()
63+ if err != nil {
64+ return err
65+ }
66+ return os.Chdir(filepath.Dir(filepath.Dir(exe)))
67+}
68+
69+// execGit replaces site with git-receive-pack or git-upload-pack on dir.
70+func execGit(verb, dir string) error {
71+ git, err := exec.LookPath("git")
72+ if err != nil {
73+ return err
4574 }
46- return errors.New("usage: git push git@<host>:<name>.git, git clone it, or ssh git@<host> remote list | new <name> | add <path or url> | rm <path or url>")
75+ return syscall.Exec(git, []string{"git", strings.TrimPrefix(verb, "git-"), dir}, os.Environ())
4776 }