Skip to content

Commit 75708ee

Browse files
author
R.A. Lucas
committed
initial commit on adding usage of git_create_remote_detached
1 parent aeb22bc commit 75708ee

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

remote.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ const (
3030

3131
// RemoteCreateOptions contains options for creating a remote
3232
type RemoteCreateOptions struct {
33-
Name string
34-
FetchSpec string
35-
Flags RemoteCreateOptionsFlag
33+
Name string
34+
FetchSpec string
35+
Flags RemoteCreateOptionsFlag
3636
}
3737

3838
type TransferProgress struct {
@@ -173,6 +173,37 @@ type Remote struct {
173173
repo *Repository
174174
}
175175

176+
type RemoteDetached struct {
177+
r Remote
178+
}
179+
180+
// NewRemoteDetached returns a detached remote that has no
181+
// reference to a repository. Used to emulate `git ls-remote <repo-url>`
182+
func NewRemoteDetached(remoteUri string) (*RemoteDetached, error) {
183+
var ptr *C.git_remote
184+
185+
runtime.LockOSThread()
186+
defer runtime.UnlockOSThread()
187+
188+
ret := C.git_remote_create_detached(&ptr, C.CString(remoteUri))
189+
if ret != 0 {
190+
return nil, MakeGitError(ret)
191+
}
192+
193+
return &RemoteDetached{r: Remote{ptr: ptr}}, nil
194+
}
195+
196+
// LsDetached returns a listing of []RemoteHead given an optional reference filter,
197+
// emulating `git ls-remote ...`
198+
func (rd *RemoteDetached) LsDetached(fetchOpts FetchOptions, filterRefs ...string) ([]RemoteHead, error) {
199+
err := rd.r.ConnectFetch(&fetchOpts.RemoteCallbacks, &fetchOpts.ProxyOptions, fetchOpts.Headers)
200+
if err != nil {
201+
return nil, err
202+
}
203+
204+
return rd.r.Ls(filterRefs...)
205+
}
206+
176207
type CertificateKind uint
177208

178209
const (

remote_test.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"golang.org/x/crypto/ssh"
1919
)
2020

21+
const TestGitRepoUri = "https://github.com/libgit2/TestGitRepository"
22+
2123
func TestListRemotes(t *testing.T) {
2224
t.Parallel()
2325
repo := createTestRepo(t)
@@ -51,7 +53,7 @@ func TestCertificateCheck(t *testing.T) {
5153
repo := createTestRepo(t)
5254
defer cleanupTestRepo(t, repo)
5355

54-
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
56+
remote, err := repo.Remotes.Create("origin", TestGitRepoUri)
5557
checkFatal(t, err)
5658
defer remote.Free()
5759

@@ -72,7 +74,7 @@ func TestRemoteConnect(t *testing.T) {
7274
repo := createTestRepo(t)
7375
defer cleanupTestRepo(t, repo)
7476

75-
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
77+
remote, err := repo.Remotes.Create("origin", TestGitRepoUri)
7678
checkFatal(t, err)
7779
defer remote.Free()
7880

@@ -95,7 +97,7 @@ func TestRemoteConnectOption(t *testing.T) {
9597
option.Name = "origin"
9698
option.Flags = RemoteCreateSkipInsteadof
9799

98-
remote, err := repo.Remotes.CreateWithOptions("https://github.com/libgit2/TestGitRepository", option)
100+
remote, err := repo.Remotes.CreateWithOptions(TestGitRepoUri, option)
99101
checkFatal(t, err)
100102

101103
err = remote.ConnectFetch(nil, nil, nil)
@@ -107,7 +109,7 @@ func TestRemoteLs(t *testing.T) {
107109
repo := createTestRepo(t)
108110
defer cleanupTestRepo(t, repo)
109111

110-
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
112+
remote, err := repo.Remotes.Create("origin", TestGitRepoUri)
111113
checkFatal(t, err)
112114
defer remote.Free()
113115

@@ -127,7 +129,7 @@ func TestRemoteLsFiltering(t *testing.T) {
127129
repo := createTestRepo(t)
128130
defer cleanupTestRepo(t, repo)
129131

130-
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
132+
remote, err := repo.Remotes.Create("origin", TestGitRepoUri)
131133
checkFatal(t, err)
132134
defer remote.Free()
133135

@@ -162,7 +164,7 @@ func TestRemotePruneRefs(t *testing.T) {
162164
err = config.SetBool("remote.origin.prune", true)
163165
checkFatal(t, err)
164166

165-
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
167+
remote, err := repo.Remotes.Create("origin", TestGitRepoUri)
166168
checkFatal(t, err)
167169
defer remote.Free()
168170

@@ -493,3 +495,40 @@ func TestRemoteSSH(t *testing.T) {
493495
t.Error("Expected remote heads")
494496
}
495497
}
498+
499+
func TestNewRemoteDetached(t *testing.T) {
500+
_, err := NewRemoteDetached(TestGitRepoUri)
501+
if err != nil {
502+
t.Fatalf("error %v", err)
503+
}
504+
}
505+
506+
func TestRemoteDetached_LsDetached(t *testing.T) {
507+
r, _ := NewRemoteDetached(TestGitRepoUri)
508+
509+
t.Run("NoInput", func(t *testing.T) {
510+
rh, err := r.LsDetached(FetchOptions{})
511+
if err != nil {
512+
t.Fatalf("error %v", err)
513+
}
514+
515+
if len(rh) == 0 {
516+
t.Fatalf("Not listing all references")
517+
}
518+
})
519+
520+
t.Run("Filters", func(t *testing.T) {
521+
rh, err := r.LsDetached(FetchOptions{}, "HEAD")
522+
if err != nil {
523+
t.Fatalf("error %v", err)
524+
}
525+
526+
if len(rh) == 0 {
527+
t.Fatalf("Not listing all references")
528+
}
529+
530+
if rh[0].Name != "HEAD" {
531+
t.Fatalf("Not listing all references")
532+
}
533+
})
534+
}

0 commit comments

Comments
 (0)