Skip to content

Commit

Permalink
feat(build): expose commit info in werf templates
Browse files Browse the repository at this point in the history
New Map {{ .Commit }} available in werf.yaml.
  • Loading branch information
ilya-lesikov committed Jan 17, 2022
1 parent a400981 commit 4c2b33a
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/werf/common/common.go
Expand Up @@ -1189,7 +1189,7 @@ func GetGiterminismManager(cmdData *CmdData) (giterminism_manager.Interface, err
return nil, err
}

headCommit, err := localGitRepo.HeadCommit(BackgroundContext())
headCommit, err := localGitRepo.HeadCommitHash(BackgroundContext())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/stage/git_mapping.go
Expand Up @@ -204,7 +204,7 @@ func (gm *GitMapping) getLatestCommit(ctx context.Context) (string, error) {
return gm.GitRepo().LatestBranchCommit(ctx, gm.Branch)
}

commit, err := gm.GitRepo().HeadCommit(ctx)
commit, err := gm.GitRepo().HeadCommitHash(ctx)
if err != nil {
return "", err
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/config/parser.go
Expand Up @@ -204,6 +204,24 @@ func renderWerfConfigYaml(ctx context.Context, customWerfConfigRelPath, customWe
}
templateData["Env"] = env

headHash, err := giterminismManager.LocalGitRepo().HeadCommitHash(ctx)
if err != nil {
return "", "", fmt.Errorf("unable to get HEAD commit hash: %s", err)
}

headTime, err := giterminismManager.LocalGitRepo().HeadCommitTime(ctx)
if err != nil {
return "", "", fmt.Errorf("unable to get HEAD commit time: %s", err)
}

templateData["Commit"] = map[string]interface{}{
"Hash": headHash,
"Date": map[string]string{
"Human": headTime.String(),
"Unix": strconv.FormatInt(headTime.Unix(), 10),
},
}

config, err := executeTemplate(tmpl, "werfConfig", templateData)

return configPath, config, err
Expand Down
40 changes: 39 additions & 1 deletion pkg/git_repo/base.go
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"sync"
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
Expand Down Expand Up @@ -66,7 +67,11 @@ func (repo *Base) initRepoHandleBackedByWorkTree(ctx context.Context, commit str
return repo.initRepoHandleBackedByWorkTreeFunc(ctx, commit)
}

func (repo *Base) HeadCommit(ctx context.Context) (string, error) {
func (repo *Base) HeadCommitHash(ctx context.Context) (string, error) {
panic("not implemented")
}

func (repo *Base) HeadCommitTime(ctx context.Context) (*time.Time, error) {
panic("not implemented")
}

Expand Down Expand Up @@ -1019,3 +1024,36 @@ func (repo *Base) ListCommitFilesWithGlob(ctx context.Context, commit string, di

return result, nil
}

func baseHeadCommitTime(repo gitRepo, ctx context.Context) (*time.Time, error) {
headCommitHash, err := repo.HeadCommitHash(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get HEAD hash: %s", err)
}

var time *time.Time
if err := repo.withRepoHandle(ctx, headCommitHash, func(repoHandle repo_handle.Handle) error {
headHash, err := newHash(headCommitHash)
if err != nil {
return fmt.Errorf("unable to create new Hash object from commit SHA %q: %s", headCommitHash, err)
}

repo := repoHandle.Repository()
if repo == nil {
return fmt.Errorf("unable to get repository from repoHandle")
}

commit, err := repo.CommitObject(headHash)
if err != nil {
return fmt.Errorf("unable to get commit object for ref %q: %s", headCommitHash, err)
}

time = &commit.Author.When

return nil
}); err != nil {
return nil, err
}

return time, nil
}
10 changes: 8 additions & 2 deletions pkg/git_repo/git_repo.go
Expand Up @@ -3,6 +3,7 @@ package git_repo
import (
"context"
"path/filepath"
"time"

"github.com/werf/werf/pkg/git_repo/repo_handle"
"github.com/werf/werf/pkg/path_matcher"
Expand Down Expand Up @@ -45,7 +46,8 @@ type GitRepo interface {
GetOrCreateArchive(ctx context.Context, opts ArchiveOptions) (Archive, error)
GetOrCreateChecksum(ctx context.Context, opts ChecksumOptions) (string, error)
GetOrCreatePatch(ctx context.Context, opts PatchOptions) (Patch, error)
HeadCommit(ctx context.Context) (string, error)
HeadCommitHash(ctx context.Context) (string, error)
HeadCommitTime(ctx context.Context) (*time.Time, error)
IsAncestor(ctx context.Context, ancestorCommit, descendantCommit string) (bool, error)
IsCommitDirectoryExist(ctx context.Context, commit, path string) (bool, error)
IsCommitExists(ctx context.Context, commit string) (bool, error)
Expand All @@ -62,8 +64,12 @@ type GitRepo interface {
ResolveCommitFilePath(ctx context.Context, commit, path string) (string, error)
TagCommit(ctx context.Context, tag string) (string, error)
WalkCommitFiles(ctx context.Context, commit string, dir string, pathMatcher path_matcher.PathMatcher, fileFunc func(notResolvedPath string) error) error
}

type gitRepo interface {
GitRepo

initRepoHandleBackedByWorkTree(ctx context.Context, commit string) (repo_handle.Handle, error)
withRepoHandle(ctx context.Context, commit string, f func(handle repo_handle.Handle) error) error
}

type Patch interface {
Expand Down
22 changes: 14 additions & 8 deletions pkg/git_repo/local.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"sync"
"time"

"github.com/go-git/go-git/v5"

Expand All @@ -27,7 +28,7 @@ type Local struct {
WorkTreeDir string
GitDir string

headCommit string
headCommitHash string

statusResult *status.Result
mutex sync.Mutex
Expand Down Expand Up @@ -81,7 +82,7 @@ func OpenLocalRepo(ctx context.Context, name, workTreeDir string, opts OpenLocal
l.GitDir,
l.WorkTreeDir,
l.getRepoWorkTreeCacheDir(l.getRepoID()),
l.headCommit,
l.headCommitHash,
true_git.SyncSourceWorktreeWithServiceBranchOptions{
ServiceBranchPrefix: opts.ServiceBranchOptions.Prefix,
GlobExcludeList: opts.ServiceBranchOptions.GlobExcludeList,
Expand All @@ -91,7 +92,7 @@ func OpenLocalRepo(ctx context.Context, name, workTreeDir string, opts OpenLocal
return l, err
}

l.headCommit = devHeadCommit
l.headCommitHash = devHeadCommit
}
}

Expand All @@ -105,9 +106,9 @@ func newLocal(name, workTreeDir, gitDir string) (l *Local, err error) {
}

l = &Local{
WorkTreeDir: workTreeDir,
GitDir: gitDir,
headCommit: headCommit,
WorkTreeDir: workTreeDir,
GitDir: gitDir,
headCommitHash: headCommit,
}
l.Base = NewBase(name, l.initRepoHandleBackedByWorkTree)

Expand Down Expand Up @@ -223,8 +224,13 @@ func (repo *Local) RemoteOriginUrl(_ context.Context) (string, error) {
return repo.remoteOriginUrl(repo.WorkTreeDir)
}

func (repo *Local) HeadCommit(_ context.Context) (string, error) {
return repo.headCommit, nil
func (repo *Local) HeadCommitHash(_ context.Context) (string, error) {
return repo.headCommitHash, nil
}

func (repo *Local) HeadCommitTime(ctx context.Context) (*time.Time, error) {
time, err := baseHeadCommitTime(repo, ctx)
return time, err
}

func (repo *Local) GetOrCreatePatch(ctx context.Context, opts PatchOptions) (Patch, error) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/git_repo/remote.go
Expand Up @@ -225,10 +225,15 @@ func (repo *Remote) Fetch(ctx context.Context) error {
})
}

func (repo *Remote) HeadCommit(_ context.Context) (string, error) {
func (repo *Remote) HeadCommitHash(_ context.Context) (string, error) {
return getHeadCommit(repo.GetClonePath())
}

func (repo *Remote) HeadCommitTime(ctx context.Context) (*time.Time, error) {
time, err := baseHeadCommitTime(repo, ctx)
return time, err
}

func (repo *Remote) findReference(rawRepo *git.Repository, reference string) (string, error) {
refs, err := rawRepo.References()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/git_repo/repo_handle/handle.go
Expand Up @@ -79,6 +79,10 @@ func (h *handle) Submodules() []SubmoduleHandle {
return h.submoduleHandleList
}

func (h *handle) Repository() *git.Repository {
return h.repository
}

type submoduleHandle struct {
Handle
config *config.Submodule
Expand Down
1 change: 1 addition & 0 deletions pkg/git_repo/repo_handle/interface.go
Expand Up @@ -14,6 +14,7 @@ import (
// caching the necessary data from the worktree during initialization,
// and then working exclusively with git objects.
type Handle interface {
Repository() *git.Repository
Submodule(submodulePath string) (SubmoduleHandle, error)
Submodules() []SubmoduleHandle
ReadBlobObjectContent(hash plumbing.Hash) ([]byte, error)
Expand Down

0 comments on commit 4c2b33a

Please sign in to comment.