Skip to content

Commit 680777b

Browse files
authoredJul 10, 2024
feat: try to infer sub-package tag (#161)
This adds support for inferring a sub-package version by trying to predict the sub-package tag using the current directory and Git root path. For example, running `svu` in a Go mono-repository with multiple packages won't return the correct tag since it won't be able to parse the latest tag `ansi/v0.1.2`. This PR initializes the `--prefix` and `--pattern` when the current directory doesn't match the Git root making `svu` work without the need to specify `svu --prefix="ansi/v" --pattern="ansi/*" ...`. P.S. maybe there's a cleaner way of doing so 🤔 Previously I had this little shell func ```sh function get-next-version() { root=$(git rev-parse --show-toplevel) if [ "$PWD" != "$root" ]; then local prefix=$(echo -n "$PWD" | sed "s|$root/||g") svu --prefix="$prefix/v" --pattern="$prefix/*" "$@" else svu "$@" fi } ``` Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com>
1 parent 26b9e38 commit 680777b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed
 

‎internal/git/git.go

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ func IsRepo() bool {
2222
return err == nil && strings.TrimSpace(out) == "true"
2323
}
2424

25+
// Root returns the root of the git repository
26+
func Root() (string, error) {
27+
out, err := run("rev-parse", "--show-toplevel")
28+
return strings.TrimSpace(out), err
29+
}
30+
2531
func getAllTags(args ...string) ([]string, error) {
2632
tags, err := run(append([]string{"-c", "versionsort.suffix=-", "tag", "--sort=-version:refname"}, args...)...)
2733
if err != nil {

‎main.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"path"
67
"runtime/debug"
8+
"strings"
79

810
"github.com/alecthomas/kingpin/v2"
11+
"github.com/caarlos0/svu/v2/internal/git"
912
"github.com/caarlos0/svu/v2/internal/svu"
1013
)
1114

@@ -18,10 +21,10 @@ var (
1821
currentCmd = app.Command("current", "prints current version").Alias("c")
1922
preReleaseCmd = app.Command("prerelease", "new pre release version based on the next version calculated from git log").
2023
Alias("pr")
21-
preRelease = app.Flag("pre-release", "adds a pre-release suffix to the version, without the semver mandatory dash prefix").
24+
preRelease = app.Flag("pre-release", "adds a pre-release suffix to the version, without the semver mandatory dash prefix").
2225
String()
23-
pattern = app.Flag("pattern", "limits calculations to be based on tags matching the given pattern").String()
24-
prefix = app.Flag("prefix", "set a custom prefix").Default("v").String()
26+
pattern = app.Flag("pattern", "limits calculations to be based on tags matching the given pattern").Default(defaults("pattern")).String()
27+
prefix = app.Flag("prefix", "set a custom prefix").Default(defaults("prefix")).String()
2528
stripPrefix = app.Flag("strip-prefix", "strips the prefix from the tag").Default("false").Bool()
2629
build = app.Flag("build", "adds a build suffix to the version, without the semver mandatory plug prefix").
2730
String()
@@ -37,6 +40,29 @@ var (
3740
Bool()
3841
)
3942

43+
func defaults(flag string) string {
44+
var def, pat string
45+
switch flag {
46+
case "prefix":
47+
def, pat = "v", "v"
48+
case "pattern":
49+
def, pat = "", "*"
50+
default:
51+
return ""
52+
}
53+
54+
cwd, wdErr := os.Getwd()
55+
gitRoot, grErr := git.Root()
56+
if wdErr == nil && grErr == nil && cwd != gitRoot {
57+
prefix := strings.TrimPrefix(cwd, gitRoot)
58+
prefix = strings.TrimPrefix(prefix, string(os.PathSeparator))
59+
prefix = strings.TrimSuffix(prefix, string(os.PathSeparator))
60+
return path.Join(prefix, pat)
61+
}
62+
63+
return def
64+
}
65+
4066
func main() {
4167
app.Author("Carlos Alexandro Becker <carlos@becker.software>")
4268
app.Version(buildVersion(version, commit, date, builtBy))

0 commit comments

Comments
 (0)