mirror of https://github.com/databricks/cli.git
Add GuessedWorktreeRoot field; this simplifies usage on the caller side; also add comment doc.
This commit is contained in:
parent
4d38475ff3
commit
8a31be78d4
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/databricks/cli/bundle"
|
"github.com/databricks/cli/bundle"
|
||||||
"github.com/databricks/cli/libs/diag"
|
"github.com/databricks/cli/libs/diag"
|
||||||
"github.com/databricks/cli/libs/git"
|
"github.com/databricks/cli/libs/git"
|
||||||
"github.com/databricks/cli/libs/vfs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type loadGitDetails struct{}
|
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)...)
|
diags = append(diags, diag.WarningFromErr(err)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.WorktreeRoot == "" {
|
b.WorktreeRoot = info.GuessedWorktreeRoot
|
||||||
b.WorktreeRoot = b.BundleRoot
|
|
||||||
} else {
|
|
||||||
b.WorktreeRoot = vfs.MustNew(info.WorktreeRoot)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Config.Bundle.Git.ActualBranch = info.CurrentBranch
|
b.Config.Bundle.Git.ActualBranch = info.CurrentBranch
|
||||||
if b.Config.Bundle.Git.Branch == "" {
|
if b.Config.Bundle.Git.Branch == "" {
|
||||||
|
|
|
@ -73,12 +73,8 @@ func (f *syncFlags) syncOptionsFromArgs(cmd *cobra.Command, args []string) (*syn
|
||||||
log.Warnf(ctx, "Failed to read git info: %s", err)
|
log.Warnf(ctx, "Failed to read git info: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.WorktreeRoot == "" {
|
|
||||||
info.WorktreeRoot = localRoot.Native()
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := sync.SyncOptions{
|
opts := sync.SyncOptions{
|
||||||
WorktreeRoot: vfs.MustNew(info.WorktreeRoot),
|
WorktreeRoot: info.GuessedWorktreeRoot,
|
||||||
LocalRoot: localRoot,
|
LocalRoot: localRoot,
|
||||||
Paths: []string{"."},
|
Paths: []string{"."},
|
||||||
Include: nil,
|
Include: nil,
|
||||||
|
|
|
@ -16,10 +16,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitRepositoryInfo 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
|
OriginURL string
|
||||||
LatestCommit string
|
LatestCommit string
|
||||||
CurrentBranch string
|
CurrentBranch string
|
||||||
WorktreeRoot string
|
|
||||||
|
// 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 {
|
type gitInfo struct {
|
||||||
|
@ -33,6 +39,15 @@ type response struct {
|
||||||
GitInfo *gitInfo `json:"git_info,omitempty"`
|
GitInfo *gitInfo `json:"git_info,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 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 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, GitRepositoryInfo will have non-empty WorktreeRoot and corresponding GuessedWorktreeRoot.
|
||||||
|
// Other strings fields will be "" and err will be nil.
|
||||||
|
// - In successful case, all fields are set to proper git repository metadata.
|
||||||
func FetchRepositoryInfo(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) {
|
func FetchRepositoryInfo(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) {
|
||||||
if strings.HasPrefix(path.Native(), "/Workspace/") && dbr.RunsOnRuntime(ctx) {
|
if strings.HasPrefix(path.Native(), "/Workspace/") && dbr.RunsOnRuntime(ctx) {
|
||||||
return fetchRepositoryInfoAPI(ctx, path, w)
|
return fetchRepositoryInfoAPI(ctx, path, w)
|
||||||
|
@ -42,9 +57,14 @@ func FetchRepositoryInfo(ctx context.Context, path vfs.Path, w *databricks.Works
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchRepositoryInfoAPI(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) {
|
func fetchRepositoryInfoAPI(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) {
|
||||||
|
result := GitRepositoryInfo{
|
||||||
|
// For convenience, this field defaults to input path, even if err is also set.
|
||||||
|
GuessedWorktreeRoot: path,
|
||||||
|
}
|
||||||
|
|
||||||
apiClient, err := client.New(w.Config)
|
apiClient, err := client.New(w.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return GitRepositoryInfo{}, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var response response
|
var response response
|
||||||
|
@ -63,23 +83,23 @@ func fetchRepositoryInfoAPI(ctx context.Context, path vfs.Path, w *databricks.Wo
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return GitRepositoryInfo{}, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if GitInfo is present and extract relevant fields
|
// Check if GitInfo is present and extract relevant fields
|
||||||
gi := response.GitInfo
|
gi := response.GitInfo
|
||||||
if gi != nil {
|
if gi != nil {
|
||||||
fixedPath := ensureWorkspacePrefix(gi.Path)
|
fixedPath := ensureWorkspacePrefix(gi.Path)
|
||||||
return GitRepositoryInfo{
|
result.OriginURL = gi.URL
|
||||||
OriginURL: gi.URL,
|
result.LatestCommit = gi.HeadCommitID
|
||||||
LatestCommit: gi.HeadCommitID,
|
result.CurrentBranch = gi.Branch
|
||||||
CurrentBranch: gi.Branch,
|
result.WorktreeRoot = fixedPath
|
||||||
WorktreeRoot: fixedPath,
|
result.GuessedWorktreeRoot = vfs.MustNew(fixedPath)
|
||||||
}, nil
|
} else {
|
||||||
|
log.Warnf(ctx, "Failed to load git info from %s", apiEndpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warnf(ctx, "Failed to load git info from %s", apiEndpoint)
|
return result, nil
|
||||||
return GitRepositoryInfo{}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureWorkspacePrefix(p string) string {
|
func ensureWorkspacePrefix(p string) string {
|
||||||
|
@ -90,17 +110,20 @@ func ensureWorkspacePrefix(p string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchRepositoryInfoDotGit(ctx context.Context, path vfs.Path) (GitRepositoryInfo, error) {
|
func fetchRepositoryInfoDotGit(ctx context.Context, path vfs.Path) (GitRepositoryInfo, error) {
|
||||||
|
result := GitRepositoryInfo{
|
||||||
|
GuessedWorktreeRoot: path,
|
||||||
|
}
|
||||||
|
|
||||||
rootDir, err := vfs.FindLeafInTree(path, GitDirectoryName)
|
rootDir, err := vfs.FindLeafInTree(path, GitDirectoryName)
|
||||||
if err != nil || rootDir == nil {
|
if err != nil || rootDir == nil {
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
return GitRepositoryInfo{}, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
return GitRepositoryInfo{}, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result := GitRepositoryInfo{
|
result.WorktreeRoot = rootDir.Native()
|
||||||
WorktreeRoot: rootDir.Native(),
|
result.GuessedWorktreeRoot = rootDir
|
||||||
}
|
|
||||||
|
|
||||||
repo, err := NewRepository(rootDir)
|
repo, err := NewRepository(rootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue