all repos

mugit @ f03a67a4b3efcfc8797ea64aa3bd3bc5e5d745b5

🐮 git server that your cow will love

mugit/internal/handlers/git.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
fix: check if ref exists before archiving, 3 months ago
1
package handlers
2
3
import (
4
	"fmt"
5
	"io"
6
	"log/slog"
7
	"net/http"
8
9
	"olexsmir.xyz/mugit/internal/git"
10
	"olexsmir.xyz/mugit/internal/git/gitx"
11
)
12
13
// multiplex, check if the request smells like gitprotocol-http(5), if so, it
14
// passes it to git smart http, otherwise renders templates
15
func (h *handlers) multiplex(w http.ResponseWriter, r *http.Request) {
16
	if r.URL.RawQuery == "service=git-receive-pack" {
17
		w.WriteHeader(http.StatusBadRequest)
18
		w.Write([]byte("http pushing is not supported"))
19
		return
20
	}
21
22
	path := r.PathValue("rest")
23
	if path == "info/refs" && r.Method == "GET" && r.URL.RawQuery == "service=git-upload-pack" {
24
		h.infoRefs(w, r)
25
	} else if path == "git-upload-pack" && r.Method == "POST" {
26
		h.uploadPack(w, r)
27
	} else if r.Method == "GET" && path == "" {
28
		h.repoIndex(w, r)
29
	} else {
30
		h.write404(w, nil)
31
	}
32
}
33
34
func (h *handlers) infoRefs(w http.ResponseWriter, r *http.Request) {
35
	path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"), "")
36
	if err != nil {
37
		h.write404(w, err)
38
		return
39
	}
40
41
	w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")
42
	w.WriteHeader(http.StatusOK)
43
	if err := gitx.InfoRefs(r.Context(), path, w); err != nil {
44
		w.WriteHeader(http.StatusInternalServerError)
45
		slog.Error("git: info/refs", "err", err)
46
		return
47
	}
48
}
49
50
func (h *handlers) uploadPack(w http.ResponseWriter, r *http.Request) {
51
	path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"), "")
52
	if err != nil {
53
		h.write404(w, err)
54
		return
55
	}
56
57
	w.Header().Set("Content-Type", "application/x-git-upload-pack-result")
58
	w.Header().Set("Cache-Control", "no-cache")
59
	w.WriteHeader(http.StatusOK)
60
61
	if err := gitx.UploadPack(r.Context(), path, true, r.Body, newFlushWriter(w)); err != nil {
62
		slog.Error("git: upload-pack", "err", err)
63
		return
64
	}
65
}
66
67
func (h *handlers) archiveHandler(w http.ResponseWriter, r *http.Request) {
68
	name := r.PathValue("name")
69
	ref := h.parseRef(r.PathValue("ref"))
70
71
	path, err := h.checkRepoPublicityAndGetPath(name, ref)
72
	if err != nil {
73
		h.write404(w, err)
74
		return
75
	}
76
77
	filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
78
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
79
	w.Header().Set("Content-Type", "application/gzip")
80
	w.WriteHeader(http.StatusOK)
81
82
	if err := gitx.ArchiveTar(r.Context(), path, ref, w); err != nil {
83
		slog.Error("git: archive", "ref", ref, "err", err)
84
		return
85
	}
86
}
87
88
func (h *handlers) checkRepoPublicityAndGetPath(name string, ref string) (string, error) {
89
	name = git.ResolveName(name)
90
	path, err := git.ResolvePath(h.c.Repo.Dir, name)
91
	if err != nil {
92
		return "", err
93
	}
94
95
	if _, oerr := git.OpenPublic(path, ref); oerr != nil {
96
		return "", oerr
97
	}
98
99
	return path, err
100
}
101
102
func (h *handlers) openPublicRepo(name, ref string) (*git.Repo, error) {
103
	name = git.ResolveName(name)
104
	path, err := git.ResolvePath(h.c.Repo.Dir, name)
105
	if err != nil {
106
		return nil, err
107
	}
108
	return git.OpenPublic(path, ref)
109
}
110
111
type flushWriter struct {
112
	w io.Writer
113
	f http.Flusher
114
}
115
116
func newFlushWriter(w http.ResponseWriter) io.Writer {
117
	f, _ := w.(http.Flusher)
118
	return &flushWriter{w: w, f: f}
119
}
120
121
func (fw *flushWriter) Write(p []byte) (int, error) {
122
	n, err := fw.w.Write(p)
123
	if fw.f != nil {
124
		fw.f.Flush()
125
	}
126
	return n, err
127
}