-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy patharchive.go
More file actions
67 lines (58 loc) · 1.55 KB
/
Copy patharchive.go
File metadata and controls
67 lines (58 loc) · 1.55 KB
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
package git
import (
"context"
"errors"
"io"
"github.com/go-git/go-git/v6/plumbing/transport"
)
// ArchiveRemote creates an archive from a remote repository.
// It returns an io.ReadCloser that yields the archive data.
// The caller must close the returned ReadCloser.
func ArchiveRemote(url string, o *ArchiveOptions) (io.ReadCloser, error) {
return ArchiveRemoteContext(context.Background(), url, o)
}
// ArchiveRemoteContext creates an archive from a remote repository.
// The provided Context can be used to cancel the operation.
func ArchiveRemoteContext(ctx context.Context, url string, o *ArchiveOptions) (io.ReadCloser, error) {
if o == nil {
o = &ArchiveOptions{}
}
if err := o.Validate(); err != nil {
return nil, err
}
if url == "" {
return nil, errors.New("remote URL is required")
}
cl, req, err := newClient(url, o.ClientOptions)
if err != nil {
return nil, err
}
req.Command = transport.UploadArchiveService
sess, err := cl.Handshake(ctx, req)
if err != nil {
return nil, err
}
// Check if session implements Archiver
arch, ok := sess.(transport.Archiver)
if !ok {
_ = sess.Close()
return nil, transport.ErrArchiveUnsupported
}
// Build arguments
var args []string
if o.Format != "" {
args = append(args, "--format="+o.Format)
}
if o.Prefix != "" {
args = append(args, "--prefix="+o.Prefix)
}
args = append(args, o.Treeish)
if len(o.Paths) > 0 {
args = append(args, "--")
args = append(args, o.Paths...)
}
return arch.Archive(ctx, &transport.ArchiveRequest{
Args: args,
Progress: o.Progress,
})
}