forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packbuilder.go
183 lines (152 loc) · 3.82 KB
/
packbuilder.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
package git
/*
#include <git2.h>
#include <git2/pack.h>
#include <stdlib.h>
extern int _go_git_packbuilder_foreach(git_packbuilder *pb, void *payload);
*/
import "C"
import (
"io"
"os"
"runtime"
"unsafe"
)
type Packbuilder struct {
ptr *C.git_packbuilder
r *Repository
}
func (repo *Repository) NewPackbuilder() (*Packbuilder, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_packbuilder
ret := C.git_packbuilder_new(&ptr, repo.ptr)
if ret != 0 {
return nil, MakeGitError(ret)
}
return newPackbuilderFromC(ptr, repo), nil
}
func newPackbuilderFromC(ptr *C.git_packbuilder, r *Repository) *Packbuilder {
pb := &Packbuilder{ptr: ptr, r: r}
runtime.SetFinalizer(pb, (*Packbuilder).Free)
return pb
}
func (pb *Packbuilder) Free() {
runtime.SetFinalizer(pb, nil)
C.git_packbuilder_free(pb.ptr)
}
func (pb *Packbuilder) Insert(id *Oid, name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert(pb.ptr, id.toC(), cname)
runtime.KeepAlive(pb)
runtime.KeepAlive(id)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) InsertCommit(id *Oid) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert_commit(pb.ptr, id.toC())
runtime.KeepAlive(pb)
runtime.KeepAlive(id)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) InsertTree(id *Oid) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert_tree(pb.ptr, id.toC())
runtime.KeepAlive(pb)
runtime.KeepAlive(id)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) InsertWalk(walk *RevWalk) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert_walk(pb.ptr, walk.ptr)
runtime.KeepAlive(pb)
runtime.KeepAlive(walk)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) ObjectCount() uint32 {
ret := uint32(C.git_packbuilder_object_count(pb.ptr))
runtime.KeepAlive(pb)
return ret
}
func (pb *Packbuilder) WriteToFile(name string, mode os.FileMode) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_write(pb.ptr, cname, C.uint(mode.Perm()), nil, nil)
runtime.KeepAlive(pb)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) Write(w io.Writer) error {
return pb.ForEach(func(slice []byte) error {
_, err := w.Write(slice)
return err
})
}
func (pb *Packbuilder) Written() uint32 {
ret := uint32(C.git_packbuilder_written(pb.ptr))
runtime.KeepAlive(pb)
return ret
}
type PackbuilderForeachCallback func([]byte) error
type packbuilderCbData struct {
callback PackbuilderForeachCallback
err error
}
//export packbuilderForEachCb
func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, handle unsafe.Pointer) int {
payload := pointerHandles.Get(handle)
data, ok := payload.(*packbuilderCbData)
if !ok {
panic("could not get packbuilder CB data")
}
slice := C.GoBytes(buf, C.int(size))
err := data.callback(slice)
if err != nil {
data.err = err
return C.GIT_EUSER
}
return 0
}
// ForEach repeatedly calls the callback with new packfile data until
// there is no more data or the callback returns an error
func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error {
data := packbuilderCbData{
callback: callback,
err: nil,
}
handle := pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C._go_git_packbuilder_foreach(pb.ptr, handle)
runtime.KeepAlive(pb)
if err == C.GIT_EUSER {
return data.err
}
if err < 0 {
return MakeGitError(err)
}
return nil
}