Skip to content

Commit

Permalink
Added support to checkout a tag
Browse files Browse the repository at this point in the history
Fixes: #1
  • Loading branch information
retr0h committed Nov 30, 2023
1 parent 2900f82 commit 7058c3e
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 117 deletions.
7 changes: 5 additions & 2 deletions Giltfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ giltDir: ~/.gilt/clone
debug: false
repositories:
- git: https://github.com/retr0h/ansible-etcd.git
version: 77a95b7
sha: 77a95b7
dstDir: /tmp/retr0h.ansible-etcd
- git: https://github.com/retr0h/ansible-etcd.git
tag: 1.1
dstDir: /tmp/retr0h.ansible-etcd
- git: https://github.com/lorin/openstack-ansible-modules.git
version: 2677cc3
sha: 2677cc3
sources:
- src: "*_manage"
dstDir: /tmp/library
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ This project is a port of [Gilt][], it is not 100% compatible with the python
version, and aims to correct poor decisions made in the python version of
Gilt.

This version of Gilt does not handle branches and tags unlike our python
friend. However, those features will be added soon.
This version of Gilt does not handle branches unlike our python friend.

## Installation

Expand All @@ -48,11 +47,13 @@ giltDir: ~/.gilt/clone
debug: false
repositories:
- git: https://github.com/retr0h/ansible-etcd.git
version: 77a95b7
sha: 77a95b7
dstDir: roles/retr0h.ansible-etcd

- git: https://github.com/retr0h/ansible-etcd.git
tag: 1.1
dstDir: roles/retr0h.ansible-etcd-tag
- git: https://github.com/lorin/openstack-ansible-modules.git
version: 2677cc3
sha: 2677cc3
sources:
- src: "*_manage"
dstDir: library
Expand Down Expand Up @@ -118,7 +119,7 @@ func main() {
Repositories: []config.Repository{
{
Git: "https://github.com/retr0h/ansible-etcd.git",
Version: "77a95b7",
SHA: "77a95b7",
DstDir: "../tmp/retr0h.ansible-etcd",
},
},
Expand Down
3 changes: 2 additions & 1 deletion internal/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package internal
// GitManager manager responsible for Git operations.
type GitManager interface {
Clone(gitURL string, cloneDir string) error
Reset(cloneDir string, gitVersion string) error
CloneByTag(gitURL string, gitTag string, cloneDir string) error
Reset(cloneDir string, gitSHA string) error
CheckoutIndex(dstDir string, cloneDir string) error
}
19 changes: 15 additions & 4 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,32 @@ func New(
}
}

// Clone as exec manager to clone repo.
// Clone git clone repo.
func (g *Git) Clone(
gitURL string,
cloneDir string,
) error {
// return g.execManager.Clone(gitURL, cloneDir)
return g.execManager.RunCmd("git", []string{"clone", gitURL, cloneDir})
}

// CloneByTag git clone repo by tag.
func (g *Git) CloneByTag(
gitURL string,
gitTag string,
cloneDir string,
) error {
return g.execManager.RunCmd(
"git",
[]string{"clone", "--depth", "1", "--branch", gitTag, gitURL, cloneDir},
)
}

// Reset to the given git version.
func (g *Git) Reset(
cloneDir string,
gitVersion string,
gitSHA string,
) error {
return g.execManager.RunCmd("git", []string{"-C", cloneDir, "reset", "--hard", gitVersion})
return g.execManager.RunCmd("git", []string{"-C", cloneDir, "reset", "--hard", gitSHA})
}

// CheckoutIndex checkout Repository.Git to Repository.DstDir.
Expand Down
22 changes: 18 additions & 4 deletions internal/git/git_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 27 additions & 8 deletions internal/git/git_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ type GitManagerPublicTestSuite struct {
ctrl *gomock.Controller
mockExec *exec.MockExecManager

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

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

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

Expand All @@ -89,19 +91,36 @@ func (suite *GitManagerPublicTestSuite) TestCloneReturnsError() {
assert.Error(suite.T(), err)
}

func (suite *GitManagerPublicTestSuite) TestCloneByTagOk() {
suite.mockExec.EXPECT().
RunCmd("git", []string{"clone", "--depth", "1", "--branch", suite.gitTag, suite.gitURL, suite.cloneDir}).
Return(nil)

err := suite.gm.CloneByTag(suite.gitURL, suite.gitTag, suite.cloneDir)
assert.NoError(suite.T(), err)
}

func (suite *GitManagerPublicTestSuite) TestCloneByTagReturnsError() {
errors := errors.New("tests error")
suite.mockExec.EXPECT().RunCmd(gomock.Any(), gomock.Any()).Return(errors)

err := suite.gm.CloneByTag(suite.gitURL, suite.gitTag, suite.cloneDir)
assert.Error(suite.T(), err)
}

func (suite *GitManagerPublicTestSuite) TestResetOk() {
suite.mockExec.EXPECT().
RunCmd("git", []string{"-C", suite.cloneDir, "reset", "--hard", suite.gitVersion})
RunCmd("git", []string{"-C", suite.cloneDir, "reset", "--hard", suite.gitSHA})

err := suite.gm.Reset(suite.cloneDir, suite.gitVersion)
err := suite.gm.Reset(suite.cloneDir, suite.gitSHA)
assert.NoError(suite.T(), err)
}

func (suite *GitManagerPublicTestSuite) TestResetReturnsError() {
errors := errors.New("tests error")
suite.mockExec.EXPECT().RunCmd(gomock.Any(), gomock.Any()).Return(errors)

err := suite.gm.Reset(suite.cloneDir, suite.gitVersion)
err := suite.gm.Reset(suite.cloneDir, suite.gitSHA)
assert.Error(suite.T(), err)
}

Expand Down
6 changes: 5 additions & 1 deletion internal/repositories/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ func (r *Repositories) getCloneHash(
":", "-",
)
replacedGitURL := replacer.Replace(c.Git)
version := c.SHA
if c.Tag != "" {
version = c.Tag
}

return fmt.Sprintf("%s-%s", replacedGitURL, c.Version)
return fmt.Sprintf("%s-%s", replacedGitURL, version)
}

