- Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathrevert.go
104 lines (84 loc) · 2.77 KB
/
revert.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
package git
/*
#include <git2.h>
*/
import"C"
import (
"runtime"
)
// RevertOptions contains options for performing a revert
typeRevertOptionsstruct {
Mainlineuint
MergeOptionsMergeOptions
CheckoutOptionsCheckoutOptions
}
funcpopulateRevertOptions(copts*C.git_revert_options, opts*RevertOptions, errorTarget*error) *C.git_revert_options {
C.git_revert_options_init(copts, C.GIT_REVERT_OPTIONS_VERSION)
ifopts==nil {
returnnil
}
copts.mainline=C.uint(opts.Mainline)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
returncopts
}
funcrevertOptionsFromC(copts*C.git_revert_options) RevertOptions {
returnRevertOptions{
Mainline: uint(copts.mainline),
MergeOptions: mergeOptionsFromC(&copts.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&copts.checkout_opts),
}
}
funcfreeRevertOptions(copts*C.git_revert_options) {
ifcopts!=nil {
return
}
freeMergeOptions(&copts.merge_opts)
freeCheckoutOptions(&copts.checkout_opts)
}
// DefaultRevertOptions initialises a RevertOptions struct with default values
funcDefaultRevertOptions() (RevertOptions, error) {
copts:= C.git_revert_options{}
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ecode:=C.git_revert_options_init(&copts, C.GIT_REVERT_OPTIONS_VERSION)
ifecode<0 {
returnRevertOptions{}, MakeGitError(ecode)
}
deferfreeRevertOptions(&copts)
returnrevertOptionsFromC(&copts), nil
}
// Revert the provided commit leaving the index updated with the results of the revert
func (r*Repository) Revert(commit*Commit, revertOptions*RevertOptions) error {
runtime.LockOSThread()
deferruntime.UnlockOSThread()
varerrerror
cOpts:=populateRevertOptions(&C.git_revert_options{}, revertOptions, &err)
deferfreeRevertOptions(cOpts)
ret:=C.git_revert(r.ptr, commit.cast_ptr, cOpts)
runtime.KeepAlive(r)
runtime.KeepAlive(commit)
ifret==C.int(ErrorCodeUser) &&err!=nil {
returnerr
}
ifret<0 {
returnMakeGitError(ret)
}
returnnil
}
// RevertCommit reverts the provided commit against "ourCommit"
// The returned index contains the result of the revert and should be freed
func (r*Repository) RevertCommit(revertCommit*Commit, ourCommit*Commit, mainlineuint, mergeOptions*MergeOptions) (*Index, error) {
runtime.LockOSThread()
deferruntime.UnlockOSThread()
cOpts:=populateMergeOptions(&C.git_merge_options{}, mergeOptions)
deferfreeMergeOptions(cOpts)
varindex*C.git_index
ecode:=C.git_revert_commit(&index, r.ptr, revertCommit.cast_ptr, ourCommit.cast_ptr, C.uint(mainline), cOpts)
runtime.KeepAlive(revertCommit)
runtime.KeepAlive(ourCommit)
ifecode<0 {
returnnil, MakeGitError(ecode)
}
returnnewIndexFromC(index, r), nil
}