git2go/walk.go

221 lines
3.9 KiB
Go
Raw Normal View History

2013-03-05 14:47:55 -06:00
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
2014-03-30 15:23:03 -05:00
"unsafe"
2013-03-05 14:47:55 -06:00
)
// RevWalk
type SortType uint
2013-03-05 14:47:55 -06:00
const (
SortNone SortType = C.GIT_SORT_NONE
SortTopological SortType = C.GIT_SORT_TOPOLOGICAL
SortTime SortType = C.GIT_SORT_TIME
SortReverse SortType = C.GIT_SORT_REVERSE
2013-03-05 14:47:55 -06:00
)
type RevWalk struct {
doNotCompare
2013-03-05 14:47:55 -06:00
ptr *C.git_revwalk
repo *Repository
}
func revWalkFromC(repo *Repository, c *C.git_revwalk) *RevWalk {
v := &RevWalk{ptr: c, repo: repo}
runtime.SetFinalizer(v, (*RevWalk).Free)
return v
}
2013-03-05 14:47:55 -06:00
func (v *RevWalk) Reset() {
C.git_revwalk_reset(v.ptr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2013-03-05 14:47:55 -06:00
}
2014-03-30 15:23:03 -05:00
func (v *RevWalk) Push(id *Oid) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revwalk_push(v.ptr, id.toC())
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
runtime.KeepAlive(id)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *RevWalk) PushGlob(glob string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(glob)
defer C.free(unsafe.Pointer(cstr))
ecode := C.git_revwalk_push_glob(v.ptr, cstr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *RevWalk) PushRange(r string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(r)
defer C.free(unsafe.Pointer(cstr))
ecode := C.git_revwalk_push_range(v.ptr, cstr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *RevWalk) PushRef(r string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(r)
defer C.free(unsafe.Pointer(cstr))
ecode := C.git_revwalk_push_ref(v.ptr, cstr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
2013-03-05 14:47:55 -06:00
}
func (v *RevWalk) PushHead() (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2013-03-05 14:47:55 -06:00
ecode := C.git_revwalk_push_head(v.ptr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2013-03-05 14:47:55 -06:00
if ecode < 0 {
err = MakeGitError(ecode)
2013-03-05 14:47:55 -06:00
}
2014-03-30 15:23:03 -05:00
return nil
}
2013-03-05 14:47:55 -06:00
2014-03-30 15:23:03 -05:00
func (v *RevWalk) Hide(id *Oid) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revwalk_hide(v.ptr, id.toC())
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
runtime.KeepAlive(id)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *RevWalk) HideGlob(glob string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(glob)
defer C.free(unsafe.Pointer(cstr))
ecode := C.git_revwalk_hide_glob(v.ptr, cstr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *RevWalk) HideRef(r string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(r)
defer C.free(unsafe.Pointer(cstr))
ecode := C.git_revwalk_hide_ref(v.ptr, cstr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *RevWalk) HideHead() (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revwalk_hide_head(v.ptr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2014-03-30 15:23:03 -05:00
if ecode < 0 {
err = MakeGitError(ecode)
}
return nil
2013-03-05 14:47:55 -06:00
}
func (v *RevWalk) Next(id *Oid) (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_revwalk_next(id.toC(), v.ptr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2013-03-05 14:47:55 -06:00
switch {
case ret < 0:
err = MakeGitError(ret)
2013-03-05 14:47:55 -06:00
}
return
}
type RevWalkIterator func(commit *Commit) bool
func (v *RevWalk) Iterate(fun RevWalkIterator) (err error) {
oid := new(Oid)
for {
err = v.Next(oid)
if IsErrorCode(err, ErrorCodeIterOver) {
2013-03-05 14:47:55 -06:00
return nil
}
if err != nil {
return err
}
commit, err := v.repo.LookupCommit(oid)
if err != nil {
return err
}
cont := fun(commit)
if !cont {
break
}
}
return nil
}
2016-02-06 21:12:35 -06:00
func (v *RevWalk) SimplifyFirstParent() {
C.git_revwalk_simplify_first_parent(v.ptr)
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2016-02-06 21:12:35 -06:00
}
func (v *RevWalk) Sorting(sm SortType) {
2013-03-05 14:47:55 -06:00
C.git_revwalk_sorting(v.ptr, C.uint(sm))
2017-07-08 09:07:51 -05:00
runtime.KeepAlive(v)
2013-03-05 14:47:55 -06:00
}
func (v *RevWalk) Free() {
runtime.SetFinalizer(v, nil)
C.git_revwalk_free(v.ptr)
2013-03-05 14:47:55 -06:00
}