Skip to content

Commit e823fc8

Browse files
Fix Git metadata detection for Workspace worktrees (#7033)
## Changes Read local Git metadata for linked worktrees when Workspace returns none. Keep Workspace metadata precedence and API error handling unchanged. ## Why Linked worktrees are not registered Git folders, so the Workspace lookup leaves `bundle.git.branch` empty on Runtime. ## Tests Unit coverage for linked worktrees, Workspace metadata precedence, and API errors. Serverless validation confirmed branch detection and branch-specific bundle paths for remote-backed and remote-less worktrees. Local formatting, lint, repository checks, unit tests, and the acceptance suite passed.
1 parent 119c71f commit e823fc8

2 files changed

Lines changed: 104 additions & 6 deletions

File tree

‎libs/git/info.go‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type response struct {
4141
GitInfo *gitInfo `json:"git_info,omitempty"`
4242
}
4343

44-
// Fetch repository information either by quering .git or by fetching it from API (for dabs-in-workspace case).
44+
// FetchRepositoryInfo reads Workspace Git metadata or a local .git checkout.
4545
// - In case we could not find git repository (including when the path does not exist), all string fields of RepositoryInfo will be "" and err will be nil.
4646
// - If there were any errors when trying to determine git root (e.g. API call returned an error or there were permission issues
4747
// reading the file system), all strings fields of RepositoryInfo will be "" and err will be non-nil.
@@ -52,7 +52,7 @@ func FetchRepositoryInfo(ctx context.Context, path string, w *databricks.Workspa
5252
var info RepositoryInfo
5353
var err error
5454
if strings.HasPrefix(path, "/Workspace/") && dbr.RunsOnRuntime(ctx) {
55-
info, err = FetchRepositoryInfoAPI(ctx, path, w)
55+
info, err = fetchRepositoryInfoFromWorkspace(ctx, path, w)
5656
} else {
5757
info, err = fetchRepositoryInfoDotGit(ctx, path)
5858
}
@@ -67,10 +67,18 @@ func FetchRepositoryInfo(ctx context.Context, path string, w *databricks.Workspa
6767
return info, err
6868
}
6969

70-
// FetchRepositoryInfoAPI reads the metadata from the workspace API, which is what
71-
// FetchRepositoryInfo does on a Databricks Runtime. Exported so that
72-
// `bundle debug fetch-repository-info --workspace-api` can reach this path
73-
// off-runtime; the product calls it through FetchRepositoryInfo.
70+
func fetchRepositoryInfoFromWorkspace(ctx context.Context, path string, w *databricks.WorkspaceClient) (RepositoryInfo, error) {
71+
info, err := FetchRepositoryInfoAPI(ctx, path, w)
72+
if err == nil && info.WorktreeRoot == "" {
73+
// Fall back to Git information from .git for unsupported Git folders like worktree folders
74+
return fetchRepositoryInfoDotGit(ctx, path)
75+
}
76+
return info, err
77+
}
78+
79+
// FetchRepositoryInfoAPI reads Workspace Git metadata, the primary source on a Runtime.
80+
// It is exported so `bundle debug fetch-repository-info --workspace-api` can use this backend
81+
// off Runtime; normal commands call FetchRepositoryInfo.
7482
//
7583
// A path that does not exist is reported as fs.ErrNotExist; callers that treat
7684
// that as "no repository here" must normalize it the way FetchRepositoryInfo does.

‎libs/git/info_test.go‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package git
2+
3+
import (
4+
"io/fs"
5+
"net/http"
6+
"net/url"
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
11+
"github.com/databricks/cli/internal/testutil"
12+
"github.com/databricks/databricks-sdk-go"
13+
"github.com/databricks/databricks-sdk-go/apierr"
14+
"github.com/databricks/databricks-sdk-go/qa"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func TestFetchRepositoryInfoFromWorkspace(t *testing.T) {
20+
dir := setupWorktree(t)
21+
worktree := filepath.Join(dir, "my_worktree")
22+
writeGitDir(t, dir, "gitdir: ../.git/worktrees/my_worktree")
23+
writeGitCommonDir(t, dir, "../..")
24+
want := RepositoryInfo{
25+
OriginURL: "https://example.test/repo.git",
26+
LatestCommit: strings.Repeat("1", 40),
27+
CurrentBranch: "projects/task",
28+
WorktreeRoot: worktree,
29+
}
30+
gitDirectoryWant := want
31+
gitDirectoryWant.WorktreeRoot = dir
32+
noGitRoot := filepath.VolumeName(dir) + string(filepath.Separator)
33+
testutil.WriteFile(t, filepath.Join(dir, ".git", "refs", "heads", want.CurrentBranch), want.LatestCommit)
34+
testutil.WriteFile(t, filepath.Join(dir, ".git", "HEAD"), "ref: refs/heads/"+want.CurrentBranch)
35+
testutil.WriteFile(t, filepath.Join(dir, ".git", "worktrees", "my_worktree", "HEAD"), "ref: refs/heads/"+want.CurrentBranch)
36+
testutil.WriteFile(t, filepath.Join(dir, ".git", "config"), "[remote \"origin\"]\n\turl = "+want.OriginURL)
37+
file := testutil.Touch(t, worktree, "subdir", "README.md")
38+
malformed := t.TempDir()
39+
testutil.WriteFile(t, filepath.Join(malformed, ".git"), "invalid gitfile")
40+
41+
for _, tc := range []struct {
42+
name string
43+
path string
44+
body string
45+
status int
46+
want RepositoryInfo
47+
wantErr error
48+
}{
49+
{name: "worktree", path: worktree, want: want},
50+
{name: "subdirectory", path: filepath.Dir(file), want: want},
51+
{
52+
name: "workspace metadata takes precedence", path: worktree,
53+
body: `{"git_info":{"branch":"api-main","head_commit_id":"2222222222222222222222222222222222222222","path":"/Users/me/api","url":"https://example.test/api.git"}}`,
54+
want: RepositoryInfo{
55+
OriginURL: "https://example.test/api.git", LatestCommit: strings.Repeat("2", 40),
56+
CurrentBranch: "api-main", WorktreeRoot: "/Workspace/Users/me/api",
57+
},
58+
},
59+
{name: "git directory", path: dir, want: gitDirectoryWant},
60+
{name: "no git metadata", path: noGitRoot, wantErr: fs.ErrNotExist},
61+
{name: "malformed gitfile", path: malformed, want: RepositoryInfo{WorktreeRoot: malformed}},
62+
{
63+
name: "permission denied", path: worktree, status: http.StatusForbidden,
64+
body: `{"error_code":"PERMISSION_DENIED","message":"denied"}`, wantErr: apierr.ErrPermissionDenied,
65+
},
66+
{
67+
name: "workspace path missing", path: worktree, status: http.StatusNotFound,
68+
body: `{"error_code":"RESOURCE_DOES_NOT_EXIST","message":"missing"}`, wantErr: fs.ErrNotExist,
69+
},
70+
} {
71+
t.Run(tc.name, func(t *testing.T) {
72+
body := tc.body
73+
if body == "" {
74+
body = `{}`
75+
}
76+
cfg, server := qa.HTTPFixtures{{
77+
Method: "GET",
78+
Resource: "/api/2.0/workspace/get-status?path=" + url.QueryEscape(tc.path) + "&return_git_info=true",
79+
Response: body,
80+
Status: tc.status,
81+
}}.Config(t)
82+
t.Cleanup(server.Close)
83+
w := databricks.Must(databricks.NewWorkspaceClient((*databricks.Config)(cfg)))
84+
85+
got, err := fetchRepositoryInfoFromWorkspace(t.Context(), tc.path, w)
86+
require.ErrorIs(t, err, tc.wantErr)
87+
assert.Equal(t, tc.want, got)
88+
})
89+
}
90+
}

0 commit comments

Comments
 (0)