mirror of https://github.com/databricks/cli.git
Clean up vfs/leaf.go
The git module now uses alternative implementation that uses os module + strings as path directly (#1945). The remaining use case is also updated to use git.FindLeafInTree, as it's a better fit.
This commit is contained in:
parent
1b2be1b2cb
commit
d99bf3ac74
|
@ -18,8 +18,8 @@ import (
|
||||||
"github.com/databricks/cli/libs/env"
|
"github.com/databricks/cli/libs/env"
|
||||||
"github.com/databricks/cli/libs/filer"
|
"github.com/databricks/cli/libs/filer"
|
||||||
"github.com/databricks/cli/libs/flags"
|
"github.com/databricks/cli/libs/flags"
|
||||||
|
"github.com/databricks/cli/libs/git"
|
||||||
"github.com/databricks/cli/libs/template"
|
"github.com/databricks/cli/libs/template"
|
||||||
"github.com/databricks/cli/libs/vfs"
|
|
||||||
"github.com/databricks/databricks-sdk-go"
|
"github.com/databricks/databricks-sdk-go"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -144,15 +144,14 @@ func getBundleRemoteRootPath(w *databricks.WorkspaceClient, t *testing.T, unique
|
||||||
}
|
}
|
||||||
|
|
||||||
func blackBoxRun(t *testing.T, root string, args ...string) (stdout string, stderr string) {
|
func blackBoxRun(t *testing.T, root string, args ...string) (stdout string, stderr string) {
|
||||||
cwd := vfs.MustNew(".")
|
gitRoot, err := git.FindLeafInTree(".", ".git")
|
||||||
gitRoot, err := vfs.FindLeafInTree(cwd, ".git")
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
t.Setenv("BUNDLE_ROOT", root)
|
t.Setenv("BUNDLE_ROOT", root)
|
||||||
|
|
||||||
// Create the command
|
// Create the command
|
||||||
cmd := exec.Command("go", append([]string{"run", "main.go"}, args...)...)
|
cmd := exec.Command("go", append([]string{"run", "main.go"}, args...)...)
|
||||||
cmd.Dir = gitRoot.Native()
|
cmd.Dir = gitRoot
|
||||||
|
|
||||||
// Create buffers to capture output
|
// Create buffers to capture output
|
||||||
var outBuffer, errBuffer bytes.Buffer
|
var outBuffer, errBuffer bytes.Buffer
|
||||||
|
|
|
@ -105,7 +105,7 @@ func ensureWorkspacePrefix(p string) string {
|
||||||
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo, error) {
|
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo, error) {
|
||||||
result := RepositoryInfo{}
|
result := RepositoryInfo{}
|
||||||
|
|
||||||
rootDir, err := findLeafInTree(path, GitDirectoryName)
|
rootDir, err := FindLeafInTree(path, GitDirectoryName)
|
||||||
if rootDir == "" {
|
if rootDir == "" {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
@ -135,7 +135,7 @@ func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findLeafInTree(p string, leafName string) (string, error) {
|
func FindLeafInTree(p string, leafName string) (string, error) {
|
||||||
var err error
|
var err error
|
||||||
for i := 0; i < 10000; i++ {
|
for i := 0; i < 10000; i++ {
|
||||||
_, err = os.Stat(filepath.Join(p, leafName))
|
_, err = os.Stat(filepath.Join(p, leafName))
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
package vfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io/fs"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FindLeafInTree returns the first path that holds `name`,
|
|
||||||
// traversing up to the root of the filesystem, starting at `p`.
|
|
||||||
func FindLeafInTree(p Path, name string) (Path, error) {
|
|
||||||
for p != nil {
|
|
||||||
_, err := fs.Stat(p, name)
|
|
||||||
|
|
||||||
// No error means we found the leaf in p.
|
|
||||||
if err == nil {
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrNotExist means we continue traversal up the tree.
|
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
|
||||||
p = p.Parent()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fs.ErrNotExist
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
package vfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFindLeafInTree(t *testing.T) {
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
root := filepath.Join(wd, "..", "..")
|
|
||||||
|
|
||||||
// Find from working directory should work.
|
|
||||||
{
|
|
||||||
out, err := FindLeafInTree(MustNew(wd), ".git")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, root, out.Native())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find from project root itself should work.
|
|
||||||
{
|
|
||||||
out, err := FindLeafInTree(MustNew(root), ".git")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, root, out.Native())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find for something that doesn't exist should work.
|
|
||||||
{
|
|
||||||
out, err := FindLeafInTree(MustNew(root), "this-leaf-doesnt-exist-anywhere")
|
|
||||||
assert.ErrorIs(t, err, os.ErrNotExist)
|
|
||||||
assert.Equal(t, nil, out)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue