8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / store / hosted.go
893 bytes
1package store 2 3import ( 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. 9func Hosted(dir string) bool { 10 _, err := git("--git-dir", dir, "config", "remote.origin.url") 11 return err != nil 12} 13 14// headHook points HEAD at the first branch pushed while HEAD's branch (main) has no commits. 15const headHook = `git rev-parse -q --verify HEAD >/dev/null || { read -r _ _ ref; case $ref in refs/heads/*) git symbolic-ref HEAD "$ref";; esac; }` 16 17// Create makes a bare repo at dir to push to, on branch main, whose post-receive hook runs the shell line hook. 18func Create(dir, hook string) error { 19 if _, err := git("init", "--bare", "--quiet", "--initial-branch=main", dir); err != nil { 20 return err 21 } 22 return os.WriteFile(filepath.Join(dir, "hooks", "post-receive"), []byte("#!/bin/sh\n"+headHook+"\n"+hook+"\n"), 0o755) 23}