Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/gogit-http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"

"github.com/go-git/go-billy/v6/osfs"
githttp "github.com/go-git/go-git/v6/backend/http"
"github.com/go-git/go-git/v6/backend"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -40,7 +40,7 @@ var rootCmd = &cobra.Command{

logger := log.Default()
loader := transport.NewFilesystemLoader(osfs.New(abs, osfs.WithBoundOS()), false)
gitmw := githttp.NewBackend(loader)
gitmw := backend.New(loader)

handler := LoggingMiddleware(logger, gitmw)

Expand Down
12 changes: 6 additions & 6 deletions cmd/gogit/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

import (
"fmt"
"net/url"
"path"
"strings"

"github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -41,16 +41,16 @@ var cloneCmd = &cobra.Command{
}
}

ep, err := transport.NewEndpoint(args[0])
ep, err := url.Parse(args[0])
if err != nil {
return err
}

opts := git.CloneOptions{
URL: args[0],
Depth: cloneDepth,
Auth: defaultAuth(ep),
Bare: cloneBare,
URL: args[0],
Depth: cloneDepth,
ClientOptions: defaultClientOptions(ep),
Bare: cloneBare,
}

if cloneTags {
Expand Down
10 changes: 5 additions & 5 deletions cmd/gogit/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package main
import (
"log"
"net"
"net/url"
"path/filepath"
"strconv"

gitserver "github.com/go-git/cli/server/git"
"github.com/go-git/go-billy/v6"
"github.com/go-git/go-billy/v6/osfs"
gitbackend "github.com/go-git/go-git/v6/backend/git"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/go-git/go-git/v6/storage"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,7 +40,7 @@ var daemonCmd = &cobra.Command{

loader := NewDirsLoader(dirs, false, daemonExportAll)
addr := net.JoinHostPort(daemonListen, strconv.Itoa(daemonPort))
be := gitbackend.NewBackend(loader)
be := gitserver.NewBackend(loader)
srv := &gitserver.Server{
Addr: addr,
Handler: gitserver.LoggingMiddleware(log.Default(), be),
Expand Down Expand Up @@ -81,16 +81,16 @@ func NewDirsLoader(dirs []string, strict, exportAll bool) *dirsLoader {
}

// Load implements transport.Loader.
func (d *dirsLoader) Load(ep *transport.Endpoint) (storage.Storer, error) {
func (d *dirsLoader) Load(u *url.URL) (storage.Storer, error) {
for i, loader := range d.loaders {
storer, err := loader.Load(ep)
storer, err := loader.Load(u)
if err == nil {
if !d.exportAll {
// We need to check if git-daemon-export-ok
// file exists and if it does not, we skip this
// repository.
dfs := d.fss[i]
okFile := filepath.Join(ep.Path, "git-daemon-export-ok")
okFile := filepath.Join(u.Path, "git-daemon-export-ok")

stat, err := dfs.Lstat(okFile)
if err != nil || (stat != nil && !stat.Mode().IsRegular()) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/gogit/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"errors"
"math"
"net/url"

"github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,7 +38,7 @@ var fetchCmd = &cobra.Command{
return err
}

ep, err := transport.NewEndpoint(remote.Config().URLs[0])
ep, err := url.Parse(remote.Config().URLs[0])
if err != nil {
return err
}
Expand All @@ -48,8 +48,8 @@ var fetchCmd = &cobra.Command{
}

opts := git.FetchOptions{
Depth: fetchDepth,
Auth: defaultAuth(ep),
Depth: fetchDepth,
ClientOptions: defaultClientOptions(ep),
}

if fetchProgress {
Expand Down
18 changes: 12 additions & 6 deletions cmd/gogit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"errors"
"fmt"
"net/url"
"os"
"strconv"

"github.com/go-git/go-git/v6/plumbing/client"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/go-git/go-git/v6/plumbing/transport/ssh"
"github.com/go-git/go-git/v6/utils/trace"
Expand Down Expand Up @@ -58,27 +60,31 @@ func main() {
}
}

func defaultAuth(ep *transport.Endpoint) transport.AuthMethod {
switch ep.Scheme {
func defaultClientOptions(u *url.URL) []client.Option {
if u == nil {
return nil
}

switch u.Scheme {
case "file", "git":
// Do nothing.
case "ssh":
if ep.User == nil {
if u.User == nil {
return nil
}

a, err := ssh.NewSSHAgentAuth(ep.User.Username())
a, err := ssh.NewSSHAgentAuth(u.User.Username())
if err != nil {
return nil
}

switch ep.Host {
switch u.Host {
case "localhost", "127.0.0.1":
// Ignore host key verification for localhost.
a.HostKeyCallback = gossh.InsecureIgnoreHostKey()
}

return a
return []client.Option{client.WithSSHAuth(a)}
case "http", "https":
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/gogit/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"errors"
"net/url"

"github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,13 +34,13 @@ var pullCmd = &cobra.Command{
return err
}

ep, err := transport.NewEndpoint(cfg.Remotes["origin"].URLs[0])
ep, err := url.Parse(cfg.Remotes["origin"].URLs[0])
if err != nil {
return err
}

opts := git.PullOptions{
Auth: defaultAuth(ep),
ClientOptions: defaultClientOptions(ep),
}
if pullProgress {
opts.Progress = cmd.OutOrStdout()
Expand Down
18 changes: 9 additions & 9 deletions cmd/gogit/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"errors"
"fmt"
"net/url"

"github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/config"
"github.com/go-git/go-git/v6/plumbing"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/spf13/cobra"
"golang.org/x/term"
)
Expand Down Expand Up @@ -43,7 +43,7 @@ var pushCmd = &cobra.Command{
return fmt.Errorf("failed to get repository config: %w", err)
}

var ep *transport.Endpoint
var ep *url.URL

remoteName := git.DefaultRemoteName

Expand All @@ -61,7 +61,7 @@ var pushCmd = &cobra.Command{

if !isRemote {
// Is this a repository URL?
ep, err = transport.NewEndpoint(args[0])
ep, err = url.Parse(args[0])
if err != nil {
// We have a remote name
remoteName = args[0]
Expand Down Expand Up @@ -91,18 +91,18 @@ var pushCmd = &cobra.Command{
return errors.New("no remote URLs")
}

ep, err = transport.NewEndpoint(remote.Config().URLs[urln])
ep, err = url.Parse(remote.Config().URLs[urln])
if err != nil {
return err
}
}

opts := git.PushOptions{
Auth: defaultAuth(ep),
RemoteName: remoteName,
RefSpecs: refspecs,
Prune: pushPrune,
Force: pushForce,
ClientOptions: defaultClientOptions(ep),
RemoteName: remoteName,
RefSpecs: refspecs,
Prune: pushPrune,
Force: pushForce,
}

var isatty bool
Expand Down
2 changes: 1 addition & 1 deletion cmd/gogit/receive-pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var receivePackCmd = &cobra.Command{
r.Storer,
os.Stdin,
os.Stdout,
&transport.ReceivePackOptions{
&transport.ReceivePackRequest{
GitProtocol: os.Getenv("GIT_PROTOCOL"),
StatelessRPC: receivePackStatelessRPC,
AdvertiseRefs: receivePackAdvertiseRefs,
Expand Down
2 changes: 1 addition & 1 deletion cmd/gogit/upload-pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var uploadPackCmd = &cobra.Command{
r.Storer,
os.Stdin,
os.Stdout,
&transport.UploadPackOptions{
&transport.UploadPackRequest{
GitProtocol: os.Getenv("GIT_PROTOCOL"),
StatelessRPC: uploadPackStatelessRPC,
AdvertiseRefs: uploadPackAdvertiseRefs,
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.25.4
require (
github.com/go-git/go-billy/v6 v6.0.0-20260410103409-85b6241850b5
github.com/go-git/go-git-fixtures/v6 v6.0.0-20260410103352-fe4fd2baf1dc
github.com/go-git/go-git/v6 v6.0.0-alpha.1
github.com/go-git/go-git/v6 v6.0.0-alpha.2
github.com/spf13/cobra v1.10.2
golang.org/x/crypto v0.50.0
golang.org/x/term v0.42.0
Expand All @@ -20,14 +20,13 @@ require (
github.com/cyphar/filepath-securejoin v0.6.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg/v2 v2.0.2 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kevinburke/ssh_config v1.6.0 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/pjbgf/sha1cd v0.5.0 // indirect
github.com/sergi/go-diff v1.4.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
golang.org/x/net v0.52.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
)
16 changes: 6 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ github.com/go-git/gcfg/v2 v2.0.2 h1:MY5SIIfTGGEMhdA7d7JePuVVxtKL7Hp+ApGDJAJ7dpo=
github.com/go-git/gcfg/v2 v2.0.2/go.mod h1:/lv2NsxvhepuMrldsFilrgct6pxzpGdSRC13ydTLSLs=
github.com/go-git/go-billy/v6 v6.0.0-20260410103409-85b6241850b5 h1:r5Y4Hn9QwQj+u6vN0Ib1MipHkanYaG8Zj0kxsnv8Bu4=
github.com/go-git/go-billy/v6 v6.0.0-20260410103409-85b6241850b5/go.mod h1:CdBVp7CXl9l3sOyNEog46cP1Pvx/hjCe9AD0mtaIUYU=
github.com/go-git/go-git-fixtures/v5 v5.1.2-0.20260122163445-0622d7459a67 h1:3hutPZF+/FBjR/9MdsLJ7e1mlt9pwHgwxMW7CrbmWII=
github.com/go-git/go-git-fixtures/v5 v5.1.2-0.20260122163445-0622d7459a67/go.mod h1:xKt0pNHST9tYHvbiLxSY27CQWFwgIxBJuDrOE0JvbZw=
github.com/go-git/go-git-fixtures/v6 v6.0.0-20260410103352-fe4fd2baf1dc h1:iNssylWjlbkLomzPcgPmvjFbwNd8p9+M6UGcm2ra0zA=
github.com/go-git/go-git-fixtures/v6 v6.0.0-20260410103352-fe4fd2baf1dc/go.mod h1:1Lr7/vYEYyl6Ir9Ku0tKrCIRreM5zovv0Jdx2MPSM4s=
github.com/go-git/go-git/v6 v6.0.0-alpha.1 h1:tGohX5luKeO50DZD0/Zqd5dYjJzKtub91S4I+1qFVIs=
github.com/go-git/go-git/v6 v6.0.0-alpha.1/go.mod h1:qtzfNHlFsnq6vCw54aT4KvFWPK5bsOpTVtFC2sysdB8=
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
github.com/go-git/go-git/v6 v6.0.0-alpha.2 h1:T3loNtDuAixNzXtlQxZhnYiYpaQ3CA4vn9RssAniEeI=
github.com/go-git/go-git/v6 v6.0.0-alpha.2/go.mod h1:oCD3i19CTz7gBpeb11ZZqL91WzqbMq9avn5KpUYy/Ak=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kevinburke/ssh_config v1.6.0 h1:J1FBfmuVosPHf5GRdltRLhPJtJpTlMdKTBjRgTaQBFY=
Expand Down Expand Up @@ -57,10 +53,10 @@ github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.42.0 h1:UiKe+zDFmJobeJ5ggPwOshJIVt6/Ft0rcfrXZDLWAWY=
Expand Down
23 changes: 21 additions & 2 deletions server/git/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"sync/atomic"
"time"

gitbackend "github.com/go-git/go-git/v6/backend/git"
"github.com/go-git/go-git/v6/backend"
"github.com/go-git/go-git/v6/plumbing/format/pktline"
"github.com/go-git/go-git/v6/plumbing/protocol/packp"
"github.com/go-git/go-git/v6/plumbing/transport"
Expand All @@ -26,7 +26,7 @@ const DefaultAddr = ":9418"
var ErrServerClosed = errors.New("server closed")

// DefaultBackend is the default global Git transport server handler.
var DefaultBackend = gitbackend.NewBackend(nil)
var DefaultBackend Handler = NewBackend(nil)

// ServerContextKey is the context key used to store the server in the context.
var ServerContextKey = &contextKey{"git-server"}
Expand All @@ -37,6 +37,12 @@ type Handler interface {
ServeTCP(ctx context.Context, c io.ReadWriteCloser, req *packp.GitProtoRequest)
}

// NewBackend creates a [Handler] backed by the given [transport.Loader].
// If loader is nil, [transport.DefaultLoader] is used.
func NewBackend(loader transport.Loader) Handler {
return &backendHandler{b: backend.New(loader)}
}

// HandlerFunc is a function that implements the Handler interface.
type HandlerFunc func(ctx context.Context, c io.ReadWriteCloser, req *packp.GitProtoRequest)

Expand All @@ -45,6 +51,19 @@ func (f HandlerFunc) ServeTCP(ctx context.Context, c io.ReadWriteCloser, req *pa
f(ctx, c, req)
}

// backendHandler wraps a [backend.Backend] to implement the [Handler] interface.
type backendHandler struct {
b *backend.Backend
}

func (bh *backendHandler) ServeTCP(ctx context.Context, c io.ReadWriteCloser, req *packp.GitProtoRequest) {
backendReq := backend.RequestFromProto(req)
if err := bh.b.Serve(ctx, c, c, backendReq); err != nil {
// Best-effort error notification; ignore if the write itself fails.
_ = renderError(c, fmt.Errorf("error serving request: %w", err))
}
}

// Server is a TCP server that handles Git protocol requests.
type Server struct {
// Addr is the address to listen on. If empty, it defaults to ":9418".
Expand Down
Loading