File: graph.go

package info (click to toggle)
golang-github-libgit2-git2go 34.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 920 kB
  • sloc: ansic: 471; sh: 85; makefile: 39
file content (69 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download
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
package git

/*
#include <git2.h>
*/
import "C"
import (
	"runtime"
)

func (repo *Repository) DescendantOf(commit, ancestor *Oid) (bool, error) {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_graph_descendant_of(repo.ptr, commit.toC(), ancestor.toC())
	runtime.KeepAlive(repo)
	runtime.KeepAlive(commit)
	runtime.KeepAlive(ancestor)
	if ret < 0 {
		return false, MakeGitError(ret)
	}

	return (ret > 0), nil
}

func (repo *Repository) AheadBehind(local, upstream *Oid) (ahead, behind int, err error) {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	var aheadT C.size_t
	var behindT C.size_t

	ret := C.git_graph_ahead_behind(&aheadT, &behindT, repo.ptr, local.toC(), upstream.toC())
	runtime.KeepAlive(repo)
	runtime.KeepAlive(local)
	runtime.KeepAlive(upstream)
	if ret < 0 {
		return 0, 0, MakeGitError(ret)
	}

	return int(aheadT), int(behindT), nil
}

// ReachableFromAny returns whether a commit is reachable from any of a list of
// commits by following parent edges.
func (repo *Repository) ReachableFromAny(commit *Oid, descendants []*Oid) (bool, error) {
	if len(descendants) == 0 {
		return false, nil
	}

	coids := make([]C.git_oid, len(descendants))
	for i := 0; i < len(descendants); i++ {
		coids[i] = *descendants[i].toC()
	}

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_graph_reachable_from_any(repo.ptr, commit.toC(), &coids[0], C.size_t(len(descendants)))
	runtime.KeepAlive(repo)
	runtime.KeepAlive(commit)
	runtime.KeepAlive(coids)
	runtime.KeepAlive(descendants)
	if ret < 0 {
		return false, MakeGitError(ret)
	}

	return (ret > 0), nil
}