xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / server / clone.go

1.1 KB
1package server
2
3import (
4	"net/http"
5	"net/http/cgi"
6	"os/exec"
7	"path/filepath"
8	"strings"
9)
10
11// clone serves `git clone <site>/<name>.git` from the project's repo, with git's smart HTTP protocol.
12// git http-backend serves only fetches: it refuses a push from a visitor with no login, and so does clone.
13// It also serves none of the repo's own files, such as its config.
14func (s *Server) clone(w http.ResponseWriter, r *http.Request, name string) {
15	st, err := s.state()
16	if err != nil {
17		http.Error(w, err.Error(), http.StatusInternalServerError)
18		return
19	}
20	i, err := st.project(name)
21	if err != nil || strings.Contains(r.URL.RawQuery+r.URL.Path, "receive-pack") {
22		http.NotFound(w, r)
23		return
24	}
25	dir, err := filepath.Abs(st.shown[i].Sources[0].Dir)
26	git, lerr := exec.LookPath("git")
27	if err != nil || lerr != nil {
28		http.Error(w, "git is missing", http.StatusInternalServerError)
29		return
30	}
31	h := &cgi.Handler{Path: git, Args: []string{"http-backend"}, Root: "/" + name + ".git",
32		Env: []string{"GIT_PROJECT_ROOT=" + dir, "GIT_HTTP_EXPORT_ALL=1"}}
33	h.ServeHTTP(w, r)
34}