-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.go
365 lines (278 loc) · 6.85 KB
/
api.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package easygit
import (
"errors"
"strings"
"time"
"github.com/libgit2/git2go"
)
// Init
// --------------------------------------------------------
func Clone(url string, repoPath string, user string, pass string) error {
called := false
_, err := git.Clone(url, repoPath, &git.CloneOptions{
FetchOptions: &git.FetchOptions{
RemoteCallbacks: git.RemoteCallbacks{
CredentialsCallback: func(url string, username_from_url string, allowed_types git.CredType) (git.ErrorCode, *git.Cred) {
if called {
return git.ErrUser, nil
}
called = true
ret, creds := git.NewCredUserpassPlaintext(user, pass)
return git.ErrorCode(ret), &creds
},
},
},
})
return err
}
// Add / Commit
// --------------------------------------------------------
func AddAll(repoPath string) error {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return err
}
idx, err := repo.Index()
if err != nil {
return err
}
err = idx.AddAll([]string{}, git.IndexAddDefault, nil)
if err != nil {
return err
}
err = idx.Write()
return err
}
func Commit(repoPath string, message string, name string, email string) error {
sig := &git.Signature{Name: name, Email: email, When: time.Now()}
repo, err := git.OpenRepository(repoPath)
if err != nil {
return err
}
idx, err := repo.Index()
if err != nil {
return err
}
treeID, err := idx.WriteTree()
if err != nil {
return err
}
tree, err := repo.LookupTree(treeID)
if err != nil {
return err
}
head, err := repo.Head()
if head == nil {
_, err = repo.CreateCommit("HEAD", sig, sig, message, tree)
return err
} else if err != nil {
return err
} else {
parent, err := repo.LookupCommit(head.Target())
if err != nil {
return err
}
_, err = repo.CreateCommit("HEAD", sig, sig, message, tree, parent)
return err
}
}
// Branches
// --------------------------------------------------------
func ListBranches(repoPath string) ([]string, error) {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, err
}
iter, err := repo.NewBranchIterator(git.BranchLocal)
if err != nil {
return nil, err
}
var branches []string
branch, _, err := iter.Next()
for err == nil {
name, _ := branch.Name()
branches = append(branches, name)
branch, _, err = iter.Next()
}
return branches, nil
}
func CheckoutBranch(repoPath string, branchName string) error {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return err
}
branch, err := repo.LookupBranch(branchName, git.BranchLocal)
if err != nil {
return err
}
commit, err := repo.LookupCommit(branch.Target())
if err != nil {
return err
}
tree, err := commit.Tree()
if err != nil {
return err
}
err = repo.CheckoutTree(tree, &git.CheckoutOpts{Strategy: git.CheckoutSafe})
if err != nil {
return err
}
err = repo.SetHead("refs/heads/" + branchName)
if err != nil {
return err
}
return nil
}
func PushBranch(repoPath string, remoteName string, branch string, user string, pass string) error {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return err
}
remote, err := repo.Remotes.Lookup(remoteName)
if err != nil {
return err
}
called := false
err = remote.Push([]string{"refs/heads/" + branch}, &git.PushOptions{
RemoteCallbacks: git.RemoteCallbacks{
CredentialsCallback: func(url string, username_from_url string, allowed_types git.CredType) (git.ErrorCode, *git.Cred) {
if called {
return git.ErrUser, nil
}
called = true
ret, creds := git.NewCredUserpassPlaintext(user, pass)
return git.ErrorCode(ret), &creds
},
},
})
return err
}
func PullBranch(repoPath string, remoteName string, branchName string, user string, pass string, name string, email string) error {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return err
}
remote, err := repo.Remotes.Lookup(remoteName)
if err != nil {
return err
}
called := false
err = remote.Fetch([]string{}, &git.FetchOptions{
RemoteCallbacks: git.RemoteCallbacks{
CredentialsCallback: func(url string, username_from_url string, allowed_types git.CredType) (git.ErrorCode, *git.Cred) {
if called {
return git.ErrUser, nil
}
called = true
ret, creds := git.NewCredUserpassPlaintext(user, pass)
return git.ErrorCode(ret), &creds
},
},
}, "")
if err != nil {
return err
}
remoteBranch, err := repo.References.Lookup("refs/remotes/" + remoteName + "/" + branchName)
if err != nil {
return err
}
mergeRemoteHead, err := repo.AnnotatedCommitFromRef(remoteBranch)
if err != nil {
return err
}
mergeHeads := make([]*git.AnnotatedCommit, 1)
mergeHeads[0] = mergeRemoteHead
if err = repo.Merge(mergeHeads, nil, nil); err != nil {
return err
}
// Check if the index has conflicts after the merge
idx, err := repo.Index()
if err != nil {
return err
}
currentBranch, err := repo.Head()
if err != nil {
return err
}
localCommit, err := repo.LookupCommit(currentBranch.Target())
if err != nil {
return err
}
// If index has conflicts, read old tree into index and
// return an error.
if idx.HasConflicts() {
repo.ResetToCommit(localCommit, git.ResetHard, &git.CheckoutOpts{})
repo.StateCleanup()
return errors.New("conflict")
}
// If everything looks fine, create a commit with the two parents
treeID, err := idx.WriteTree()
if err != nil {
return err
}
tree, err := repo.LookupTree(treeID)
if err != nil {
return err
}
remoteCommit, err := repo.LookupCommit(remoteBranch.Target())
if err != nil {
return err
}
sig := &git.Signature{Name: name, Email: email, When: time.Now()}
_, err = repo.CreateCommit("HEAD", sig, sig, "merged", tree, localCommit, remoteCommit)
if err != nil {
return err
}
repo.StateCleanup()
return nil
}
func CreateBranch(repoPath string, from string, to string) error {
repo, err := git.OpenRepository(repoPath)
if err != nil {
return err
}
fromBranch, err := repo.LookupBranch(from, git.BranchLocal)
if err != nil {
return err
}
fromCommit, err := repo.LookupCommit(fromBranch.Target())
if err != nil {
return err
}
_, err = repo.CreateBranch(to, fromCommit, false)
if err != nil {
return err
}
return nil
}
func CurrentBranch(repoPath string) (string, error) {
repo, repoErr := git.OpenRepository(repoPath)
if repoErr != nil {
return "", repoErr
}
head, headErr := repo.Head()
if repoErr != nil {
return "", headErr
}
if head == nil {
return "", headErr
}
//find the branch name
branch := ""
branchElements := strings.Split(head.Name(), "/")
if len(branchElements) == 3 {
branch = branchElements[2]
}
return branch, nil
}
func DeleteBranch(repoPath string, branchName string) error {
repo, repoErr := git.OpenRepository(repoPath)
if repoErr != nil {
return repoErr
}
branch, branchErr := repo.LookupBranch(branchName, git.BranchLocal)
if branchErr != nil {
return branchErr
}
return branch.Delete()
}