Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion commands/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ func newReleaseCommand() simplecobra.Commander {
step int
skipPush bool
try bool
version string
)

return &simpleCommand{
name: "release",
short: "Release a new version of Hugo",
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
rel, err := releaser.New(skipPush, try, step)
rel, err := releaser.New(skipPush, try, step, version)
if err != nil {
return err
}
Expand All @@ -47,6 +48,7 @@ func newReleaseCommand() simplecobra.Commander {
cmd.PersistentFlags().BoolVarP(&skipPush, "skip-push", "", false, "skip pushing to remote")
cmd.PersistentFlags().BoolVarP(&try, "try", "", false, "no changes")
cmd.PersistentFlags().IntVarP(&step, "step", "", 0, "step to run (1: set new version 2: prepare next dev version)")
cmd.PersistentFlags().StringVarP(&version, "version", "", "", "version to release (derived from branch name if not set)")
_ = cmd.RegisterFlagCompletionFunc("step", cobra.FixedCompletions([]string{"1", "2"}, cobra.ShellCompDirectiveNoFileComp))
},
}
Expand Down
7 changes: 7 additions & 0 deletions common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ var (
_ compare.Comparer = (*VersionString)(nil)
)

// IsAlphaBetaOrRC returns whether this version is an alpha, beta, or release candidate.
func (v Version) IsAlphaBetaOrRC() bool {
s := strings.ToLower(v.Suffix)
// e.g. "alpha.1", "beta.2", "rc.3"
return strings.Contains(s, "alpha.") || strings.Contains(s, "beta.") || strings.Contains(s, "rc.")
}

func (v Version) String() string {
return version(v.Major, v.Minor, v.PatchLevel, v.Suffix)
}
Expand Down
47 changes: 26 additions & 21 deletions releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,32 @@ import (
const commitPrefix = "releaser:"

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

prefix := "release-"
branch, err := git("rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return nil, err
}
branch = strings.TrimSpace(branch)
if version == "" {
prefix := "release-"
branch, err := git("rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return nil, err
}
branch = strings.TrimSpace(branch)

if !strings.HasPrefix(branch, prefix) {
return nil, fmt.Errorf("branch %q is not a release branch", branch)
}

if !strings.HasPrefix(branch, prefix) {
return nil, fmt.Errorf("branch %q is not a release branch", branch)
version = strings.TrimPrefix(branch, prefix)
}

version := strings.TrimPrefix(branch, prefix)
version = strings.TrimPrefix(version, "v")

logf("Branch: %s|Version: v%s\n", branch, version)
logf("Version: v%s\n", version)

rh := &ReleaseHandler{branchVersion: version, skipPush: skipPush, try: try, step: step}
rh := &ReleaseHandler{version: version, skipPush: skipPush, try: try, step: step}

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

// 1 or 2.
step int
Expand All @@ -89,8 +93,8 @@ func (r *ReleaseHandler) Run() error {
newVersion, finalVersion := r.calculateVersions()
version := newVersion.String()
tag := "v" + version
mainVersion := newVersion
mainVersion.PatchLevel = 0

logf("New version %q (prerelease: %t), final version %q\n", newVersion, newVersion.IsAlphaBetaOrRC(), finalVersion)

r.gitPull()

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

func (r ReleaseHandler) calculateVersions() (version.Version, version.Version) {
newVersion := version.MustParseVersion(r.branchVersion)
finalVersion := newVersion.Next()
finalVersion.PatchLevel = 0

if newVersion.Suffix != "-test" {
newVersion.Suffix = ""
newVersion := version.MustParseVersion(r.version)
var finalVersion version.Version
if newVersion.IsAlphaBetaOrRC() {
finalVersion = newVersion
} else {
finalVersion = newVersion.Next()
}

finalVersion.PatchLevel = 0
finalVersion.Suffix = "-DEV"

return newVersion, finalVersion
Expand Down