Skip to content

Commit

Permalink
feat(metadata): add git ref (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Oct 17, 2018
1 parent 5caf3b5 commit 18d8905
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path"
"path/filepath"

"gopkg.in/src-d/go-git.v4"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
Expand Down Expand Up @@ -52,6 +52,18 @@ func (g *Git) Branch() (branch string, isBranch bool, err error) {
return branch, isBranch, err
}

// Ref returns the current git ref name.
func (g *Git) Ref() (ref string, err error) {
r, err := g.repo.Head()
if err != nil {
return
}

ref = r.Name().String()

return ref, err
}

// SHA returns the sha of the current commit.
func (g *Git) SHA() (sha string, err error) {
ref, err := g.repo.Head()
Expand Down
14 changes: 14 additions & 0 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Image struct {
// Git contains git specific metadata.
type Git struct {
Branch string
Ref string
Message string
SHA string
Tag string
Expand Down Expand Up @@ -129,6 +130,9 @@ func addMetadataForGit(m *Metadata) error {
if err = addBranchMetadataForGit(g, m); err != nil {
return err
}
if err = addRefMetadataForGit(g, m); err != nil {
return err
}
if err = addMessageMetadataForGit(g, m); err != nil {
return err
}
Expand Down Expand Up @@ -156,6 +160,16 @@ func addBranchMetadataForGit(g *git.Git, m *Metadata) error {
return nil
}

func addRefMetadataForGit(g *git.Git, m *Metadata) error {
ref, err := g.Ref()
if err != nil {
return err
}
m.Git.Ref = ref

return nil
}

func addMessageMetadataForGit(g *git.Git, m *Metadata) error {
message, err := g.Message()
if err != nil {
Expand Down

0 comments on commit 18d8905

Please sign in to comment.