Merge pull request #24 from carlosmn/packbuilder-abort
Allow aborting the pack writing operation
This commit is contained in:
commit
01d1a5c5d5
|
@ -76,10 +76,11 @@ func (pb *Packbuilder) WriteToFile(name string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pb *Packbuilder) Write(w io.Writer) error {
|
func (pb *Packbuilder) Write(w io.Writer) error {
|
||||||
ch := pb.ForEach()
|
ch, stop := pb.ForEach()
|
||||||
for slice := range ch {
|
for slice := range ch {
|
||||||
_, err := w.Write(slice)
|
_, err := w.Write(slice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
close(stop)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,22 +91,40 @@ func (pb *Packbuilder) Written() uint32 {
|
||||||
return uint32(C.git_packbuilder_written(pb.ptr))
|
return uint32(C.git_packbuilder_written(pb.ptr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type packbuilderCbData struct {
|
||||||
|
ch chan<- []byte
|
||||||
|
stop <-chan bool
|
||||||
|
}
|
||||||
|
|
||||||
//export packbuilderForEachCb
|
//export packbuilderForEachCb
|
||||||
func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, payload unsafe.Pointer) int {
|
func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, payload unsafe.Pointer) int {
|
||||||
ch := *(*chan []byte)(payload)
|
data := (*packbuilderCbData)(payload)
|
||||||
|
ch := data.ch
|
||||||
|
stop := data.stop
|
||||||
|
|
||||||
slice := C.GoBytes(buf, C.int(size))
|
slice := C.GoBytes(buf, C.int(size))
|
||||||
ch <- slice
|
select {
|
||||||
|
case <- stop:
|
||||||
|
return -1
|
||||||
|
case ch <- slice:
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pb *Packbuilder) forEachWrap(ch chan []byte) {
|
func (pb *Packbuilder) forEachWrap(data *packbuilderCbData) {
|
||||||
C._go_git_packbuilder_foreach(pb.ptr, unsafe.Pointer(&ch))
|
C._go_git_packbuilder_foreach(pb.ptr, unsafe.Pointer(data))
|
||||||
close(ch)
|
close(data.ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pb *Packbuilder) ForEach() chan []byte {
|
// Foreach sends the packfile as slices through the "data" channel. If
|
||||||
ch := make(chan []byte, 0)
|
// you want to stop the pack-building process (e.g. there's an error
|
||||||
go pb.forEachWrap(ch)
|
// writing to the output), close or write a value into the "stop"
|
||||||
return ch
|
// channel.
|
||||||
|
func (pb *Packbuilder) ForEach() (data <-chan []byte, stop chan<- bool) {
|
||||||
|
ch := make(chan []byte)
|
||||||
|
stop := make(chan bool)
|
||||||
|
data := packbuilderCbData{ch, stop}
|
||||||
|
go pb.forEachWrap(&data)
|
||||||
|
return ch, stop
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue