-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
363 lines (333 loc) 路 9.4 KB
/
project.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/mod/modfile"
"moul.io/multipmuri"
"moul.io/u"
)
type project struct {
Path string
Git struct {
Root string
MainBranch string
CurrentBranch string
OriginRemotes []string
InMainBranch bool
IsDirty *bool
CloneURL string
HTMLURL string
RepoName string
RepoOwner string
Metadata struct {
HasGo *bool `json:"HasGo,omitempty"`
HasDocker *bool `json:"HasDocker,omitempty"`
HasLibrary *bool `json:"HasLibrary,omitempty"`
HasBinary *bool `json:"HasBinary,omitempty"`
GoModPath string `json:"GoModPath,omitempty"`
} `json:"Metadata,omitempty"`
head *plumbing.Reference
repo *git.Repository
origin *git.Remote
workTree *git.Worktree
status git.Status
}
}
//nolint:nestif,gocognit
func projectFromPath(path string) (*project, error) {
abs, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("incorrect path: %q: %w", path, err)
}
path = abs
if !u.DirExists(path) {
return nil, fmt.Errorf("path is not a directory: %q", path) //nolint:goerr113
}
project := &project{Path: path}
project.Git.Root = gitFindRootDir(path)
if project.Git.Root != "" {
// open local repo
{
repo, err := git.PlainOpen(project.Git.Root)
if err != nil {
return nil, fmt.Errorf("failed to open git repo: %q: %w", project.Git.Root, err)
}
project.Git.repo = repo
}
// current branch
{
head, err := project.Git.repo.Head()
if err != nil {
return nil, fmt.Errorf("failed to get HEAD: %w", err)
}
project.Git.head = head
project.Git.CurrentBranch = project.Git.head.Name().Short()
}
// 'origin' remote
{
origin, err := project.Git.repo.Remote("origin")
if err != nil {
return nil, fmt.Errorf("failed to get 'origin' remote: %w", err)
}
project.Git.origin = origin
project.Git.OriginRemotes = origin.Config().URLs
project.Git.CloneURL = origin.Config().URLs[0]
ret, err := multipmuri.DecodeString(project.Git.CloneURL)
if err != nil {
return nil, fmt.Errorf("failed to parse the clone URL: %w", err)
}
repo := multipmuri.RepoEntity(ret)
if typed, ok := repo.(*multipmuri.GitHubRepo); ok {
project.Git.RepoName = typed.RepoID()
project.Git.RepoOwner = typed.OwnerID()
project.Git.HTMLURL = typed.String()
} else {
// FIXME: guess name
// FIXME: guess owner
panic("not implemented")
}
}
// main branch
{
logger.Debug("rep.Reference(refs/remotes/origin/HEAD)")
ref, err := project.Git.repo.Reference("refs/remotes/origin/HEAD", true)
if err == nil {
project.Git.MainBranch = strings.TrimPrefix(ref.Name().Short(), "origin/")
} else { // if it fails, we try to fetch origin and then we retry
logger.Debug("origin.List()")
refs, err := project.Git.origin.List(&git.ListOptions{})
if err != nil {
logger.Warn("failed to list origin refs", zap.Error(err))
project.Git.MainBranch = "n/a"
}
for _, ref := range refs {
if ref.Name() == "HEAD" {
project.Git.MainBranch = ref.Target().Short()
}
}
}
}
if project.Git.MainBranch != "n/a" && project.Git.MainBranch != "" && project.Git.CurrentBranch != "" {
project.Git.InMainBranch = project.Git.MainBranch == project.Git.CurrentBranch
}
// work tree
{
workTree, err := project.Git.repo.Worktree()
if err != nil {
return nil, fmt.Errorf("failed to get worktree: %w", err)
}
project.Git.workTree = workTree
}
// metadata
{
logger.Debug("guess metadata")
// guess it
if u.FileExists(filepath.Join(project.Path, "Dockerfile")) { // FIXME: look for other dockerfiles
project.Git.Metadata.HasDocker = u.BoolPtr(true)
project.Git.Metadata.HasBinary = u.BoolPtr(true)
} else {
project.Git.Metadata.HasDocker = u.BoolPtr(false)
if u.FileExists(filepath.Join(project.Path, "main.go")) { // FIXME: improve check
project.Git.Metadata.HasBinary = u.BoolPtr(true)
} else {
project.Git.Metadata.HasBinary = u.BoolPtr(false)
}
}
if u.FileExists(filepath.Join(project.Path, "go.mod")) {
project.Git.Metadata.HasGo = u.BoolPtr(true)
content, err := ioutil.ReadFile(filepath.Join(project.Path, "go.mod"))
if err != nil {
return nil, fmt.Errorf("read go.mod: %w", err)
}
project.Git.Metadata.GoModPath = modfile.ModulePath(content)
} else {
goFiles, err := filepath.Glob(filepath.Join(project.Path, "*.go")) // FIXME: recursive
if err != nil {
return nil, fmt.Errorf("glob: %w", err)
}
project.Git.Metadata.HasGo = u.BoolPtr(len(goFiles) > 0)
}
project.Git.Metadata.HasLibrary = u.BoolPtr(*project.Git.Metadata.HasGo && !*project.Git.Metadata.HasBinary)
// override it from metadata file
// FIXME: TODO
}
} else {
logger.Warn("project not within a git directory", zap.String("path", path))
}
return project, nil
}
func gitFindRootDir(path string) string {
for {
if u.DirExists(filepath.Join(path, ".git")) {
return path
}
parent := filepath.Dir(path)
if parent == path {
break
}
path = parent
}
return ""
}
func (p *project) updateStatus() error {
status, err := p.Git.workTree.Status()
if err != nil {
return fmt.Errorf("failed to get status: %w", err)
}
p.Git.status = status
p.Git.IsDirty = u.BoolPtr(!status.IsClean())
return nil
}
func (p *project) prepareWorkspace(opts projectOpts) error {
if p.Git.Root == "" {
return fmt.Errorf("not implemented: non-git projects")
}
// check if dirty
{
if p.Git.IsDirty == nil {
if err := p.updateStatus(); err != nil {
return fmt.Errorf("update status: %w", err)
}
}
if *p.Git.IsDirty && opts.Reset {
// reset
{
err := p.Git.workTree.Reset(&git.ResetOptions{
Mode: git.HardReset,
})
if err != nil {
return fmt.Errorf("reset worktree: %w", err)
}
}
if err := p.updateStatus(); err != nil {
return fmt.Errorf("update status: %w", err)
}
}
if *p.Git.IsDirty {
return fmt.Errorf("worktree is dirty, please commit or discard changes before retrying") //nolint:goerr113
}
}
if opts.Fetch {
logger.Debug("fetch origin", zap.String("project", p.Path))
err := p.Git.origin.Fetch(&git.FetchOptions{
Progress: os.Stderr,
})
switch err {
case git.NoErrAlreadyUpToDate:
// skip
case nil:
// skip
default:
return fmt.Errorf("failed to fetch origin: %w", err)
}
}
if opts.CheckoutMainBranch && !p.Git.InMainBranch {
logger.Debug("project is not using the main branch",
zap.String("current", p.Git.CurrentBranch),
zap.String("main", p.Git.MainBranch),
)
mainBranch, err := p.Git.repo.Branch(p.Git.MainBranch)
if err != nil {
return fmt.Errorf("failed to get ref for main branch: %q: %w", p.Git.MainBranch, err)
}
err = p.Git.workTree.Checkout(&git.CheckoutOptions{
Branch: mainBranch.Merge,
})
if err != nil {
return fmt.Errorf("failed to checkout main branch: %q: %w", p.Git.MainBranch, err)
}
err = p.Git.workTree.Pull(&git.PullOptions{})
switch err {
case git.NoErrAlreadyUpToDate: // skip
case nil: // skip
default:
return fmt.Errorf("failed to pull main branch: %q: %w", p.Git.MainBranch, err)
}
}
// check if the project looks like a one that can be maintained by repoman
{
var errs error
for _, expected := range []string{"Makefile", "rules.mk"} {
if !u.FileExists(filepath.Join(p.Path, expected)) {
errs = multierr.Append(errs, fmt.Errorf("missing file: %q", expected))
}
}
if errs != nil {
return fmt.Errorf("project is not compatible with repoman: %w", errs)
}
}
return nil
}
func (p *project) showDiff() error {
script := `
main() {
# apply changes
git diff
git diff --cached
git status
}
main
`
cmd := exec.Command("/bin/sh", "-xec", script)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
cmd.Dir = p.Path
cmd.Env = os.Environ()
err := cmd.Run()
if err != nil {
return fmt.Errorf("publish script execution failed: %w", err)
}
return nil
}
func (p *project) openPR(branchName string, title string) error {
logger.Debug("opening a PR", zap.String("branch", branchName), zap.String("title", title))
initMoulBotEnv()
script := `
main() {
# apply changes
git branch -D {{.branchName}} || true
git checkout -b {{.branchName}}
git commit -s -a -m {{.title}} -m {{.body}}
git push -u origin {{.branchName}} -f
hub pull-request -m {{.title}} -m {{.body}} || hub pr list -f "- %pC%>(8)%i%Creset %U - %t% l%n"
}
main
`
body := "more details: https://github.com/moul/repoman"
script = strings.ReplaceAll(script, "{{.branchName}}", fmt.Sprintf("%q", branchName))
script = strings.ReplaceAll(script, "{{.title}}", fmt.Sprintf("%q", title))
script = strings.ReplaceAll(script, "{{.body}}", fmt.Sprintf("%q", body))
cmd := exec.Command("/bin/sh", "-xec", script)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
cmd.Dir = p.Path
cmd.Env = os.Environ()
err := cmd.Run()
if err != nil {
return fmt.Errorf("publish script execution failed: %w", err)
}
return nil
}
func (p *project) pushChanges(opts projectOpts, branchName string, prTitle string) error {
if opts.ShowDiff {
err := p.showDiff()
if err != nil {
return fmt.Errorf("show diff: %w", err)
}
}
if opts.OpenPR {
err := p.openPR(branchName, prTitle)
if err != nil {
return fmt.Errorf("open PR: %w", err)
}
}
return nil
}