Skip to content

Commit

Permalink
feat: merge tag and sha fields into version
Browse files Browse the repository at this point in the history
Commit d306437 made the handling of
these two fields 100% equivalent.

Breaking change will need to migrate `Giltfile.yaml`
`repositories.tag` and/or `repositories.sha` to `repositories.version`

From:

```yaml
repositories:
  - git: https://github.com/retr0h/ansible-etcd.git
    sha: 77a95b7
    dstDir: /tmp/retr0h.ansible-etcd
  - git: https://github.com/retr0h/ansible-etcd.git
    tag: 1.1
    dstDir: /tmp/retr0h.ansible-etcd-tag
```

To:

```yaml
repositories:
  - git: https://github.com/retr0h/ansible-etcd.git
    version: 77a95b7
    dstDir: /tmp/retr0h.ansible-etcd
  - git: https://github.com/retr0h/ansible-etcd.git
    version: 1.1
    dstDir: /tmp/retr0h.ansible-etcd
```

Fixes: #74
  • Loading branch information
retr0h committed Dec 29, 2023
1 parent 4333bcc commit 664a4c8
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 120 deletions.
6 changes: 3 additions & 3 deletions Giltfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ giltDir: ~/.gilt/clone
debug: false
repositories:
- git: https://github.com/retr0h/ansible-etcd.git
sha: 77a95b7
version: 77a95b7
dstDir: /tmp/retr0h.ansible-etcd
- git: https://github.com/retr0h/ansible-etcd.git
tag: 1.1
version: 1.1
dstDir: /tmp/retr0h.ansible-etcd
- git: https://github.com/lorin/openstack-ansible-modules.git
sha: 2677cc3
version: 2677cc3
sources:
- src: "*_manage"
dstDir: /tmp/library
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ giltDir: ~/.gilt/clone
debug: false
repositories:
- git: https://github.com/retr0h/ansible-etcd.git
sha: 77a95b7
version: 77a95b7
dstDir: roles/retr0h.ansible-etcd
- git: https://github.com/retr0h/ansible-etcd.git
tag: 1.1
version: 1.1
dstDir: roles/retr0h.ansible-etcd-tag
- git: https://github.com/lorin/openstack-ansible-modules.git
sha: 2677cc3
version: 2677cc3
sources:
- src: "*_manage"
dstDir: library
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
Repositories: []config.Repository{
{
Git: "https://github.com/retr0h/ansible-etcd.git",
SHA: "77a95b7",
Version: "77a95b7",
DstDir: "../tmp/retr0h.ansible-etcd",
},
},
Expand Down
2 changes: 1 addition & 1 deletion examples/go-client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module example.com/client

go 1.21.0

replace github.com/retr0h/go-gilt v0.0.0 => ../../../go-gilt/
replace github.com/retr0h/go-gilt => ../../../go-gilt/

