ikurotime / gitgud

public
main / internal/infra/git/http_backend.go
671 B · Go Raw
 1package git
 2
 3import (
 4	"net/http"
 5	"net/http/cgi"
 6	"os/exec"
 7	"path/filepath"
 8	"strings"
 9)
10
11type Backend struct {
12	reposDir    string
13	httpBackend string
14}
15
16func NewBackend(reposDir string) (*Backend, error) {
17	out, err := exec.Command("git", "--exec-path").Output()
18	if err != nil {
19		return nil, err
20	}
21	bin := filepath.Join(strings.TrimSpace(string(out)), "git-http-backend")
22	return &Backend{reposDir: reposDir, httpBackend: bin}, nil
23}
24
25func (b *Backend) Handler(remoteUser string) http.Handler {
26	return &cgi.Handler{
27		Path: b.httpBackend,
28		Env: []string{
29			"GIT_PROJECT_ROOT=" + b.reposDir,
30			"GIT_HTTP_EXPORT_ALL=1",
31			"REMOTE_USER=" + remoteUser,
32		},
33	}
34}