all repos — legit @ fba146ac6867b13c40802c4d7a21a8a32571473c

web frontend for git

routes/git.go (view raw)

 1package routes
 2
 3import (
 4	"compress/gzip"
 5	"io"
 6	"log"
 7	"net/http"
 8	"path/filepath"
 9
10	"git.icyphox.sh/legit/git/service"
11)
12
13func (d *deps) InfoRefs(w http.ResponseWriter, r *http.Request) {
14	name := r.PathValue("name")
15	name = filepath.Clean(name)
16
17	repo := filepath.Join(d.c.Repo.ScanPath, name)
18
19	w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")
20	w.WriteHeader(http.StatusOK)
21
22	cmd := service.ServiceCommand{
23		Dir:    repo,
24		Stdout: w,
25	}
26
27	if err := cmd.InfoRefs(); err != nil {
28		http.Error(w, err.Error(), 500)
29		log.Printf("git: failed to execute git-upload-pack (info/refs) %s", err)
30		return
31	}
32}
33
34func (d *deps) UploadPack(w http.ResponseWriter, r *http.Request) {
35	name := r.PathValue("name")
36	name = filepath.Clean(name)
37
38	repo := filepath.Join(d.c.Repo.ScanPath, name)
39
40	w.Header().Set("content-type", "application/x-git-upload-pack-result")
41	w.Header().Set("Connection", "Keep-Alive")
42	w.Header().Set("Transfer-Encoding", "chunked")
43	w.WriteHeader(http.StatusOK)
44
45	cmd := service.ServiceCommand{
46		Dir:    repo,
47		Stdout: w,
48	}
49
50	var reader io.ReadCloser
51	reader = r.Body
52
53	if r.Header.Get("Content-Encoding") == "gzip" {
54		reader, err := gzip.NewReader(r.Body)
55		if err != nil {
56			http.Error(w, err.Error(), 500)
57			log.Printf("git: failed to create gzip reader: %s", err)
58			return
59		}
60		defer reader.Close()
61	}
62
63	cmd.Stdin = reader
64	if err := cmd.UploadPack(); err != nil {
65		http.Error(w, err.Error(), 500)
66		log.Printf("git: failed to execute git-upload-pack %s", err)
67		return
68	}
69}