require (
github.com/lmittmann/tint v1.0.3
Expand Down
4 changes: 0 additions & 4 deletions examples/go-client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ github.com/lmittmann/tint v1.0.3/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/retr0h/go-gilt v1.0.2 h1:WBl+x23HnJXwmcpTRom9vcnrvICQUffU2oC6znLTf0E=
github.com/retr0h/go-gilt v1.0.2/go.mod h1:7YrTY3MuqXXCh7009FHglJQ0U2XVnJ8K2CMiIGdSp0A=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -35,8 +33,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
Expand Down
6 changes: 3 additions & 3 deletions examples/go-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func main() {
GiltDir: "~/.gilt",
Repositories: []config.Repository{
{
Git: "https://github.com/retr0h/ansible-etcd.git",
SHA: "77a95b7",
DstDir: "../../test/integration/tmp/retr0h.ansible-etcd",
Git: "https://github.com/retr0h/ansible-etcd.git",
Version: "77a95b7",
DstDir: "../../test/integration/tmp/retr0h.ansible-etcd",
},
},
}
Expand Down
20 changes: 9 additions & 11 deletions internal/git/git_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ type GitManagerPublicTestSuite struct {
ctrl *gomock.Controller
mockExec *exec.MockExecManager

gitURL string
gitSHA string
gitTag string
cloneDir string
dstDir string
gitURL string
gitVersion string
cloneDir string
dstDir string

gm internal.GitManager
}
Expand All @@ -66,8 +65,7 @@ func (suite *GitManagerPublicTestSuite) SetupTest() {
defer suite.ctrl.Finish()

suite.gitURL = "https://example.com/user/repo.git"
suite.gitSHA = "abc123"
suite.gitTag = "v1.1"
suite.gitVersion = "abc123"
suite.cloneDir = "/cloneDir"
suite.dstDir = "/dstDir"

Expand All @@ -93,18 +91,18 @@ func (suite *GitManagerPublicTestSuite) TestCloneReturnsError() {

func (suite *GitManagerPublicTestSuite) TestWorktreeOk() {
suite.mockExec.EXPECT().
RunCmdInDir("git", []string{"worktree", "add", "--force", suite.dstDir, suite.gitSHA}, suite.cloneDir).
RunCmdInDir("git", []string{"worktree", "add", "--force", suite.dstDir, suite.gitVersion}, suite.cloneDir).
Return(nil)
err := suite.gm.Worktree(suite.cloneDir, suite.gitSHA, suite.dstDir)
err := suite.gm.Worktree(suite.cloneDir, suite.gitVersion, suite.dstDir)
assert.NoError(suite.T(), err)
}

func (suite *GitManagerPublicTestSuite) TestWorktreeError() {
errors := errors.New("tests error")
suite.mockExec.EXPECT().
RunCmdInDir("git", []string{"worktree", "add", "--force", suite.dstDir, suite.gitSHA}, suite.cloneDir).
RunCmdInDir("git", []string{"worktree", "add", "--force", suite.dstDir, suite.gitVersion}, suite.cloneDir).
Return(errors)
err := suite.gm.Worktree(suite.cloneDir, suite.gitSHA, suite.dstDir)
err := suite.gm.Worktree(suite.cloneDir, suite.gitVersion, suite.dstDir)
assert.Error(suite.T(), err)
}

Expand Down
30 changes: 15 additions & 15 deletions internal/repositories/repositories_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type RepositoriesPublicTestSuite struct {
dstDir string
giltDir string
gitURL string
gitSHA string
gitVersion string
repoConfigDstDir []config.Repository
logger *slog.Logger
}
Expand Down Expand Up @@ -84,12 +84,12 @@ func (suite *RepositoriesPublicTestSuite) SetupTest() {
suite.dstDir = "/dstDir"
suite.giltDir = "/giltDir"
suite.gitURL = "https://example.com/user/repo.git"
suite.gitSHA = "abc1234"
suite.gitVersion = "abc1234"
suite.repoConfigDstDir = []config.Repository{
{
Git: suite.gitURL,
SHA: suite.gitSHA,
DstDir: suite.dstDir,
Git: suite.gitURL,
Version: suite.gitVersion,
DstDir: suite.dstDir,
},
}

Expand Down Expand Up @@ -140,8 +140,8 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCloneErrors
func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCopySources() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitVersion,
Sources: []config.Source{
{
Src: "srcDir",
Expand All @@ -164,8 +164,8 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCopySources() {
func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCopySourcesErrors() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitVersion,
Sources: []config.Source{
{
Src: "srcDir",
Expand All @@ -189,9 +189,9 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCopySources
func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCommands() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
SHA: suite.gitSHA,
DstDir: suite.dstDir,
Git: suite.gitURL,
Version: suite.gitVersion,
DstDir: suite.dstDir,
Commands: []config.Command{
{
Cmd: "touch",
Expand All @@ -214,9 +214,9 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCommands() {
func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCommandErrors() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
SHA: suite.gitSHA,
DstDir: suite.dstDir,
Git: suite.gitURL,
Version: suite.gitVersion,
DstDir: suite.dstDir,
Commands: []config.Command{
{
Cmd: "touch",
Expand Down
8 changes: 1 addition & 7 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@ func (r *Repository) Worktree(
cloneDir string,
targetDir string,
) error {
var version string
if c.SHA != "" {
version = c.SHA
} else {
version = c.Tag
}
return r.gitManager.Worktree(cloneDir, version, targetDir)
return r.gitManager.Worktree(cloneDir, c.Version, targetDir)
}

// CopySources copy Repository.Src to Repository.DstFile or Repository.DstDir.
Expand Down
49 changes: 24 additions & 25 deletions internal/repository/repository_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (suite *RepositoryPublicTestSuite) TestCloneOk() {
repo := suite.NewRepositoryManager()

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
}
targetDir := filepath.Join(suite.cloneDir, filepath.Base(c.Git))

Expand All @@ -100,8 +100,8 @@ func (suite *RepositoryPublicTestSuite) TestCloneReturnsErrorWhenCloneErrors() {
repo := suite.NewRepositoryManager()

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
}

errors := errors.New("tests error")
Expand All @@ -117,8 +117,8 @@ func (suite *RepositoryPublicTestSuite) TestCloneDoesNotCloneWhenCloneDirExists(
repo := suite.NewRepositoryManager()

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
}
targetDir := filepath.Join(suite.cloneDir, filepath.Base(c.Git))

Expand All @@ -140,8 +140,8 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesOkWhenSourceIsDirAndDstDi
createFileSpecs(specs)

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
Sources: []config.Source{
{
Src: filepath.Base(specs[0].srcDir),
Expand Down Expand Up @@ -170,8 +170,8 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesReturnsErrorWhenSourceIsD
createFileSpecs(specs)

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
Sources: []config.Source{
{
Src: filepath.Base(specs[0].srcDir),
Expand Down Expand Up @@ -199,8 +199,8 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesOkWhenSourceIsDirAndDstDi
createFileSpecs(specs)

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
Sources: []config.Source{
{
Src: filepath.Base(specs[0].srcDir),
Expand Down Expand Up @@ -246,8 +246,8 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesOkWhenSourceIsFilesAndDst
createFileSpecs(specs)

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
Sources: []config.Source{
{
Src: "subDir/*_manage",
Expand Down Expand Up @@ -298,8 +298,8 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesReturnsErrorWhenSourceIsF
createFileSpecs(specs)

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
Sources: []config.Source{
{
Src: "subDir/*.txt",
Expand Down Expand Up @@ -327,8 +327,8 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesOkWhenSourceIsFileAndDstF
createFileSpecs(specs)

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
Sources: []config.Source{
{
Src: "1.txt",
Expand Down Expand Up @@ -357,8 +357,8 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesReturnsErrorWhenSourceIsF
createFileSpecs(specs)

c := config.Repository{
Git: suite.gitURL,
SHA: suite.gitSHA,
Git: suite.gitURL,
Version: suite.gitSHA,
Sources: []config.Source{
{
Src: "1.txt",
Expand All @@ -377,9 +377,9 @@ func (suite *RepositoryPublicTestSuite) TestCopySourcesReturnsErrorWhenSourceIsF
func (suite *RepositoryPublicTestSuite) TestWorktreeTagOk() {
repo := suite.NewRepositoryManager()
c := config.Repository{
Tag: suite.gitTag,
Version: suite.gitTag,
}
suite.mockGit.EXPECT().Worktree(suite.cloneDir, c.Tag, suite.dstDir).Return(nil)
suite.mockGit.EXPECT().Worktree(suite.cloneDir, c.Version, suite.dstDir).Return(nil)

err := repo.Worktree(c, suite.cloneDir, suite.dstDir)
assert.NoError(suite.T(), err)
Expand All @@ -389,10 +389,9 @@ func (suite *RepositoryPublicTestSuite) TestWorktreeSHAOk() {
repo := suite.NewRepositoryManager()
// Implicitly test that SHA overrides Tag
c := config.Repository{
Tag: suite.gitTag,
SHA: suite.gitSHA,
Version: suite.gitSHA,
}
suite.mockGit.EXPECT().Worktree(suite.cloneDir, c.SHA, suite.dstDir).Return(nil)
suite.mockGit.EXPECT().Worktree(suite.cloneDir, c.Version, suite.dstDir).Return(nil)

err := repo.Worktree(c, suite.cloneDir, suite.dstDir)
assert.NoError(suite.T(), err)
Expand Down
11 changes: 2 additions & 9 deletions pkg/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@
package config

import (
"regexp"

"github.com/go-playground/validator/v10"
)

// registerValidators register customer validators.
func registerValidators(v *validator.Validate) error {
return v.RegisterValidation("git_sha", func(fl validator.FieldLevel) bool {
if fl.Field().String() == "" {
return true
}
re := regexp.MustCompile("^[0-9a-f]{5,40}$")
return re.MatchString(fl.Field().String())
})
// noop - hook for future validators
return nil
}

// Validate validates a structs exposed fields.
Expand Down
Loading

0 comments on commit 664a4c8

Please sign in to comment.