forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
441 lines (364 loc) · 12.9 KB
/
merge.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package git
/*
#include <git2.h>
extern git_annotated_commit** _go_git_make_merge_head_array(size_t len);
extern void _go_git_annotated_commit_array_set(git_annotated_commit** array, git_annotated_commit* ptr, size_t n);
extern git_annotated_commit* _go_git_annotated_commit_array_get(git_annotated_commit** array, size_t n);
extern int _go_git_merge_file(git_merge_file_result*, char*, size_t, char*, unsigned int, char*, size_t, char*, unsigned int, char*, size_t, char*, unsigned int, git_merge_file_options*);
*/
import "C"
import (
"reflect"
"runtime"
"unsafe"
)
type AnnotatedCommit struct {
ptr *C.git_annotated_commit
r *Repository
}
func newAnnotatedCommitFromC(ptr *C.git_annotated_commit, r *Repository) *AnnotatedCommit {
mh := &AnnotatedCommit{ptr: ptr, r: r}
runtime.SetFinalizer(mh, (*AnnotatedCommit).Free)
return mh
}
func (mh *AnnotatedCommit) Free() {
runtime.SetFinalizer(mh, nil)
C.git_annotated_commit_free(mh.ptr)
}
func (r *Repository) AnnotatedCommitFromFetchHead(branchName string, remoteURL string, oid *Oid) (*AnnotatedCommit, error) {
cbranchName := C.CString(branchName)
defer C.free(unsafe.Pointer(cbranchName))
cremoteURL := C.CString(remoteURL)
defer C.free(unsafe.Pointer(cremoteURL))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_from_fetchhead(&ptr, r.ptr, cbranchName, cremoteURL, oid.toC())
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newAnnotatedCommitFromC(ptr, r), nil
}
func (r *Repository) LookupAnnotatedCommit(oid *Oid) (*AnnotatedCommit, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_lookup(&ptr, r.ptr, oid.toC())
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newAnnotatedCommitFromC(ptr, r), nil
}
func (r *Repository) AnnotatedCommitFromRef(ref *Reference) (*AnnotatedCommit, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_from_ref(&ptr, r.ptr, ref.ptr)
runtime.KeepAlive(r)
runtime.KeepAlive(ref)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newAnnotatedCommitFromC(ptr, r), nil
}
type MergeTreeFlag int
const (
// Detect renames that occur between the common ancestor and the "ours"
// side or the common ancestor and the "theirs" side. This will enable
// the ability to merge between a modified and renamed file.
MergeTreeFindRenames MergeTreeFlag = C.GIT_MERGE_FIND_RENAMES
// If a conflict occurs, exit immediately instead of attempting to
// continue resolving conflicts. The merge operation will fail with
// GIT_EMERGECONFLICT and no index will be returned.
MergeTreeFailOnConflict MergeTreeFlag = C.GIT_MERGE_FAIL_ON_CONFLICT
)
type MergeOptions struct {
Version uint
TreeFlags MergeTreeFlag
RenameThreshold uint
TargetLimit uint
FileFavor MergeFileFavor
//TODO: Diff similarity metric
}
func mergeOptionsFromC(opts *C.git_merge_options) MergeOptions {
return MergeOptions{
Version: uint(opts.version),
TreeFlags: MergeTreeFlag(opts.flags),
RenameThreshold: uint(opts.rename_threshold),
TargetLimit: uint(opts.target_limit),
FileFavor: MergeFileFavor(opts.file_favor),
}
}
func DefaultMergeOptions() (MergeOptions, error) {
opts := C.git_merge_options{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_merge_init_options(&opts, C.GIT_MERGE_OPTIONS_VERSION)
if ecode < 0 {
return MergeOptions{}, MakeGitError(ecode)
}
return mergeOptionsFromC(&opts), nil
}
func (mo *MergeOptions) toC() *C.git_merge_options {
if mo == nil {
return nil
}
return &C.git_merge_options{
version: C.uint(mo.Version),
flags: C.git_merge_flag_t(mo.TreeFlags),
rename_threshold: C.uint(mo.RenameThreshold),
target_limit: C.uint(mo.TargetLimit),
file_favor: C.git_merge_file_favor_t(mo.FileFavor),
}
}
type MergeFileFavor int
const (
MergeFileFavorNormal MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_NORMAL
MergeFileFavorOurs MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_OURS
MergeFileFavorTheirs MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_THEIRS
MergeFileFavorUnion MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_UNION
)
func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOptions, checkoutOptions *CheckoutOpts) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cMergeOpts := mergeOptions.toC()
cCheckoutOpts := checkoutOptions.toC()
defer freeCheckoutOpts(cCheckoutOpts)
gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads))
for i := 0; i < len(theirHeads); i++ {
gmerge_head_array[i] = theirHeads[i].ptr
}
ptr := unsafe.Pointer(&gmerge_head_array[0])
err := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOpts)
runtime.KeepAlive(theirHeads)
if err < 0 {
return MakeGitError(err)
}
return nil
}
type MergeAnalysis int
const (
MergeAnalysisNone MergeAnalysis = C.GIT_MERGE_ANALYSIS_NONE
MergeAnalysisNormal MergeAnalysis = C.GIT_MERGE_ANALYSIS_NORMAL
MergeAnalysisUpToDate MergeAnalysis = C.GIT_MERGE_ANALYSIS_UP_TO_DATE
MergeAnalysisFastForward MergeAnalysis = C.GIT_MERGE_ANALYSIS_FASTFORWARD
MergeAnalysisUnborn MergeAnalysis = C.GIT_MERGE_ANALYSIS_UNBORN
)
type MergePreference int
const (
MergePreferenceNone MergePreference = C.GIT_MERGE_PREFERENCE_NONE
MergePreferenceNoFastForward MergePreference = C.GIT_MERGE_PREFERENCE_NO_FASTFORWARD
MergePreferenceFastForwardOnly MergePreference = C.GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
)
// MergeAnalysis returns the possible actions which could be taken by
// a 'git-merge' command. There may be multiple answers, so the first
// return value is a bitmask of MergeAnalysis values.
func (r *Repository) MergeAnalysis(theirHeads []*AnnotatedCommit) (MergeAnalysis, MergePreference, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads))
for i := 0; i < len(theirHeads); i++ {
gmerge_head_array[i] = theirHeads[i].ptr
}
ptr := unsafe.Pointer(&gmerge_head_array[0])
var analysis C.git_merge_analysis_t
var preference C.git_merge_preference_t
err := C.git_merge_analysis(&analysis, &preference, r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)))
runtime.KeepAlive(theirHeads)
if err < 0 {
return MergeAnalysisNone, MergePreferenceNone, MakeGitError(err)
}
return MergeAnalysis(analysis), MergePreference(preference), nil
}
func (r *Repository) MergeCommits(ours *Commit, theirs *Commit, options *MergeOptions) (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
copts := options.toC()
var ptr *C.git_index
ret := C.git_merge_commits(&ptr, r.ptr, ours.cast_ptr, theirs.cast_ptr, copts)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newIndexFromC(ptr, r), nil
}
func (r *Repository) MergeTrees(ancestor *Tree, ours *Tree, theirs *Tree, options *MergeOptions) (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
copts := options.toC()
var ancestor_ptr *C.git_tree
if ancestor != nil {
ancestor_ptr = ancestor.cast_ptr
}
var ptr *C.git_index
ret := C.git_merge_trees(&ptr, r.ptr, ancestor_ptr, ours.cast_ptr, theirs.cast_ptr, copts)
runtime.KeepAlive(ancestor)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newIndexFromC(ptr, r), nil
}
func (r *Repository) MergeBase(one *Oid, two *Oid) (*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var oid C.git_oid
ret := C.git_merge_base(&oid, r.ptr, one.toC(), two.toC())
runtime.KeepAlive(one)
runtime.KeepAlive(two)
runtime.KeepAlive(r)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newOidFromC(&oid), nil
}
// MergeBases retrieves the list of merge bases between two commits.
//
// If none are found, an empty slice is returned and the error is set
// approprately
func (r *Repository) MergeBases(one, two *Oid) ([]*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var coids C.git_oidarray
ret := C.git_merge_bases(&coids, r.ptr, one.toC(), two.toC())
runtime.KeepAlive(one)
runtime.KeepAlive(two)
if ret < 0 {
return make([]*Oid, 0), MakeGitError(ret)
}
oids := make([]*Oid, coids.count)
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(coids.ids)),
Len: int(coids.count),
Cap: int(coids.count),
}
goSlice := *(*[]C.git_oid)(unsafe.Pointer(&hdr))
for i, cid := range goSlice {
oids[i] = newOidFromC(&cid)
}
return oids, nil
}
//TODO: int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[]);
//TODO: GIT_EXTERN(int) git_merge_base_octopus(git_oid *out,git_repository *repo,size_t length,const git_oid input_array[]);
type MergeFileResult struct {
Automergeable bool
Path string
Mode uint
Contents []byte
ptr *C.git_merge_file_result
}
func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult {
var path string
if c.path != nil {
path = C.GoString(c.path)
}
originalBytes := C.GoBytes(unsafe.Pointer(c.ptr), C.int(c.len))
gobytes := make([]byte, len(originalBytes))
copy(gobytes, originalBytes)
r := &MergeFileResult{
Automergeable: c.automergeable != 0,
Path: path,
Mode: uint(c.mode),
Contents: gobytes,
ptr: c,
}
runtime.SetFinalizer(r, (*MergeFileResult).Free)
return r
}
func (r *MergeFileResult) Free() {
runtime.SetFinalizer(r, nil)
C.git_merge_file_result_free(r.ptr)
}
type MergeFileInput struct {
Path string
Mode uint
Contents []byte
}
type MergeFileFlags int
const (
MergeFileDefault MergeFileFlags = C.GIT_MERGE_FILE_DEFAULT
MergeFileStyleMerge MergeFileFlags = C.GIT_MERGE_FILE_STYLE_MERGE
MergeFileStyleDiff MergeFileFlags = C.GIT_MERGE_FILE_STYLE_DIFF3
MergeFileStyleSimplifyAlnum MergeFileFlags = C.GIT_MERGE_FILE_SIMPLIFY_ALNUM
)
type MergeFileOptions struct {
AncestorLabel string
OurLabel string
TheirLabel string
Favor MergeFileFavor
Flags MergeFileFlags
MarkerSize uint16
}
func mergeFileOptionsFromC(c C.git_merge_file_options) MergeFileOptions {
return MergeFileOptions{
AncestorLabel: C.GoString(c.ancestor_label),
OurLabel: C.GoString(c.our_label),
TheirLabel: C.GoString(c.their_label),
Favor: MergeFileFavor(c.favor),
Flags: MergeFileFlags(c.flags),
MarkerSize: uint16(c.marker_size),
}
}
func populateCMergeFileOptions(c *C.git_merge_file_options, options MergeFileOptions) {
c.ancestor_label = C.CString(options.AncestorLabel)
c.our_label = C.CString(options.OurLabel)
c.their_label = C.CString(options.TheirLabel)
c.favor = C.git_merge_file_favor_t(options.Favor)
c.flags = C.git_merge_file_flag_t(options.Flags)
c.marker_size = C.ushort(options.MarkerSize)
}
func freeCMergeFileOptions(c *C.git_merge_file_options) {
C.free(unsafe.Pointer(c.ancestor_label))
C.free(unsafe.Pointer(c.our_label))
C.free(unsafe.Pointer(c.their_label))
}
func MergeFile(ancestor MergeFileInput, ours MergeFileInput, theirs MergeFileInput, options *MergeFileOptions) (*MergeFileResult, error) {
ancestorPath := C.CString(ancestor.Path)
defer C.free(unsafe.Pointer(ancestorPath))
var ancestorContents *byte
if len(ancestor.Contents) > 0 {
ancestorContents = &ancestor.Contents[0]
}
oursPath := C.CString(ours.Path)
defer C.free(unsafe.Pointer(oursPath))
var oursContents *byte
if len(ours.Contents) > 0 {
oursContents = &ours.Contents[0]
}
theirsPath := C.CString(theirs.Path)
defer C.free(unsafe.Pointer(theirsPath))
var theirsContents *byte
if len(theirs.Contents) > 0 {
theirsContents = &theirs.Contents[0]
}
var copts *C.git_merge_file_options
if options != nil {
copts = &C.git_merge_file_options{}
ecode := C.git_merge_file_init_options(copts, C.GIT_MERGE_FILE_OPTIONS_VERSION)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
populateCMergeFileOptions(copts, *options)
defer freeCMergeFileOptions(copts)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var result C.git_merge_file_result
ecode := C._go_git_merge_file(&result,
(*C.char)(unsafe.Pointer(ancestorContents)), C.size_t(len(ancestor.Contents)), ancestorPath, C.uint(ancestor.Mode),
(*C.char)(unsafe.Pointer(oursContents)), C.size_t(len(ours.Contents)), oursPath, C.uint(ours.Mode),
(*C.char)(unsafe.Pointer(theirsContents)), C.size_t(len(theirs.Contents)), theirsPath, C.uint(theirs.Mode),
copts)
runtime.KeepAlive(ancestor)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newMergeFileResultFromC(&result), nil
}
// TODO: GIT_EXTERN(int) git_merge_file_from_index(git_merge_file_result *out,git_repository *repo,const git_index_entry *ancestor, const git_index_entry *ours, const git_index_entry *theirs, const git_merge_file_options *opts);