File: lfsapi.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (76 lines) | stat: -rw-r--r-- 2,116 bytes parent folder | download
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
package lfsapi

import (
	"github.com/git-lfs/git-lfs/v3/config"
	"github.com/git-lfs/git-lfs/v3/creds"
	"github.com/git-lfs/git-lfs/v3/errors"
	"github.com/git-lfs/git-lfs/v3/lfshttp"
	"github.com/git-lfs/git-lfs/v3/ssh"
	"github.com/git-lfs/git-lfs/v3/tr"
	"github.com/rubyist/tracerx"
)

type Client struct {
	Endpoints   EndpointFinder
	Credentials creds.CredentialHelper

	credContext *creds.CredentialHelperContext

	client  *lfshttp.Client
	context lfshttp.Context
	access  []creds.AccessMode
}

func NewClient(ctx lfshttp.Context) (*Client, error) {
	if ctx == nil {
		ctx = lfshttp.NewContext(nil, nil, nil)
	}

	gitEnv := ctx.GitEnv()
	osEnv := ctx.OSEnv()

	httpClient, err := lfshttp.NewClient(ctx)
	if err != nil {
		return nil, errors.Wrap(err, tr.Tr.Get("error creating HTTP client"))
	}

	c := &Client{
		Endpoints:   NewEndpointFinder(ctx),
		client:      httpClient,
		context:     ctx,
		credContext: creds.NewCredentialHelperContext(gitEnv, osEnv),
		access:      creds.AllAccessModes(),
	}

	return c, nil
}

func (c *Client) Context() lfshttp.Context {
	return c.context
}

// SSHTransfer returns either an suitable transfer object or nil if the
// server is not using an SSH remote or the git-lfs-transfer style of SSH
// remote.
func (c *Client) SSHTransfer(operation, remote string) *ssh.SSHTransfer {
	if len(operation) == 0 {
		return nil
	}
	endpoint := c.Endpoints.Endpoint(operation, remote)
	if len(endpoint.SSHMetadata.UserAndHost) == 0 {
		return nil
	}
	uc := config.NewURLConfig(c.context.GitEnv())
	if val, ok := uc.Get("lfs", endpoint.OriginalUrl, "sshtransfer"); ok && val != "negotiate" && val != "always" {
		tracerx.Printf("skipping pure SSH protocol connection by request (%s, %s)", operation, remote)
		return nil
	}
	ctx := c.Context()
	tracerx.Printf("attempting pure SSH protocol connection (%s, %s)", operation, remote)
	sshTransfer, err := ssh.NewSSHTransfer(ctx.OSEnv(), ctx.GitEnv(), &endpoint.SSHMetadata, operation)
	if err != nil {
		tracerx.Printf("pure SSH protocol connection failed (%s, %s): %s", operation, remote, err)
		return nil
	}
	return sshTransfer
}