forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.go
99 lines (81 loc) · 2.68 KB
/
indexer.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
package git
/*
#include <git2.h>
extern const git_oid * git_indexer_hash(const git_indexer *idx);
extern int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_transfer_progress *stats);
extern int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats);
extern int _go_git_indexer_new(git_indexer **out, const char *path, unsigned int mode, git_odb *odb, void *progress_cb_payload);
extern void git_indexer_free(git_indexer *idx);
*/
import "C"
import (
"reflect"
"runtime"
"unsafe"
)
// Indexer can post-process packfiles and create an .idx file for efficient
// lookup.
type Indexer struct {
ptr *C.git_indexer
stats C.git_transfer_progress
callbacks RemoteCallbacks
callbacksHandle unsafe.Pointer
}
// NewIndexer creates a new indexer instance.
func NewIndexer(packfilePath string, odb *Odb, callback TransferProgressCallback) (indexer *Indexer, err error) {
indexer = new(Indexer)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var odbPtr *C.git_odb = nil
if odb != nil {
odbPtr = odb.ptr
}
indexer.callbacks.TransferProgressCallback = callback
indexer.callbacksHandle = pointerHandles.Track(&indexer.callbacks)
cstr := C.CString(packfilePath)
defer C.free(unsafe.Pointer(cstr))
ret := C._go_git_indexer_new(&indexer.ptr, cstr, 0, odbPtr, indexer.callbacksHandle)
runtime.KeepAlive(odb)
if ret < 0 {
pointerHandles.Untrack(indexer.callbacksHandle)
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(indexer, (*Indexer).Free)
return indexer, nil
}
// Write adds data to the indexer.
func (indexer *Indexer) Write(data []byte) (int, error) {
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ptr := unsafe.Pointer(header.Data)
size := C.size_t(header.Len)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_indexer_append(indexer.ptr, ptr, size, &indexer.stats)
runtime.KeepAlive(indexer)
if ret < 0 {
return 0, MakeGitError(ret)
}
return len(data), nil
}
// Commit finalizes the pack and index. It resolves any pending deltas and
// writes out the index file.
//
// It also returns the packfile's hash. A packfile's name is derived from the
// sorted hashing of all object names.
func (indexer *Indexer) Commit() (*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_indexer_commit(indexer.ptr, &indexer.stats)
if ret < 0 {
return nil, MakeGitError(ret)
}
id := newOidFromC(C.git_indexer_hash(indexer.ptr))
runtime.KeepAlive(indexer)
return id, nil
}
// Free frees the indexer and its resources.
func (indexer *Indexer) Free() {
pointerHandles.Untrack(indexer.callbacksHandle)
runtime.SetFinalizer(indexer, nil)
C.git_indexer_free(indexer.ptr)
}