This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_client.go
159 lines (142 loc) · 3.61 KB
/
git_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"os"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)
type gitClient struct {
repository *git.Repository
worktree *git.Worktree
cnf *config.Config
}
func NewGitClient(workspacePath string) (*gitClient, error) {
repository, err := git.PlainOpen(workspacePath)
if err != nil {
return nil, err
}
fmt.Printf("repositoy: %+v\n", repository)
worktree, err := repository.Worktree()
if err != nil {
return nil, err
}
fmt.Printf("worktree: %+v\n", worktree)
cnf, err := repository.Config()
if err != nil {
return nil, err
}
fmt.Printf("config: %+v\n", cnf)
return &gitClient{
repository: repository,
worktree: worktree,
cnf: cnf,
}, nil
}
func (gc *gitClient) createRemote(repositoryName string) error {
if _, err := gc.repository.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{"https://github.com/" + repositoryName + ".git"},
}); err != nil {
return err
}
cnf, err := gc.repository.Config()
if err != nil {
return err
}
gc.cnf = cnf
return nil
}
func (gc *gitClient) deleteRemote() error {
if err := gc.repository.DeleteRemote("origin"); err != nil {
return err
}
cnf, err := gc.repository.Config()
if err != nil {
return err
}
gc.cnf = cnf
return nil
}
func (gc *gitClient) setInstead(accessToken string, insteadOfURLs []string) error {
useURL := "https://" + accessToken + "@github.com"
for _, insteadOfURL := range insteadOfURLs {
gc.cnf.URLs[insteadOfURL] = &config.URL{Name: useURL, InsteadOf: insteadOfURL}
}
if err := gc.repository.SetConfig(gc.cnf); err != nil {
return err
}
cnf, err := gc.repository.Config()
if err != nil {
return err
}
gc.cnf = cnf
return nil
}
func (gc *gitClient) checkout(branch string) error {
return gc.worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.ReferenceName("refs/heads/" + branch), Create: true})
}
func (gc *gitClient) addFiles(filePaths []string) error {
pathsCount := len(filePaths)
for i, filePath := range filePaths {
fmt.Printf("[%d/%d] git add %v\n", i, pathsCount, filePath)
blobHash, err := gc.worktree.Add(filePath)
if err != nil {
return err
}
fmt.Printf("blob hash is %s\n", blobHash)
}
return nil
}
func (gc *gitClient) commit(message string) error {
commitHash, err := gc.worktree.Commit(message, &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: gc.cnf.Author.Name,
Email: gc.cnf.Author.Email,
},
Committer: &object.Signature{
Name: gc.cnf.User.Name,
Email: gc.cnf.User.Email,
},
})
if err != nil {
return err
}
fmt.Printf("commit hash is %s\n", commitHash)
return nil
}
func (gc *gitClient) pushWithUserNamePassword(password string) error {
return gc.repository.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth{
Username: gc.cnf.User.Name,
Password: password,
},
Progress: os.Stdout,
})
}
func (gc *gitClient) pushWithAccessToken(accessToken string) error {
return gc.repository.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth{
Username: gc.cnf.User.Name,
Password: accessToken,
},
Progress: os.Stdout,
})
}
func (gc *gitClient) pushWithSSH(pemFilePath string, password string) error {
publicKey, err := ssh.NewPublicKeysFromFile(ssh.DefaultUsername, pemFilePath, password)
if err != nil {
return err
}
return gc.repository.Push(&git.PushOptions{
RemoteName: "origin",
Auth: publicKey,
Progress: os.Stdout,
})
}