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
 5package model
 6
 7import (
 8	"time"
 9
10	"github.com/tidwall/gjson"
11)
12
13type Commit struct {
14	Branch     string    `json:"branch"`
15	Hash       string    `json:"hash"`
16	Message    string    `json:"message"`
17	Date       time.Time `json:"date"`
18	Committer  string    `json:"committer"`
19	ParentHash string    `json:"parentHash"`
20}
21
22func commitFromGjson(j gjson.Result) Commit {
23	return Commit{
24		Branch:     j.Get("branch").String(),
25		Hash:       j.Get("hash").String(),
26		Message:    j.Get("message").String(),
27		Date:       j.Get("date").Time(),
28		Committer:  j.Get("committer").String(),
29		ParentHash: j.Get("parentHash").String(),
30	}
31}
32
33func CommitFromString(commit string) Commit {
34	return commitFromGjson(gjson.Parse(commit))
35}
36
37func CommitsFromString(commits string) []Commit {
38	jsonCommits := gjson.Parse(commits).Array()
39	res := make([]Commit, len(jsonCommits))
40	for i, jsonCommit := range jsonCommits {
41		res[i] = commitFromGjson(jsonCommit)
42	}
43	return res
44}