github.com/databricks/cli@v0.203.0/bundle/config/mutator/load_git_details.go (about)

     1  package mutator
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/databricks/cli/bundle"
     7  	"github.com/databricks/cli/libs/git"
     8  	"github.com/databricks/cli/libs/log"
     9  )
    10  
    11  type loadGitDetails struct{}
    12  
    13  func LoadGitDetails() *loadGitDetails {
    14  	return &loadGitDetails{}
    15  }
    16  
    17  func (m *loadGitDetails) Name() string {
    18  	return "LoadGitDetails"
    19  }
    20  
    21  func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) error {
    22  	// Load relevant git repository
    23  	repo, err := git.NewRepository(b.Config.Path)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	// Read branch name of current checkout
    29  	branch, err := repo.CurrentBranch()
    30  	if err == nil {
    31  		b.Config.Bundle.Git.ActualBranch = branch
    32  		if b.Config.Bundle.Git.Branch == "" {
    33  			// Only load branch if there's no user defined value
    34  			b.Config.Bundle.Git.Inferred = true
    35  			b.Config.Bundle.Git.Branch = branch
    36  		}
    37  	} else {
    38  		log.Warnf(ctx, "failed to load current branch: %s", err)
    39  	}
    40  
    41  	// load commit hash if undefined
    42  	if b.Config.Bundle.Git.Commit == "" {
    43  		commit, err := repo.LatestCommit()
    44  		if err != nil {
    45  			log.Warnf(ctx, "failed to load latest commit: %s", err)
    46  		} else {
    47  			b.Config.Bundle.Git.Commit = commit
    48  		}
    49  	}
    50  	// load origin url if undefined
    51  	if b.Config.Bundle.Git.OriginURL == "" {
    52  		remoteUrl := repo.OriginUrl()
    53  		b.Config.Bundle.Git.OriginURL = remoteUrl
    54  	}
    55  	return nil
    56  }