// getCacheDir create the cacheDir if it doesn't exist.
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
gitVersion string
gitSHA 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.gitVersion = "abc1234"
suite.gitSHA = "abc1234"
suite.repoConfigDstDir = []config.Repository{
{
Git: suite.gitURL,
Version: suite.gitVersion,
DstDir: suite.dstDir,
Git: suite.gitURL,
SHA: suite.gitSHA,
DstDir: suite.dstDir,
},
}

Expand Down Expand Up @@ -150,8 +150,8 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCheckoutInd
func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCopySources() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
Version: suite.gitVersion,
Git: suite.gitURL,
SHA: suite.gitSHA,
Sources: []config.Source{
{
Src: "srcDir",
Expand All @@ -174,8 +174,8 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCopySources() {
func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCopySourcesErrors() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
Version: suite.gitVersion,
Git: suite.gitURL,
SHA: suite.gitSHA,
Sources: []config.Source{
{
Src: "srcDir",
Expand All @@ -197,9 +197,9 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCopySources
func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCommands() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
Version: suite.gitVersion,
DstDir: suite.dstDir,
Git: suite.gitURL,
SHA: suite.gitSHA,
DstDir: suite.dstDir,
Commands: []config.Command{
{
Cmd: "touch",
Expand All @@ -222,9 +222,9 @@ func (suite *RepositoriesPublicTestSuite) TestOverlayOkWhenCommands() {
func (suite *RepositoriesPublicTestSuite) TestOverlayReturnsErrorWhenCommandErrors() {
repoConfig := []config.Repository{
{
Git: suite.gitURL,
Version: suite.gitVersion,
DstDir: suite.dstDir,
Git: suite.gitURL,
SHA: suite.gitSHA,
DstDir: suite.dstDir,
Commands: []config.Command{
{
Cmd: "touch",
Expand Down
30 changes: 22 additions & 8 deletions internal/repositories/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,38 @@ func (suite *RepositoriesTestSuite) TestgetCloneDirOk() {
repos := suite.NewTestRepositories(suite.giltDir)

got := repos.getCloneDir(
"/giltDir",
suite.giltDir,
config.Repository{
Version: "abc123",
Git: "https://example.com/user/repo2.git",
SHA: "abc123",
},
)
assert.Equal(suite.T(), got, "/giltDir/-abc123")
assert.Equal(suite.T(), "/giltDir/https---example.com-user-repo2.git-abc123", got)
}

func (suite *RepositoriesTestSuite) TestgetCloneHash() {
func (suite *RepositoriesTestSuite) TestgetCloneDirOkByTag() {
repos := suite.NewTestRepositories(suite.giltDir)

got := repos.getCloneDir(
"https://example.com/user/repo2.git",
suite.giltDir,
config.Repository{
Version: "abc123",
Git: "https://example.com/user/repo2.git",
Tag: "v1.1",
},
)
assert.Equal(suite.T(), got, "https:/example.com/user/repo2.git/-abc123")
assert.Equal(suite.T(), "/giltDir/https---example.com-user-repo2.git-v1.1", got)
}

func (suite *RepositoriesTestSuite) TestgetCloneHashOk() {
repos := suite.NewTestRepositories(suite.giltDir)

got := repos.getCloneHash(
config.Repository{
Git: "https://example.com/user/repo2.git",
SHA: "abc123",
},
)
assert.Equal(suite.T(), "https---example.com-user-repo2.git-abc123", got)
}

func (suite *RepositoriesTestSuite) TestgetCacheDir() {
Expand All @@ -108,7 +122,7 @@ func (suite *RepositoriesTestSuite) TestgetCacheDir() {
expectedDir := "/giltDir/cache"
got, err := repos.getCacheDir()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), got, expectedDir)
assert.Equal(suite.T(), expectedDir, got)

exists, err := afero.Exists(suite.appFs, expectedDir)
assert.NoError(suite.T(), err)
Expand Down
21 changes: 15 additions & 6 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,34 @@ func glob(
}

// Clone clone Repository.Git to Repository.getCloneDir, and hard checkout
// to Repository.Version.
// to Repository.SHA.
func (r *Repository) Clone(
c config.Repository,
cloneDir string,
) error {
r.logger.Info(
"cloning",
slog.String("repository", c.Git),
slog.String("version", c.Version),
slog.String("sha", c.SHA),
slog.String("tag", c.Tag),
slog.String("dstDir", cloneDir),
)

if _, err := r.appFs.Stat(cloneDir); os.IsNotExist(err) {
if err := r.gitManager.Clone(c.Git, cloneDir); err != nil {
return err
if c.SHA != "" {
if err := r.gitManager.Clone(c.Git, cloneDir); err != nil {
return err
}

if err := r.gitManager.Reset(cloneDir, c.SHA); err != nil {
return err
}
}

if err := r.gitManager.Reset(cloneDir, c.Version); err != nil {
return err
if c.Tag != "" {
if err := r.gitManager.CloneByTag(c.Git, c.Tag, cloneDir); err != nil {
return err
}
}
} else {
r.logger.Warn(
Expand Down
Loading

0 comments on commit 7058c3e

Please sign in to comment.