-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
162 lines (134 loc) · 4.92 KB
/
config.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
package gotagger
import (
"encoding/json"
"fmt"
"github.com/sassoftware/gotagger/mapper"
)
type config struct {
DefaultIncrement string `json:"defaultIncrement"`
IncrementDirtyWorktree string `json:"incrementDirtyWorktree"`
ExcludeModules []string `json:"excludeModules"`
IgnoreModules bool `json:"ignoreModules"`
IncrementMappings map[string]string `json:"incrementMappings"`
IncrementPreReleaseMinor bool `json:"incrementPreReleaseMinor"`
VersionPrefix *string `json:"versionPrefix"`
}
// Config represents how to tag a repo.
//
// If no default is mentioned, the option defaults to go's zero-value.
type Config struct {
// CreateTag represents whether to create the tag.
CreateTag bool
// ExcludeModules is a list of module names or paths to exclude.
ExcludeModules []string
// IgnoreModules controls whether gotagger will ignore the existence of
// go.mod files when determining how to version a project.
IgnoreModules bool
// RemoteName represents the name of the remote repository. Defaults to origin.
RemoteName string
// PreMajor controls whether gotagger will increase the major version from 0
// to 1 for breaking changes.
PreMajor bool
// PushTag represents whether to push the tag to the remote git repository.
PushTag bool
// VersionPrefix is a string that will be added to the front of the version. Defaults to 'v'.
VersionPrefix string
// DirtyWorktreeIncrement is a string that sets how to increment the version
// if there are no new commits, but the worktree is "dirty".
DirtyWorktreeIncrement mapper.Increment
// CommitTypeTable used for looking up version increments based on the commit type.
CommitTypeTable mapper.Table
// Force controls whether gotagger will create a tag even if HEAD is not a "release" commit.
Force bool
// Paths is a list of sub-paths within the repo to restrict the git
// history used to calculate a version. The versions returned will be
// prefixed with their path.
Paths []string
/* TODO
// PreRelease is the string that will be used to generate pre-release versions. The
// string may be a Golang text template. Valid arguments are:
//
// - .CommitsSince
// The number of commits since the previous release.
PreRelease string
*/
}
// ParseJSON unmarshals a byte slice containing mappings of commit type to semver increment. Mappings determine
// how much to increment the semver based on the commit type. The 'release' commit type has special meaning to gotagger
// and cannot be overridden in the config file. Unknown commit types will fall back to the config default.
// Invalid increments will throw an error. Duplicate type definitions will take the last entry.
func (c *Config) ParseJSON(data []byte) error {
// unmarshal our private struct
cfg := config{
IncrementMappings: make(map[string]string),
}
if err := json.Unmarshal(data, &cfg); err != nil {
return err
}
// validate dirty worktree increment
inc, err := mapper.Convert(cfg.IncrementDirtyWorktree)
switch {
case err != nil:
return fmt.Errorf("invalid dirty worktree increment: %s", cfg.IncrementDirtyWorktree)
case inc == mapper.IncrementMajor:
return fmt.Errorf("major version increments are not allowed for dirty worktrees")
default:
c.DirtyWorktreeIncrement = inc
}
// version prefix is a pointer
// so the config file can set it to ""
// and we can preserve the default of "v"
if cfg.VersionPrefix != nil {
c.VersionPrefix = *cfg.VersionPrefix
}
// we do not allow configuring the release type,
// as it means something particular to gotagger
if _, ok := cfg.IncrementMappings["release"]; ok {
return fmt.Errorf("release mapping is not allowed")
}
// generate the commit type table from the parsed mappings
var table mapper.Mapper
for typ, inc := range cfg.IncrementMappings {
conversion, err := mapper.Convert(inc)
if err != nil {
return err
}
if conversion == mapper.IncrementMajor {
return fmt.Errorf("major version increments cannot be mapped to commit types. use the commit spec directives for this")
}
if table == nil {
table = make(mapper.Mapper)
}
table[typ] = conversion
continue
}
// default increment to patch
if cfg.DefaultIncrement == "" {
cfg.DefaultIncrement = "patch"
}
def, err := mapper.Convert(cfg.DefaultIncrement)
if err != nil {
return err
}
c.CommitTypeTable = mapper.NewTable(table, def)
// copy over static values
c.ExcludeModules = cfg.ExcludeModules
c.IgnoreModules = cfg.IgnoreModules
c.PreMajor = cfg.IncrementPreReleaseMinor
return nil
}
// NewDefaultConfig returns a Config with default options set.
//
// If an option is not mentioned, then the default is the zero-value for its type.
//
// - RemoteName
// origin
// - VersionPrefix
// v
func NewDefaultConfig() Config {
return Config{
CommitTypeTable: mapper.NewTable(nil, mapper.IncrementPatch),
RemoteName: "origin",
VersionPrefix: "v",
}
}