summaryrefslogtreecommitdiffstats
path: root/routes/git.go
blob: 5cde6f189b0b7acb7eb951aa5097f5eac6b25c26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package routes

import (
	"errors"
	"log"
	"net/http"
	"path/filepath"

	"github.com/alexedwards/flow"
	"github.com/go-git/go-billy/v5/osfs"
	"github.com/go-git/go-git/v5/plumbing/format/pktline"
	"github.com/go-git/go-git/v5/plumbing/protocol/packp"
	"github.com/go-git/go-git/v5/plumbing/transport"
	"github.com/go-git/go-git/v5/plumbing/transport/server"
)

func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	name = filepath.Clean(name)

	repo := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		repo = filepath.Join(repo, category)
	}
	repo = filepath.Join(repo, name)

	w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")

	ep, err := transport.NewEndpoint("/")
	if err != nil {
		http.Error(w, err.Error(), 500)
		log.Printf("git: %s", err)
		return
	}

	billyfs := osfs.New(repo)
	loader := server.NewFilesystemLoader(billyfs)
	srv := server.NewServer(loader)
	session, err := srv.NewUploadPackSession(ep, nil)
	if err != nil {
		billyfs := osfs.New(repo + ".git")
		loader := server.NewFilesystemLoader(billyfs)
		srv := server.NewServer(loader)
		session, err = srv.NewUploadPackSession(ep, nil)
		if err != nil {
			http.Error(w, err.Error(), 500)
			log.Printf("git: %s", err)
			return
		}
	}

	ar, err := session.AdvertisedReferencesContext(r.Context())
	if errors.Is(err, transport.ErrRepositoryNotFound) {
		http.Error(w, err.Error(), 404)
		return
	} else if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	ar.Prefix = [][]byte{
		[]byte("# service=git-upload-pack"),
		pktline.Flush,
	}

	if err = ar.Encode(w); err != nil {
		http.Error(w, err.Error(), 500)
		log.Printf("git: %s", err)
		return
	}
}

func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) {
	name := flow.Param(r.Context(), "name")
	name = filepath.Clean(name)

	repo := d.c.Repo.ScanPath
	category := flow.Param(r.Context(), "category")
	if category != "" {
		repo = filepath.Join(repo, category)
	}
	repo = filepath.Join(repo, name)

	w.Header().Set("content-type", "application/x-git-upload-pack-result")

	upr := packp.NewUploadPackRequest()
	err := upr.Decode(r.Body)
	if err != nil {
		http.Error(w, err.Error(), 400)
		log.Printf("git: %s", err)
		return
	}

	ep, err := transport.NewEndpoint("/")
	if err != nil {
		http.Error(w, err.Error(), 500)
		log.Printf("git: %s", err)
		return
	}

	billyfs := osfs.New(repo)
	loader := server.NewFilesystemLoader(billyfs)
	svr := server.NewServer(loader)
	session, err := svr.NewUploadPackSession(ep, nil)
	if err != nil {
		billyfs := osfs.New(repo + ".git")
		loader := server.NewFilesystemLoader(billyfs)
		svr := server.NewServer(loader)
		session, err = svr.NewUploadPackSession(ep, nil)
		if err != nil {
			http.Error(w, err.Error(), 500)
			log.Printf("git: %s", err)
			return
		}
	}

	res, err := session.UploadPack(r.Context(), upr)
	if err != nil {
		http.Error(w, err.Error(), 500)
		log.Printf("git: %s", err)
		return
	}

	if err = res.Encode(w); err != nil {
		http.Error(w, err.Error(), 500)
		log.Printf("git: %s", err)
		return
	}
}