Skip to content

Commit

Permalink
feat: Working on initial tagging.
Browse files Browse the repository at this point in the history
This also allows us to test the CI.
  • Loading branch information
Curtis Jewell committed Dec 29, 2023
1 parent a361205 commit cf58b53
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 125 deletions.
172 changes: 131 additions & 41 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ package cmd

import (
"errors"
"fmt"
"log/slog"
"os"
"path"
"slices"
"sort"

git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// TODO: Check out using github.com/charmbracelet/bubbletea instead of promptui

// VersionSegment specifies what segment of the version is being changed.
type VersionSegment int

Expand All @@ -52,6 +56,12 @@ const (
versionPre
)

var vsName = []string{"major", "minor", "patch", "alpha", "beta", "gamma", "rc", "pre"}

func (vs VersionSegment) String() string {
return vsName[vs]
}

var (
cfgFile string
repo *git.Repository
Expand All @@ -71,7 +81,6 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
fmt.Println(Version())
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand All @@ -88,7 +97,7 @@ func init() {
rootCmd.Flags().Bool("patch", false, "Increment patch version")
}

// initConfig reads in config file and ENV variables if set.
// initConfig reads in and creates or updates a config file.
func initConfig() error {

// Find current directory.
Expand Down Expand Up @@ -133,20 +142,22 @@ func initConfig() error {

func nextTag(_ *cobra.Command, _ []string) error {
// Start by checking for a clean tree.
wt, err := repo.Worktree()
if err != nil {
return err
}
status, err := wt.Status()
if err != nil {
return err
}
if !status.IsClean() {
return errors.New("git tree is not clean")
}
/*
wt, err := repo.Worktree()
if err != nil {
return err
}
status, err := wt.Status()
if err != nil {
return err
}
if !status.IsClean() {
return errors.New("git tree is not clean")
}
*/

tags := make(map[string]*object.Tag)
tagNames := make([]string, 100)
tagNames := make([]string, 0, 100)

// Start by checking if there are any tags
iter, err := repo.Tags()
Expand All @@ -171,40 +182,81 @@ func nextTag(_ *cobra.Command, _ []string) error {
return err
}

// sort.Slice()
if len(tagNames) == 0 {
initialTag, err := askInitialTagging()
if err != nil {
return err
}
return doTagging(initialTag)
}

/*
tagVersions := make(map[string]*ParsedVersion)
for _, s := range tagNames {
pv := parseVersion(s)
tagVersions[s] = pv
}

VERSION=""
sort.Slice(tagNames, func(i, j int) bool {
tvj := tagVersions[tagNames[j]]
if tvj == nil {
return false
}

#get parameters
while getopts v: flag
do
case "${flag}" in
v) VERSION=${OPTARG};;
esac
done
tvi := tagVersions[tagNames[i]]
if tvi == nil {
return true
}

#get highest tag number, and add 1.0.0 if doesn't exist
CURRENT_VERSION=`git describe --abbrev=0 --tags 2>/dev/null`
if tvi.major < tvj.major {
return true
}
if tvi.major > tvj.major {
return false
}

if [[ $CURRENT_VERSION == '' ]]
then
CURRENT_VERSION='v1.0.0'
fi
echo "Current Version: $CURRENT_VERSION"
if tvi.minor < tvj.minor {
return true
}
if tvi.minor > tvj.minor {
return false
}

# Get rid of the v by splitting it off.
CURRENT_VERSION_PARTS=(${CURRENT_VERSION//v/ })
VNUM=${CURRENT_VERSION_PARTS[0]}
if tvi.patch < tvj.patch {
return true
}
if tvi.patch > tvj.patch {
return false
}

if tvi.lowerCategory < tvj.lowerCategory {
return true
}
if tvi.lowerCategory > tvj.lowerCategory {
return false
}

if tvi.lower < tvj.lower {
return true
}
if tvi.lower > tvj.lower {
return false
}

#replace . with space so can split into an array
CURRENT_VERSION_PARTS=(${VNUM//./ })
if tvi.isPre && !tvj.isPre {
return true
}
return false
})

// To turn the slice around so that the greatest versions are first
slices.Reverse(tagNames)

currentVersion := tagNames[0]
slog.Info("Current version: " + currentVersion)

#get number parts
VNUM1=${CURRENT_VERSION_PARTS[0]}
VNUM2=${CURRENT_VERSION_PARTS[1]}
VNUM3=${CURRENT_VERSION_PARTS[2]}
/*
VERSION=""
if [[ $VERSION == 'major' ]]
then
Expand Down Expand Up @@ -249,3 +301,41 @@ func nextTag(_ *cobra.Command, _ []string) error {

return nil
}

func askInitialTagging() (string, error) {
var versionInitial string
initialV := viper.GetBool("initial_v")
if initialV {
versionInitial = "v0.1.0"
} else {
versionInitial = "0.1.0"
}

menu := promptui.Select{
Label: "Create initial tag to version " + versionInitial,
CursorPos: 0,
Items: []string{"Yes", "No"},
}

_, ok, err := menu.Run()
if err != nil {
return "", errors.New("Cancelled initial tagging")
}
if ok == "No" {
return "", errors.New("Cancelled initial tagging")
}

return versionInitial, nil
}

func doTagging(_ string) error {
return errors.New("Tagging not implemented yet")
}

func replaceInFile(_, _, _ string) error {
return errors.New("replaceInFile not implemented yet")
}

func createFile(filename string) error {

Check warning on line 339 in cmd/root.go

View workflow job for this annotation

GitHub Actions / build

parameter 'filename' seems to be unused, consider removing or renaming it as _
return errors.New("createFile not implemented yet")
}
86 changes: 2 additions & 84 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,5 @@
/*
Copyright © 2023 Curtis Jewell <golang@curtisjewell.name>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// Package cmd implements the CLI structure of git-next-tag.
package cmd

import (
"fmt"
"regexp"
"runtime/debug"
)

// From https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/,
// altered to the use case.

// commit is the commit hash
// uncommitted is whether there were other uncommitted changes when built
// FullCommit is the full commit string.
var commit, uncommitted, fullCommit = func() (string, bool, string) {
commit := ""
modified := false
commitInfo := ""
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
commit = setting.Value
case "vcs.modified":
if setting.Value == "true" {
modified = true
commitInfo = " + uncommitted changes"
}
}
}
}
if commit == "" {
return "unknown", false, " status - not built from repository"
}
return commit, modified, fmt.Sprint(commit, commitInfo)
}()

// version is the
var version = func() string { return "v0.0.0+pre" }()

var rxVersion *regexp.Regexp

func init() {
rxVersion, _ = regexp.Compile(`v?(\d+)[.](\d+)[.](\d+)(?:-(alpha|beta|gamma|rc)[.](\d+))?(?:[+](pre))?`)
}

// FullVersion returns the version, including git status.
func FullVersion() string {
v := version
matches := rxVersion.FindStringSubmatch(v)
if len(matches) > 0 && matches[6] == "pre" {
v += fmt.Sprintf(" (snapshot: %s)", fullCommit)
}
return v
}

// Version returns the base version.
func Version() string {
return version
}

// TODO
// struct ParsedVersion {}
// func GetParsedVersion() *ParsedVersion {}
// Version is the current version of the library or command
var Version = func() string { return "v0.1.0+pre" }()
Loading

0 comments on commit cf58b53

Please sign in to comment.