Function to find the Git repository containing a bundle (#289)

## Changes

Useful functions from #277.

## Tests

Tests pass.
This commit is contained in:
Pieter Noordhuis 2023-03-29 16:36:35 +02:00 committed by GitHub
parent 8fd3dccca9
commit 8af934bbbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -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)
}

View File

@ -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()

View File

@ -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"))