Skip to content

Commit

Permalink
Switch to folders.FindDirWithLeaf (#1963)
Browse files Browse the repository at this point in the history
## Changes
Remove two duplicate implementations of the same logic, switch
everywhere to folders.FindDirWithLeaf.

Add Abs() call to FindDirWithLeaf, it cannot really work on relative
paths.

## Tests
Existing tests.
  • Loading branch information
denik authored Dec 11, 2024
1 parent 67f08ba commit 4236e71
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 104 deletions.
6 changes: 5 additions & 1 deletion bundle/config/mutator/load_git_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mutator

import (
"context"
"errors"
"os"
"path/filepath"

"github.com/databricks/cli/bundle"
Expand All @@ -24,7 +26,9 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
var diags diag.Diagnostics
info, err := git.FetchRepositoryInfo(ctx, b.BundleRoot.Native(), b.WorkspaceClient())
if err != nil {
diags = append(diags, diag.WarningFromErr(err)...)
if !errors.Is(err, os.ErrNotExist) {
diags = append(diags, diag.WarningFromErr(err)...)
}
}

if info.WorktreeRoot == "" {
Expand Down
7 changes: 3 additions & 4 deletions internal/bundle/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/folders"
"github.com/databricks/cli/libs/template"
"github.com/databricks/cli/libs/vfs"
"github.com/databricks/databricks-sdk-go"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -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) {
cwd := vfs.MustNew(".")
gitRoot, err := vfs.FindLeafInTree(cwd, ".git")
gitRoot, err := folders.FindDirWithLeaf(".", ".git")
require.NoError(t, err)

t.Setenv("BUNDLE_ROOT", root)

// Create the command
cmd := exec.Command("go", append([]string{"run", "main.go"}, args...)...)
cmd.Dir = gitRoot.Native()
cmd.Dir = gitRoot

// Create buffers to capture output
var outBuffer, errBuffer bytes.Buffer
Expand Down
4 changes: 2 additions & 2 deletions internal/git_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestAccFetchRepositoryInfoAPI_FromNonRepo(t *testing.T) {
assert.NoError(t, err)
} else {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.msg)
assert.ErrorContains(t, err, test.msg)
}
assertEmptyGitInfo(t, info)
})
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestAccFetchRepositoryInfoDotGit_FromNonGitRepo(t *testing.T) {
for _, input := range tests {
t.Run(input, func(t *testing.T) {
info, err := git.FetchRepositoryInfo(ctx, input, wt.W)
assert.NoError(t, err)
assert.ErrorIs(t, err, os.ErrNotExist)
assertEmptyGitInfo(t, info)
})
}
Expand Down
4 changes: 4 additions & 0 deletions libs/folders/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
// FindDirWithLeaf returns the first directory that holds `leaf`,
// traversing up to the root of the filesystem, starting at `dir`.
func FindDirWithLeaf(dir string, leaf string) (string, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return "", err
}
for {
_, err := os.Stat(filepath.Join(dir, leaf))

Expand Down
32 changes: 2 additions & 30 deletions libs/git/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package git

import (
"context"
"errors"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"strings"

"github.com/databricks/cli/libs/dbr"
"github.com/databricks/cli/libs/folders"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/vfs"
"github.com/databricks/databricks-sdk-go"
Expand Down Expand Up @@ -105,7 +102,7 @@ func ensureWorkspacePrefix(p string) string {
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo, error) {
result := RepositoryInfo{}

rootDir, err := findLeafInTree(path, GitDirectoryName)
rootDir, err := folders.FindDirWithLeaf(path, GitDirectoryName)
if rootDir == "" {
return result, err
}
Expand Down Expand Up @@ -134,28 +131,3 @@ func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo

return result, nil
}

func findLeafInTree(p string, leafName string) (string, error) {
var err error
for i := 0; i < 10000; i++ {
_, err = os.Stat(filepath.Join(p, leafName))

if err == nil {
// Found [leafName] in p
return p, nil
}

// ErrNotExist means we continue traversal up the tree.
if errors.Is(err, fs.ErrNotExist) {
parent := filepath.Dir(p)
if parent == p {
return "", nil
}
p = parent
continue
}
break
}

return "", err
}
29 changes: 0 additions & 29 deletions libs/vfs/leaf.go

This file was deleted.

38 changes: 0 additions & 38 deletions libs/vfs/leaf_test.go

This file was deleted.

0 comments on commit 4236e71

Please sign in to comment.