-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.go
347 lines (278 loc) · 7.29 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package git
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"time"
homedir "github.com/mitchellh/go-homedir"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/config"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// Repository holds the repository meta data
type Repository struct {
// Path is the full system path to the git repository
Path string
root *git.Repository
}
// User is the relevant user information
type User struct {
// Name is the name in Git's global .config
Name string
// Email is the email in Git's global .config
Email string
// HomeFolder is the system's home folder
HomeFolder string
}
// Init populates the User type with information parsed from the system
func (u *User) Init() error {
folder, err := homedir.Dir()
if err != nil {
return err
}
u.HomeFolder = folder
err = u.load()
if err != nil {
return err
}
return nil
}
// Load parses the users git config file into User
func (u *User) load() error {
const p string = ".gitconfig"
var name string
var email string
file := path.Join(u.HomeFolder, p)
f, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("Git config could not be read: %s", err)
}
c := config.NewDecoder(bytes.NewBuffer(f))
cfg := &config.Config{}
if err := c.Decode(cfg); err != nil {
return fmt.Errorf("%s", err)
}
for _, section := range cfg.Sections {
if section.Name != "user" {
continue
}
for _, option := range section.Options {
switch option.Key {
case "name":
name = option.Value
case "email":
email = option.Value
default:
continue
}
}
}
u.Name = name
u.Email = email
return nil
}
// Load a git repository from disk
func (r *Repository) Load() error {
s, err := git.PlainOpen(r.Path)
if err != nil {
return err
}
r.root = s
return nil
}
// CreateBranch creates a new branch based on an existing branch or returns an error
func (r *Repository) CreateBranch(origin string, new string) error {
origin = fmt.Sprintf("refs/heads/%s", origin)
new = fmt.Sprintf("refs/heads/%s", new)
w, err := r.root.Worktree()
if err != nil {
return fmt.Errorf("Unable to load the work tree: %s", err)
}
ref, err := r.root.Reference(plumbing.ReferenceName(origin), false)
if err != nil {
return err
}
o := &git.CheckoutOptions{}
o.Branch = plumbing.ReferenceName(new)
o.Create = true
o.Hash = ref.Hash()
if err = w.Checkout(o); err != nil {
return fmt.Errorf("Unable to create a new branch: %s", err)
}
return nil
}
// CreateOrphanBranch creates an orphan branch or returns an error
func (r *Repository) CreateOrphanBranch(u *User, s string) error {
name := fmt.Sprintf("refs/heads/%s", s)
w, err := r.root.Worktree()
if err != nil {
return fmt.Errorf("Unable to load the work tree: %s", err)
}
o := &git.CheckoutOptions{}
o.Branch = plumbing.ReferenceName(name)
o.Create = true
if err = w.Checkout(o); err != nil {
return fmt.Errorf("Unable to create a new branch: %s", err)
}
var h []plumbing.Hash
msg := fmt.Sprintf("creating branch for: %s", s)
_, err = w.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: u.Name,
Email: u.Email,
When: time.Now(),
},
Parents: h,
})
if err != nil {
return fmt.Errorf("Unable to make the initial commit: %s", err)
}
return nil
}
// CheckoutBranch switches to an existing branch or returns an error
func (r *Repository) CheckoutBranch(s string) error {
name := fmt.Sprintf("refs/heads/%s", s)
w, err := r.root.Worktree()
if err != nil {
return fmt.Errorf("Unable to load the work tree: %s", err)
}
o := &git.CheckoutOptions{}
o.Branch = plumbing.ReferenceName(name)
o.Create = false
if err = w.Checkout(o); err != nil {
return fmt.Errorf("Unable to checkout branch: %s", err)
}
return nil
}
// AddTagBranch adds a tag to a branch or returns an error
func (r *Repository) AddTagBranch(tag string, s string) error {
name := fmt.Sprintf("refs/heads/%s", s)
ref, err := r.root.Reference(plumbing.ReferenceName(name), false)
if err != nil {
return err
}
commit, err := r.root.CommitObject(ref.Hash())
if err != nil {
return err
}
tr := plumbing.NewHashReference(plumbing.ReferenceName(tag), commit.Hash)
if err := r.root.Storer.SetReference(tr); err != nil {
return err
}
return nil
}
// TagBranch creates/switches to a branch based on a tag name or returns an error
func (r *Repository) TagBranch(s string, create bool) error {
tag := fmt.Sprintf("refs/tags/%s", s)
name := fmt.Sprintf("refs/heads/%s", s)
ref, err := r.root.Reference(plumbing.ReferenceName(tag), false)
if err != nil {
return err
}
w, err := r.root.Worktree()
if err != nil {
return fmt.Errorf("Unable to load the work tree: %s", err)
}
o := &git.CheckoutOptions{}
o.Hash = ref.Hash()
o.Branch = plumbing.ReferenceName(name)
o.Create = create
if err = w.Checkout(o); err != nil {
return fmt.Errorf("Unable to create a new branch: %s", err)
}
return nil
}
// CommitFile adds the file & commits it or returns an error
func (r *Repository) CommitFile(u *User, filename string, msg string) error {
w, err := r.root.Worktree()
if err != nil {
return fmt.Errorf("Unable to load the work tree: %s", err)
}
_, err = w.Add(filename)
if err != nil {
return fmt.Errorf("Unable to git add the file: %s", err)
}
_, err = w.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: u.Name,
Email: u.Email,
When: time.Now(),
},
All: true,
})
if err != nil {
return fmt.Errorf("Unable to commit: %s", err)
}
return nil
}
// Commit makes a commit or returns an error
func (r *Repository) Commit(u *User, filename string, msg string) error {
w, err := r.root.Worktree()
if err != nil {
return fmt.Errorf("Unable to load the work tree: %s", err)
}
_, err = w.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: u.Name,
Email: u.Email,
When: time.Now(),
},
All: true,
})
if err != nil {
return fmt.Errorf("Unable to commit: %s", err)
}
return nil
}
// ListBranches returns a list of all branches in the repository
func (r *Repository) ListBranches() []string {
var b []string
refs, _ := r.root.References()
refs.ForEach(func(ref *plumbing.Reference) error {
if ref.Type() == plumbing.HashReference {
if ref.Name().IsBranch() {
b = append(b, ref.Name().Short())
}
}
return nil
})
return b
}
// BranchExists returns true if a branch exists based on its name or false if it doesn't
func (r *Repository) BranchExists(n string) bool {
b := false
refs, _ := r.root.References()
refs.ForEach(func(ref *plumbing.Reference) error {
if ref.Type() == plumbing.HashReference {
if ref.Name().IsBranch() && ref.Name().Short() == n {
b = true
}
}
return nil
})
return b
}
// RemoveBranch deletes a branch based on its name or returns an error
func (r *Repository) RemoveBranch(n string) error {
err := r.root.Storer.RemoveReference(plumbing.ReferenceName("refs/heads/" + n))
if err != nil {
return err
}
return nil
}
// TagExists returns true if a tag reference exists or false if it doesn't
func (r *Repository) TagExists(n string) bool {
b := false
refs, _ := r.root.References()
refs.ForEach(func(ref *plumbing.Reference) error {
if ref.Type() == plumbing.HashReference {
if ref.Name().IsTag() && ref.Name().Short() == n {
b = true
}
}
return nil
})
return b
}