restechnica / semverbot

A CLI which automates semver versioning.
Mozilla Public License 2.0
133 stars 6 forks source link

[FEAT] 'dig' until semver tag #41

Closed ToneVDB closed 2 years ago

ToneVDB commented 2 years ago

Situation

We have a repo that has the following tags

1.0.1
1.1.0
2.0.0
2.1.0
PROD

Behaviour

When running

sbot release version
sbot push version

sbot creates a new tag 0.0.1 as the last tag is PROD - This is by design at the moment

Expected behaviour

sbot should ignore the first x amount of non semver tags when running the same commands the expectation is that sbot creates for example the 2.1.1 tag

Potential fix

Add a --dig / --depth comand line option / config file option that searches further back to find the 'latest' semver tag This could also become the default in case the oldest tag is non-semver compliant

Example code

To retrive the last 3 tags in git, sorted by 'potential' semver structure you could use

git tag --sort=-version:refname | head -n 3

This would yield the following example output:

2.1.0
2.0.0
1.1.0

'ignoring' the PROD tag.

A solution in pkg/git/cli.go could be:

// GetLastNumberAnnotatedTags gets the last x amount of semver-like tags - default is 5 tags
// Returns the list of git tags and an error if the command fails
func (api CLI) GetLastNumberAnnotatedTags(amount int) (tag string, err error) {
    if amount == 0 {
        amount = 5
    }
    strAmount := strconv.Itoa(amount)
    return api.Commander.Output("git", "tag", "--sort=-version:refname", "|", "head", "-n", strAmount)
}
shiouen commented 2 years ago

Looks good to me, a few notes:

lightweight tags

The current method for getting the latest tag ignores lightweight tags, the proposed method shows both annotated and lightweight tags. We will need to update the docs to reflect this.

sbot will keep creating annotated tags, but reading lightweight tags shouldn't be a breaking change afaik.

Windows compatibility

The head command is not available on Windows unless using WSL. We should mimic the head command in Go code.

That said, I'll have a crack at it!

TODO:

  1. extend the git api with the git tag command
  2. replace GetLatestAnnotatedTag usage with the new command
  3. update the docs about digging for a semver tag
  4. update the docs about no longer ignoring lightweight tags