modver

package module
v2.10.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 15 Imported by: 1

README

Modver

Go Reference Go Report Card Tests Coverage Status Mentioned in Awesome Go

This is modver, a tool that helps you obey semantic versioning rules in your Go module.

It can read and compare two different versions of the same module, from two different directories, or two different Git commits, or the base and head of a Git pull request. It then reports whether the changes require an increase in the major-version number, the minor-version number, or the patchlevel.

Installation and usage

Modver can be used from the command line, or in your Go program, or with GitHub Actions.

Command-line interface

Install the modver command like this:

go install github.com/bobg/modver/v2/cmd/modver@latest

Assuming the current directory is the root of a cloned Git repository, you can run it like this:

$ modver -git .git HEAD~1 HEAD

to tell what kind of version-number change is needed for the latest commit. The -git .git gives the path to the repository’s info; it can also be something like https://github.com/bobg/modver. The arguments HEAD~1 and HEAD specify two Git revisions to compare; in this case, the latest two commits on the current branch. These could also be tags or commit hashes.

GitHub Action

You can arrange for Modver to inspect the changes on your pull-request branch as part of a GitHub Actions-based continuous-integration step. It will add a comment to the pull request with its findings, and will update the comment as new commits are pushed to the branch.

To do this, you’ll need a directory in your GitHub repository named .github/workflows, and a Yaml file containing (at least) the following:

name: Tests

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: 1.19

      - name: Modver
        if: ${{ github.event_name == 'pull_request' }}
        uses: bobg/[email protected]
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          pull_request_url: https://github.com/${{ github.repository }}/pull/${{ github.event.number }}

This can be combined with other steps that run unit tests, etc. You can change Tests to whatever name you like, and should change main to the name of your repository’s default branch. If your pull request is on a GitHub server other than github.com, change the hostname in the pull_request_url parameter to match.

Note the fetch-depth: 0 parameter for the Checkout step. This causes GitHub Actions to create a clone of your repo with its full history, as opposed to the default, which is a shallow clone. Modver requires enough history to be present in the clone for it to access the “base” and “head” revisions of your pull-request branch.

For more information about configuring GitHub Actions, see the GitHub Actions documentation.

Go library

Modver also has a simple API for use from within Go programs. Add it to your project with go get github.com/bobg/modver/v2@latest. See the Go doc page for information about how to use it.

Semantic versioning

Briefly, a major-version bump is needed for incompatible changes in the public API, such as when a type is removed or renamed, or parameters or results are added to or removed from a function. Old callers cannot expect to use the new version without being updated.

A minor-version bump is needed when new features are added to the public API, like a new entrypoint or new fields in an existing struct. Old callers can continue using the new version without being updated, but callers depending on the new features cannot use the old version.

A patchlevel bump is needed for most other changes.

The result produced by modver is the minimal change required. The actual change required may be greater. For example, if a new method is added to a type, this function will return Minor. However, if something also changed about an existing method that breaks the old contract - it accepts a narrower range of inputs, for example, or returns errors in some new cases - that may well require a major-version bump, and this function can't detect those cases.

You can be assured, however, that if this function returns Major, a minor-version bump won't suffice, and if this function returns Minor, a patchlevel bump won't suffice, etc.

The modver command (in the cmd/modver subdirectory) can be used, among other ways, to test that each commit to a Git repository increments the module’s version number appropriately. This is done for modver itself using GitHub Actions, here.

(Note that the standard actions/checkout@v2 action, for cloning a repository during GitHub Actions, creates a shallow clone with just one commit’s worth of history. For the usage here to work, you’ll need more history: at least two commit’s worth and maybe more to pull in the latest tag for the previous revision. The clone depth can be overridden with the fetch-depth parameter, which modver does here.)

Documentation

Overview

