2023-04-26 14:54:36 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/git"
|
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-04-26 14:54:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type loadGitDetails struct{}
|
|
|
|
|
|
|
|
func LoadGitDetails() *loadGitDetails {
|
|
|
|
return &loadGitDetails{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loadGitDetails) Name() string {
|
|
|
|
return "LoadGitDetails"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) error {
|
2023-04-26 14:54:36 +00:00
|
|
|
// Load relevant git repository
|
|
|
|
repo, err := git.NewRepository(b.Config.Path)
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-04-26 14:54:36 +00:00
|
|
|
}
|
|
|
|
// load branch name if undefined
|
|
|
|
if b.Config.Bundle.Git.Branch == "" {
|
|
|
|
branch, err := repo.CurrentBranch()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf(ctx, "failed to load current branch: %s", err)
|
|
|
|
} else {
|
|
|
|
b.Config.Bundle.Git.Branch = branch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// load commit hash if undefined
|
|
|
|
if b.Config.Bundle.Git.Commit == "" {
|
|
|
|
commit, err := repo.LatestCommit()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf(ctx, "failed to load latest commit: %s", err)
|
|
|
|
} else {
|
|
|
|
b.Config.Bundle.Git.Commit = commit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// load origin url if undefined
|
|
|
|
if b.Config.Bundle.Git.OriginURL == "" {
|
|
|
|
remoteUrl := repo.OriginUrl()
|
|
|
|
b.Config.Bundle.Git.OriginURL = remoteUrl
|
|
|
|
}
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-26 14:54:36 +00:00
|
|
|
}
|