-
Notifications
You must be signed in to change notification settings - Fork 128
/
repo_remote.go
423 lines (370 loc) · 12.5 KB
/
repo_remote.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2019 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"bytes"
"strings"
"time"
)
// LsRemoteOptions contains arguments for listing references in a remote
// repository.
//
// Docs: https://git-scm.com/docs/git-ls-remote
type LsRemoteOptions struct {
// Indicates whether include heads.
Heads bool
// Indicates whether include tags.
Tags bool
// Indicates whether to not show peeled tags or pseudo refs.
Refs bool
// The list of patterns to filter results.
Patterns []string
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// LsRemote returns a list references in the remote repository.
func LsRemote(url string, opts ...LsRemoteOptions) ([]*Reference, error) {
var opt LsRemoteOptions
if len(opts) > 0 {
opt = opts[0]
}
cmd := NewCommand("ls-remote", "--quiet").AddOptions(opt.CommandOptions)
if opt.Heads {
cmd.AddArgs("--heads")
}
if opt.Tags {
cmd.AddArgs("--tags")
}
if opt.Refs {
cmd.AddArgs("--refs")
}
cmd.AddArgs(url)
if len(opt.Patterns) > 0 {
cmd.AddArgs(opt.Patterns...)
}
stdout, err := cmd.RunWithTimeout(opt.Timeout)
if err != nil {
return nil, err
}
lines := bytes.Split(stdout, []byte("\n"))
refs := make([]*Reference, 0, len(lines))
for i := range lines {
fields := bytes.Fields(lines[i])
if len(fields) < 2 {
continue
}
refs = append(refs, &Reference{
ID: string(fields[0]),
Refspec: string(fields[1]),
})
}
return refs, nil
}
// IsURLAccessible returns true if given remote URL is accessible via Git within
// given timeout.
func IsURLAccessible(timeout time.Duration, url string) bool {
_, err := LsRemote(url, LsRemoteOptions{
Patterns: []string{"HEAD"},
Timeout: timeout,
})
return err == nil
}
// RemoteAddOptions contains options to add a remote address.
//
// Docs: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emaddem
type RemoteAddOptions struct {
// Indicates whether to execute git fetch after the remote information is set
// up.
Fetch bool
// Indicates whether to add remote as mirror with --mirror=fetch.
MirrorFetch bool
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// Deprecated: Use RemoteAddOptions instead.
type AddRemoteOptions = RemoteAddOptions
// RemoteAdd adds a new remote to the repository in given path.
func RemoteAdd(repoPath, name, url string, opts ...RemoteAddOptions) error {
var opt RemoteAddOptions
if len(opts) > 0 {
opt = opts[0]
}
cmd := NewCommand("remote", "add").AddOptions(opt.CommandOptions)
if opt.Fetch {
cmd.AddArgs("-f")
}
if opt.MirrorFetch {
cmd.AddArgs("--mirror=fetch")
}
_, err := cmd.AddArgs(name, url).RunInDirWithTimeout(opt.Timeout, repoPath)
return err
}
// Deprecated: Use RemoteAdd instead.
func RepoAddRemote(repoPath, name, url string, opts ...RemoteAddOptions) error {
return RemoteAdd(repoPath, name, url, opts...)
}
// RemoteAdd adds a new remote to the repository.
func (r *Repository) RemoteAdd(name, url string, opts ...RemoteAddOptions) error {
return RemoteAdd(r.path, name, url, opts...)
}
// Deprecated: Use RemoteAdd instead.
func (r *Repository) AddRemote(name, url string, opts ...RemoteAddOptions) error {
return RemoteAdd(r.path, name, url, opts...)
}
// RemoteRemoveOptions contains arguments for removing a remote from the
// repository.
//
// Docs: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emremoveem
type RemoteRemoveOptions struct {
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// Deprecated: Use RemoteRemoveOptions instead.
type RemoveRemoteOptions = RemoteRemoveOptions
// RemoteRemove removes a remote from the repository in given path.
func RemoteRemove(repoPath, name string, opts ...RemoteRemoveOptions) error {
var opt RemoteRemoveOptions
if len(opts) > 0 {
opt = opts[0]
}
_, err := NewCommand("remote", "remove").
AddOptions(opt.CommandOptions).
AddArgs(name).
RunInDirWithTimeout(opt.Timeout, repoPath)
if err != nil {
// the error status may differ from git clients
if strings.Contains(err.Error(), "error: No such remote") ||
strings.Contains(err.Error(), "fatal: No such remote") {
return ErrRemoteNotExist
}
return err
}
return nil
}
// Deprecated: Use RemoteRemove instead.
func RepoRemoveRemote(repoPath, name string, opts ...RemoteRemoveOptions) error {
return RemoteRemove(repoPath, name, opts...)
}
// RemoteRemove removes a remote from the repository.
func (r *Repository) RemoteRemove(name string, opts ...RemoteRemoveOptions) error {
return RemoteRemove(r.path, name, opts...)
}
// Deprecated: Use RemoteRemove instead.
func (r *Repository) RemoveRemote(name string, opts ...RemoteRemoveOptions) error {
return RemoteRemove(r.path, name, opts...)
}
// RemotesOptions contains arguments for listing remotes of the repository.
// /
// Docs: https://git-scm.com/docs/git-remote#_commands
type RemotesOptions struct {
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// Remotes lists remotes of the repository in given path.
func Remotes(repoPath string, opts ...RemotesOptions) ([]string, error) {
var opt RemotesOptions
if len(opts) > 0 {
opt = opts[0]
}
stdout, err := NewCommand("remote").
AddOptions(opt.CommandOptions).
RunInDirWithTimeout(opt.Timeout, repoPath)
if err != nil {
return nil, err
}
return bytesToStrings(stdout), nil
}
// Remotes lists remotes of the repository.
func (r *Repository) Remotes(opts ...RemotesOptions) ([]string, error) {
return Remotes(r.path, opts...)
}
// RemoteGetURLOptions contains arguments for retrieving URL(s) of a remote of
// the repository.
//
// Docs: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emget-urlem
type RemoteGetURLOptions struct {
// Indicates whether to get push URLs instead of fetch URLs.
Push bool
// Indicates whether to get all URLs, including lists that are not part of main
// URLs. This option is independent of the Push option.
All bool
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// RemoteGetURL retrieves URL(s) of a remote of the repository in given path.
func RemoteGetURL(repoPath, name string, opts ...RemoteGetURLOptions) ([]string, error) {
var opt RemoteGetURLOptions
if len(opts) > 0 {
opt = opts[0]
}
cmd := NewCommand("remote", "get-url").AddOptions(opt.CommandOptions)
if opt.Push {
cmd.AddArgs("--push")
}
if opt.All {
cmd.AddArgs("--all")
}
stdout, err := cmd.AddArgs(name).RunInDirWithTimeout(opt.Timeout, repoPath)
if err != nil {
return nil, err
}
return bytesToStrings(stdout), nil
}
// RemoteGetURL retrieves URL(s) of a remote of the repository in given path.
func (r *Repository) RemoteGetURL(name string, opts ...RemoteGetURLOptions) ([]string, error) {
return RemoteGetURL(r.path, name, opts...)
}
// RemoteSetURLOptions contains arguments for setting an URL of a remote of the
// repository.
//
// Docs: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emset-urlem
type RemoteSetURLOptions struct {
// Indicates whether to get push URLs instead of fetch URLs.
Push bool
// The regex to match existing URLs to replace (instead of first).
Regex string
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// RemoteSetURL sets first URL of the remote with given name of the repository
// in given path.
func RemoteSetURL(repoPath, name, newurl string, opts ...RemoteSetURLOptions) error {
var opt RemoteSetURLOptions
if len(opts) > 0 {
opt = opts[0]
}
cmd := NewCommand("remote", "set-url").AddOptions(opt.CommandOptions)
if opt.Push {
cmd.AddArgs("--push")
}
cmd.AddArgs(name, newurl)
if opt.Regex != "" {
cmd.AddArgs(opt.Regex)
}
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
if err != nil {
if strings.Contains(err.Error(), "No such URL found") {
return ErrURLNotExist
} else if strings.Contains(err.Error(), "No such remote") {
return ErrRemoteNotExist
}
return err
}
return nil
}
// RemoteSetURL sets the first URL of the remote with given name of the
// repository.
func (r *Repository) RemoteSetURL(name, newurl string, opts ...RemoteSetURLOptions) error {
return RemoteSetURL(r.path, name, newurl, opts...)
}
// RemoteSetURLAddOptions contains arguments for appending an URL to a remote
// of the repository.
//
// Docs: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emset-urlem
type RemoteSetURLAddOptions struct {
// Indicates whether to get push URLs instead of fetch URLs.
Push bool
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// RemoteSetURLAdd appends an URL to the remote with given name of the
// repository in given path. Use RemoteSetURL to overwrite the URL(s) instead.
func RemoteSetURLAdd(repoPath, name, newurl string, opts ...RemoteSetURLAddOptions) error {
var opt RemoteSetURLAddOptions
if len(opts) > 0 {
opt = opts[0]
}
cmd := NewCommand("remote", "set-url").
AddOptions(opt.CommandOptions).
AddArgs("--add")
if opt.Push {
cmd.AddArgs("--push")
}
cmd.AddArgs(name, newurl)
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
if err != nil && strings.Contains(err.Error(), "Will not delete all non-push URLs") {
return ErrNotDeleteNonPushURLs
}
return err
}
// RemoteSetURLAdd appends an URL to the remote with given name of the
// repository. Use RemoteSetURL to overwrite the URL(s) instead.
func (r *Repository) RemoteSetURLAdd(name, newurl string, opts ...RemoteSetURLAddOptions) error {
return RemoteSetURLAdd(r.path, name, newurl, opts...)
}
// RemoteSetURLDeleteOptions contains arguments for deleting an URL of a remote
// of the repository.
//
// Docs: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emset-urlem
type RemoteSetURLDeleteOptions struct {
// Indicates whether to get push URLs instead of fetch URLs.
Push bool
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
//
// Deprecated: Use CommandOptions.Timeout instead.
Timeout time.Duration
// The additional options to be passed to the underlying git.
CommandOptions
}
// RemoteSetURLDelete deletes the remote with given name of the repository in
// given path.
func RemoteSetURLDelete(repoPath, name, regex string, opts ...RemoteSetURLDeleteOptions) error {
var opt RemoteSetURLDeleteOptions
if len(opts) > 0 {
opt = opts[0]
}
cmd := NewCommand("remote", "set-url").
AddOptions(opt.CommandOptions).
AddArgs("--delete")
if opt.Push {
cmd.AddArgs("--push")
}
cmd.AddArgs(name, regex)
_, err := cmd.RunInDirWithTimeout(opt.Timeout, repoPath)
if err != nil && strings.Contains(err.Error(), "Will not delete all non-push URLs") {
return ErrNotDeleteNonPushURLs
}
return err
}
// RemoteSetURLDelete deletes all URLs matching regex of the remote with given
// name of the repository.
func (r *Repository) RemoteSetURLDelete(name, regex string, opts ...RemoteSetURLDeleteOptions) error {
return RemoteSetURLDelete(r.path, name, regex, opts...)
}