Skip to content

Commit c365581

Browse files
committed
go-git: Properly set the username for the authentication method
Just using "git" isn't right. We need to be parsing that from the url.
1 parent dceaf6d commit c365581

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

src/gitjournal.go

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111

1212
git "github.com/go-git/go-git/v5"
1313
"github.com/go-git/go-git/v5/config"
14+
"github.com/go-git/go-git/v5/plumbing/transport"
1415
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
1516
"github.com/go-git/go-git/v5/storage/memory"
17+
1618
stdssh "golang.org/x/crypto/ssh"
1719
)
1820

@@ -26,12 +28,26 @@ func GitClone(url *C.char, directory *C.char, privateKey *C.char, privateKeyLen
2628
return nil
2729
}
2830

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+
2946
func gitClone(url string, directory string, privateKey []byte, password string) error {
30-
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
47+
auth, err := buildAuth(url, privateKey, password)
3148
if err != nil {
3249
return err
3350
}
34-
publicKeys.HostKeyCallback = stdssh.InsecureIgnoreHostKey()
3551

3652
/*
3753
progressFile, err := os.OpenFile("/tmp/123.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
@@ -42,7 +58,7 @@ func gitClone(url string, directory string, privateKey []byte, password string)
4258
*/
4359

4460
_, err = git.PlainClone(directory, false, &git.CloneOptions{
45-
Auth: publicKeys,
61+
Auth: auth,
4662
URL: url,
4763
// Progress: progressFile,
4864
})
@@ -63,19 +79,32 @@ func GitFetch(remote *C.char, directory *C.char, privateKey *C.char, privateKeyL
6379
return nil
6480
}
6581

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+
6696
func gitFetch(remote string, directory string, privateKey []byte, password string) error {
67-
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
97+
r, err := git.PlainOpen(directory)
6898
if err != nil {
6999
return err
70100
}
71-
publicKeys.HostKeyCallback = stdssh.InsecureIgnoreHostKey()
72101

73-
r, err := git.PlainOpen(directory)
102+
auth, err := buildAuthForRemote(r, remote, privateKey, password)
74103
if err != nil {
75104
return err
76105
}
77106

78-
err = r.Fetch(&git.FetchOptions{RemoteName: remote, Auth: publicKeys})
107+
err = r.Fetch(&git.FetchOptions{RemoteName: remote, Auth: auth})
79108
if err == git.NoErrAlreadyUpToDate {
80109
return nil
81110
}
@@ -98,18 +127,17 @@ func GitPush(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLe
98127
}
99128

100129
func gitPush(remote string, directory string, privateKey []byte, password string) error {
101-
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
130+
r, err := git.PlainOpen(directory)
102131
if err != nil {
103132
return err
104133
}
105-
publicKeys.HostKeyCallback = stdssh.InsecureIgnoreHostKey()
106134

107-
r, err := git.PlainOpen(directory)
135+
auth, err := buildAuthForRemote(r, remote, privateKey, password)
108136
if err != nil {
109137
return err
110138
}
111139

112-
err = r.Push(&git.PushOptions{RemoteName: remote, Auth: publicKeys})
140+
err = r.Push(&git.PushOptions{RemoteName: remote, Auth: auth})
113141
if err == git.NoErrAlreadyUpToDate {
114142
return nil
115143
}
@@ -138,19 +166,17 @@ func GitDefaultBranch(remoteUrl *C.char, privateKey *C.char, privateKeyLen C.int
138166
}
139167

140168
func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (int, string) {
141-
publicKeys, err := ssh.NewPublicKeys("git", privateKey, password)
169+
auth, err := buildAuth(remoteUrl, privateKey, password)
142170
if err != nil {
143-
fmt.Println("generate publickeys failed:", err.Error())
144171
return 1, ""
145172
}
146-
publicKeys.HostKeyCallback = stdssh.InsecureIgnoreHostKey()
147173

148174
remote := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
149175
Name: "origin",
150176
URLs: []string{remoteUrl},
151177
})
152178

153-
refs, err := remote.List(&git.ListOptions{Auth: publicKeys})
179+
refs, err := remote.List(&git.ListOptions{Auth: auth})
154180
if err != nil {
155181
fmt.Println("git remote list failed:", err.Error())
156182
return 1, ""
@@ -179,6 +205,19 @@ func main() {
179205
password = os.Args[4]
180206
}
181207
182-
gitClone(url, directory, privateKeyFile, password)
208+
privateKey, err := os.ReadFile(privateKeyFile)
209+
if err != nil {
210+
panic(err)
211+
}
212+
213+
fmt.Println("URL:", url)
214+
fmt.Println("Directory:", directory)
215+
fmt.Println("PrivateKey:", privateKey)
216+
fmt.Println("Password:", password)
217+
218+
err = gitClone(url, directory, privateKey, password)
219+
if err != nil {
220+
panic(err)
221+
}
183222
}
184223
*/

0 commit comments

Comments
 (0)