generated from axetroy/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
7_whatchanged.go
158 lines (122 loc) · 2.93 KB
/
7_whatchanged.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
package whatchanged
import (
"context"
"io"
"net/url"
"os"
"path/filepath"
"regexp"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/pkg/errors"
"github.com/release-lab/whatchanged/internal/client"
)
var (
gitHTTPURLReg = regexp.MustCompile(`^https?:\/\/.+$`)
gitSSHURLReg = regexp.MustCompile(`^git@.+$`)
)
// generate changelog
// project: it could be a file path or a git URL
func Generate(ctx context.Context, project string, w io.Writer, options *Options) error {
var (
err error
)
if options == nil {
options = &Options{
Format: FormatMarkdown,
Preset: PresetDefault,
}
} else {
if options.Format == "" {
options.Format = FormatMarkdown
}
if options.Preset == "" {
options.Preset = PresetDefault
}
}
if options.Branch == "" {
options.Branch = "master"
}
if options.TemplateFile != "" {
cwd, err := os.Getwd()
if err != nil {
return errors.WithStack(err)
}
if !filepath.IsAbs(options.TemplateFile) {
options.TemplateFile = filepath.Join(cwd, options.TemplateFile)
}
}
var g *client.GitClient
if gitHTTPURLReg.MatchString(project) || gitSSHURLReg.MatchString(project) {
cloneOptions := git.CloneOptions{
URL: project,
Progress: os.Stderr,
SingleBranch: true,
ReferenceName: plumbing.NewBranchReferenceName(options.Branch),
Tags: git.AllTags,
}
if gitHTTPURLReg.MatchString(project) {
u, err := url.Parse(project)
if err != nil {
return errors.WithStack(err)
}
// if url has contains the user infomation
if u.User != nil {
auth := &http.BasicAuth{
Username: u.User.Username(),
}
if pwd, isSetPwd := u.User.Password(); isSetPwd {
auth.Password = pwd
}
cloneOptions.Auth = auth
}
}
repo, err := git.CloneContext(ctx, memory.NewStorage(), nil, &cloneOptions)
if err != nil {
return errors.WithStack(err)
}
g = &client.GitClient{
Repository: repo,
}
} else {
g, err = client.NewGitClient(project)
if err != nil {
return errors.WithStack(err)
}
}
scope, err := Parse(g, options.Version)
if err != nil {
return errors.WithStack(err)
}
splices, err := Extract(g, scope)
if err != nil {
return errors.WithStack(err)
}
ctxs, err := Transform(g, splices)
if err != nil {
return errors.WithStack(err)
}
output, err := GenerateFromContext(g, ctxs, options.Format, options.Preset, options.TemplateFile, options.Template)
if err != nil {
return errors.WithStack(err)
}
if options.SkipFormat {
_, err = Write(output, w)
if err != nil {
return errors.WithStack(err)
}
return nil
} else {
formattedOutput, err := Format(output, options.Format)
if err != nil {
return errors.WithStack(err)
}
_, err = Write(formattedOutput, w)
if err != nil {
return errors.WithStack(err)
}
return nil
}
}