-
Notifications
You must be signed in to change notification settings - Fork 63
/
git.go
254 lines (212 loc) · 6.14 KB
/
git.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package manifest
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
gitconfig "github.com/go-git/go-git/v5/config"
gitplumbing "github.com/go-git/go-git/v5/plumbing"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
giturl "github.com/kubescape/go-git-url"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/unikraft"
)
type GitProvider struct {
repo string
remote *git.Remote
refs []*gitplumbing.Reference
mopts *ManifestOptions
branch string
ctx context.Context
}
// NewGitProvider attempts to parse a provided path as a Git repository
func NewGitProvider(ctx context.Context, path string, opts ...ManifestOption) (Provider, error) {
isSSH := false
fullpath := path
if isSSHURL(path) {
isSSH = true
// This is a quirk of go-git, if we have determined it was an SSH path and
// it does not contain the prefix, we should include it so it can be
// recognised internally by the module.
if strings.HasPrefix(path, "git@") {
fullpath = "ssh://" + path
}
}
gitURL, err := giturl.NewGitURL(fullpath) // initialize and parse the URL
if err != nil {
return nil, err
}
// Check if the remote URL is a Git repository
remote := git.NewRemote(nil, &gitconfig.RemoteConfig{
Name: "origin",
URLs: []string{fullpath},
})
lopts := &git.ListOptions{}
provider := GitProvider{
repo: path,
remote: remote,
mopts: NewManifestOptions(opts...),
branch: gitURL.GetBranchName(),
ctx: ctx,
}
if isSSH {
user := gitURL.GetURL().User.Username()
if user == "" {
user = "git"
}
lopts.Auth, err = gitssh.DefaultAuthBuilder(user)
if err != nil {
return nil, err
}
} else if auth, ok := provider.mopts.auths[gitURL.GetHostName()]; ok {
if len(auth.User) > 0 {
lopts.Auth = &githttp.BasicAuth{
Username: auth.User,
Password: auth.Token,
}
} else if len(auth.Token) > 0 {
lopts.Auth = &githttp.TokenAuth{
Token: auth.Token,
}
}
}
// If this is a valid Git repository then let's generate a Manifest based on
// what we can read from the remote
provider.refs, err = remote.ListContext(ctx, lopts)
if err != nil {
return nil, fmt.Errorf("could not list remote git repository: %v", err)
}
return &provider, nil
}
// probeChannels is an internal method which matches Git branches for the
// repository and uses this as a ManifestChannel
func (gp *GitProvider) probeChannels() []ManifestChannel {
var channels []ManifestChannel
// This is unikraft-centric ettiquette where there exists two branches:
// "stable" and "staging". If these two channels exist, then later on we'll
// update the "stable" channel to be the default.
haveStaging := false
haveStable := false
for _, ref := range gp.refs {
// Branches are channels
if ref.Name().IsBranch() {
if len(gp.branch) > 0 && ref.Name().Short() != gp.branch {
continue
}
channel := ManifestChannel{
Name: ref.Name().Short(),
Resource: gp.repo,
}
// Unikraft-centric naming conventions
if ref.Name().Short() == "staging" {
haveStaging = true
} else if ref.Name().Short() == "stable" {
haveStable = true
}
channels = append(channels, channel)
continue
}
}
if haveStaging && haveStable {
for i, channel := range channels {
if channel.Name == "stable" {
channel.Default = true
channels[i] = channel
}
}
} else if len(gp.branch) > 0 && len(channels) == 1 {
channels[0].Default = true
}
return channels
}
// probeVersions is an internal method which matches Git tags for the repository
// and uses this as a ManifestVersion
func (gp *GitProvider) probeVersions() []ManifestVersion {
var versions []ManifestVersion
for _, ref := range gp.refs {
// Tags are versions
if ref.Name().IsTag() {
if len(gp.branch) > 0 && ref.Name().Short() != gp.branch {
continue
}
ver := ref.Name().Short()
version := ManifestVersion{
Version: ver,
Resource: gp.repo,
}
// This is a unikraft-centric ettiquette where the Unikraft core
// repository's version is referenced via the `RELEASE-` prefix. If
// this is the case, we can select the Git SHA as the version and
// subsequently set the Unikraft core version in one.
if strings.HasPrefix(ver, "RELEASE-") {
version.Unikraft = strings.TrimPrefix(ver, "RELEASE-")
version.Version = ref.Hash().String()[:7]
version.Type = ManifestVersionGitSha
}
versions = append(versions, version)
}
}
return versions
}
func (gp *GitProvider) Manifests() ([]*Manifest, error) {
base := filepath.Base(gp.repo)
ext := filepath.Ext(gp.repo)
if len(ext) > 0 {
base = strings.TrimSuffix(base, ext)
}
t, n, _, err := unikraft.GuessTypeNameVersion(base)
if err != nil {
return nil, err
}
manifest := &Manifest{
Type: t,
Name: n,
Origin: gp.repo,
Provider: gp,
}
log.G(gp.ctx).Infof("probing %s", gp.repo)
manifest.Channels = append(manifest.Channels, gp.probeChannels()...)
manifest.Versions = append(manifest.Versions, gp.probeVersions()...)
// TODO: Set the latest version
// if len(manifest.Versions) > 0 {
// }
return []*Manifest{manifest}, nil
}
func (gp *GitProvider) PullManifest(ctx context.Context, manifest *Manifest, popts ...pack.PullOption) error {
if useGit {
return pullGit(ctx, manifest, popts...)
}
manifest.mopts = gp.mopts
if err := pullArchive(ctx, manifest, popts...); err != nil {
log.G(ctx).Trace(err)
return pullGit(ctx, manifest, popts...)
}
return nil
}
func (gp *GitProvider) DeleteManifest(context.Context) error {
return fmt.Errorf("not implemented: manifest.GitProvider.DeleteManifest")
}
func (gp *GitProvider) String() string {
return "git"
}
// isSSHURL determines if the provided URL forms an SSH connection
func isSSHURL(path string) bool {
for _, prefix := range []string{
"ssh://",
"ssh+git://",
"git+ssh://",
"git@",
} {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}