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 { Date } from "date";
  6import { __server, Commit } from "./server";
  7
  8export function init(
  9  repoNamePtr: i32,
 10  repoNameSize: i32,
 11  confHasChangedPtr: i32,
 12  confHasChangedSize: i32,
 13  confPtr: i32,
 14  confSize: i32
 15): void {
 16  const repoName = String.UTF8.decodeUnsafe(repoNamePtr, repoNameSize);
 17  const serializedConf = String.UTF8.decodeUnsafe(confPtr, confSize);
 18  const confHasChanged = String.UTF8.decodeUnsafe(
 19    confHasChangedPtr,
 20    confHasChangedSize
 21  );
 22  if (__server && __server!.plugin) {
 23    __server!.plugin!.init(repoName, confHasChanged == "true", serializedConf);
 24  }
 25}
 26
 27export function startCommit(
 28  branchPtr: i32,
 29  branchSize: i32,
 30  hashPrt: i32,
 31  hashSize: i32,
 32  msgPrt: i32,
 33  msgSize: i32,
 34  datePtr: i32,
 35  dateSize: i32,
 36  committerPtr: i32,
 37  committerSize: i32,
 38  parentHashPtr: i32,
 39  parentHashSize: i32
 40): void {
 41  if (__server && __server!.plugin) {
 42    const branch = String.UTF8.decodeUnsafe(branchPtr, branchSize);
 43    const hash = String.UTF8.decodeUnsafe(hashPrt, hashSize);
 44    const msg = String.UTF8.decodeUnsafe(msgPrt, msgSize);
 45    const date = String.UTF8.decodeUnsafe(datePtr, dateSize);
 46    const committer = String.UTF8.decodeUnsafe(committerPtr, committerSize);
 47    const parentHash = String.UTF8.decodeUnsafe(parentHashPtr, parentHashSize);
 48    const commit = Commit.build(
 49      branch,
 50      hash,
 51      msg,
 52      Date.fromString(date),
 53      committer,
 54      parentHash
 55    );
 56    __server!.plugin!.startCommit(commit);
 57  }
 58}
 59
 60export function addFile(ptr: i32, size: i32): void {
 61  if (__server && __server!.plugin) {
 62    __server!.plugin!.addFile(String.UTF8.decodeUnsafe(ptr, size));
 63  }
 64}
 65
 66export function modFile(
 67  fromPtr: i32,
 68  fromSize: i32,
 69  toPtr: i32,
 70  toSize: i32
 71): void {
 72  if (__server && __server!.plugin) {
 73    __server!.plugin!.modFile(
 74      String.UTF8.decodeUnsafe(fromPtr, fromSize),
 75      String.UTF8.decodeUnsafe(toPtr, toSize)
 76    );
 77  }
 78}
 79
 80export function delFile(ptr: i32, size: i32): void {
 81  if (__server && __server!.plugin) {
 82    __server!.plugin!.delFile(String.UTF8.decodeUnsafe(ptr, size));
 83  }
 84}
 85
 86export function endCommit(
 87  branchPtr: i32,
 88  branchSize: i32,
 89  hashPrt: i32,
 90  hashSize: i32,
 91  msgPrt: i32,
 92  msgSize: i32,
 93  datePtr: i32,
 94  dateSize: i32,
 95  committerPtr: i32,
 96  committerSize: i32,
 97  parentHashPtr: i32,
 98  parentHashSize: i32
 99): void {
100  if (__server && __server!.plugin) {
101    const branch = String.UTF8.decodeUnsafe(branchPtr, branchSize);
102    const hash = String.UTF8.decodeUnsafe(hashPrt, hashSize);
103    const msg = String.UTF8.decodeUnsafe(msgPrt, msgSize);
104    const date = String.UTF8.decodeUnsafe(datePtr, dateSize);
105    const committer = String.UTF8.decodeUnsafe(committerPtr, committerSize);
106    const parentHash = String.UTF8.decodeUnsafe(parentHashPtr, parentHashSize);
107    const commit = Commit.build(
108      branch,
109      hash,
110      msg,
111      Date.fromString(date),
112      committer,
113      parentHash
114    );
115    __server!.plugin!.endCommit(commit);
116  }
117}
118
119export function finish(): void {
120  if (__server && __server!.plugin) {
121    __server!.plugin!.finish();
122  }
123}
124
125export function defaultRun(): string {
126  if (__server) {
127    return __server!.run;
128  }
129  return "[]";
130}
131
132export function malloc(size: usize): usize {
133  const ptr = __new(size, 1);
134  __pin(ptr);
135  return ptr;
136}
137
138export function free(ptr: usize): void {
139  __unpin(ptr);
140}