Skip to content

Commit 403e7a5

Browse files
committed
Simplify defaultBranch
It doesn't need a proper repo.
1 parent 271e88d commit 403e7a5

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

bin/git.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ void main(List<String> arguments) {
4747
bindings.push(arguments[1], directory, pemBytes, arguments[3]);
4848
break;
4949
case 'defaultBranch':
50-
var branch = bindings.defaultBranch(
51-
arguments[1], directory, pemBytes, arguments[3]);
50+
var branch =
51+
bindings.defaultBranch(arguments[1], pemBytes, arguments[3]);
5252
print("DefaultBranch: $branch");
5353
break;
5454
}

lib/go_git_dart.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ class GitBindings {
136136
}
137137

138138
String defaultBranch(
139-
String remote,
140-
String directory,
139+
String remoteUrl,
141140
Uint8List pemBytes,
142141
String password,
143142
) {
@@ -146,13 +145,11 @@ class GitBindings {
146145
cPemBytes[i] = pemBytes[i];
147146
}
148147

149-
var remoteName = remote.toNativeUtf8();
150-
var cloneDir = directory.toNativeUtf8();
148+
var remoteUrlN = remoteUrl.toNativeUtf8();
151149
var pemPassphrase = password.toNativeUtf8();
152150

153151
var retValue = lib.GitDefaultBranch(
154-
remoteName.cast<Char>(),
155-
cloneDir.cast<Char>(),
152+
remoteUrlN.cast<Char>(),
156153
cPemBytes.cast<Char>(),
157154
pemBytes.length,
158155
pemPassphrase.cast<Char>(),
@@ -162,8 +159,7 @@ class GitBindings {
162159
// }
163160

164161
malloc.free(cPemBytes);
165-
malloc.free(remoteName);
166-
malloc.free(cloneDir);
162+
malloc.free(remoteUrlN);
167163
malloc.free(pemPassphrase);
168164

169165
var branch = retValue.cast<Utf8>().toDartString();

src/gitjournal.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"unsafe"
1111

1212
git "github.com/go-git/go-git/v5"
13+
"github.com/go-git/go-git/v5/config"
1314
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
15+
"github.com/go-git/go-git/v5/storage/memory"
1416
stdssh "golang.org/x/crypto/ssh"
1517
)
1618

@@ -128,33 +130,26 @@ type GitDefaultBranchResult struct {
128130
*/
129131

130132
//export GitDefaultBranch
131-
func GitDefaultBranch(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
132-
err, val := gitDefaultBranch(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
133+
func GitDefaultBranch(remoteUrl *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
134+
err, val := gitDefaultBranch(C.GoString(remoteUrl), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
133135
if err != 0 {
134136
return nil
135137
}
136138
return C.CString(val)
137139
}
138140

139-
func gitDefaultBranch(remoteName string, directory string, privateKey []byte, password string) (int, string) {
141+
func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (int, string) {
140142
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
141143
if err != nil {
142144
fmt.Println("generate publickeys failed:", err.Error())
143145
return 1, ""
144146
}
145147
publicKeys.HostKeyCallback = stdssh.InsecureIgnoreHostKey()
146148

147-
repo, err := git.PlainOpen(directory)
148-
if err != nil {
149-
fmt.Println("git open failed:", err.Error())
150-
return 1, ""
151-
}
152-
153-
remote, err := repo.Remote(remoteName)
154-
if err != nil {
155-
fmt.Println("git remote failed:", err.Error())
156-
return 1, ""
157-
}
149+
remote := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
150+
Name: "origin",
151+
URLs: []string{remoteUrl},
152+
})
158153

159154
refs, err := remote.List(&git.ListOptions{Auth: publicKeys})
160155
if err != nil {

0 commit comments

Comments
 (0)