|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io/fs" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/databricks/cli/libs/dbr" |
| 11 | + "github.com/databricks/cli/libs/log" |
| 12 | + "github.com/databricks/cli/libs/vfs" |
| 13 | + "github.com/databricks/databricks-sdk-go" |
| 14 | + "github.com/databricks/databricks-sdk-go/client" |
| 15 | +) |
| 16 | + |
| 17 | +type GitRepositoryInfo struct { |
| 18 | + OriginURL string |
| 19 | + LatestCommit string |
| 20 | + CurrentBranch string |
| 21 | + WorktreeRoot vfs.Path |
| 22 | +} |
| 23 | + |
| 24 | +type gitInfo struct { |
| 25 | + Branch string `json:"branch"` |
| 26 | + HeadCommitID string `json:"head_commit_id"` |
| 27 | + Path string `json:"path"` |
| 28 | + URL string `json:"url"` |
| 29 | +} |
| 30 | + |
| 31 | +type response struct { |
| 32 | + GitInfo *gitInfo `json:"git_info,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +func FetchRepositoryInfo(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) { |
| 36 | + if strings.HasPrefix(path.Native(), "/Workspace/") && dbr.RunsOnRuntime(ctx) { |
| 37 | + return FetchRepositoryInfoAPI(ctx, path, w) |
| 38 | + } else { |
| 39 | + return FetchRepositoryInfoDotGit(ctx, path) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func FetchRepositoryInfoAPI(ctx context.Context, path vfs.Path, w *databricks.WorkspaceClient) (GitRepositoryInfo, error) { |
| 44 | + apiClient, err := client.New(w.Config) |
| 45 | + if err != nil { |
| 46 | + return GitRepositoryInfo{}, err |
| 47 | + } |
| 48 | + |
| 49 | + var response response |
| 50 | + const apiEndpoint = "/api/2.0/workspace/get-status" |
| 51 | + |
| 52 | + err = apiClient.Do( |
| 53 | + ctx, |
| 54 | + http.MethodGet, |
| 55 | + apiEndpoint, |
| 56 | + nil, |
| 57 | + map[string]string{ |
| 58 | + "path": path.Native(), |
| 59 | + "return_git_info": "true", |
| 60 | + }, |
| 61 | + &response, |
| 62 | + ) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + return GitRepositoryInfo{}, err |
| 66 | + } |
| 67 | + |
| 68 | + // Check if GitInfo is present and extract relevant fields |
| 69 | + gi := response.GitInfo |
| 70 | + if gi == nil { |
| 71 | + log.Warnf(ctx, "Failed to load git info from %s", apiEndpoint) |
| 72 | + } else { |
| 73 | + fixedPath := fixResponsePath(gi.Path) |
| 74 | + return GitRepositoryInfo{ |
| 75 | + OriginURL: gi.URL, |
| 76 | + LatestCommit: gi.HeadCommitID, |
| 77 | + CurrentBranch: gi.Branch, |
| 78 | + WorktreeRoot: vfs.MustNew(fixedPath), |
| 79 | + }, nil |
| 80 | + } |
| 81 | + |
| 82 | + return GitRepositoryInfo{ |
| 83 | + WorktreeRoot: path, |
| 84 | + }, nil |
| 85 | +} |
| 86 | + |
| 87 | +func fixResponsePath(path string) string { |
| 88 | + if !strings.HasPrefix(path, "/Workspace/") { |
| 89 | + return "/Workspace/" + path |
| 90 | + } |
| 91 | + return path |
| 92 | +} |
| 93 | + |
| 94 | +func FetchRepositoryInfoDotGit(ctx context.Context, path vfs.Path) (GitRepositoryInfo, error) { |
| 95 | + rootDir, err := vfs.FindLeafInTree(path, GitDirectoryName) |
| 96 | + if err != nil { |
| 97 | + if !errors.Is(err, fs.ErrNotExist) { |
| 98 | + return GitRepositoryInfo{}, err |
| 99 | + } |
| 100 | + rootDir = path |
| 101 | + } |
| 102 | + |
| 103 | + repo, err := NewRepository(rootDir) |
| 104 | + if err != nil { |
| 105 | + return GitRepositoryInfo{}, err |
| 106 | + } |
| 107 | + |
| 108 | + branch, err := repo.CurrentBranch() |
| 109 | + if err != nil { |
| 110 | + return GitRepositoryInfo{}, nil |
| 111 | + } |
| 112 | + |
| 113 | + commit, err := repo.LatestCommit() |
| 114 | + if err != nil { |
| 115 | + return GitRepositoryInfo{}, nil |
| 116 | + } |
| 117 | + |
| 118 | + return GitRepositoryInfo{ |
| 119 | + OriginURL: repo.OriginUrl(), |
| 120 | + LatestCommit: commit, |
| 121 | + CurrentBranch: branch, |
| 122 | + WorktreeRoot: rootDir, |
| 123 | + }, nil |
| 124 | +} |
0 commit comments