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
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto eol=lf

*.exe binary
*.zip binary
*.tar.gz binary
*.snap binary
80 changes: 20 additions & 60 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v6
with:
ref: ${{ github.ref }}

- name: Prepare repository
run: git checkout "${GITHUB_REF:11}"
fetch-depth: 0

- name: Install Go
uses: actions/setup-go@v1
uses: actions/setup-go@v6
with:
go-version: 1.13.1
go-version: 1.26.3

- name: test
run: make test
Expand All @@ -24,68 +21,31 @@ jobs:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/master'
permissions:
contents: write
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v6
with:
ref: ${{ github.ref }}

- name: Prepare repository
run: git checkout "${GITHUB_REF:11}"
fetch-depth: 0

- uses: syncromatics/gogitver-action@v0.0.2
- uses: syncromatics/gogitver-action@v0.0.7
id: gogitver

- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.gogitver.outputs.version }}
release_name: Release ${{ steps.gogitver.outputs.version }}
draft: false
prerelease: false

- name: Install Go
uses: actions/setup-go@v1
uses: actions/setup-go@v6
with:
go-version: 1.13.1
go-version: 1.26.3

- name: build
run: make package

- uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./artifacts/linux.tar.gz
asset_name: linux.tar.gz
asset_content_type: application/gzip

- uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./artifacts/windows.zip
asset_name: windows.zip
asset_content_type: application/zip

- uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./artifacts/arm.tar.gz
asset_name: arm.tar.gz
asset_content_type: application/gzip

- uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Release
uses: softprops/action-gh-release@v2
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./artifacts/darwin.tar.gz
asset_name: darwin.tar.gz
asset_content_type: application/gzip
tag_name: ${{ steps.gogitver.outputs.version }}
name: Release ${{ steps.gogitver.outputs.version }}
files: |
./artifacts/linux.tar.gz
./artifacts/windows.zip
./artifacts/arm.tar.gz
./artifacts/darwin.tar.gz
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

artifacts/
.vscode/
.idea
43 changes: 37 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,30 @@ gogitver is a tool to determine the semantic version of a project based on keywo

### Installing

#### Snap
```snap install gogitver```
#### Go install

If you have Go installed, install the latest tagged release directly from GitHub:

```sh
go install github.com/syncromatics/gogitver/cmd/gogitver@latest
```

For a reproducible install, use a specific version tag:

```sh
go install github.com/syncromatics/gogitver/cmd/gogitver@v1.2.0
```

Make sure Go's binary directory is on your `PATH`. This is usually `$GOPATH/bin`, or `$HOME/go/bin` when `GOPATH` is not set.

#### Binaries

