all repos — elgit @ b51ef07d24db92f9c8a6f383f5e76a483adae458

fork of legit: web frontend for git, written in go

routes/git.go (view raw)

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
package routes

import (
	"bytes"
	"compress/gzip"
	"io"
	"log"
	"net/http"
	"path"

	"alin.ovh/elgit/git/service"
	"github.com/savsgio/atreugo/v11"
	"github.com/valyala/fasthttp"
)

func (d *deps) InfoRefs(rc *atreugo.RequestCtx) error {
	category, _ := rc.UserValue("category").(string)
	name, _ := rc.UserValue("name").(string)
	repoName := path.Join(category, name)

	repo, err := d.GetCleanPath(repoName)
	if err != nil {
		log.Printf("getcleanpath error: %v", err)

		return ErrNotFound
	}

	svc := rc.QueryArgs().Peek("service")
	if string(svc) == "git-receive-pack" {
		return rc.TextResponse("no pushing allowed!", fasthttp.StatusBadRequest)
	}

	rc.SetStatusCode(http.StatusOK)
	rc.Response.Header.Set("content-type", "application/x-git-upload-pack-advertisement")
	rc.Response.Header.Set("cache-control", "no-cache")

	cmd := service.Command{
		Dir:    repo,
		Stdout: rc,
	}

	return cmd.InfoRefs(rc)
}

func (d *deps) UploadPack(rc *atreugo.RequestCtx) error {
	category, _ := rc.UserValue("category").(string)
	name, _ := rc.UserValue("name").(string)
	repoName := path.Join(category, name)

	repo, err := d.GetCleanPath(repoName)
	if err != nil {
		log.Printf("getcleanpath error: %v", err)

		return ErrNotFound
	}

	rc.SetStatusCode(http.StatusOK)
	rc.SetContentType("application/x-git-upload-pack-result")
	rc.Response.Header.Set("Connection", "Keep-Alive")
	rc.Response.Header.Set("Transfer-Encoding", "chunked")
	rc.Response.Header.Set("Cache-Control", "no-cache")

	var reader io.Reader
	if rc.Request.IsBodyStream() {
		reader = rc.Request.BodyStream()
		defer func() {
			err := rc.Request.CloseBodyStream()
			if err != nil {
				log.Printf("git: failed to close gzip reader: %s", err)
			}
		}()
	} else {
		reader = bytes.NewReader(rc.Request.Body())
	}

	if bytes.Contains(rc.Request.Header.ContentEncoding(), []byte("gzip")) {
		reader, err = gzip.NewReader(reader)
		if err != nil {
			return rc.ErrorResponse(err, 500)
		}
	}

	cmd := service.Command{
		Dir:    repo,
		Stdout: rc,
		Stdin:  reader,
	}

	return cmd.UploadPack(rc)
}