Skip to content

Commit d4508bd

Browse files
committed
Return proper error strings from most methods
This only doesn't work for the default_branch command, as I'll need to return two values.
1 parent fb3479d commit d4508bd

File tree

2 files changed

+66
-57
lines changed

2 files changed

+66
-57
lines changed

gitjournal.go

+51-51
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"os"
54

65
/*
76
#include <stdlib.h>
@@ -14,106 +13,107 @@ import (
1413
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
1514
)
1615

17-
const errPublicKeysFailed = 55
18-
const errGitCloneFailed = 56
19-
const errGitOpenFailed = 57
20-
const errGitPullFailed = 59
21-
const errGitRemoteOpenFailed = 59
22-
const errGitRemoteListFailed = 60
23-
2416
//export GitClone
25-
func GitClone(url *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) int {
26-
return gitClone(C.GoString(url), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
17+
func GitClone(url *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
18+
err := gitClone(C.GoString(url), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
19+
if err != nil {
20+
return C.CString(err.Error())
21+
}
22+
23+
return nil
2724
}
2825

29-
func gitClone(url string, directory string, privateKey []byte, password string) int {
26+
func gitClone(url string, directory string, privateKey []byte, password string) error {
3027
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
3128
if err != nil {
32-
fmt.Println("generate publickeys failed:", err.Error())
33-
return errPublicKeysFailed
29+
return err
3430
}
3531

36-
progressFile, err := os.OpenFile("/tmp/123.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
37-
if err != nil {
38-
panic(err)
39-
}
40-
defer progressFile.Close()
32+
/*
33+
progressFile, err := os.OpenFile("/tmp/123.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
34+
if err != nil {
35+
panic(err)
36+
}
37+
defer progressFile.Close()
38+
*/
4139

4240
_, err = git.PlainClone(directory, false, &git.CloneOptions{
43-
Auth: publicKeys,
44-
URL: url,
45-
Progress: progressFile,
41+
Auth: publicKeys,
42+
URL: url,
43+
// Progress: progressFile,
4644
})
4745
if err != nil {
48-
fmt.Println("git clone failed:", err.Error())
49-
return errGitCloneFailed
46+
return err
5047
}
5148

52-
return 0
49+
return nil
5350
}
5451

5552
//export GitFetch
56-
func GitFetch(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) int {
57-
return gitFetch(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
53+
func GitFetch(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
54+
err := gitFetch(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
55+
if err != nil {
56+
return C.CString(err.Error())
57+
}
58+
59+
return nil
5860
}
5961

60-
func gitFetch(remote string, directory string, privateKey []byte, password string) int {
62+
func gitFetch(remote string, directory string, privateKey []byte, password string) error {
6163
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
6264
if err != nil {
63-
fmt.Println("generate publickeys failed:", err.Error())
64-
return errPublicKeysFailed
65+
return err
6566
}
6667

6768
fmt.Println("git fetch", directory)
6869
r, err := git.PlainOpen(directory)
6970
if err != nil {
70-
fmt.Println("git open failed:", err.Error())
71-
return errGitOpenFailed
71+
return err
7272
}
7373

7474
err = r.Fetch(&git.FetchOptions{RemoteName: remote, Auth: publicKeys})
7575
if err == git.NoErrAlreadyUpToDate {
76-
return 0
76+
return nil
7777
}
7878

7979
if err != nil {
80-
fmt.Println("git pull failed:", err.Error())
81-
return errGitPullFailed
80+
return err
8281
}
8382

84-
return 0
83+
return nil
8584
}
8685

8786
//export GitPush
88-
func GitPush(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) int {
89-
return gitPush(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
87+
func GitPush(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
88+
err := gitPush(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
89+
if err != nil {
90+
return C.CString(err.Error())
91+
}
92+
93+
return nil
9094
}
9195

92-
func gitPush(remote string, directory string, privateKey []byte, password string) int {
96+
func gitPush(remote string, directory string, privateKey []byte, password string) error {
9397
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
9498
if err != nil {
95-
fmt.Println("generate publickeys failed:", err.Error())
96-
return errPublicKeysFailed
99+
return err
97100
}
98101

99-
fmt.Println("git push", directory)
100102
r, err := git.PlainOpen(directory)
101103
if err != nil {
102-
fmt.Println("git open failed:", err.Error())
103-
return errGitOpenFailed
104+
return err
104105
}
105106

106107
err = r.Push(&git.PushOptions{RemoteName: remote, Auth: publicKeys})
107108
if err == git.NoErrAlreadyUpToDate {
108-
return 0
109+
return nil
109110
}
110111

111112
if err != nil {
112-
fmt.Println("git push failed:", err.Error())
113-
return errGitPullFailed
113+
return err
114114
}
115115

116-
return 0
116+
return nil
117117
}
118118

119119
/*
@@ -136,25 +136,25 @@ func gitDefaultBranch(remoteName string, directory string, privateKey []byte, pa
136136
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
137137
if err != nil {
138138
fmt.Println("generate publickeys failed:", err.Error())
139-
return errPublicKeysFailed, ""
139+
return 1, ""
140140
}
141141

142142
repo, err := git.PlainOpen(directory)
143143
if err != nil {
144144
fmt.Println("git open failed:", err.Error())
145-
return errGitOpenFailed, ""
145+
return 1, ""
146146
}
147147

148148
remote, err := repo.Remote(remoteName)
149149
if err != nil {
150150
fmt.Println("git remote failed:", err.Error())
151-
return errGitRemoteOpenFailed, ""
151+
return 1, ""
152152
}
153153

154154
refs, err := remote.List(&git.ListOptions{Auth: publicKeys})
155155
if err != nil {
156156
fmt.Println("git remote list failed:", err.Error())
157-
return errGitRemoteListFailed, ""
157+
return 1, ""
158158
}
159159

160160
defaultBranch := ""

main.dart

+15-6
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ class GitBindings {
3535
pemBytes.length,
3636
pemPassphrase.cast<Char>(),
3737
);
38-
if (retValue != 0) {
39-
throw Exception("GitClone failed with error code: $retValue");
38+
if (retValue != nullptr) {
39+
var err = retValue.cast<Utf8>().toDartString();
40+
lib.free(retValue.cast());
41+
42+
throw Exception("GitClone failed with error: $err");
4043
}
4144

4245
malloc.free(cPemBytes);
@@ -67,8 +70,11 @@ class GitBindings {
6770
pemBytes.length,
6871
pemPassphrase.cast<Char>(),
6972
);
70-
if (retValue != 0) {
71-
throw Exception("GitFetch failed with error code: $retValue");
73+
if (retValue != nullptr) {
74+
var err = retValue.cast<Utf8>().toDartString();
75+
lib.free(retValue.cast());
76+
77+
throw Exception("GitFetch failed with error code: $err");
7278
}
7379

7480
malloc.free(cPemBytes);
@@ -99,8 +105,11 @@ class GitBindings {
99105
pemBytes.length,
100106
pemPassphrase.cast<Char>(),
101107
);
102-
if (retValue != 0) {
103-
throw Exception("GitPush failed with error code: $retValue");
108+
if (retValue != nullptr) {
109+
var err = retValue.cast<Utf8>().toDartString();
110+
lib.free(retValue.cast());
111+
112+
throw Exception("GitPush failed with error code: $err");
104113
}
105114

106115
malloc.free(cPemBytes);

0 commit comments

Comments
 (0)