Debian Codegraph @

pkg/otbuiltin/log.go

raw
1 package otbuiltin
2
3 import (
4 "fmt"
5 "time"
6 "unsafe"
7
8 glib "github.com/ostreedev/ostree-go/pkg/glibobject"
9 )
10
11 // #cgo pkg-config: ostree-1
12 // #include <stdlib.h>
13 // #include <glib.h>
14 // #include <ostree.h>
15 // #include "builtin.go.h"
16 import "C"
17
18 // LogEntry is a struct for the various pieces of data in a log entry
19 type LogEntry struct {
20 Checksum []byte
21 Variant []byte
22 Timestamp time.Time
23 Subject string
24 Body string
25 }
26
27 // Convert the log entry to a string
28 func (l LogEntry) String() string {
29 if len(l.Variant) == 0 {
30 return fmt.Sprintf("%s\n%s\n\n\t%s\n\n\t%s\n\n", l.Checksum, l.Timestamp, l.Subject, l.Body)
31 }
32 return fmt.Sprintf("%s\n%s\n\n", l.Checksum, l.Variant)
33 }
34
35 type ostreeDumpFlags uint
36
37 const (
38 ostreeDumpNone ostreeDumpFlags = 0
39 ostreeDumpRaw ostreeDumpFlags = 1 << iota
40 )
41
42 // logOptions contains all of the options for initializing an ostree repo
43 type logOptions struct {
44 // Raw determines whether to show raw variant data
45 Raw bool
46 }
47
48 // NewLogOptions instantiates and returns a logOptions struct with default values set
49 func NewLogOptions() logOptions {
50 return logOptions{}
51 }
52
53 // Log shows the logs of a branch starting with a given commit or ref. Returns a
54 // slice of log entries on success and an error otherwise
55 func Log(repoPath, branch string, options logOptions) ([]LogEntry, error) {
56 // attempt to open the repository
57 repo, err := OpenRepo(repoPath)
58 if err != nil {
59 return nil, err
60 }
61
62 cbranch := C.CString(branch)
63 defer C.free(unsafe.Pointer(cbranch))
64 var checksum *C.char
65 defer C.free(unsafe.Pointer(checksum))
66 var cerr *C.GError
67 defer C.free(unsafe.Pointer(cerr))
68
69 flags := ostreeDumpNone
70 if options.Raw {
71 flags |= ostreeDumpRaw
72 }
73
74 if !glib.GoBool(glib.GBoolean(C.ostree_repo_resolve_rev(repo.native(), cbranch, C.FALSE, &checksum, &cerr))) {
75 return nil, generateError(cerr)
76 }
77
78 return logCommit(repo, checksum, false, flags)
79 }
80
81 func logCommit(repo *Repo, checksum *C.char, isRecursive bool, flags ostreeDumpFlags) ([]LogEntry, error) {
82 var variant *C.GVariant
83 var gerr = glib.NewGError()
84 var cerr = (*C.GError)(gerr.Ptr())
85 defer C.free(unsafe.Pointer(cerr))
86
87 if !glib.GoBool(glib.GBoolean(C.ostree_repo_load_variant(repo.native(), C.OSTREE_OBJECT_TYPE_COMMIT, checksum, &variant, &cerr))) {
88 if isRecursive && glib.GoBool(glib.GBoolean(C.g_error_matches(cerr, C.g_io_error_quark(), C.G_IO_ERROR_NOT_FOUND))) {
89 return nil, nil
90 }
91 return nil, generateError(cerr)
92 }
93
94 // Get the parent of this commit
95 parent := (*C.char)(C.ostree_commit_get_parent(variant))
96 defer C.free(unsafe.Pointer(parent))
97
98 entries := make([]LogEntry, 0, 1)
99 if parent != nil {
100 var err error
101 entries, err = logCommit(repo, parent, true, flags)
102 if err != nil {
103 return nil, err
104 }
105 }
106
107 nextLogEntry := dumpLogObject(C.OSTREE_OBJECT_TYPE_COMMIT, checksum, variant, flags)
108 entries = append(entries, nextLogEntry)
109
110 return entries, nil
111 }
112
113 func dumpLogObject(objectType C.OstreeObjectType, checksum *C.char, variant *C.GVariant, flags ostreeDumpFlags) LogEntry {
114 csum := []byte(C.GoString(checksum))
115
116 if (flags & ostreeDumpRaw) != 0 {
117 return dumpVariant(variant, csum)
118 }
119
120 switch objectType {
121 case C.OSTREE_OBJECT_TYPE_COMMIT:
122 return dumpCommit(variant, flags, csum)
123 default:
124 return LogEntry{
125 Checksum: csum,
126 }
127 }
128 }
129
130 func dumpVariant(variant *C.GVariant, csum []byte) LogEntry {
131 var logVariant []byte
132 if C.G_BYTE_ORDER != C.G_BIG_ENDIAN {
133 byteswappedVariant := C.g_variant_byteswap(variant)
134 logVariant = []byte(C.GoString((*C.char)(C.g_variant_print(byteswappedVariant, C.TRUE))))
135 } else {
136 logVariant = []byte(C.GoString((*C.char)(C.g_variant_print(variant, C.TRUE))))
137 }
138
139 return LogEntry{
140 Checksum: csum,
141 Variant: logVariant,
142 }
143 }
144
145 func dumpCommit(variant *C.GVariant, flags ostreeDumpFlags, csum []byte) LogEntry {
146 var subject *C.char
147 defer C.free(unsafe.Pointer(subject))
148 var body *C.char
149 defer C.free(unsafe.Pointer(body))
150 var timeBigE C.guint64
151
152 C._g_variant_get_commit_dump(variant, C.CString("(a{sv}aya(say)&s&stayay)"), &subject, &body, &timeBigE)
153
154 // Translate to a host-endian epoch and convert to Go timestamp
155 timeHostE := C._guint64_from_be(timeBigE)
156 timestamp := time.Unix((int64)(timeHostE), 0)
157
158 return LogEntry{
159 Timestamp: timestamp,
160 Subject: C.GoString(subject),
161 Body: C.GoString(body),
162 }
163 }