Skip to content

Commit 7331977

Browse files
committed
defaultBranch: Implement proper error handling
The branchName is passed via an input parameter, instead of relying on the return value.
1 parent 1723ea4 commit 7331977

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

lib/go_git_dart.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,30 @@ class GitBindings {
150150

151151
var remoteUrlN = remoteUrl.toNativeUtf8();
152152
var pemPassphrase = password.toNativeUtf8();
153+
var outputBranch = malloc.allocate<Pointer<Char>>(0);
153154

154155
var retValue = lib.GitDefaultBranch(
155156
remoteUrlN.cast<Char>(),
156157
cPemBytes.cast<Char>(),
157158
pemBytes.length,
158159
pemPassphrase.cast<Char>(),
160+
outputBranch,
159161
);
160-
// if (retValue != 0) {
161-
// throw Exception("GitDefaultBranch failed with error code: $retValue");
162-
// }
162+
if (retValue != nullptr) {
163+
var err = retValue.cast<Utf8>().toDartString();
164+
lib.free(retValue.cast());
165+
166+
throw Exception("GitPush failed with error code: $err");
167+
}
163168

164169
malloc.free(cPemBytes);
165170
malloc.free(remoteUrlN);
166171
malloc.free(pemPassphrase);
167172

168-
var branch = retValue.cast<Utf8>().toDartString();
169-
lib.free(retValue.cast());
173+
var branch = outputBranch.value.cast<Utf8>().toDartString();
174+
lib.free(outputBranch.value.cast());
175+
malloc.free(outputBranch);
176+
170177
return branch;
171178
}
172179
}

lib/go_git_dart_bindings_generated.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,12 +2164,14 @@ class GoGitDartBindings {
21642164
ffi.Pointer<ffi.Char> privateKey,
21652165
int privateKeyLen,
21662166
ffi.Pointer<ffi.Char> password,
2167+
ffi.Pointer<ffi.Pointer<ffi.Char>> outputBranchName,
21672168
) {
21682169
return _GitDefaultBranch(
21692170
remoteUrl,
21702171
privateKey,
21712172
privateKeyLen,
21722173
password,
2174+
outputBranchName,
21732175
);
21742176
}
21752177

@@ -2179,10 +2181,15 @@ class GoGitDartBindings {
21792181
ffi.Pointer<ffi.Char>,
21802182
ffi.Pointer<ffi.Char>,
21812183
ffi.Int,
2182-
ffi.Pointer<ffi.Char>)>>('GitDefaultBranch');
2184+
ffi.Pointer<ffi.Char>,
2185+
ffi.Pointer<ffi.Pointer<ffi.Char>>)>>('GitDefaultBranch');
21832186
late final _GitDefaultBranch = _GitDefaultBranchPtr.asFunction<
2184-
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>,
2185-
ffi.Pointer<ffi.Char>, int, ffi.Pointer<ffi.Char>)>();
2187+
ffi.Pointer<ffi.Char> Function(
2188+
ffi.Pointer<ffi.Char>,
2189+
ffi.Pointer<ffi.Char>,
2190+
int,
2191+
ffi.Pointer<ffi.Char>,
2192+
ffi.Pointer<ffi.Pointer<ffi.Char>>)>();
21862193
}
21872194

21882195
final class __mbstate_t extends ffi.Union {

src/git.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ func gitPush(remote string, directory string, privateKey []byte, password string
113113
return nil
114114
}
115115

116-
func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (int, string) {
116+
func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (string, error) {
117117
auth, err := buildAuth(remoteUrl, privateKey, password)
118118
if err != nil {
119-
return 1, ""
119+
return "", err
120120
}
121121

122122
remote := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
@@ -126,8 +126,7 @@ func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (int
126126

127127
refs, err := remote.List(&git.ListOptions{Auth: auth})
128128
if err != nil {
129-
fmt.Println("git remote list failed:", err.Error())
130-
return 1, ""
129+
return "", err
131130
}
132131

133132
defaultBranch := ""
@@ -138,5 +137,5 @@ func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (int
138137
}
139138
}
140139

141-
return 0, defaultBranch
140+
return defaultBranch, nil
142141
}

src/gitjournal.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ func GitPush(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLe
4040
}
4141

4242
//export GitDefaultBranch
43-
func GitDefaultBranch(remoteUrl *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
44-
err, val := gitDefaultBranch(C.GoString(remoteUrl), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
45-
if err != 0 {
46-
return nil
43+
func GitDefaultBranch(remoteUrl *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char, outputBranchName **C.char) *C.char {
44+
val, err := gitDefaultBranch(C.GoString(remoteUrl), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
45+
if err != nil {
46+
return C.CString(err.Error())
4747
}
48-
return C.CString(val)
48+
49+
*outputBranchName = C.CString(val)
50+
return nil
4951
}
5052

5153
func main() {}

0 commit comments

Comments
 (0)