Skip to content

Commit

Permalink
Get head reference for commit flag on build cmd
Browse files Browse the repository at this point in the history
Signed-off-by: alexandreh2ag <[email protected]>
  • Loading branch information
alexandreh2ag committed May 8, 2024
1 parent ae83a4c commit f67ee6f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cli/build/commit.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package build

import (
"fmt"
"github.com/alexandreh2ag/mib/container/docker"
"github.com/alexandreh2ag/mib/context"
"github.com/alexandreh2ag/mib/git"
"github.com/alexandreh2ag/mib/loader"
"github.com/alexandreh2ag/mib/printer"
"github.com/spf13/cobra"
)

Expand All @@ -17,7 +19,7 @@ func GetCommitCmd(ctx *context.Context) *cobra.Command {
RunE: GetCommitRunFn(ctx),
}

cmd.Flags().String(Commit, "", "Commit sha")
cmd.Flags().String(Commit, "", "Commit sha, if empty get head reference")

return cmd
}
Expand All @@ -26,11 +28,21 @@ func GetCommitRunFn(ctx *context.Context) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
pushImages, _ := cmd.Flags().GetBool(PushImages)
commitHash, _ := cmd.Flags().GetString(Commit)

builder := ctx.Builders.GetInstance(docker.KeyBuilder)
gitManager, errCreateGit := git.CreateGit(ctx)
if errCreateGit != nil {
return errCreateGit
}

if commitHash == "" {
hash, errHead := gitManager.Head()
if errHead != nil {
return fmt.Errorf("fail when get head git reference: %v", errHead)
}
commitHash = hash
}

images, err := loader.LoadImages(ctx)
if err != nil {
return err
Expand All @@ -41,6 +53,11 @@ func GetCommitRunFn(ctx *context.Context) func(*cobra.Command, []string) error {
}

images.FlagChanged(loader.RemoveExtExcludePath(ctx.WorkingDir, ctx.Config.Build.ExtensionExclude, filesChanged))

if len(images) > 0 {
cmd.Println(printer.DisplayImagesTree(images))
}

errBuild := builder.BuildImages(images, pushImages)
if errBuild != nil {
return errBuild
Expand Down
46 changes: 46 additions & 0 deletions cli/build/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ func TestGetCommitRunFn(t *testing.T) {
assert.NoError(t, err)
},
},
{
name: "SuccessWithoutCommitFlag",
imageData: "name: foo\ntag: 0.1",
preFn: func(ctx *context.Context, ctrl *gomock.Controller) {
m := mockgit.NewMockManager(ctrl)

mibGit.CreateGit = func(ctx *context.Context) (mibGit.Manager, error) {
return m, nil
}
gomock.InOrder(
m.EXPECT().Head().Times(1).Return("xxx", nil),
m.EXPECT().GetCommitFilesChanged(gomock.Eq("xxx")).Times(1).Return(
[]string{"foo/Dockerfile"},
nil,
),
)

builderDocker := mock_types_container.NewMockBuilderImage(ctrl)
builderDocker.EXPECT().BuildImages(gomock.Any(), gomock.Eq(true)).Times(1).Return(nil)
ctx.Builders[docker.KeyBuilder] = builderDocker
},
cmdArgs: []string{
"--" + PushImages,
},
checkFn: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
{
name: "ErrorCreateGitManger",
imageData: "name: foo\ntag: 0.1",
Expand All @@ -66,6 +94,24 @@ func TestGetCommitRunFn(t *testing.T) {
assert.Contains(t, err.Error(), "error")
},
},
{
name: "ErrorGetHeadReference",
imageData: "name: foo\ntag: 0.1",
preFn: func(ctx *context.Context, ctrl *gomock.Controller) {
m := mockgit.NewMockManager(ctrl)
mibGit.CreateGit = func(ctx *context.Context) (mibGit.Manager, error) {
return m, nil
}
m.EXPECT().Head().Times(1).Return("", errors.New("error"))
builderDocker := mock_types_container.NewMockBuilderImage(ctrl)
ctx.Builders[docker.KeyBuilder] = builderDocker
},
cmdArgs: []string{},
checkFn: func(t *testing.T, err error) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "error")
},
},
{
name: "FailLoadImages",
imageData: "name: foo\ntag: ",
Expand Down
4 changes: 4 additions & 0 deletions cli/build/dirty.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/alexandreh2ag/mib/context"
"github.com/alexandreh2ag/mib/git"
"github.com/alexandreh2ag/mib/loader"
"github.com/alexandreh2ag/mib/printer"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -32,6 +33,9 @@ func GetDirtyRunFn(ctx *context.Context) func(*cobra.Command, []string) error {
}
filesChanged := git.GetStageFilesChanged(gitManager)
images.FlagChanged(loader.RemoveExtExcludePath(ctx.WorkingDir, ctx.Config.Build.ExtensionExclude, filesChanged))
if len(images) > 0 {
cmd.Println(printer.DisplayImagesTree(images))
}
errBuild := builder.BuildImages(images, pushImages)
if errBuild != nil {
return errBuild
Expand Down
10 changes: 10 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Repository interface {
ResolveRevision(in plumbing.Revision) (*plumbing.Hash, error)
CommitObject(h plumbing.Hash) (*object.Commit, error)
CommitObjects() (object.CommitIter, error)
Head() (*plumbing.Reference, error)
}

type Worktree interface {
Expand All @@ -37,6 +38,7 @@ type Worktree interface {
}

type Manager interface {
Head() (string, error)
Status() (git.Status, error)
ResolveRevision(in plumbing.Revision) (*plumbing.Hash, error)
CommitFileContent(hash *plumbing.Hash, path string) (string, error)
Expand All @@ -52,6 +54,14 @@ type Git struct {
w Worktree
}

func (g Git) Head() (string, error) {
head, err := g.r.Head()
if err != nil {
return "", err
}
return head.String(), nil
}

func (g Git) CreateCommit(msg string, opts *git.CommitOptions) (plumbing.Hash, error) {
return g.w.Commit(msg, opts)
}
Expand Down
18 changes: 18 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ func TestGit_Status(t *testing.T) {
assert.Equal(t, want, got)
}

func TestGit_Head_Success(t *testing.T) {

ctx := context.TestContext(nil)
ctx.WorkingDir = "/app/repo"

repo := initGitRepo(t, ctx)
_ = afero.WriteFile(ctx.FS, filepath.Join(ctx.WorkingDir, "foo/mib.yml"), []byte("name: foo\ntag: 0.1"), 0644)
_ = afero.WriteFile(ctx.FS, filepath.Join(ctx.WorkingDir, "foo/Dockerfile"), []byte("FROM debian:latest"), 0644)
_ = stageAllAndCommit(t, repo, ctx.WorkingDir, "Add image")

worktree, err := repo.Worktree()
assert.NoError(t, err)
gitManger := &Git{r: repo, w: worktree}
got, err := gitManger.Head()
assert.NoError(t, err)
assert.NotEqual(t, "", got)
}

func TestGit_ResolveRevision(t *testing.T) {

hash := plumbing.NewHash("hash")
Expand Down

0 comments on commit f67ee6f

Please sign in to comment.