summaryrefslogtreecommitdiff
path: root/push.go
blob: 0694fe3522e162bcc7e8e2bef55ce814d9908f8c (plain)
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
package git

/*
#include <git2.h>
#include <git2/errors.h>

int _go_git_push_status_foreach(git_push *push, void *data);
int _go_git_push_set_callbacks(git_push *push, void *packbuilder_progress_data, void *transfer_progress_data);

*/
import "C"
import (
	"runtime"
	"unsafe"
)

type Push struct {
	ptr *C.git_push

	packbuilderProgress *PackbuilderProgressCallback
	transferProgress    *PushTransferProgressCallback
}

func newPushFromC(cpush *C.git_push) *Push {
	p := &Push{ptr: cpush}
	runtime.SetFinalizer(p, (*Push).Free)
	return p
}

func (p *Push) Free() {
	runtime.SetFinalizer(p, nil)
	C.git_push_free(p.ptr)
}

func (remote *Remote) NewPush() (*Push, error) {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	var cpush *C.git_push
	ret := C.git_push_new(&cpush, remote.ptr)
	if ret < 0 {
		return nil, MakeGitError(ret)
	}
	return newPushFromC(cpush), nil
}

func (p *Push) Finish() error {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_push_finish(p.ptr)
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil
}

func (p *Push) UnpackOk() bool {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_push_unpack_ok(p.ptr)
	if ret == 0 {
		return false
	} else {
		return true
	}

}

func (p *Push) UpdateTips(sig *Signature, msg string) error {

	var csig *C.git_signature = nil
	if sig != nil {
		csig = sig.toC()
		defer C.free(unsafe.Pointer(csig))
	}

	cmsg := C.CString(msg)
	defer C.free(unsafe.Pointer(cmsg))

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_push_update_tips(p.ptr, csig, cmsg)
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil
}

func (p *Push) AddRefspec(refspec string) error {

	crefspec := C.CString(refspec)
	defer C.free(unsafe.Pointer(crefspec))

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_push_add_refspec(p.ptr, crefspec)
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil
}

type PushOptions struct {
	Version       uint
	PbParallelism uint
}

func (p *Push) SetOptions(opts PushOptions) error {
	copts := C.git_push_options{version: C.uint(opts.Version), pb_parallelism: C.uint(opts.PbParallelism)}

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_push_set_options(p.ptr, &copts)
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil
}

type StatusForeachFunc func(ref string, msg string) int

//export statusForeach
func statusForeach(_ref *C.char, _msg *C.char, _data unsafe.Pointer) C.int {
	ref := C.GoString(_ref)
	msg := C.GoString(_msg)

	cb := (*StatusForeachFunc)(_data)

	return C.int((*cb)(ref, msg))
}

func (p *Push) StatusForeach(callback StatusForeachFunc) error {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C._go_git_push_status_foreach(p.ptr, unsafe.Pointer(&callback))
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil

}

type PushCallbacks struct {
	PackbuilderProgress *PackbuilderProgressCallback
	TransferProgress    *PushTransferProgressCallback
}

type PackbuilderProgressCallback func(stage int, current uint, total uint) int
type PushTransferProgressCallback func(current uint, total uint, bytes uint) int

//export packbuilderProgress
func packbuilderProgress(stage C.int, current C.uint, total C.uint, data unsafe.Pointer) C.int {
	return C.int((*(*PackbuilderProgressCallback)(data))(int(stage), uint(current), uint(total)))
}

//export pushTransferProgress
func pushTransferProgress(current C.uint, total C.uint, bytes C.size_t, data unsafe.Pointer) C.int {
	return C.int((*(*PushTransferProgressCallback)(data))(uint(current), uint(total), uint(bytes)))
}

func (p *Push) SetCallbacks(callbacks PushCallbacks) {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	// save callbacks so they don't get GC'd
	p.packbuilderProgress = callbacks.PackbuilderProgress
	p.transferProgress = callbacks.TransferProgress

	C._go_git_push_set_callbacks(p.ptr, unsafe.Pointer(p.packbuilderProgress), unsafe.Pointer(p.transferProgress))
}