Skip to content
Permalink
Browse files
refactor: making it easier to merge with pro (#3463)
this improves mergeability between pro and oss a bit

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Oct 14, 2022
1 parent a15b7ec commit 2b0bdffb3e9b14b7c3a9cd7508d5a7c6438b9fa7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 28 deletions.
@@ -59,3 +59,19 @@ func Clean(output string, err error) (string, error) {
}
return output, err
}

// CleanAllLines returns all the non empty lines of the output, cleaned up.
func CleanAllLines(output string, err error) ([]string, error) {
var result []string
for _, line := range strings.Split(output, "\n") {
l := strings.TrimSpace(strings.ReplaceAll(line, "'", ""))
if l == "" {
continue
}
result = append(result, l)
}
if err != nil {
err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
}
return result, err
}
@@ -36,10 +36,15 @@ func TestGitWarning(t *testing.T) {
testlib.GitCommit(t, "foobar")
testlib.GitBranch(t, "tags/1.2.3")
testlib.GitTag(t, "1.2.3")
testlib.GitTag(t, "nightly")

out, err := git.Run(ctx, "describe", "--tags", "--abbrev=0", "tags/1.2.3^")
require.NoError(t, err)
require.Equal(t, "1.2.2\n", out)

tags, err := git.CleanAllLines(git.Run(ctx, "tag", "--points-at", "HEAD", "--sort", "-version:refname"))
require.NoError(t, err)
require.ElementsMatch(t, []string{"1.2.3", "nightly"}, tags)
}

func TestRepo(t *testing.T) {
@@ -52,16 +57,32 @@ func TestRepo(t *testing.T) {

func TestClean(t *testing.T) {
ctx := context.Background()
out, err := git.Clean("asdasd 'ssadas'\nadasd", nil)
require.NoError(t, err)
require.Equal(t, "asdasd ssadas", out)

out, err = git.Clean(git.Run(ctx, "command-that-dont-exist"))
require.Error(t, err)
require.Empty(t, out)
require.Equal(
t,
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
err.Error(),
)
t.Run("success", func(t *testing.T) {
out, err := git.Clean("asdasd 'ssadas'\nadasd", nil)
require.NoError(t, err)
require.Equal(t, "asdasd ssadas", out)
})

t.Run("error", func(t *testing.T) {
out, err := git.Clean(git.Run(ctx, "command-that-dont-exist"))
require.Error(t, err)
require.Empty(t, out)
require.Equal(
t,
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
err.Error(),
)
})

t.Run("all lines error", func(t *testing.T) {
out, err := git.CleanAllLines(git.Run(ctx, "command-that-dont-exist"))
require.Error(t, err)
require.Empty(t, out)
require.Equal(
t,
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
err.Error(),
)
})
}
@@ -222,36 +222,90 @@ func getTagWithFormat(ctx *context.Context, tag, format string) (string, error)
}

func getTag(ctx *context.Context) (string, error) {
var tag string
var err error
for _, fn := range []func() (string, error){
func() (string, error) {
return os.Getenv("GORELEASER_CURRENT_TAG"), nil
for _, fn := range []func() ([]string, error){
getFromEnv("GORELEASER_CURRENT_TAG"),
func() ([]string, error) {
return gitTagsPointingAt(ctx, "HEAD")
},
func() (string, error) {
return git.Clean(git.Run(ctx, "tag", "--points-at", "HEAD", "--sort", "-version:refname"))
},
func() (string, error) {
return git.Clean(git.Run(ctx, "describe", "--tags", "--abbrev=0"))
func() ([]string, error) {
// this will get the last tag, even if it wasn't made against the
// last commit...
return git.CleanAllLines(gitDescribe(ctx, "HEAD"))
},
} {
tag, err = fn()
if tag != "" || err != nil {
return tag, err
tags, err := fn()
if len(tags) > 0 {
return tags[0], err
}
if err != nil {
return "", err
}
}

return tag, err
return "", nil
}

func getPreviousTag(ctx *context.Context, current string) (string, error) {
if tag := os.Getenv("GORELEASER_PREVIOUS_TAG"); tag != "" {
return tag, nil
for _, fn := range []func() ([]string, error){
getFromEnv("GORELEASER_PREVIOUS_TAG"),
func() ([]string, error) {
sha, err := previousTagSha(ctx, current)
if err != nil {
return nil, err
}
return gitTagsPointingAt(ctx, sha)
},
} {
tags, err := fn()
if len(tags) > 0 {
return tags[0], err
}
if err != nil {
return "", err
}
}

return git.Clean(git.Run(ctx, "describe", "--tags", "--abbrev=0", fmt.Sprintf("tags/%s^", current)))
return "", nil
}

func gitTagsPointingAt(ctx *context.Context, ref string) ([]string, error) {
return git.CleanAllLines(git.Run(
ctx,
"tag",
"--points-at",
ref,
"--sort",
"-version:refname",
))
}

func gitDescribe(ctx *context.Context, ref string) (string, error) {
return git.Clean(git.Run(
ctx,
"describe",
"--tags",
"--abbrev=0",
ref,
))
}

func previousTagSha(ctx *context.Context, current string) (string, error) {
tag, err := gitDescribe(ctx, fmt.Sprintf("tags/%s^", current))
if err != nil {
return "", err
}
return git.Clean(git.Run(ctx, "show", "--format='%h'", tag, "--quiet"))
}

func getURL(ctx *context.Context) (string, error) {
return git.Clean(git.Run(ctx, "ls-remote", "--get-url"))
}

func getFromEnv(s string) func() ([]string, error) {
return func() ([]string, error) {
if tag := os.Getenv(s); tag != "" {
return []string{tag}, nil
}
return nil, nil
}
}

0 comments on commit 2b0bdff

Please sign in to comment.