@@ -6,16 +6,7 @@ import (
66 #include <stdlib.h>
77 */
88 "C"
9- "fmt"
109 "unsafe"
11-
12- git "github.com/go-git/go-git/v5"
13- "github.com/go-git/go-git/v5/config"
14- "github.com/go-git/go-git/v5/plumbing/transport"
15- "github.com/go-git/go-git/v5/plumbing/transport/ssh"
16- "github.com/go-git/go-git/v5/storage/memory"
17-
18- stdssh "golang.org/x/crypto/ssh"
1910)
2011
2112//export GitClone
@@ -28,47 +19,6 @@ func GitClone(url *C.char, directory *C.char, privateKey *C.char, privateKeyLen
2819 return nil
2920}
3021
31- func buildAuth (url string , privateKey []byte , password string ) (transport.AuthMethod , error ) {
32- ep , err := transport .NewEndpoint (url )
33- if err != nil {
34- return nil , err
35- }
36-
37- publicKeys , err := ssh .NewPublicKeys (ep .User , privateKey , password )
38- if err != nil {
39- return nil , err
40- }
41- publicKeys .HostKeyCallback = stdssh .InsecureIgnoreHostKey ()
42-
43- return publicKeys , nil
44- }
45-
46- func gitClone (url string , directory string , privateKey []byte , password string ) error {
47- auth , err := buildAuth (url , privateKey , password )
48- if err != nil {
49- return err
50- }
51-
52- /*
53- progressFile, err := os.OpenFile("/tmp/123.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
54- if err != nil {
55- panic(err)
56- }
57- defer progressFile.Close()
58- */
59-
60- _ , err = git .PlainClone (directory , false , & git.CloneOptions {
61- Auth : auth ,
62- URL : url ,
63- // Progress: progressFile,
64- })
65- if err != nil {
66- return err
67- }
68-
69- return nil
70- }
71-
7222//export GitFetch
7323func GitFetch (remote * C.char , directory * C.char , privateKey * C.char , privateKeyLen C.int , password * C.char ) * C.char {
7424 err := gitFetch (C .GoString (remote ), C .GoString (directory ), C .GoBytes (unsafe .Pointer (privateKey ), privateKeyLen ), C .GoString (password ))
@@ -79,43 +29,6 @@ func GitFetch(remote *C.char, directory *C.char, privateKey *C.char, privateKeyL
7929 return nil
8030}
8131
82- func buildAuthForRemote (repo * git.Repository , remoteName string , privateKey []byte , password string ) (transport.AuthMethod , error ) {
83- rem , err := repo .Remote (remoteName )
84- if err != nil {
85- return nil , err
86- }
87-
88- urls := rem .Config ().URLs
89- if len (urls ) == 0 {
90- return nil , fmt .Errorf ("no remote url" )
91- }
92-
93- return buildAuth (urls [0 ], privateKey , password )
94- }
95-
96- func gitFetch (remote string , directory string , privateKey []byte , password string ) error {
97- r , err := git .PlainOpen (directory )
98- if err != nil {
99- return err
100- }
101-
102- auth , err := buildAuthForRemote (r , remote , privateKey , password )
103- if err != nil {
104- return err
105- }
106-
107- err = r .Fetch (& git.FetchOptions {RemoteName : remote , Auth : auth })
108- if err == git .NoErrAlreadyUpToDate {
109- return nil
110- }
111-
112- if err != nil {
113- return err
114- }
115-
116- return nil
117- }
118-
11932//export GitPush
12033func GitPush (remote * C.char , directory * C.char , privateKey * C.char , privateKeyLen C.int , password * C.char ) * C.char {
12134 err := gitPush (C .GoString (remote ), C .GoString (directory ), C .GoBytes (unsafe .Pointer (privateKey ), privateKeyLen ), C .GoString (password ))
@@ -126,36 +39,6 @@ func GitPush(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLe
12639 return nil
12740}
12841
129- func gitPush (remote string , directory string , privateKey []byte , password string ) error {
130- r , err := git .PlainOpen (directory )
131- if err != nil {
132- return err
133- }
134-
135- auth , err := buildAuthForRemote (r , remote , privateKey , password )
136- if err != nil {
137- return err
138- }
139-
140- err = r .Push (& git.PushOptions {RemoteName : remote , Auth : auth })
141- if err == git .NoErrAlreadyUpToDate {
142- return nil
143- }
144-
145- if err != nil {
146- return err
147- }
148-
149- return nil
150- }
151-
152- /*
153- type GitDefaultBranchResult struct {
154- err int
155- val *C.char
156- }
157- */
158-
15942//export GitDefaultBranch
16043func GitDefaultBranch (remoteUrl * C.char , privateKey * C.char , privateKeyLen C.int , password * C.char ) * C.char {
16144 err , val := gitDefaultBranch (C .GoString (remoteUrl ), C .GoBytes (unsafe .Pointer (privateKey ), privateKeyLen ), C .GoString (password ))
@@ -165,34 +48,6 @@ func GitDefaultBranch(remoteUrl *C.char, privateKey *C.char, privateKeyLen C.int
16548 return C .CString (val )
16649}
16750
168- func gitDefaultBranch (remoteUrl string , privateKey []byte , password string ) (int , string ) {
169- auth , err := buildAuth (remoteUrl , privateKey , password )
170- if err != nil {
171- return 1 , ""
172- }
173-
174- remote := git .NewRemote (memory .NewStorage (), & config.RemoteConfig {
175- Name : "origin" ,
176- URLs : []string {remoteUrl },
177- })
178-
179- refs , err := remote .List (& git.ListOptions {Auth : auth })
180- if err != nil {
181- fmt .Println ("git remote list failed:" , err .Error ())
182- return 1 , ""
183- }
184-
185- defaultBranch := ""
186- for _ , ref := range refs {
187- if ref .Name () == "HEAD" {
188- defaultBranch = ref .Target ().Short ()
189- break
190- }
191- }
192-
193- return 0 , defaultBranch
194- }
195-
19651func main () {}
19752
19853/*
0 commit comments