Skip to content

Commit

Permalink
Add git commands for versioning (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Sep 11, 2024
1 parent f8dfbab commit 7d6a60c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 35 deletions.
14 changes: 8 additions & 6 deletions build/go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
module build

go 1.22
go 1.22.0

toolchain go1.22.6

replace github.com/outofforest/build/v2 => ../

require (
github.com/outofforest/build/v2 v2.2.0
github.com/outofforest/tools v1.0.0
github.com/outofforest/tools v1.0.4
)

require (
github.com/outofforest/ioc/v2 v2.5.2 // indirect
github.com/outofforest/libexec v0.3.9 // indirect
github.com/outofforest/logger v0.5.4 // indirect
github.com/outofforest/logger v0.5.5 // indirect
github.com/outofforest/parallel v0.2.3 // indirect
github.com/outofforest/run v0.6.0 // indirect
github.com/outofforest/run v0.8.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ridge/must v0.6.0 // indirect
github.com/samber/lo v1.47.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
19 changes: 10 additions & 9 deletions build/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ github.com/outofforest/libexec v0.3.9 h1:KvVLuKDHqpydwNoKm+j36hi9DVPU61X4oHonlZ5
github.com/outofforest/libexec v0.3.9/go.mod h1:J2rUB/m0ER8UNOHd3/UQM55bvh1cbMwhb8gibeF/zyo=
github.com/outofforest/logger v0.3.3/go.mod h1:+M5sO17Va9V33t28Qs9VqRQ8bFV501Uhq2PtQY+R3Ms=
github.com/outofforest/logger v0.3.4/go.mod h1:wOsyVEu2nnueGK+IZuD1tOWYx6tXGV48earpJsDPT3Y=
github.com/outofforest/logger v0.5.4 h1:mRxOxRrm1ppUueQiv+ektljGAGhUM9wOLmkmVpQuMqo=
github.com/outofforest/logger v0.5.4/go.mod h1:czsrxU2w6KlZ31gbJt164H2k0ckoygXbYqCx7V2DAHM=
github.com/outofforest/logger v0.5.5 h1:ndsFPPojrRMH2BZzk1MOmf+imooVqrzOG78NSqGfx/Q=
github.com/outofforest/logger v0.5.5/go.mod h1:+h+DeEKd7Q2kYrRy8qME0cTgl8+7SqeFDWCti9cIgVk=
github.com/outofforest/parallel v0.2.3 h1:DRIgHr7XTL4LLgsTqrj041kulv4ajtbCkRbkOG5psWY=
github.com/outofforest/parallel v0.2.3/go.mod h1:cu210xIjJtOMXR2ERzEcNA2kr0Z0xfZjSKw2jTxAQ2E=
github.com/outofforest/run v0.6.0 h1:t/3vAodvU5L5vJ3BB0qRgfviX+T3JJmLgPN6G2WQs3U=
github.com/outofforest/run v0.6.0/go.mod h1:l7TAtA/zG+7JEH017Qrfm7gzmMyM0gjd+CereJdJSpI=
github.com/outofforest/tools v1.0.0 h1:VS5FJ8ug+TPe+vArMAeWza+Qve+5ENZQxXs6kNSfWIs=
github.com/outofforest/tools v1.0.0/go.mod h1:fn8F7Z4F4SdyWCSIQIf2NvHbN08fDCum+aDVEbxSPUs=
github.com/outofforest/run v0.8.0 h1:p3tSksh6AWzxsZ+baZzeQhgsfugKz6d6RaE7xfQYt5M=
github.com/outofforest/run v0.8.0/go.mod h1:4d6txrC3wfOSFy64bwxZweKUQsccDs9IvtIbAAFAk7E=
github.com/outofforest/tools v1.0.4 h1:PdZVOeEbHDwQoVMifrOFz0CzTGDxDNBvv295Z2A5mps=
github.com/outofforest/tools v1.0.4/go.mod h1:fn8F7Z4F4SdyWCSIQIf2NvHbN08fDCum+aDVEbxSPUs=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/ridge/must v0.6.0 h1:INravc0/PCJjZgfNADzGOS8/ubNykJYmyJshuz6uiCg=
github.com/ridge/must v0.6.0/go.mod h1:dm1IMngycGzvmpsFY1A5TU18Y5Yg6MgtkJ0iJbca0VA=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
Expand Down Expand Up @@ -57,6 +56,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -71,8 +72,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
module github.com/outofforest/build/v2

go 1.22
go 1.22.0

toolchain go1.22.6

require (
github.com/outofforest/libexec v0.3.9
github.com/outofforest/logger v0.5.4
github.com/outofforest/run v0.6.0
github.com/outofforest/tools v1.0.0
github.com/outofforest/logger v0.5.5
github.com/outofforest/run v0.8.0
github.com/outofforest/tools v1.0.4
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.47.0
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
golang.org/x/mod v0.21.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/outofforest/ioc/v2 v2.5.2 // indirect
github.com/outofforest/parallel v0.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ridge/must v0.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
19 changes: 10 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ github.com/outofforest/libexec v0.3.9 h1:KvVLuKDHqpydwNoKm+j36hi9DVPU61X4oHonlZ5
github.com/outofforest/libexec v0.3.9/go.mod h1:J2rUB/m0ER8UNOHd3/UQM55bvh1cbMwhb8gibeF/zyo=
github.com/outofforest/logger v0.3.3/go.mod h1:+M5sO17Va9V33t28Qs9VqRQ8bFV501Uhq2PtQY+R3Ms=
github.com/outofforest/logger v0.3.4/go.mod h1:wOsyVEu2nnueGK+IZuD1tOWYx6tXGV48earpJsDPT3Y=
github.com/outofforest/logger v0.5.4 h1:mRxOxRrm1ppUueQiv+ektljGAGhUM9wOLmkmVpQuMqo=
github.com/outofforest/logger v0.5.4/go.mod h1:czsrxU2w6KlZ31gbJt164H2k0ckoygXbYqCx7V2DAHM=
github.com/outofforest/logger v0.5.5 h1:ndsFPPojrRMH2BZzk1MOmf+imooVqrzOG78NSqGfx/Q=
github.com/outofforest/logger v0.5.5/go.mod h1:+h+DeEKd7Q2kYrRy8qME0cTgl8+7SqeFDWCti9cIgVk=
github.com/outofforest/parallel v0.2.3 h1:DRIgHr7XTL4LLgsTqrj041kulv4ajtbCkRbkOG5psWY=
github.com/outofforest/parallel v0.2.3/go.mod h1:cu210xIjJtOMXR2ERzEcNA2kr0Z0xfZjSKw2jTxAQ2E=
github.com/outofforest/run v0.6.0 h1:t/3vAodvU5L5vJ3BB0qRgfviX+T3JJmLgPN6G2WQs3U=
github.com/outofforest/run v0.6.0/go.mod h1:l7TAtA/zG+7JEH017Qrfm7gzmMyM0gjd+CereJdJSpI=
github.com/outofforest/tools v1.0.0 h1:VS5FJ8ug+TPe+vArMAeWza+Qve+5ENZQxXs6kNSfWIs=
github.com/outofforest/tools v1.0.0/go.mod h1:fn8F7Z4F4SdyWCSIQIf2NvHbN08fDCum+aDVEbxSPUs=
github.com/outofforest/run v0.8.0 h1:p3tSksh6AWzxsZ+baZzeQhgsfugKz6d6RaE7xfQYt5M=
github.com/outofforest/run v0.8.0/go.mod h1:4d6txrC3wfOSFy64bwxZweKUQsccDs9IvtIbAAFAk7E=
github.com/outofforest/tools v1.0.4 h1:PdZVOeEbHDwQoVMifrOFz0CzTGDxDNBvv295Z2A5mps=
github.com/outofforest/tools v1.0.4/go.mod h1:fn8F7Z4F4SdyWCSIQIf2NvHbN08fDCum+aDVEbxSPUs=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/ridge/must v0.6.0 h1:INravc0/PCJjZgfNADzGOS8/ubNykJYmyJshuz6uiCg=
github.com/ridge/must v0.6.0/go.mod h1:dm1IMngycGzvmpsFY1A5TU18Y5Yg6MgtkJ0iJbca0VA=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
Expand Down Expand Up @@ -60,6 +59,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -74,8 +75,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
2 changes: 1 addition & 1 deletion make.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var defaultCommandRegistry = newCommandRegistry()
// Main receives configuration and runs registeredCommands.
func Main(name, version string) {
commands := defaultCommandRegistry.commands
run.New().Run("build", func(ctx context.Context) error {
run.New().Run(context.Background(), "build", func(ctx context.Context) error {
flags := logger.Flags(logger.DefaultConfig, "build")
if err := flags.Parse(os.Args[1:]); err != nil {
return err
Expand Down
79 changes: 75 additions & 4 deletions pkg/tools/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,95 @@ import (
"context"
"fmt"
"os/exec"
"strings"

"github.com/pkg/errors"
"golang.org/x/mod/semver"

"github.com/outofforest/build/v2/pkg/types"
"github.com/outofforest/libexec"
)

// IsStatusClean checks that there are no uncommitted files in the repo.
func IsStatusClean(ctx context.Context, _ types.DepsFunc) error {
clean, info, err := status(ctx)
if err != nil {
return err
}
if !clean {
fmt.Println(info)
return errors.New("repository contains uncommitted changes")
}
return nil
}

// HeadHash returns hash of the latest commit in the repository.
func HeadHash(ctx context.Context, repoPath string) (string, error) {
buf := &bytes.Buffer{}
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = repoPath
cmd.Stdout = buf
if err := libexec.Exec(ctx, cmd); err != nil {
return "", errors.Wrap(err, "git command failed")
}
return strings.TrimSuffix(buf.String(), "\n"), nil
}

// DirtyHeadHash returns hash of the latest commit in the repository, adding "-dirty" suffix
// if there are uncommitted changes.
func DirtyHeadHash(ctx context.Context, repoPath string) (string, error) {
hash, err := HeadHash(ctx, repoPath)
if err != nil {
return "", err
}

clean, _, err := status(ctx)
if err != nil {
return "", err
}
if !clean {
hash += "-dirty"
}

return hash, nil
}

// HeadTags returns the list of tags applied to the latest commit.
func HeadTags(ctx context.Context, repoPath string) ([]string, error) {
buf := &bytes.Buffer{}
cmd := exec.Command("git", "tag", "--points-at", "HEAD")
cmd.Dir = repoPath
cmd.Stdout = buf
if err := libexec.Exec(ctx, cmd); err != nil {
return nil, errors.Wrap(err, "git command failed")
}
return strings.Split(strings.TrimSuffix(buf.String(), "\n"), "\n"), nil
}

// VersionFromTag returns version taken from tag present in the commit.
func VersionFromTag(ctx context.Context, repoPath string) (string, error) {
tags, err := HeadTags(ctx, repoPath)
if err != nil {
return "", err
}

for _, tag := range tags {
if semver.IsValid(tag) {
return tag, nil
}
}
return "", nil
}

func status(ctx context.Context) (bool, string, error) {
buf := &bytes.Buffer{}
cmd := exec.Command("git", "status", "-s")
cmd.Stdout = buf
if err := libexec.Exec(ctx, cmd); err != nil {
return errors.Wrap(err, "git command failed")
return false, "", errors.Wrap(err, "git command failed")
}
if buf.Len() > 0 {
fmt.Println(buf)
return errors.New("repository contains uncommitted changes")
return false, buf.String(), nil
}
return nil
return true, "", nil
}

0 comments on commit 7d6a60c

Please sign in to comment.