-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitClient.go
More file actions
169 lines (142 loc) · 4.18 KB
/
gitClient.go
File metadata and controls
169 lines (142 loc) · 4.18 KB
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
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"os"
"path/filepath"
"time"
git "github.com/libgit2/git2go/v31"
)
// Represents the `git clone` command.
func gitClone(user, password, url, path string) {
fmt.Println("Cloning", url)
credentialsCallback := func(url, username string, allowedTypes git.CredentialType) (*git.Credential, error) {
credential, err := git.NewCredentialUserpassPlaintext(user, password)
return credential, err
}
certificateCheckCallback := func(cert *git.Certificate, valid bool, hostname string) git.ErrorCode {
return 0
}
cloneOptions := &git.CloneOptions{
CheckoutOpts: &git.CheckoutOptions{
Strategy: git.CheckoutForce,
DisableFilters: true,
DirMode: 0,
FileMode: 0,
FileOpenFlags: 0,
NotifyFlags: git.CheckoutNotifyAll,
},
FetchOptions: &git.FetchOptions{
RemoteCallbacks: git.RemoteCallbacks{
CredentialsCallback: credentialsCallback,
CertificateCheckCallback: certificateCheckCallback,
},
Prune: 0,
UpdateFetchhead: false,
DownloadTags: 0,
ProxyOptions: git.ProxyOptions{},
},
Bare: false,
}
if _, err := git.Clone(url, path, cloneOptions); err != nil {
logHandler("debug", err.Error())
return
}
}
// Represents the `git add origin remote` command
func gitRemoteAddOriginURL(path, url string) *git.Repository {
var repo *git.Repository
var err error
if repo, err = git.InitRepository(path, false); err != nil {
logHandler("debug", err.Error())
}
if _, err = repo.Remotes.Create("origin", url); err != nil {
logHandler("debug", err.Error())
}
return repo
}
// Represents the `git add` command.
func gitAdd(repo *git.Repository) {
var idx *git.Index
var err error
if idx, err = repo.Index(); err != nil {
logHandler("debug", err.Error())
}
if err = idx.AddAll([]string{}, git.IndexAddDefault, nil); err != nil {
logHandler("debug", err.Error())
}
if err = idx.Write(); err != nil {
logHandler("debug", err.Error())
}
}
// Represents the `git commit` command.
func gitCommit(repo *git.Repository, msg, name, email string) {
var idx *git.Index
var objectId *git.Oid
var treeId *git.Tree
var err error
if idx, err = repo.Index(); err != nil {
logHandler("debug", err.Error())
}
if objectId, err = idx.WriteTreeTo(repo); err != nil {
logHandler("debug", err.Error())
}
if treeId, err = repo.LookupTree(objectId); err != nil {
logHandler("debug", err.Error())
}
signature := &git.Signature{
Name: name,
Email: email,
When: time.Now(),
}
if _, err = repo.CreateCommit(
"HEAD",
signature,
signature,
msg+" on "+time.Now().Format("2 Jan 2006"),
treeId,
); err != nil {
logHandler("debug", err.Error())
}
}
//Represents the `git push` command.
func gitPush(repo *git.Repository, repoName, user, password, branch, url string) {
var remote *git.Remote
var err error
credentialsCallback := func(url, username string, allowedTypes git.CredentialType) (*git.Credential, error) {
credential, err := git.NewCredentialUserpassPlaintext(user, password)
return credential, err
}
certificateCheckCallback := func(cert *git.Certificate, valid bool, hostname string) git.ErrorCode {
return 0
}
pushOptions := &git.PushOptions{
RemoteCallbacks: git.RemoteCallbacks{
CredentialsCallback: credentialsCallback,
CertificateCheckCallback: certificateCheckCallback,
},
PbParallelism: 0,
Headers: []string{},
}
if remote, err = repo.Remotes.Create(branch, url); err != nil {
logHandler("debug", err.Error())
}
if err = remote.Push([]string{"refs/heads/" + branch}, pushOptions); err != nil {
logHandler("debug", err.Error())
}
fmt.Printf("Pushing repo %q to GitHub\n", repoName)
}
// Execute `git add remote origin; git add; git commit; git push`
func gitRepo(url, user, pass, repoName, branch, message, author, email string, private bool) {
var repoDir string
var err error
if repoDir, err = filepath.Abs(repoName); err != nil {
logHandler("debug", err.Error())
}
if err := os.RemoveAll(repoDir + "/.git"); err != nil {
logHandler("debug", err.Error())
}
githubRepo := gitRemoteAddOriginURL(repoDir, url)
gitAdd(githubRepo)
gitCommit(githubRepo, message, author, email)
gitPush(githubRepo, repoName, user, pass, branch, url)
}