Skip to content

Commit

Permalink
Reverse engineer python pre version from original version
Browse files Browse the repository at this point in the history
Test by doing conversion in parallel with assert
  • Loading branch information
danielrbradley committed Dec 21, 2022
1 parent 917e816 commit 2218df7
Showing 1 changed file with 73 additions and 5 deletions.
78 changes: 73 additions & 5 deletions pkg/gitversion/gitversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -130,6 +131,14 @@ func GetLanguageVersionsWithOptions(opts LanguageVersionsOptions) (*LanguageVers
preVersion = fmt.Sprintf("%s%sdirty", preVersion, separator)
}

newPythonPreVersion, err := getPythonPreVersion(preVersion)
if err != nil {
return nil, err
}
if newPythonPreVersion != pythonPreVersion {
return nil, fmt.Errorf("expected %q found %q for %q", pythonPreVersion, newPythonPreVersion, preVersion)
}

// a base version with the pre release info
baseVersion := fmt.Sprintf("%d.%d.%d", genericVersion.Major, genericVersion.Minor, genericVersion.Patch)

Expand All @@ -147,6 +156,65 @@ func GetLanguageVersionsWithOptions(opts LanguageVersionsOptions) (*LanguageVers
}, nil
}

func getPythonPreVersion(preVersion string) (string, error) {
if preVersion == "" {
return preVersion, nil
}

prefix, rest := getPythonPrePrefix(preVersion)
// Find a number in the middle of non-words (- or .)
numRe := regexp.MustCompile(`\W(\d+)\W`)
nums := numRe.FindStringSubmatch(rest)
num := ""
// Our match group is in the 2nd array entry.
if len(nums) == 2 {
num = nums[1]
}

isDirty := strings.Contains(preVersion, "dirty")

// Python uses PEP440, but Pypi has some curiosities.
pythonPreVersion := ""

// PEP440 (https://peps.python.org/pep-0440/) says pre-release parts MUST have a number in them,
// but we want to support tags like `v1.0.0-alpha`. If no number is present add `0` to keep PEP440
// happy.
var pythonPreSuffix string
if num == "" {
pythonPreSuffix = "0"
} else {
// Trim the initial "."
pythonPreSuffix = num
}

if prefix != "" {
pythonPreVersion = fmt.Sprintf("%s%s", prefix, pythonPreSuffix)
}

// Detect if the git worktree is dirty, and add `dirty` to the version if it is
if isDirty {
pythonPreVersion = fmt.Sprintf("%s+dirty", pythonPreVersion)
}

return pythonPreVersion, nil
}

func getPythonPrePrefix(preVersion string) (string, string) {
if strings.HasPrefix(preVersion, "-dev") {
return "d", preVersion[4:]
}
if strings.HasPrefix(preVersion, "-alpha") {
return "a", preVersion[5:]
}
if strings.HasPrefix(preVersion, "-beta") {
return "b", preVersion[5:]
}
if strings.HasPrefix(preVersion, "-rc") {
return "rc", preVersion[3:]
}
return "", ""
}

// See GetLanguageVersionsWithOptions.
func GetLanguageVersions(repo *git.Repository, commitish plumbing.Revision, omitCommitHash bool,
releasePrefix string, isPrerelease bool) (*LanguageVersions, error) {
Expand Down Expand Up @@ -231,11 +299,11 @@ func versionAtCommitForRepo(repo *git.Repository, commitish plumbing.Revision, r

// determineBaseVersion returns an appropriate semantic versionComponents by the following process:
//
// - If `commitish` has a tag exactly associated with it, the versionComponents component of the tag
// is returned.
// - If `commitish` does not have an exact tag associated, the versionComponents component of the most
// recent exact tag is returned.
// - Otherwise, "v0.0.0" is returned
// - If `commitish` has a tag exactly associated with it, the versionComponents component of the tag
// is returned.
// - If `commitish` does not have an exact tag associated, the versionComponents component of the most
// recent exact tag is returned.
// - Otherwise, "v0.0.0" is returned
//
// The second return value is true if an exact tag match was made.
//
Expand Down

0 comments on commit 2218df7

Please sign in to comment.