mirror of https://github.com/databricks/cli.git
Compare commits
No commits in common. "a064d91ac52f104ef6fa2712c9643e8f53fe7545" and "a623cfdd23fe9de5ffe7327379a4c4c91c16f165" have entirely different histories.
a064d91ac5
...
a623cfdd23
|
@ -7,7 +7,6 @@ import (
|
|||
"github.com/databricks/cli/bundle"
|
||||
"github.com/databricks/cli/libs/diag"
|
||||
"github.com/databricks/cli/libs/git"
|
||||
"github.com/databricks/cli/libs/vfs"
|
||||
)
|
||||
|
||||
type loadGitDetails struct{}
|
||||
|
@ -27,11 +26,7 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
|
|||
diags = append(diags, diag.WarningFromErr(err)...)
|
||||
}
|
||||
|
||||
if info.WorktreeRoot == "" {
|
||||
b.WorktreeRoot = b.BundleRoot
|
||||
} else {
|
||||
b.WorktreeRoot = vfs.MustNew(info.WorktreeRoot)
|
||||
}
|
||||
b.WorktreeRoot = info.GuessedWorktreeRoot
|
||||
|
||||
b.Config.Bundle.Git.ActualBranch = info.CurrentBranch
|
||||
if b.Config.Bundle.Git.Branch == "" {
|
||||
|
@ -53,8 +48,8 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
|
|||
relBundlePath, err := filepath.Rel(b.WorktreeRoot.Native(), b.BundleRoot.Native())
|
||||
if err != nil {
|
||||
diags = append(diags, diag.FromErr(err)...)
|
||||
} else {
|
||||
b.Config.Bundle.Git.BundleRootPath = filepath.ToSlash(relBundlePath)
|
||||
}
|
||||
|
||||
b.Config.Bundle.Git.BundleRootPath = filepath.ToSlash(relBundlePath)
|
||||
return diags
|
||||
}
|
||||
|
|
|
@ -73,16 +73,8 @@ func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*syn
|
|||
log.Warnf(ctx, "Failed to read git info: %s", err)
|
||||
}
|
||||
|
||||
var WorktreeRoot vfs.Path
|
||||
|
||||
if info.WorktreeRoot == "" {
|
||||
WorktreeRoot = localRoot
|
||||
} else {
|
||||
WorktreeRoot = vfs.MustNew(info.WorktreeRoot)
|
||||
}
|
||||
|
||||
opts := sync.SyncOptions{
|
||||
WorktreeRoot: WorktreeRoot,
|
||||
WorktreeRoot: info.GuessedWorktreeRoot,
|
||||
LocalRoot: localRoot,
|
||||
Paths: []string{"."},
|
||||
Include: nil,
|
||||
|
|
|
@ -3,7 +3,6 @@ package internal
|
|||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -17,18 +16,18 @@ import (
|
|||
const examplesRepoUrl = "https://github.com/databricks/bundle-examples"
|
||||
const examplesRepoProvider = "gitHub"
|
||||
|
||||
func assertFullGitInfo(t *testing.T, expectedRoot string, info git.RepositoryInfo) {
|
||||
func assertFullGitInfo(t *testing.T, expectedRoot string, info git.GitRepositoryInfo) {
|
||||
assert.Equal(t, "main", info.CurrentBranch)
|
||||
assert.NotEmpty(t, info.LatestCommit)
|
||||
assert.Equal(t, examplesRepoUrl, info.OriginURL)
|
||||
assert.Equal(t, expectedRoot, info.WorktreeRoot)
|
||||
}
|
||||
|
||||
func assertEmptyGitInfo(t *testing.T, info git.RepositoryInfo) {
|
||||
func assertEmptyGitInfo(t *testing.T, info git.GitRepositoryInfo) {
|
||||
assertSparseGitInfo(t, "", info)
|
||||
}
|
||||
|
||||
func assertSparseGitInfo(t *testing.T, expectedRoot string, info git.RepositoryInfo) {
|
||||
func assertSparseGitInfo(t *testing.T, expectedRoot string, info git.GitRepositoryInfo) {
|
||||
assert.Equal(t, "", info.CurrentBranch)
|
||||
assert.Equal(t, "", info.LatestCommit)
|
||||
assert.Equal(t, "", info.OriginURL)
|
||||
|
@ -40,7 +39,7 @@ func TestAccFetchRepositoryInfoAPI_FromRepo(t *testing.T) {
|
|||
me, err := wt.W.CurrentUser.Me(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
targetPath := acc.RandomName(path.Join("/Workspace/Users", me.UserName, "/testing-clone-bundle-examples-"))
|
||||
targetPath := acc.RandomName("/Workspace/Users/" + me.UserName + "/testing-clone-bundle-examples-")
|
||||
stdout, stderr := RequireSuccessfulRun(t, "repos", "create", examplesRepoUrl, examplesRepoProvider, "--path", targetPath)
|
||||
t.Cleanup(func() {
|
||||
RequireSuccessfulRun(t, "repos", "delete", targetPath)
|
||||
|
@ -51,7 +50,7 @@ func TestAccFetchRepositoryInfoAPI_FromRepo(t *testing.T) {
|
|||
ctx = dbr.MockRuntime(ctx, true)
|
||||
|
||||
for _, inputPath := range []string{
|
||||
path.Join(targetPath, "knowledge_base/dashboard_nyc_taxi"),
|
||||
targetPath + "/knowledge_base/dashboard_nyc_taxi",
|
||||
targetPath,
|
||||
} {
|
||||
t.Run(inputPath, func(t *testing.T) {
|
||||
|
@ -67,13 +66,14 @@ func TestAccFetchRepositoryInfoAPI_FromNonRepo(t *testing.T) {
|
|||
me, err := wt.W.CurrentUser.Me(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
rootPath := acc.RandomName(path.Join("/Workspace/Users", me.UserName, "testing-nonrepo-"))
|
||||
_, stderr := RequireSuccessfulRun(t, "workspace", "mkdirs", path.Join(rootPath, "a/b/c"))
|
||||
rootPath := acc.RandomName("/Workspace/Users/" + me.UserName + "/testing-nonrepo-")
|
||||
_, stderr := RequireSuccessfulRun(t, "workspace", "mkdirs", rootPath+"/a/b/c")
|
||||
t.Cleanup(func() {
|
||||
RequireSuccessfulRun(t, "workspace", "delete", "--recursive", rootPath)
|
||||
})
|
||||
|
||||
assert.Empty(t, stderr.String())
|
||||
//assert.NotEmpty(t, stdout.String())
|
||||
ctx = dbr.MockRuntime(ctx, true)
|
||||
|
||||
tests := []struct {
|
||||
|
@ -81,7 +81,7 @@ func TestAccFetchRepositoryInfoAPI_FromNonRepo(t *testing.T) {
|
|||
msg string
|
||||
}{
|
||||
{
|
||||
input: path.Join(rootPath, "a/b/c"),
|
||||
input: rootPath + "/a/b/c",
|
||||
msg: "",
|
||||
},
|
||||
{
|
||||
|
@ -89,7 +89,7 @@ func TestAccFetchRepositoryInfoAPI_FromNonRepo(t *testing.T) {
|
|||
msg: "",
|
||||
},
|
||||
{
|
||||
input: path.Join(rootPath, "/non-existent"),
|
||||
input: rootPath + "/non-existent",
|
||||
msg: "doesn't exist",
|
||||
},
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ func TestAccFetchRepositoryInfoDotGit_FromGitRepo(t *testing.T) {
|
|||
repo := cloneRepoLocally(t, examplesRepoUrl)
|
||||
|
||||
for _, inputPath := range []string{
|
||||
filepath.Join(repo, "knowledge_base/dashboard_nyc_taxi"),
|
||||
repo + "/knowledge_base/dashboard_nyc_taxi",
|
||||
repo,
|
||||
} {
|
||||
t.Run(inputPath, func(t *testing.T) {
|
||||
|
@ -140,12 +140,12 @@ func TestAccFetchRepositoryInfoDotGit_FromNonGitRepo(t *testing.T) {
|
|||
|
||||
tempDir := t.TempDir()
|
||||
root := filepath.Join(tempDir, "repo")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, "a/b/c"), 0700))
|
||||
require.NoError(t, os.MkdirAll(root+"/a/b/c", 0700))
|
||||
|
||||
tests := []string{
|
||||
filepath.Join(root, "a/b/c"),
|
||||
root + "/a/b/c",
|
||||
root,
|
||||
filepath.Join(root, "/non-existent"),
|
||||
root + "/non-existent",
|
||||
}
|
||||
|
||||
for _, input := range tests {
|
||||
|
@ -162,9 +162,9 @@ func TestAccFetchRepositoryInfoDotGit_FromBrokenGitRepo(t *testing.T) {
|
|||
|
||||
tempDir := t.TempDir()
|
||||
root := filepath.Join(tempDir, "repo")
|
||||
path := filepath.Join(root, "a/b/c")
|
||||
path := root + "/a/b/c"
|
||||
require.NoError(t, os.MkdirAll(path, 0700))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, ".git"), []byte(""), 0000))
|
||||
require.NoError(t, os.WriteFile(root+"/.git", []byte(""), 0000))
|
||||
|
||||
info, err := git.FetchRepositoryInfo(ctx, path, wt.W)
|
||||
assert.NoError(t, err)
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
"github.com/databricks/databricks-sdk-go/client"
|
||||
)
|
||||
|
||||
type RepositoryInfo struct {
|
||||
type GitRepositoryInfo struct {
|
||||
// Various metadata about the repo. Each could be "" if it could not be read. No error is returned for such case.
|
||||
OriginURL string
|
||||
LatestCommit string
|
||||
|
@ -25,6 +25,9 @@ type RepositoryInfo struct {
|
|||
|
||||
// Absolute path to determined worktree root or "" if worktree root could not be determined.
|
||||
WorktreeRoot string
|
||||
|
||||
// vfs.Path variant of WorktreeRoot if WorktreeRoot is set; otherwise defaults to input path.
|
||||
GuessedWorktreeRoot vfs.Path
|
||||
}
|
||||
|
||||
type gitInfo struct {
|
||||
|
@ -39,13 +42,15 @@ type response struct {
|
|||
}
|
||||
|
||||
// Fetch repository information either by quering .git or by fetching it from API (for dabs-in-workspace case).
|
||||
// - In case we could not find git repository, all string fields of RepositoryInfo will be "" and err will be nil.
|
||||
// - In case we could not find git repository, all string fields of GitRepositoryInfo will be "" and err will be nil.
|
||||
// - If there were any errors when trying to determine git root (e.g. API call returned an error or there were permission issues
|
||||
// reading the file system), all strings fields of RepositoryInfo will be "" and err will be non-nil.
|
||||
// reading the file system), all strings fields of GitRepositoryInfo will be "" and err will be non-nil.
|
||||
// - For convenience, GuessedWorktreeRoot parameter will be set to path in the above two cases.
|
||||
// - If we could determine git worktree root but there were errors when reading metadata (origin, branch, commit), those errors
|
||||
// will be logged as warnings, RepositoryInfo is guaranteed to have non-empty WorktreeRoot and other fields on best effort basis.
|
||||
// will be logged as warnings, GitRepositoryInfo is guaranteed to have non-empty WorktreeRoot and corresponding GuessedWorktreeRoot
|
||||
// and other fields on best effort basis. The err will be nil.
|
||||
// - In successful case, all fields are set to proper git repository metadata.
|
||||
func FetchRepositoryInfo(ctx context.Context, path string, w *databricks.WorkspaceClient) (RepositoryInfo, error) {
|
||||
func FetchRepositoryInfo(ctx context.Context, path string, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) {
|
||||
if strings.HasPrefix(path, "/Workspace/") && dbr.RunsOnRuntime(ctx) {
|
||||
return fetchRepositoryInfoAPI(ctx, path, w)
|
||||
} else {
|
||||
|
@ -53,8 +58,11 @@ func FetchRepositoryInfo(ctx context.Context, path string, w *databricks.Workspa
|
|||
}
|
||||
}
|
||||
|
||||
func fetchRepositoryInfoAPI(ctx context.Context, path string, w *databricks.WorkspaceClient) (RepositoryInfo, error) {
|
||||
result := RepositoryInfo{}
|
||||
func fetchRepositoryInfoAPI(ctx context.Context, path string, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) {
|
||||
result := GitRepositoryInfo{
|
||||
// For convenience, this field defaults to input path, even if err is also set.
|
||||
GuessedWorktreeRoot: vfs.MustNew(path),
|
||||
}
|
||||
|
||||
apiClient, err := client.New(w.Config)
|
||||
if err != nil {
|
||||
|
@ -88,6 +96,8 @@ func fetchRepositoryInfoAPI(ctx context.Context, path string, w *databricks.Work
|
|||
result.LatestCommit = gi.HeadCommitID
|
||||
result.CurrentBranch = gi.Branch
|
||||
result.WorktreeRoot = fixedPath
|
||||
// Note, this won't work on Windows since vfs.MustNew will call filepath.Abs
|
||||
result.GuessedWorktreeRoot = vfs.MustNew(fixedPath)
|
||||
} else {
|
||||
log.Warnf(ctx, "Failed to load git info from %s", apiEndpoint)
|
||||
}
|
||||
|
@ -102,8 +112,10 @@ func ensureWorkspacePrefix(p string) string {
|
|||
return p
|
||||
}
|
||||
|
||||
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo, error) {
|
||||
result := RepositoryInfo{}
|
||||
func fetchRepositoryInfoDotGit(ctx context.Context, path string) (GitRepositoryInfo, error) {
|
||||
result := GitRepositoryInfo{
|
||||
GuessedWorktreeRoot: vfs.MustNew(path),
|
||||
}
|
||||
|
||||
rootDir, err := findLeafInTree(path, GitDirectoryName)
|
||||
if rootDir == "" {
|
||||
|
@ -111,8 +123,9 @@ func fetchRepositoryInfoDotGit(ctx context.Context, path string) (RepositoryInfo
|
|||
}
|
||||
|
||||
result.WorktreeRoot = rootDir
|
||||
result.GuessedWorktreeRoot = vfs.MustNew(rootDir)
|
||||
|
||||
repo, err := NewRepository(vfs.MustNew(rootDir))
|
||||
repo, err := NewRepository(result.GuessedWorktreeRoot)
|
||||
if err != nil {
|
||||
log.Warnf(ctx, "failed to read .git: %s", err)
|
||||
|
||||
|
@ -141,7 +154,7 @@ func findLeafInTree(p string, leafName string) (string, error) {
|
|||
_, err = os.Stat(filepath.Join(p, leafName))
|
||||
|
||||
if err == nil {
|
||||
// Found [leafName] in p
|
||||
// found .git in p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue