GitRoot
craft your forge, build your project, grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: MIT
4
5import { JSON } from "json-as";
6import { CommitStr } from "./model/commit";
7import { ConfPlugin, pluginSdkTypeTinygo } from "./model/pluginconf";
8import { __server } from "./server";
9
10export function init(
11 repoNamePtr: i32,
12 repoNameSize: i32,
13 confHasChangedPtr: i32,
14 confHasChangedSize: i32,
15 confPtr: i32,
16 confSize: i32
17): void {
18 const repoName = String.UTF8.decodeUnsafe(repoNamePtr, repoNameSize);
19 const serializedConf = String.UTF8.decodeUnsafe(confPtr, confSize);
20 const confHasChanged = String.UTF8.decodeUnsafe(
21 confHasChangedPtr,
22 confHasChangedSize
23 );
24 if (__server && __server!.plugin) {
25 __server!.plugin!.init(repoName, confHasChanged == "true", serializedConf);
26 }
27}
28
29export function startCommit(commitPtr: i32, commitSize: i32): void {
30 if (__server && __server!.plugin) {
31 const commit = JSON.parse<CommitStr>(
32 String.UTF8.decodeUnsafe(commitPtr, commitSize)
33 );
34 __server!.plugin!.startCommit(commit.toCommit());
35 }
36}
37
38export function addFile(ptr: i32, size: i32): void {
39 if (__server && __server!.plugin) {
40 __server!.plugin!.addFile(String.UTF8.decodeUnsafe(ptr, size));
41 }
42}
43
44export function modFile(
45 fromPtr: i32,
46 fromSize: i32,
47 toPtr: i32,
48 toSize: i32
49): void {
50 if (__server && __server!.plugin) {
51 __server!.plugin!.modFile(
52 String.UTF8.decodeUnsafe(fromPtr, fromSize),
53 String.UTF8.decodeUnsafe(toPtr, toSize)
54 );
55 }
56}
57
58export function delFile(ptr: i32, size: i32): void {
59 if (__server && __server!.plugin) {
60 __server!.plugin!.delFile(String.UTF8.decodeUnsafe(ptr, size));
61 }
62}
63
64export function endCommit(commitPtr: i32, commitSize: i32): void {
65 if (__server && __server!.plugin) {
66 const commit = JSON.parse<CommitStr>(
67 String.UTF8.decodeUnsafe(commitPtr, commitSize)
68 );
69 __server!.plugin!.endCommit(commit.toCommit());
70 }
71}
72
73export function finish(): void {
74 if (__server && __server!.plugin) {
75 __server!.plugin!.finish();
76 }
77}
78
79export function pluginConf(): string {
80 if (__server) {
81 const conf = ConfPlugin.build(__server!.run, "0.2.0", pluginSdkTypeTinygo);
82 return JSON.stringify<ConfPlugin>(conf);
83 }
84 return "{}";
85}
86
87export function malloc(size: usize): usize {
88 const ptr = __new(size, 1);
89 // @ts-ignore
90 __pin(ptr);
91 return ptr;
92}
93
94export function free(ptr: usize): void {
95 // @ts-ignore
96 __unpin(ptr);
97}