Skip to content

Commit 6630759

Browse files
committed
release: Support alpha, beta, and RC releases
1 parent 596517a commit 6630759

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

‎commands/release.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ func newReleaseCommand() simplecobra.Commander {
2828
step int
2929
skipPush bool
3030
try bool
31+
version string
3132
)
3233

3334
return &simpleCommand{
3435
name: "release",
3536
short: "Release a new version of Hugo",
3637
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
37-
rel, err := releaser.New(skipPush, try, step)
38+
rel, err := releaser.New(skipPush, try, step, version)
3839
if err != nil {
3940
return err
4041
}
@@ -47,6 +48,7 @@ func newReleaseCommand() simplecobra.Commander {
4748
cmd.PersistentFlags().BoolVarP(&skipPush, "skip-push", "", false, "skip pushing to remote")
4849
cmd.PersistentFlags().BoolVarP(&try, "try", "", false, "no changes")
4950
cmd.PersistentFlags().IntVarP(&step, "step", "", 0, "step to run (1: set new version 2: prepare next dev version)")
51+
cmd.PersistentFlags().StringVarP(&version, "version", "", "", "version to release (derived from branch name if not set)")
5052
_ = cmd.RegisterFlagCompletionFunc("step", cobra.FixedCompletions([]string{"1", "2"}, cobra.ShellCompDirectiveNoFileComp))
5153
},
5254
}

‎common/version/version.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ var (
4444
_ compare.Comparer = (*VersionString)(nil)
4545
)
4646

47+
// IsAlphaBetaOrRC returns whether this version is an alpha, beta, or release candidate.
48+
func (v Version) IsAlphaBetaOrRC() bool {
49+
s := strings.ToLower(v.Suffix)
50+
// e.g. "alpha.1", "beta.2", "rc.3"
51+
return strings.Contains(s, "alpha.") || strings.Contains(s, "beta.") || strings.Contains(s, "rc.")
52+
}
53+
4754
func (v Version) String() string {
4855
return version(v.Major, v.Minor, v.PatchLevel, v.Suffix)
4956
}

‎releaser/releaser.go‎

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,32 @@ import (
3030
const commitPrefix = "releaser:"
3131

3232
// New initializes a ReleaseHandler.
33-
func New(skipPush, try bool, step int) (*ReleaseHandler, error) {
33+
// Note that version is only used for testig. In CI we derive the version from the branch name.
34+
func New(skipPush, try bool, step int, version string) (*ReleaseHandler, error) {
3435
if step < 1 || step > 2 {
3536
return nil, fmt.Errorf("step must be 1 or 2")
3637
}
3738

38-
prefix := "release-"
39-
branch, err := git("rev-parse", "--abbrev-ref", "HEAD")
40-
if err != nil {
41-
return nil, err
42-
}
43-
branch = strings.TrimSpace(branch)
39+
if version == "" {
40+
prefix := "release-"
41+
branch, err := git("rev-parse", "--abbrev-ref", "HEAD")
42+
if err != nil {
43+
return nil, err
44+
}
45+
branch = strings.TrimSpace(branch)
46+
47+
if !strings.HasPrefix(branch, prefix) {
48+
return nil, fmt.Errorf("branch %q is not a release branch", branch)
49+
}
4450

45-
if !strings.HasPrefix(branch, prefix) {
46-
return nil, fmt.Errorf("branch %q is not a release branch", branch)
51+
version = strings.TrimPrefix(branch, prefix)
4752
}
4853

49-
version := strings.TrimPrefix(branch, prefix)
5054
version = strings.TrimPrefix(version, "v")
5155

52-
logf("Branch: %s|Version: v%s\n", branch, version)
56+
logf("Version: v%s\n", version)
5357

54-
rh := &ReleaseHandler{branchVersion: version, skipPush: skipPush, try: try, step: step}
58+
rh := &ReleaseHandler{version: version, skipPush: skipPush, try: try, step: step}
5559

5660
if try {
5761
rh.git = func(args ...string) (string, error) {
@@ -70,7 +74,7 @@ func New(skipPush, try bool, step int) (*ReleaseHandler, error) {
7074
// go run -tags release main.go release --skip-publish --try -r 0.90.0
7175
// Or a variation of the above -- the skip-publish flag makes sure that any changes are performed to the local Git only.
7276
type ReleaseHandler struct {
73-
branchVersion string
77+
version string
7478

7579
// 1 or 2.
7680
step int
@@ -89,8 +93,8 @@ func (r *ReleaseHandler) Run() error {
8993
newVersion, finalVersion := r.calculateVersions()
9094
version := newVersion.String()
9195
tag := "v" + version
92-
mainVersion := newVersion
93-
mainVersion.PatchLevel = 0
96+
97+
logf("New version %q (prerelease: %t), final version %q\n", newVersion, newVersion.IsAlphaBetaOrRC(), finalVersion)
9498

9599
r.gitPull()
96100

@@ -167,14 +171,15 @@ func (r *ReleaseHandler) bumpVersions(ver version.Version) error {
167171
}
168172

169173
func (r ReleaseHandler) calculateVersions() (version.Version, version.Version) {
170-
newVersion := version.MustParseVersion(r.branchVersion)
171-
finalVersion := newVersion.Next()
172-
finalVersion.PatchLevel = 0
173-
174-
if newVersion.Suffix != "-test" {
175-
newVersion.Suffix = ""
174+
newVersion := version.MustParseVersion(r.version)
175+
var finalVersion version.Version
176+
if newVersion.IsAlphaBetaOrRC() {
177+
finalVersion = newVersion
178+
} else {
179+
finalVersion = newVersion.Next()
176180
}
177181

182+
finalVersion.PatchLevel = 0
178183
finalVersion.Suffix = "-DEV"
179184

180185
return newVersion, finalVersion

0 commit comments

Comments
 (0)