-
Notifications
You must be signed in to change notification settings - Fork 1
/
dep.go
213 lines (179 loc) · 4.73 KB
/
dep.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
package templit
import (
"fmt"
"net/url"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)
// DepInfo contains information about an embed URL.
type DepInfo struct {
Host string
Owner string
Repo string
Path string
Block string
Tag string
}
// String returns the string representation of a DepInfo.
func (d DepInfo) String() string {
var builder strings.Builder
builder.WriteString(d.Host)
builder.WriteRune('/')
builder.WriteString(d.Owner)
builder.WriteRune('/')
builder.WriteString(d.Repo)
if d.Path != "" {
builder.WriteRune('/')
builder.WriteString(d.Path)
}
if d.Block != "" {
builder.WriteRune('#')
builder.WriteString(d.Block)
}
if d.Tag != "" {
builder.WriteRune('@')
builder.WriteString(d.Tag)
}
return builder.String()
}
// ParseDepURL is a parsed embed URL.
func ParseDepURL(rawURL string) (*DepInfo, error) {
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
rawURL = "https://" + rawURL
}
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
// Extract the tag if it exists before splitting the path
fullPath := strings.Trim(u.Path, "/")
tag := ""
if idx := strings.Index(fullPath, "@"); idx != -1 {
fullPath, tag = splitAtSign(fullPath)
}
// Split path into components
pathParts := strings.Split(fullPath, "/")
if len(pathParts) < 2 {
return nil, fmt.Errorf("invalid path format in embed URL")
}
owner := pathParts[0]
repo := pathParts[1]
path := ""
if len(pathParts) > 2 {
path = strings.Join(pathParts[2:], "/")
}
// If fragment contains the tag, then prioritize it over the tag in the path
block, fragmentTag := extractBlockAndTag(u.Fragment)
if fragmentTag != "" {
tag = fragmentTag
}
return &DepInfo{
Host: u.Host,
Owner: owner,
Repo: repo,
Path: path,
Block: block,
Tag: tag,
}, nil
}
// splitAtSign splits the given string at the '@' sign and returns both parts.
func splitAtSign(s string) (string, string) {
parts := strings.Split(s, "@")
if len(parts) > 1 {
return parts[0], parts[1]
}
return parts[0], ""
}
// extractBlockAndTag extracts the block and tag from a fragment.
func extractBlockAndTag(fragment string) (string, string) {
parts := strings.Split(fragment, "@")
if len(parts) > 1 {
return parts[0], parts[1]
} else if len(parts) == 1 {
return parts[0], ""
}
return "", ""
}
// GitClient is an interface that abstracts Git operations.
type GitClient interface {
Clone(host, owner, repo, dest string) error
Checkout(path, branch string) error
DefaultBranch() string
}
// DefaultGitClient provides a default implementation for the GitClient interface.
type DefaultGitClient struct {
Token string
defaultBranch string
}
// NewDefaultGitClient creates a new DefaultGitClient with the given token.
func NewDefaultGitClient(defaultBranch string, token string) *DefaultGitClient {
if defaultBranch == "" {
defaultBranch = "main"
}
return &DefaultGitClient{
Token: token,
defaultBranch: defaultBranch,
}
}
// DefaultBranch returns the default branch name.
func (d *DefaultGitClient) DefaultBranch() string {
return d.defaultBranch
}
// Clone clones a Git repository to the given destination.
func (d *DefaultGitClient) Clone(host, owner, repo, dest string) error {
repoURL := fmt.Sprintf("%s/%s/%s.git", host, owner, repo)
if !strings.HasPrefix(repoURL, "https://") {
repoURL = fmt.Sprintf("https://%s", repoURL)
}
var auth *http.BasicAuth
if d.Token != "" {
auth = &http.BasicAuth{
Username: "username", // this can be anything except an empty string
Password: d.Token,
}
}
_, err := git.PlainClone(dest, false, &git.CloneOptions{
URL: repoURL,
Auth: auth,
})
if err != nil {
return fmt.Errorf("failed to clone repo %s: %w", repoURL, err)
}
return nil
}
// Checkout checks out a branch, tag or commit hash in a Git repository.
func (d *DefaultGitClient) Checkout(path, ref string) error {
r, err := git.PlainOpen(path)
if err != nil {
return err
}
w, err := r.Worktree()
if err != nil {
return err
}
// Try to checkout branch
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/remotes/origin/" + ref),
})
if err == nil {
return nil // Branch checked out successfully
}
// If branch checkout fails, try tag
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/tags/" + ref),
})
if err == nil {
return nil // Tag checked out successfully
}
// If tag checkout also fails, try using it as a commit hash
commitHash := plumbing.NewHash(ref)
err = w.Checkout(&git.CheckoutOptions{
Hash: commitHash,
})
if err != nil {
return fmt.Errorf("failed to checkout reference %s: %w", ref, err)
}
return nil
}