Package modver compares two versions of the same Go module. It can tell whether the differences require at least a patchlevel version change, or a minor version change, or a major version change, according to semver rules (https://semver.org/).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGit added in v2.1.0

func GetGit(ctx context.Context) string

GetGit returns the value of the gitPath string added to `ctx` with WithGit. If the key is not set the default value is an empty string.

func Pretty added in v2.3.0

func Pretty(out io.Writer, res Result)

Pretty writes a pretty representation of res to out.

func WithGit added in v2.1.0

func WithGit(ctx context.Context, gitPath string) context.Context

WithGit decorates a context with the value of the gitPath string. This is the path of an executable to use for Git operations in calls to CompareGit. Without it, the go-git library is used. (But a git program is preferable.) Retrieve it with GetGit.

Types

type Result

type Result interface {
	Code() ResultCode
	String() string
	// contains filtered or unexported methods
}

Result is the result of Compare.

func Compare

func Compare(olders, newers []*packages.Package) Result

Compare compares an "older" version of a Go module to a "newer" version of the same module. It tells whether the changes from "older" to "newer" require an increase in the major, minor, or patchlevel version numbers, according to semver rules (https://semver.org/).

Briefly, a major-version bump is needed for incompatible changes in the public API, such as when a type is removed or renamed, or parameters or results are added to or removed from a function. Old callers cannot expect to use the new version without being updated.

A minor-version bump is needed when new features are added to the public API, like a new entrypoint or new fields in an existing struct. Old callers _can_ continue using the new version without being updated, but callers depending on the new features cannot use the old version.

A patchlevel bump is needed for most other changes.

The result of Compare is the _minimal_ change required. The actual change required may be greater. For example, if a new method is added to a type, this function will return Minor. However, if something also changed about an existing method that breaks the old contract - it accepts a narrower range of inputs, for example, or returns errors in some new cases - that may well require a major-version bump, and this function can't detect those cases.

You can be assured, however, that if this function returns Major, a minor-version bump won't suffice, and if this function returns Minor, a patchlevel bump won't suffice, etc.

The packages passed to this function should have no load errors (that is, len(p.Errors) should be 0 for each package p in `olders` and `newers`). If you are using packages.Load (see https://pkg.go.dev/golang.org/x/tools/go/packages#Load), you will need at least

packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo

in your Config.Mode. See CompareDirs for an example of how to call Compare with the result of packages.Load.

func CompareDirs

func CompareDirs(older, newer string) (Result, error)

CompareDirs loads Go modules from the directories at older and newer and calls Compare on the results.

func CompareGit

func CompareGit(ctx context.Context, repoURL, olderRev, newerRev string) (Result, error)

CompareGit compares the Go packages in two revisions of a Git repository at the given URL.

func CompareGit2 added in v2.8.0

func CompareGit2(ctx context.Context, olderRepoURL, olderRev, newerRepoURL, newerRev string) (Result, error)

CompareGit2 compares the Go packages in one revision each of two Git repositories.

func CompareGit2With added in v2.8.0

func CompareGit2With(ctx context.Context, olderRepoURL, olderRev, newerRepoURL, newerRev string, f func(older, newer string) (Result, error)) (Result, error)

CompareGit2With compares the Go packages in one revision each of two Git repositories. It uses the given callback function to perform the comparison.

The callback function receives the paths to two directories, each containing a clone of one of the repositories at its selected revision.

Note that CompareGit2(...) is simply CompareGit2With(..., CompareDirs).

func CompareGitWith

func CompareGitWith(ctx context.Context, repoURL, olderRev, newerRev string, f func(older, newer string) (Result, error)) (Result, error)

CompareGitWith compares the Go packages in two revisions of a Git repository at the given URL. It uses the given callback function to perform the comparison.

The callback function receives the paths to two directories, containing two clones of the repo: one checked out at the older revision and one checked out at the newer revision.

Note that CompareGit(...) is simply CompareGitWith(..., CompareDirs).

type ResultCode

type ResultCode int

ResultCode is the required version-bump level as detected by Compare.

const (
	None ResultCode = iota
	Patchlevel
	Minor
	Major
)

Values for ResultCode.

func (ResultCode) Code

func (r ResultCode) Code() ResultCode

Code implements Result.Code.

func (ResultCode) MarshalText added in v2.9.0

func (r ResultCode) MarshalText() ([]byte, error)

func (ResultCode) String

func (r ResultCode) String() string

String implements Result.String.

func (*ResultCode) UnmarshalText added in v2.9.0

func (r *ResultCode) UnmarshalText(text []byte) error

Directories

Path Synopsis
cmd
modver
Command modver compares two versions of the same Go packages and tells whether a Major, Minor, or Patchlevel version bump (or None) is needed to go from one to the other.
Command modver compares two versions of the same Go packages and tells whether a Major, Minor, or Patchlevel version bump (or None) is needed to go from one to the other.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL