@@ -2,52 +2,103 @@ package git
22
33import (
44 "errors"
5+ "os"
56 "strings"
67
7- "github.com/go-git/go-git/v5"
8- "github.com/go-git/go-git/v5/plumbing/transport"
9- "github.com/go-git/go-git/v5/plumbing/transport/http"
10- gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
8+ git2go "github.com/libgit2/git2go/v34"
9+
1110 "golang.org/x/crypto/ssh"
1211)
1312
1413type ConnectionOptions struct {
15- Repository string
16- Branch string
17- Auth transport.AuthMethod
14+ Directory string
15+ Repository string
16+ Branch string
17+ Authentication * Authentication
18+ IgnoreSslHostKey bool
19+ PullRebase bool
20+ Signature * Signature
21+ }
22+
23+ type Authentication struct {
24+ BasicAuth * BasicAuth
25+ SshKey * SshKey
26+ }
27+
28+ type BasicAuth struct {
29+ Username string
30+ Password string
31+ }
32+
33+ type SshKey struct {
34+ PrivateKey []byte
35+ Passphrase * string
36+ Signer * ssh.Signer
37+ }
38+
39+ type Signature struct {
40+ Name string
41+ Email string
1842}
1943
2044type Connection struct {
21- Repository * git .Repository
45+ Repository * git2go .Repository
2246 Options * ConnectionOptions
2347}
2448
2549func NewGitConnection (options * ConnectionOptions ) (* Connection , error ) {
26- return & Connection {
50+ connection := & Connection {
2751 Options : options ,
28- }, nil
52+ }
53+
54+ if options .Directory != "" {
55+ stat , err := os .Stat (options .Directory )
56+ if err == nil {
57+ if stat .IsDir () {
58+ repository , err := git2go .OpenRepository (options .Directory )
59+ if err != nil {
60+ return nil , err
61+ }
62+ connection .Repository = repository
63+ }
64+ }
65+ }
66+
67+ if options .Signature == nil {
68+ connection .Options .Signature = & Signature {
69+ Name : "GitOps CI User" ,
70+ Email : "gitops@example.com" ,
71+ }
72+ } else {
73+ connection .Options .Signature = options .Signature
74+ }
75+
76+ return connection , nil
2977}
3078
31- func GetAuthFromUsernamePassword (username string , password string ) (transport.AuthMethod , error ) {
32- return & http.BasicAuth {
33- Username : username ,
34- Password : password ,
79+ func GetAuthFromUsernamePassword (username string , password string ) (* Authentication , error ) {
80+ return & Authentication {
81+ BasicAuth : & BasicAuth {
82+ Username : username ,
83+ Password : password ,
84+ },
3585 }, nil
3686}
3787
38- func GetAuthFromBasicAuthString (basicAuth string ) (transport. AuthMethod , error ) {
88+ func GetAuthFromBasicAuthString (basicAuth string ) (* Authentication , error ) {
3989 split := strings .Split (basicAuth , ":" )
4090 if len (split ) != 2 {
4191 return nil , errors .New ("invalid basic auth string" )
4292 }
43- return & http.BasicAuth {
44- Username : split [0 ],
45- Password : split [1 ],
93+ return & Authentication {
94+ BasicAuth : & BasicAuth {
95+ Username : split [0 ],
96+ Password : split [1 ],
97+ },
4698 }, nil
4799}
48100
49- func GetAuthFromSshKey (sshKey []byte , sshKeyPassphrase * string , noStrictHostKeyChecking bool ) (transport.AuthMethod , error ) {
50-
101+ func GetAuthFromSshKey (sshKey []byte , sshKeyPassphrase * string ) (* Authentication , error ) {
51102 var signer ssh.Signer
52103 if sshKeyPassphrase != nil {
53104 _signer , err := ssh .ParsePrivateKeyWithPassphrase (sshKey , []byte (* sshKeyPassphrase ))
@@ -63,11 +114,28 @@ func GetAuthFromSshKey(sshKey []byte, sshKeyPassphrase *string, noStrictHostKeyC
63114 signer = _signer
64115 }
65116
66- auth := & gitssh.PublicKeys {User : "git" , Signer : signer }
117+ return & Authentication {
118+ SshKey : & SshKey {
119+ PrivateKey : sshKey ,
120+ Passphrase : sshKeyPassphrase ,
121+ Signer : & signer ,
122+ },
123+ }, nil
124+ }
125+
126+ func (c * Connection ) credentialsCallback (url string , usernameFromURL string , allowedTypes git2go.CredentialType ) (* git2go.Cred , error ) {
127+
128+ if c .Options .Authentication == nil {
129+ return git2go .NewCredentialDefault ()
130+ }
131+
132+ if c .Options .Authentication .BasicAuth != nil {
133+ return git2go .NewCredentialUserpassPlaintext (c .Options .Authentication .BasicAuth .Username , c .Options .Authentication .BasicAuth .Password )
134+ }
67135
68- if noStrictHostKeyChecking {
69- auth . HostKeyCallback = ssh . InsecureIgnoreHostKey ( )
136+ if c . Options . Authentication . SshKey != nil {
137+ return git2go . NewCredentialSSHKeyFromSigner ( usernameFromURL , * c . Options . Authentication . SshKey . Signer )
70138 }
71139
72- return auth , nil
140+ return git2go . NewCredentialDefault ()
73141}
0 commit comments