-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.go
165 lines (148 loc) · 3.73 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
package git
import (
"archive/tar"
"compress/gzip"
"context"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport"
gogitHttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v49/github"
"go.f110.dev/xerrors"
"go.f110.dev/mono/go/githubutil"
)
func Clone(ctx context.Context, appId, installationId int64, privateKeyFile, dir, repo, commit string) error {
var auth *gogitHttp.BasicAuth
rt := http.DefaultTransport
if _, err := os.Stat(privateKeyFile); !os.IsNotExist(err) {
app, err := githubutil.NewApp(appId, installationId, privateKeyFile)
if err != nil {
return err
}
token, err := app.JWT()
if err != nil {
return err
}
auth = &gogitHttp.BasicAuth{Username: "octocat", Password: token}
rt = githubutil.NewTransportWithApp(http.DefaultTransport, app)
}
archiveDownloadable := false
u, err := url.Parse(repo)
if err == nil {
if u.Scheme == "https" && u.Hostname() == "github.com" {
archiveDownloadable = true
}
}
if commit != "" && archiveDownloadable {
return checkoutCommit(ctx, dir, repo, commit, rt)
} else {
return cloneByGit(ctx, dir, repo, commit, 1, auth)
}
}
func checkoutCommit(ctx context.Context, dir, u, commit string, rt http.RoundTripper) error {
addr := u
if strings.HasSuffix(u, ".git") {
addr = strings.TrimSuffix(u, ".git")
}
parsed, err := url.Parse(addr)
if err != nil {
return xerrors.WithStack(err)
}
s := strings.SplitN(parsed.Path, "/", 3)
ghClient := github.NewClient(&http.Client{Transport: rt})
archiveLink, _, err := ghClient.Repositories.GetArchiveLink(
ctx,
s[1], // owner
s[2], // repo
github.Tarball,
&github.RepositoryContentGetOptions{Ref: commit},
true,
)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, archiveLink.String(), nil)
if err != nil {
return xerrors.WithStack(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return xerrors.WithStack(err)
}
defer res.Body.Close()
gzReader, err := gzip.NewReader(res.Body)
if err != nil {
return xerrors.WithStack(err)
}
tarReader := tar.NewReader(gzReader)
for {
h, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return xerrors.WithStack(err)
}
d, f := filepath.Split(h.Name)
if d == "" {
continue
}
s := strings.Split(d, "/")
filename := filepath.Join(dir, strings.Join(s[1:], "/"), f)
switch h.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(filename, os.FileMode(h.Mode)); err != nil {
return xerrors.WithStack(err)
}
continue
case tar.TypeSymlink:
if err := os.Symlink(h.Linkname, filename); err != nil {
return xerrors.WithStack(err)
}
continue
}
b, err := io.ReadAll(tarReader)
if err != nil {
return xerrors.WithStack(err)
}
dirname := filepath.Dir(filename)
if _, err := os.Stat(dirname); os.IsNotExist(err) {
if err := os.MkdirAll(dirname, 755); err != nil {
return xerrors.WithStack(err)
}
}
if err := os.WriteFile(filename, b, os.FileMode(h.Mode)); err != nil {
return xerrors.WithStack(err)
}
}
return nil
}
func cloneByGit(ctx context.Context, dir, repo, commit string, depth int, auth transport.AuthMethod) error {
if commit != "" {
depth = 0
}
r, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
URL: repo,
Depth: depth,
Auth: auth,
})
if err != nil {
return xerrors.WithStack(err)
}
if commit != "" {
tree, err := r.Worktree()
if err != nil {
return xerrors.WithStack(err)
}
if err := tree.Checkout(&git.CheckoutOptions{Hash: plumbing.NewHash(commit)}); err != nil {
return xerrors.WithStack(err)
}
}
return nil
}