git: pure go git clone

This commit is contained in:
Anirudh Oppiliappan 2022-12-24 14:57:44 +05:30
parent 983dabb33c
commit fa66c4faa8
No known key found for this signature in database
GPG Key ID: 8A93F96F78C5D4C4
4 changed files with 118 additions and 35 deletions

4
go.mod
View File

@ -6,8 +6,8 @@ require (
github.com/alexedwards/flow v0.0.0-20220806114457-cf11be9e0e03
github.com/bluekeyes/go-gitdiff v0.7.0
github.com/dustin/go-humanize v1.0.0
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-git/v5 v5.5.1
github.com/sosedoff/gitkit v0.3.0
golang.org/x/sys v0.3.0
gopkg.in/yaml.v3 v3.0.0
)
@ -19,8 +19,6 @@ require (
github.com/cloudflare/circl v1.3.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect

5
go.sum
View File

@ -35,8 +35,6 @@ github.com/go-git/go-git-fixtures/v4 v4.3.1 h1:y5z6dd3qi8Hl+stezc8p3JxDkoTRqMAlK
github.com/go-git/go-git-fixtures/v4 v4.3.1/go.mod h1:8LHG1a3SRW71ettAD/jW13h8c6AqjVSeL11RAdgaqpo=
github.com/go-git/go-git/v5 v5.5.1 h1:5vtv2TB5PM/gPM+EvsHJ16hJh4uAkdGcKilcwY7FYwo=
github.com/go-git/go-git/v5 v5.5.1/go.mod h1:uz5PQ3d0gz7mSgzZhSJToM6ALPaKCdSnl58/Xb5hzr8=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
@ -67,8 +65,6 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skeema/knownhosts v1.1.0 h1:Wvr9V0MxhjRbl3f9nMnKnFfiWTJmtECJ9Njkea3ysW0=
github.com/skeema/knownhosts v1.1.0/go.mod h1:sKFq3RD6/TKZkSWn8boUbDC7Qkgcv+8XXijpFO6roag=
github.com/sosedoff/gitkit v0.3.0 h1:TfINVRNUM+GcFa+LGhZ3RcWN86Im1M6i8qs0IsgMy90=
github.com/sosedoff/gitkit v0.3.0/go.mod h1:V3EpGZ0nvCBhXerPsbDeqtyReNb48cwP9KtkUYTKT5I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@ -78,7 +74,6 @@ github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=

108
routes/git.go Normal file
View File

@ -0,0 +1,108 @@
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 := filepath.Join(d.c.Repo.ScanPath, 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 {
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 := filepath.Join(d.c.Repo.ScanPath, 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 {
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
}
}

View File

@ -1,26 +1,16 @@
package routes
import (
"log"
"net/http"
"path/filepath"
"git.icyphox.sh/legit/config"
"github.com/alexedwards/flow"
"github.com/sosedoff/gitkit"
)
type depsWrapper struct {
actualDeps deps
gitsvc *gitkit.Server
}
// Checks for gitprotocol-http(5) specific smells; if found, passes
// the request on to the git http service, else render the web frontend.
func (dw *depsWrapper) Multiplex(w http.ResponseWriter, r *http.Request) {
func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) {
path := flow.Param(r.Context(), "...")
name := flow.Param(r.Context(), "name")
name = filepath.Clean(name)
if r.URL.RawQuery == "service=git-receive-pack" {
w.WriteHeader(http.StatusBadRequest)
@ -28,12 +18,14 @@ func (dw *depsWrapper) Multiplex(w http.ResponseWriter, r *http.Request) {
return
}
if path == "info/refs" && r.URL.RawQuery == "service=git-upload-pack" && r.Method == "GET" {
dw.gitsvc.ServeHTTP(w, r)
if path == "info/refs" &&
r.URL.RawQuery == "service=git-upload-pack" &&
r.Method == "GET" {
d.InfoRefs(w, r)
} else if path == "git-upload-pack" && r.Method == "POST" {
dw.gitsvc.ServeHTTP(w, r)
d.UploadPack(w, r)
} else if r.Method == "GET" {
dw.actualDeps.RepoIndex(w, r)
d.RepoIndex(w, r)
}
}
@ -41,29 +33,19 @@ func Handlers(c *config.Config) *flow.Mux {
mux := flow.New()
d := deps{c}
gitsvc := gitkit.New(gitkit.Config{
Dir: c.Repo.ScanPath,
AutoCreate: false,
})
if err := gitsvc.Setup(); err != nil {
log.Fatalf("git server: %s", err)
}
dw := depsWrapper{actualDeps: d, gitsvc: gitsvc}
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
d.Write404(w)
})
mux.HandleFunc("/", d.Index, "GET")
mux.HandleFunc("/static/:file", d.ServeStatic, "GET")
mux.HandleFunc("/:name", dw.Multiplex, "GET", "POST")
mux.HandleFunc("/:name", d.Multiplex, "GET", "POST")
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
mux.HandleFunc("/:name/refs", d.Refs, "GET")
mux.HandleFunc("/:name/...", dw.Multiplex, "GET", "POST")
mux.HandleFunc("/:name/...", d.Multiplex, "GET", "POST")
return mux
}