@@ -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