Skip to content

Commit 9934eb2

Browse files
committed
Merge branch 'fix/detect-default-branch'
2 parents 1ebbfe0 + 452fe39 commit 9934eb2

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

git/git.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func CloneRepo(ctx context.Context, logf func(string, ...any), opts CloneRepoOpt
9494
}
9595
reference := parsed.Fragment
9696
if reference == "" && opts.SingleBranch {
97-
reference = "refs/heads/main"
97+
// When SingleBranch is true and no branch is specified, don't set a
98+
// default. go-git will automatically detect and use the remote's HEAD
99+
// (default branch) via the "+HEAD:refs/remotes/origin/HEAD" refspec.
100+
logf("No branch specified, using remote's default branch")
98101
}
99102
parsed.RawFragment = ""
100103
parsed.Fragment = ""

git/git_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,39 @@ func TestCloneRepo(t *testing.T) {
168168
}
169169
}
170170

171+
func TestCloneRepo_NonMainDefaultBranch(t *testing.T) {
172+
t.Parallel()
173+
174+
// Test that cloning works when the remote's default branch is not "main".
175+
// This verifies that we detect the remote's HEAD instead of hardcoding "main".
176+
for _, defaultBranch := range []string{"master", "develop", "trunk"} {
177+
defaultBranch := defaultBranch
178+
t.Run(defaultBranch, func(t *testing.T) {
179+
t.Parallel()
180+
srvFS := memfs.New()
181+
_ = gittest.NewRepoWithBranch(t, srvFS, defaultBranch,
182+
gittest.Commit(t, "README.md", "Hello from "+defaultBranch, "Initial commit"),
183+
)
184+
srv := httptest.NewServer(gittest.NewServer(srvFS))
185+
t.Cleanup(srv.Close)
186+
187+
clientFS := memfs.New()
188+
cloned, err := git.CloneRepo(context.Background(), t.Logf, git.CloneRepoOptions{
189+
Path: "/workspace",
190+
RepoURL: srv.URL,
191+
Storage: clientFS,
192+
SingleBranch: true, // This triggers the default branch detection
193+
})
194+
require.NoError(t, err)
195+
require.True(t, cloned)
196+
197+
// Verify the file was cloned correctly
198+
readme := mustRead(t, clientFS, "/workspace/README.md")
199+
require.Equal(t, "Hello from "+defaultBranch, readme)
200+
})
201+
}
202+
}
203+
171204
func TestShallowCloneRepo(t *testing.T) {
172205
t.Parallel()
173206

testutil/gittest/gittest.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,20 @@ func Commit(t *testing.T, path, content, msg string) CommitFunc {
251251
}
252252
}
253253

254-
// NewRepo returns a new Git repository.
254+
// NewRepo returns a new Git repository with "main" as the default branch.
255255
func NewRepo(t *testing.T, fs billy.Filesystem, commits ...CommitFunc) *git.Repository {
256+
return NewRepoWithBranch(t, fs, "main", commits...)
257+
}
258+
259+
// NewRepoWithBranch returns a new Git repository with a custom default branch.
260+
func NewRepoWithBranch(t *testing.T, fs billy.Filesystem, defaultBranch string, commits ...CommitFunc) *git.Repository {
256261
t.Helper()
257262
storage := filesystem.NewStorage(fs, cache.NewObjectLRU(cache.DefaultMaxSize))
258263
repo, err := git.Init(storage, fs)
259264
require.NoError(t, err)
260265

261-
// This changes the default ref to main instead of master.
262-
h := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/main"))
266+
// Set the default branch to the specified name.
267+
h := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.ReferenceName("refs/heads/"+defaultBranch))
263268
err = storage.SetReference(h)
264269
require.NoError(t, err)
265270

0 commit comments

Comments
 (0)