-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly read git metadata when inside Workspace
Since there is no .git directory in Workspace file system, we need to make an API call to fetch git checkout status (root of the repo, current branch, etc). (api/2.0/workspace/get-status?return_git_info=true). Refactor Repository to accept repository root rather than calculate it. This helps, because Repository is currently created in multiple places and finding the repository root is expensive.
- Loading branch information
Showing
15 changed files
with
217 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package git | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io/fs" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/databricks/cli/libs/dbr" | ||
"github.com/databricks/cli/libs/log" | ||
"github.com/databricks/cli/libs/vfs" | ||
"github.com/databricks/databricks-sdk-go" | ||
"github.com/databricks/databricks-sdk-go/client" | ||
) | ||
|
||
type GitRepositoryInfo struct { | ||
OriginURL string | ||
LatestCommit string | ||
CurrentBranch string | ||
WorktreeRoot vfs.Path | ||
} | ||
|
||
type gitInfo struct { | ||
Branch string `json:"branch"` | ||
HeadCommitID string `json:"head_commit_id"` | ||
Path string `json:"path"` | ||
URL string `json:"url"` | ||
} | ||
|
||
type response struct { | ||
GitInfo *gitInfo `json:"git_info,omitempty"` | ||
} | ||
|
||
func FetchRepositoryInfo(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) { | ||
if strings.HasPrefix(path.Native(), "/Workspace/") && dbr.RunsOnRuntime(ctx) { | ||
return FetchRepositoryInfoAPI(ctx, path, w) | ||
} else { | ||
return FetchRepositoryInfoDotGit(ctx, path) | ||
} | ||
} | ||
|
||
func FetchRepositoryInfoAPI(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) { | ||
apiClient, err := client.New(w.Config) | ||
if err != nil { | ||
return GitRepositoryInfo{}, err | ||
} | ||
|
||
var response response | ||
const apiEndpoint = "/api/2.0/workspace/get-status" | ||
|
||
err = apiClient.Do( | ||
ctx, | ||
http.MethodGet, | ||
apiEndpoint, | ||
nil, | ||
map[string]string{ | ||
"path": path.Native(), | ||
"return_git_info": "true", | ||
}, | ||
&response, | ||
) | ||
|
||
if err != nil { | ||
return GitRepositoryInfo{}, err | ||
} | ||
|
||
// Check if GitInfo is present and extract relevant fields | ||
gi := response.GitInfo | ||
if gi == nil { | ||
log.Warnf(ctx, "Failed to load git info from %s", apiEndpoint) | ||
} else { | ||
fixedPath := fixResponsePath(gi.Path) | ||
return GitRepositoryInfo{ | ||
OriginURL: gi.URL, | ||
LatestCommit: gi.HeadCommitID, | ||
CurrentBranch: gi.Branch, | ||
WorktreeRoot: vfs.MustNew(fixedPath), | ||
}, nil | ||
} | ||
|
||
return GitRepositoryInfo{ | ||
WorktreeRoot: path, | ||
}, nil | ||
} | ||
|
||
func fixResponsePath(path string) string { | ||
if !strings.HasPrefix(path, "/Workspace/") { | ||
return "/Workspace/" + path | ||
} | ||
return path | ||
} | ||
|
||
func FetchRepositoryInfoDotGit(ctx context.Context, path vfs.Path) (GitRepositoryInfo, error) { | ||
rootDir, err := vfs.FindLeafInTree(path, GitDirectoryName) | ||
if err != nil { | ||
if !errors.Is(err, fs.ErrNotExist) { | ||
return GitRepositoryInfo{}, err | ||
} | ||
rootDir = path | ||
} | ||
|
||
repo, err := NewRepository(rootDir) | ||
if err != nil { | ||
return GitRepositoryInfo{}, err | ||
} | ||
|
||
branch, err := repo.CurrentBranch() | ||
if err != nil { | ||
return GitRepositoryInfo{}, nil | ||
} | ||
|
||
commit, err := repo.LatestCommit() | ||
if err != nil { | ||
return GitRepositoryInfo{}, nil | ||
} | ||
|
||
return GitRepositoryInfo{ | ||
OriginURL: repo.OriginUrl(), | ||
LatestCommit: commit, | ||
CurrentBranch: branch, | ||
WorktreeRoot: rootDir, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.