@@ -7,7 +7,10 @@ package git
7
7
git_credtype_t _go_git_cred_credtype(git_cred *cred);
8
8
*/
9
9
import "C"
10
- import "unsafe"
10
+ import (
11
+ "runtime"
12
+ "unsafe"
13
+ )
11
14
12
15
type CredType uint
13
16
@@ -22,6 +25,12 @@ type Cred struct {
22
25
ptr * C.git_cred
23
26
}
24
27
28
+ func newCred () * Cred {
29
+ cred := & Cred {}
30
+ runtime .SetFinalizer (cred , (* Cred ).Free )
31
+ return cred
32
+ }
33
+
25
34
func (o * Cred ) HasUsername () bool {
26
35
if C .git_cred_has_username (o .ptr ) == 1 {
27
36
return true
@@ -33,24 +42,36 @@ func (o *Cred) Type() CredType {
33
42
return (CredType )(C ._go_git_cred_credtype (o .ptr ))
34
43
}
35
44
36
- func credFromC (ptr * C.git_cred ) * Cred {
37
- return & Cred {ptr }
45
+ func (o * Cred ) Free () {
46
+ C .git_cred_free (o .ptr )
47
+ runtime .SetFinalizer (o , nil )
48
+ o .ptr = nil
38
49
}
39
50
40
- func NewCredUserpassPlaintext (username string , password string ) (int , Cred ) {
41
- cred := Cred {}
51
+ func NewCredUserpassPlaintext (username string , password string ) (* Cred , error ) {
52
+ runtime .LockOSThread ()
53
+ defer runtime .UnlockOSThread ()
54
+
55
+ cred := newCred ()
42
56
cusername := C .CString (username )
43
57
defer C .free (unsafe .Pointer (cusername ))
44
58
cpassword := C .CString (password )
45
59
defer C .free (unsafe .Pointer (cpassword ))
46
60
ret := C .git_cred_userpass_plaintext_new (& cred .ptr , cusername , cpassword )
47
- return int (ret ), cred
61
+ if ret != 0 {
62
+ cred .Free ()
63
+ return nil , MakeGitError (ret )
64
+ }
65
+ return cred , nil
48
66
}
49
67
50
68
// NewCredSshKey creates new ssh credentials reading the public and private keys
51
69
// from the file system.
52
- func NewCredSshKey (username string , publicKeyPath string , privateKeyPath string , passphrase string ) (int , Cred ) {
53
- cred := Cred {}
70
+ func NewCredSshKey (username string , publicKeyPath string , privateKeyPath string , passphrase string ) (* Cred , error ) {
71
+ runtime .LockOSThread ()
72
+ defer runtime .UnlockOSThread ()
73
+
74
+ cred := newCred ()
54
75
cusername := C .CString (username )
55
76
defer C .free (unsafe .Pointer (cusername ))
56
77
cpublickey := C .CString (publicKeyPath )
@@ -60,13 +81,20 @@ func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string,
60
81
cpassphrase := C .CString (passphrase )
61
82
defer C .free (unsafe .Pointer (cpassphrase ))
62
83
ret := C .git_cred_ssh_key_new (& cred .ptr , cusername , cpublickey , cprivatekey , cpassphrase )
63
- return int (ret ), cred
84
+ if ret != 0 {
85
+ cred .Free ()
86
+ return nil , MakeGitError (ret )
87
+ }
88
+ return cred , nil
64
89
}
65
90
66
91
// NewCredSshKeyFromMemory creates new ssh credentials using the publicKey and privateKey
67
92
// arguments as the values for the public and private keys.
68
- func NewCredSshKeyFromMemory (username string , publicKey string , privateKey string , passphrase string ) (int , Cred ) {
69
- cred := Cred {}
93
+ func NewCredSshKeyFromMemory (username string , publicKey string , privateKey string , passphrase string ) (* Cred , error ) {
94
+ runtime .LockOSThread ()
95
+ defer runtime .UnlockOSThread ()
96
+
97
+ cred := newCred ()
70
98
cusername := C .CString (username )
71
99
defer C .free (unsafe .Pointer (cusername ))
72
100
cpublickey := C .CString (publicKey )
@@ -76,19 +104,37 @@ func NewCredSshKeyFromMemory(username string, publicKey string, privateKey strin
76
104
cpassphrase := C .CString (passphrase )
77
105
defer C .free (unsafe .Pointer (cpassphrase ))
78
106
ret := C .git_cred_ssh_key_memory_new (& cred .ptr , cusername , cpublickey , cprivatekey , cpassphrase )
79
- return int (ret ), cred
107
+ if ret != 0 {
108
+ cred .Free ()
109
+ return nil , MakeGitError (ret )
110
+ }
111
+ return cred , nil
80
112
}
81
113
82
- func NewCredSshKeyFromAgent (username string ) (int , Cred ) {
83
- cred := Cred {}
114
+ func NewCredSshKeyFromAgent (username string ) (* Cred , error ) {
115
+ runtime .LockOSThread ()
116
+ defer runtime .UnlockOSThread ()
117
+
118
+ cred := newCred ()
84
119
cusername := C .CString (username )
85
120
defer C .free (unsafe .Pointer (cusername ))
86
121
ret := C .git_cred_ssh_key_from_agent (& cred .ptr , cusername )
87
- return int (ret ), cred
122
+ if ret != 0 {
123
+ cred .Free ()
124
+ return nil , MakeGitError (ret )
125
+ }
126
+ return cred , nil
88
127
}
89
128
90
- func NewCredDefault () (int , Cred ) {
91
- cred := Cred {}
129
+ func NewCredDefault () (* Cred , error ) {
130
+ runtime .LockOSThread ()
131
+ defer runtime .UnlockOSThread ()
132
+
133
+ cred := newCred ()
92
134
ret := C .git_cred_default_new (& cred .ptr )
93
- return int (ret ), cred
135
+ if ret != 0 {
136
+ cred .Free ()
137
+ return nil , MakeGitError (ret )
138
+ }
139
+ return cred , nil
94
140
}
0 commit comments