Skip to content

Commit 1723ea4

Browse files
committed
go: Split up the code into multiple files
The c-lib part is now in a different file.
1 parent c365581 commit 1723ea4

File tree

3 files changed

+143
-146
lines changed

3 files changed

+143
-146
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
build:
3-
cd src && go build -o gitjournal.so -buildmode=c-shared gitjournal.go
3+
cd src && go build -o gitjournal.so -buildmode=c-shared .
44
dart run ffigen

src/git.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
git "github.com/go-git/go-git/v5"
7+
"github.com/go-git/go-git/v5/config"
8+
"github.com/go-git/go-git/v5/plumbing/transport"
9+
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
10+
"github.com/go-git/go-git/v5/storage/memory"
11+
12+
stdssh "golang.org/x/crypto/ssh"
13+
)
14+
15+
func buildAuth(url string, privateKey []byte, password string) (transport.AuthMethod, error) {
16+
ep, err := transport.NewEndpoint(url)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
publicKeys, err := ssh.NewPublicKeys(ep.User, privateKey, password)
22+
if err != nil {
23+
return nil, err
24+
}
25+
publicKeys.HostKeyCallback = stdssh.InsecureIgnoreHostKey()
26+
27+
return publicKeys, nil
28+
}
29+
30+
func gitClone(url string, directory string, privateKey []byte, password string) error {
31+
auth, err := buildAuth(url, privateKey, password)
32+
if err != nil {
33+
return err
34+
}
35+
36+
/*
37+
progressFile, err := os.OpenFile("/tmp/123.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
38+
if err != nil {
39+
panic(err)
40+
}
41+
defer progressFile.Close()
42+
*/
43+
44+
_, err = git.PlainClone(directory, false, &git.CloneOptions{
45+
Auth: auth,
46+
URL: url,
47+
// Progress: progressFile,
48+
})
49+
if err != nil {
50+
return err
51+
}
52+
53+
return nil
54+
}
55+
56+
func buildAuthForRemote(repo *git.Repository, remoteName string, privateKey []byte, password string) (transport.AuthMethod, error) {
57+
rem, err := repo.Remote(remoteName)
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
urls := rem.Config().URLs
63+
if len(urls) == 0 {
64+
return nil, fmt.Errorf("no remote url")
65+
}
66+
67+
return buildAuth(urls[0], privateKey, password)
68+
}
69+
70+
func gitFetch(remote string, directory string, privateKey []byte, password string) error {
71+
r, err := git.PlainOpen(directory)
72+
if err != nil {
73+
return err
74+
}
75+
76+
auth, err := buildAuthForRemote(r, remote, privateKey, password)
77+
if err != nil {
78+
return err
79+
}
80+
81+
err = r.Fetch(&git.FetchOptions{RemoteName: remote, Auth: auth})
82+
if err == git.NoErrAlreadyUpToDate {
83+
return nil
84+
}
85+
86+
if err != nil {
87+
return err
88+
}
89+
90+
return nil
91+
}
92+
93+
func gitPush(remote string, directory string, privateKey []byte, password string) error {
94+
r, err := git.PlainOpen(directory)
95+
if err != nil {
96+
return err
97+
}
98+
99+
auth, err := buildAuthForRemote(r, remote, privateKey, password)
100+
if err != nil {
101+
return err
102+
}
103+
104+
err = r.Push(&git.PushOptions{RemoteName: remote, Auth: auth})
105+
if err == git.NoErrAlreadyUpToDate {
106+
return nil
107+
}
108+
109+
if err != nil {
110+
return err
111+
}
112+
113+
return nil
114+
}
115+
116+
func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (int, string) {
117+
auth, err := buildAuth(remoteUrl, privateKey, password)
118+
if err != nil {
119+
return 1, ""
120+
}
121+
122+
remote := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
123+
Name: "origin",
124+
URLs: []string{remoteUrl},
125+
})
126+
127+
refs, err := remote.List(&git.ListOptions{Auth: auth})
128+
if err != nil {
129+
fmt.Println("git remote list failed:", err.Error())
130+
return 1, ""
131+
}
132+
133+
defaultBranch := ""
134+
for _, ref := range refs {
135+
if ref.Name() == "HEAD" {
136+
defaultBranch = ref.Target().Short()
137+
break
138+
}
139+
}
140+
141+
return 0, defaultBranch
142+
}

src/gitjournal.go

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -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
7323
func 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
12033
func 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
16043
func 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-
19651
func main() {}
19752

19853
/*

0 commit comments

Comments
 (0)