1
1
package main
2
2
3
3
import (
4
- "os"
5
4
6
5
/*
7
6
#include <stdlib.h>
@@ -14,106 +13,107 @@ import (
14
13
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
15
14
)
16
15
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
-
24
16
//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
27
24
}
28
25
29
- func gitClone (url string , directory string , privateKey []byte , password string ) int {
26
+ func gitClone (url string , directory string , privateKey []byte , password string ) error {
30
27
publicKeys , err := ssh .NewPublicKeys ("git" , privateKey , password )
31
28
if err != nil {
32
- fmt .Println ("generate publickeys failed:" , err .Error ())
33
- return errPublicKeysFailed
29
+ return err
34
30
}
35
31
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
+ */
41
39
42
40
_ , 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,
46
44
})
47
45
if err != nil {
48
- fmt .Println ("git clone failed:" , err .Error ())
49
- return errGitCloneFailed
46
+ return err
50
47
}
51
48
52
- return 0
49
+ return nil
53
50
}
54
51
55
52
//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
58
60
}
59
61
60
- func gitFetch (remote string , directory string , privateKey []byte , password string ) int {
62
+ func gitFetch (remote string , directory string , privateKey []byte , password string ) error {
61
63
publicKeys , err := ssh .NewPublicKeys ("git" , privateKey , password )
62
64
if err != nil {
63
- fmt .Println ("generate publickeys failed:" , err .Error ())
64
- return errPublicKeysFailed
65
+ return err
65
66
}
66
67
67
68
fmt .Println ("git fetch" , directory )
68
69
r , err := git .PlainOpen (directory )
69
70
if err != nil {
70
- fmt .Println ("git open failed:" , err .Error ())
71
- return errGitOpenFailed
71
+ return err
72
72
}
73
73
74
74
err = r .Fetch (& git.FetchOptions {RemoteName : remote , Auth : publicKeys })
75
75
if err == git .NoErrAlreadyUpToDate {
76
- return 0
76
+ return nil
77
77
}
78
78
79
79
if err != nil {
80
- fmt .Println ("git pull failed:" , err .Error ())
81
- return errGitPullFailed
80
+ return err
82
81
}
83
82
84
- return 0
83
+ return nil
85
84
}
86
85
87
86
//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
90
94
}
91
95
92
- func gitPush (remote string , directory string , privateKey []byte , password string ) int {
96
+ func gitPush (remote string , directory string , privateKey []byte , password string ) error {
93
97
publicKeys , err := ssh .NewPublicKeys ("git" , privateKey , password )
94
98
if err != nil {
95
- fmt .Println ("generate publickeys failed:" , err .Error ())
96
- return errPublicKeysFailed
99
+ return err
97
100
}
98
101
99
- fmt .Println ("git push" , directory )
100
102
r , err := git .PlainOpen (directory )
101
103
if err != nil {
102
- fmt .Println ("git open failed:" , err .Error ())
103
- return errGitOpenFailed
104
+ return err
104
105
}
105
106
106
107
err = r .Push (& git.PushOptions {RemoteName : remote , Auth : publicKeys })
107
108
if err == git .NoErrAlreadyUpToDate {
108
- return 0
109
+ return nil
109
110
}
110
111
111
112
if err != nil {
112
- fmt .Println ("git push failed:" , err .Error ())
113
- return errGitPullFailed
113
+ return err
114
114
}
115
115
116
- return 0
116
+ return nil
117
117
}
118
118
119
119
/*
@@ -136,25 +136,25 @@ func gitDefaultBranch(remoteName string, directory string, privateKey []byte, pa
136
136
publicKeys , err := ssh .NewPublicKeys ("git" , privateKey , password )
137
137
if err != nil {
138
138
fmt .Println ("generate publickeys failed:" , err .Error ())
139
- return errPublicKeysFailed , ""
139
+ return 1 , ""
140
140
}
141
141
142
142
repo , err := git .PlainOpen (directory )
143
143
if err != nil {
144
144
fmt .Println ("git open failed:" , err .Error ())
145
- return errGitOpenFailed , ""
145
+ return 1 , ""
146
146
}
147
147
148
148
remote , err := repo .Remote (remoteName )
149
149
if err != nil {
150
150
fmt .Println ("git remote failed:" , err .Error ())
151
- return errGitRemoteOpenFailed , ""
151
+ return 1 , ""
152
152
}
153
153
154
154
refs , err := remote .List (& git.ListOptions {Auth : publicKeys })
155
155
if err != nil {
156
156
fmt .Println ("git remote list failed:" , err .Error ())
157
- return errGitRemoteListFailed , ""
157
+ return 1 , ""
158
158
}
159
159
160
160
defaultBranch := ""
0 commit comments