diff --git a/bundle/bundle.go b/bundle/bundle.go index 5e6d5438..fd746824 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -7,11 +7,14 @@ package bundle import ( + "fmt" "os" "path/filepath" "sync" "github.com/databricks/bricks/bundle/config" + "github.com/databricks/bricks/folders" + "github.com/databricks/bricks/libs/git" "github.com/databricks/bricks/libs/locker" "github.com/databricks/databricks-sdk-go" "github.com/hashicorp/terraform-exec/tfexec" @@ -115,3 +118,12 @@ func (b *Bundle) CacheDir(paths ...string) (string, error) { return dir, nil } + +func (b *Bundle) GitRepository() (*git.Repository, error) { + rootPath, err := folders.FindDirWithLeaf(b.Config.Path, ".git") + if err != nil { + return nil, fmt.Errorf("unable to locate repository root: %w", err) + } + + return git.NewRepository(rootPath) +} diff --git a/libs/git/repository.go b/libs/git/repository.go index ba788f9b..00dc335e 100644 --- a/libs/git/repository.go +++ b/libs/git/repository.go @@ -32,6 +32,11 @@ type Repository struct { ignore map[string][]ignoreRules } +// Root returns the repository root. +func (r *Repository) Root() string { + return r.rootPath +} + // loadConfig loads and combines user specific and repository specific configuration files. func (r *Repository) loadConfig() (*config, error) { config, err := globalGitConfig() diff --git a/libs/git/repository_test.go b/libs/git/repository_test.go index 63eade73..34fbf928 100644 --- a/libs/git/repository_test.go +++ b/libs/git/repository_test.go @@ -26,6 +26,9 @@ func TestRepository(t *testing.T) { tr := testRepository{t, repo} require.NoError(t, err) + // Check that the root path is real. + assert.True(t, filepath.IsAbs(repo.Root())) + // Check that top level ignores work. assert.True(t, tr.Ignore(".DS_Store")) assert.True(t, tr.Ignore("foo.pyc"))