-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrepository.go
215 lines (186 loc) · 4.35 KB
/
repository.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
package caddy_webhook
import (
"context"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage/memory"
"go.uber.org/zap"
)
const (
DefaultRemote = "origin"
DefaultBranch = "main"
)
// Repo tells information about the git repository.
type Repo struct {
URL string
Path string
Branch string
Depth int
Secret string
Submodule git.SubmoduleRescursivity
Auth transport.AuthMethod
repo *git.Repository
log *zap.Logger
cmd *Cmd
refName plumbing.ReferenceName
}
// NewRepo creates a new repo with options.
func NewRepo(w *WebHook) *Repo {
r := &Repo{
URL: w.Repository,
Path: w.Path,
Branch: w.Branch,
Depth: w.depth,
Secret: w.Secret,
Auth: w.auth,
cmd: w.cmd,
log: w.log,
}
return r
}
// Setup initializes the git repository by either cloning or opening it.
func (r *Repo) Setup(ctx context.Context) error {
var err error
r.log.Info("setting up repository", zap.String("path", r.Path))
err = r.setRef(ctx)
if err != nil {
return err
}
r.repo, err = git.PlainOpen(r.Path)
if err == nil {
// If the path directory is a git repository, set up the remote as 'origin'
err = r.repo.DeleteRemote(DefaultRemote)
if err != nil && err != git.ErrRemoteNotFound {
return err
}
_, err = r.repo.CreateRemote(&config.RemoteConfig{
Name: DefaultRemote,
URLs: []string{r.URL},
})
if err != nil {
return err
}
err = r.fetch(ctx)
if err != nil {
return err
}
err = r.checkout(r.refName)
if err != nil {
return err
}
} else if err == git.ErrRepositoryNotExists {
// If the path directory is not a git repository, clone it from url.
r.repo, err = git.PlainCloneContext(ctx, r.Path, false, &git.CloneOptions{
URL: r.URL,
Auth: r.Auth,
RemoteName: DefaultRemote,
ReferenceName: r.refName,
Depth: r.Depth,
RecurseSubmodules: r.Submodule,
Tags: git.AllTags,
})
if err != nil {
return err
}
} else {
return err
}
r.log.Info("setting up repository successful")
if r.cmd != nil {
go r.cmd.Run(r.log)
}
return nil
}
// Update pulls updates from the remote repository into current worktree.
func (r *Repo) Update(ctx context.Context) error {
var err error
if r.refName.IsBranch() {
err = r.pull(ctx)
}
if r.cmd != nil {
go r.cmd.Run(r.log)
}
if err != nil {
return err
}
return nil
}
func (r *Repo) fetch(ctx context.Context) error {
if err := r.repo.FetchContext(ctx, &git.FetchOptions{
RemoteName: DefaultRemote,
Depth: r.Depth,
Auth: r.Auth,
Tags: git.AllTags,
}); err != nil && err != git.NoErrAlreadyUpToDate {
return err
}
return nil
}
func (r *Repo) pull(ctx context.Context) error {
worktree, err := r.repo.Worktree()
if err != nil {
return err
}
if err := worktree.PullContext(ctx, &git.PullOptions{
RemoteName: DefaultRemote,
ReferenceName: r.refName,
Depth: r.Depth,
Auth: r.Auth,
}); err != nil {
return err
}
return nil
}
func (r *Repo) checkout(ref plumbing.ReferenceName) error {
worktree, err := r.repo.Worktree()
if err != nil {
return err
}
if err := worktree.Checkout(&git.CheckoutOptions{Branch: ref}); err != nil {
return err
}
return nil
}
func (r *Repo) setRef(ctx context.Context) error {
remote := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: DefaultRemote,
URLs: []string{r.URL},
})
if err := remote.FetchContext(ctx, &git.FetchOptions{
RemoteName: DefaultRemote,
Depth: r.Depth,
Auth: r.Auth,
Tags: git.AllTags,
}); err != nil && err != git.NoErrAlreadyUpToDate {
return err
}
refs, err := remote.List(&git.ListOptions{
Auth: r.Auth,
})
if err != nil {
return err
}
if r.Branch == "" {
r.refName = plumbing.NewBranchReferenceName(DefaultBranch)
} else {
branchRef := plumbing.NewBranchReferenceName(r.Branch)
tagRef := plumbing.NewTagReferenceName(r.Branch)
for _, ref := range refs {
if ref.Name() == branchRef {
r.refName = branchRef
break
}
if ref.Name() == tagRef {
r.refName = tagRef
break
}
}
if r.refName == plumbing.ReferenceName("") {
return fmt.Errorf("reference with name '%s' not found", r.Branch)
}
}
return nil
}