-
Notifications
You must be signed in to change notification settings - Fork 4
/
git.go
169 lines (142 loc) · 4.19 KB
/
git.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
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"github.com/go-git/go-git/v5"
"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/storage/memory"
)
const gitUsername = "some-user"
// create branch from default branch
func createBranch(org string, repo string, branch string, format string) {
auth := &http.BasicAuth{
Username: gitUsername, // anything except an empty string
Password: args.Token,
}
// Clone the given repository to the memory
repoURL := fmt.Sprintf("https://github.com/%s/%s.git", org, repo)
Info("git clone %s", repoURL)
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: repoURL,
Auth: auth,
})
CheckIfError(err)
// Create a new branch to the current HEAD
Info("git branch %s", branch)
headRef, err := r.Head()
CheckIfError(err)
fmt.Println(headRef)
// Create a new plumbing.HashReference object with the name of the branch
// and the hash from the HEAD. The reference name should be a full reference
// name and not an abbreviated one, as is used on the git cli.
ref := plumbing.NewHashReference(plumbing.NewBranchReferenceName(branch), headRef.Hash())
// The created reference is saved in the storage.
err = r.Storer.SetReference(ref)
CheckIfError(err)
// push using default options
Info("git push")
err = r.Push(&git.PushOptions{Auth: auth})
CheckIfError(err)
}
func addFile(org, repo, branch, fileFrom, fileTo, commitMessage, gitEmail, format string) {
if fileFrom == "" {
CheckIfError(fmt.Errorf("fileName is empty"))
}
if fileTo == "" {
fileTo = fileFrom
}
if commitMessage == "" {
commitMessage = "Change " + fileTo
}
auth := &http.BasicAuth{
Username: gitUsername, // anything except an empty string
Password: args.Token,
}
dir, err := ioutil.TempDir("", repo+"-"+branch)
if err != nil {
log.Fatal(err)
}
fmt.Println(dir)
defer os.RemoveAll(dir)
// Clone the given repository to the memory
repoURL := fmt.Sprintf("https://github.com/%s/%s.git", org, repo)
Info("git clone %s", repoURL)
r, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: repoURL,
Auth: auth,
ReferenceName: plumbing.NewBranchReferenceName(branch),
SingleBranch: true,
Depth: 1,
})
CheckIfError(err)
w, err := r.Worktree()
CheckIfError(err)
// ... we need a file to commit so let's create a new file inside of the
// worktree of the project using the go standard library.
// Info("echo \"hello world!\" > example-git-file")
// filename := filepath.Join(dir, files)
copyFile(fileFrom, filepath.Join(dir, fileTo))
// err = ioutil.WriteFile(filename, []byte("hello world!"), 0644)
// CheckIfError(err)
// Adds the new file to the staging area.
Info("git add %s -> %s", fileFrom, fileTo)
_, err = w.Add(fileTo)
CheckIfError(err)
// We can verify the current status of the worktree using the method Status.
Info("git status --porcelain")
status, err := w.Status()
CheckIfError(err)
fmt.Println(status)
// Commits the current staging area to the repository, with the new file
// just created. We should provide the object.Signature of Author of the
// commit.
Info("git commit -m \"%s\"", commitMessage)
commit, err := w.Commit(commitMessage, &git.CommitOptions{
Author: &object.Signature{
Email: gitEmail,
When: time.Now(),
},
})
CheckIfError(err)
// Prints the current HEAD to verify that all worked well.
Info("git show -s")
obj, err := r.CommitObject(commit)
CheckIfError(err)
fmt.Println(obj)
// push using default options
Info("git push")
err = r.Push(&git.PushOptions{Auth: auth})
CheckIfError(err)
}
func ensureDirExists(fileName string) {
dirName := filepath.Dir(fileName)
os.MkdirAll(dirName, os.ModePerm)
}
func copyFile(from, to string) {
input, err := ioutil.ReadFile(from)
if err != nil {
fmt.Println(err)
return
}
ensureDirExists(to)
err = ioutil.WriteFile(to, input, 0644)
if err != nil {
fmt.Println("Error creating", to)
fmt.Println(err)
return
}
}
// CheckIfError should be used to naively panics if an error is not nil.
func CheckIfError(err error) {
if err == nil {
return
}
Error(err)
os.Exit(1)
}