To install download the latest release from the [releases](https://github.com/syncromatics/gogitver/releases) page for your machine architecture and place the binary in your path. You can then run the executable while in the path of your project and it should output the current version. You can then use this version to tag container images, helm charts, etc.

#### Snap

The Snap package is outdated and should be avoided for new installs. New versions are not currently published to Snap.

### Usage

To get this most out of this tool you should be adding keywords to your git commits.
Expand All @@ -36,28 +53,42 @@ minor-version-bump-message: '(feat|feature|minor)\(.*\)'
patch-version-bump-message: '(patch|fix)\(.*\)'
```

You can also override the name and location of this file by providing the settings flag ```gotgitver --settings=./anotherfile.yaml```
You can also override the name and location of this file by providing the settings flag ```gogitver --settings=./anotherfile.yaml```

### Primary branch

gogitver uses the repository's primary branch as the stable version baseline. By default it looks for `main`, `master`, `origin/main`, then `origin/master`. For a single branch name, a local ref is preferred over its remote counterpart (a local `main` is used before `origin/main`).

If both a `main` and a `master` branch exist anywhere — local or remote, in any combination — gogitver refuses to guess the version baseline and requires an explicit primary branch. This prevents it from silently switching the baseline during a branch migration:

```sh
gogitver --primary-branch=main
```

The `--primary-branch` value also takes a local ref or `origin/<name>`, so you can point the baseline at any branch, not just `main` or `master`.

Use `--forbid-behind-primary` to error when the current branch calculates a version lower than the resolved primary branch. The older `--forbid-behind-master` flag remains available as a deprecated compatibility alias.

## Development

### Requirements

This project requires at least [Go](https://golang.org/dl/) 1.11 because it makes use of go modules for dependencies.
This project requires [Go](https://golang.org/dl/) 1.26.3.

### Building

To build the project simply run ```make build``` which will generate the binaries and put them in the artifacts folder.

## Built With

* [go-git](https://github.com/src-d/go-git) - The git interface
* [go-git](https://github.com/go-git/go-git) - The git interface

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

## Acknowledgments

* [go-git](https://github.com/src-d/go-git) for allowing interactions with git to be easy and without dependencies
* [go-git](https://github.com/go-git/go-git) for allowing interactions with git to be easy and without dependencies
* [GitVersion](https://github.com/GitTools/GitVersion) for the inspiration
* [Visual Studio Code](https://code.visualstudio.com/) for just being an all around great editor
48 changes: 37 additions & 11 deletions cmd/gogitver/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"os"
"strconv"

"github.com/syncromatics/gogitver/pkg/git"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/syncromatics/gogitver/pkg/git"

gogit "gopkg.in/src-d/go-git.v4"
gogit "github.com/go-git/go-git/v5"
)

var rootCmd = &cobra.Command{
Expand All @@ -32,11 +32,13 @@ func init() {
for _, cmd := range cmds {
cmd.Flags().String("path", ".", "the path to the git repository")
cmd.Flags().String("settings", "./.gogitver.yaml", "the file that contains the settings")
cmd.Flags().String("primary-branch", "", "the primary branch used as the stable version baseline")
cmd.Flags().Bool("trim-branch-prefix", false, "Trim branch prefixes feature/ and hotfix/ from prerelease label")
cmd.Flags().BoolP("verbose", "v", false, "Show information about how the version was calculated")
}

rootCmd.Flags().Bool("forbid-behind-master", false, "error if the current branch's calculated version is behind the calculated version of refs/heads/master")
rootCmd.Flags().Bool("forbid-behind-primary", false, "error if the current branch's calculated version is behind the calculated version of the primary branch")
rootCmd.Flags().Bool("forbid-behind-master", false, "deprecated alias for --forbid-behind-primary")

rootCmd.AddCommand(prereleaseCmd)
}
Expand Down Expand Up @@ -78,19 +80,36 @@ func getRepoAndSettings(cmd *cobra.Command) (*gogit.Repository, *git.Settings) {
}

func getBoolFromFlag(cmd *cobra.Command, flagName string) bool {
result, err := strconv.ParseBool(cmd.Flag(flagName).Value.String())
flag := cmd.Flag(flagName)
if flag == nil {
return false
}

result, err := strconv.ParseBool(flag.Value.String())
if err != nil {
result = false
}
return result
}

func isFlagChanged(cmd *cobra.Command, flagName string) bool {
flag := cmd.Flag(flagName)
return flag != nil && flag.Changed
}

func getBranchSettings(cmd *cobra.Command) *git.BranchSettings {
fbm := getBoolFromFlag(cmd, "forbid-behind-master")
fbp := getBoolFromFlag(cmd, "forbid-behind-primary")
trimPrefix := getBoolFromFlag(cmd, "trim-branch-prefix")
primaryBranch := ""
if flag := cmd.Flag("primary-branch"); flag != nil {
primaryBranch = flag.Value.String()
}
return &git.BranchSettings{
ForbidBehindMaster: fbm,
TrimBranchPrefix: trimPrefix,
ForbidBehindMaster: fbm,
ForbidBehindPrimary: fbp,
TrimBranchPrefix: trimPrefix,
PrimaryBranch: primaryBranch,
}
}

Expand All @@ -103,6 +122,10 @@ func runRoot(cmd *cobra.Command, args []string) {
}

branchSettings := getBranchSettings(cmd)
if branchSettings.ForbidBehindMaster && isFlagChanged(cmd, "forbid-behind-master") {
fmt.Fprintln(os.Stderr, "warning: --forbid-behind-master is deprecated; use --forbid-behind-primary")
}

version, err := git.GetCurrentVersion(r, s, branchSettings, v)
if err != nil {
panic(err)
Expand All @@ -113,17 +136,20 @@ func runRoot(cmd *cobra.Command, args []string) {

func runPrerelease(cmd *cobra.Command, args []string) {
r, s := getRepoAndSettings(cmd)
trimPrefix := getBoolFromFlag(cmd, "trim-branch-prefix")
branchSettings := &git.BranchSettings{
TrimBranchPrefix: trimPrefix,
}
branchSettings := getBranchSettings(cmd)

label, err := git.GetPrereleaseLabel(r, s, branchSettings)
if err != nil {
panic(err)
}

if label == "master" {
primaryBranch, err := git.GetPrimaryBranchName(r, branchSettings)
if git.IsAmbiguousPrimaryBranchError(err) {
panic(err)
}
// The label command can still return the current branch label if the
// primary branch is missing; only ambiguous main/master detection is unsafe.
if err == nil && label == primaryBranch {
label = ""
}

Expand Down
44 changes: 34 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
module github.com/syncromatics/gogitver

go 1.13
go 1.26.3

require (
github.com/coreos/go-semver v0.2.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pkg/errors v0.8.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.1 // indirect
github.com/stretchr/testify v1.2.2
gopkg.in/src-d/go-billy.v4 v4.2.1
gopkg.in/src-d/go-git.v4 v4.7.1
gopkg.in/yaml.v2 v2.2.1
github.com/coreos/go-semver v0.3.1
github.com/go-git/go-billy/v5 v5.9.0
github.com/go-git/go-git/v5 v5.19.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/yaml.v3 v3.0.1
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.1.6 // indirect
github.com/cloudflare/circl v1.6.3 // indirect
github.com/cyphar/filepath-securejoin v0.6.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/pjbgf/sha1cd v0.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.1 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.50.0 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sys v0.43.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading