aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/ipc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--forged/internal/ipc/git2c/build.go119
-rw-r--r--forged/internal/ipc/git2c/cmd_init_repo.go26
-rw-r--r--forged/internal/ipc/git2c/extra.go286
-rw-r--r--forged/internal/ipc/git2c/perror.go62
-rw-r--r--forged/internal/ipc/git2c/tree.go118
-rw-r--r--forged/internal/ipc/irc/bot.go170
-rw-r--r--forged/internal/ipc/irc/config.go13
-rw-r--r--forged/internal/ipc/irc/conn.go58
-rw-r--r--forged/internal/ipc/irc/doc.go2
-rw-r--r--forged/internal/ipc/irc/errors.go8
-rw-r--r--forged/internal/ipc/irc/message.go126
-rw-r--r--forged/internal/ipc/irc/source.go51
12 files changed, 600 insertions, 439 deletions
diff --git a/forged/internal/ipc/git2c/build.go b/forged/internal/ipc/git2c/build.go
new file mode 100644
index 0000000..3d1b7a0
--- /dev/null
+++ b/forged/internal/ipc/git2c/build.go
@@ -0,0 +1,119 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package git2c
+
+import (
+ "encoding/hex"
+ "fmt"
+ "path"
+ "sort"
+ "strings"
+)
+
+func (c *Client) BuildTreeRecursive(repoPath, baseTreeHex string, updates map[string]string) (string, error) {
+ treeCache := make(map[string][]TreeEntryRaw)
+ var walk func(prefix, hexid string) error
+ walk = func(prefix, hexid string) error {
+ ents, err := c.TreeListByOID(repoPath, hexid)
+ if err != nil {
+ return err
+ }
+ treeCache[prefix] = ents
+ for _, e := range ents {
+ if e.Mode == 40000 {
+ sub := path.Join(prefix, e.Name)
+ if err := walk(sub, e.OID); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+ }
+ if err := walk("", baseTreeHex); err != nil {
+ return "", err
+ }
+
+ for p, blob := range updates {
+ parts := strings.Split(p, "/")
+ dir := strings.Join(parts[:len(parts)-1], "/")
+ name := parts[len(parts)-1]
+ entries := treeCache[dir]
+ found := false
+ for i := range entries {
+ if entries[i].Name == name {
+ if blob == "" {
+ entries = append(entries[:i], entries[i+1:]...)
+ } else {
+ entries[i].Mode = 0o100644
+ entries[i].OID = blob
+ }
+ found = true
+ break
+ }
+ }
+ if !found && blob != "" {
+ entries = append(entries, TreeEntryRaw{Mode: 0o100644, Name: name, OID: blob})
+ }
+ treeCache[dir] = entries
+ }
+
+ built := make(map[string]string)
+ var build func(prefix string) (string, error)
+ build = func(prefix string) (string, error) {
+ entries := treeCache[prefix]
+ for i := range entries {
+ if entries[i].Mode == 0o40000 || entries[i].Mode == 40000 {
+ sub := path.Join(prefix, entries[i].Name)
+ var ok bool
+ var oid string
+ if oid, ok = built[sub]; !ok {
+ var err error
+ oid, err = build(sub)
+ if err != nil {
+ return "", err
+ }
+ }
+ entries[i].Mode = 0o40000
+ entries[i].OID = oid
+ }
+ }
+ sort.Slice(entries, func(i, j int) bool {
+ ni, nj := entries[i].Name, entries[j].Name
+ if ni == nj {
+ return entries[i].Mode != 0o40000 && entries[j].Mode == 0o40000
+ }
+ if strings.HasPrefix(nj, ni) && len(ni) < len(nj) {
+ return entries[i].Mode != 0o40000
+ }
+ if strings.HasPrefix(ni, nj) && len(nj) < len(ni) {
+ return entries[j].Mode == 0o40000
+ }
+ return ni < nj
+ })
+ wr := make([]TreeEntryRaw, 0, len(entries))
+ for _, e := range entries {
+ if e.OID == "" {
+ continue
+ }
+ if e.Mode == 40000 {
+ e.Mode = 0o40000
+ }
+ if _, err := hex.DecodeString(e.OID); err != nil {
+ return "", fmt.Errorf("invalid OID hex for %s/%s: %w", prefix, e.Name, err)
+ }
+ wr = append(wr, TreeEntryRaw{Mode: e.Mode, Name: e.Name, OID: e.OID})
+ }
+ id, err := c.WriteTree(repoPath, wr)
+ if err != nil {
+ return "", err
+ }
+ built[prefix] = id
+ return id, nil
+ }
+ root, err := build("")
+ if err != nil {
+ return "", err
+ }
+ return root, nil
+}
diff --git a/forged/internal/ipc/git2c/cmd_init_repo.go b/forged/internal/ipc/git2c/cmd_init_repo.go
new file mode 100644
index 0000000..ae1e92a
--- /dev/null
+++ b/forged/internal/ipc/git2c/cmd_init_repo.go
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package git2c
+
+import "fmt"
+
+func (c *Client) InitRepo(repoPath, hooksPath string) error {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(15); err != nil {
+ return fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(hooksPath)); err != nil {
+ return fmt.Errorf("sending hooks path failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return Perror(status)
+ }
+ return nil
+}
diff --git a/forged/internal/ipc/git2c/extra.go b/forged/internal/ipc/git2c/extra.go
new file mode 100644
index 0000000..4d3a07e
--- /dev/null
+++ b/forged/internal/ipc/git2c/extra.go
@@ -0,0 +1,286 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package git2c
+
+import (
+ "encoding/hex"
+ "fmt"
+ "time"
+)
+
+func (c *Client) ResolveRef(repoPath, refType, refName string) (string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(3); err != nil {
+ return "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(refType)); err != nil {
+ return "", fmt.Errorf("sending ref type failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(refName)); err != nil {
+ return "", fmt.Errorf("sending ref name failed: %w", err)
+ }
+
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return "", fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return "", Perror(status)
+ }
+ id, err := c.reader.ReadData()
+ if err != nil {
+ return "", fmt.Errorf("reading oid failed: %w", err)
+ }
+ return hex.EncodeToString(id), nil
+}
+
+func (c *Client) ListBranches(repoPath string) ([]string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return nil, fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(4); err != nil {
+ return nil, fmt.Errorf("sending command failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return nil, fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return nil, Perror(status)
+ }
+ count, err := c.reader.ReadUint()
+ if err != nil {
+ return nil, fmt.Errorf("reading count failed: %w", err)
+ }
+ branches := make([]string, 0, count)
+ for range count {
+ name, err := c.reader.ReadData()
+ if err != nil {
+ return nil, fmt.Errorf("reading branch name failed: %w", err)
+ }
+ branches = append(branches, string(name))
+ }
+ return branches, nil
+}
+
+func (c *Client) FormatPatch(repoPath, commitHex string) (string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(5); err != nil {
+ return "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(commitHex)); err != nil {
+ return "", fmt.Errorf("sending commit failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return "", fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return "", Perror(status)
+ }
+ buf, err := c.reader.ReadData()
+ if err != nil {
+ return "", fmt.Errorf("reading patch failed: %w", err)
+ }
+ return string(buf), nil
+}
+
+func (c *Client) CommitPatch(repoPath, commitHex string) (parentHex string, stats string, patch string, err error) {
+ if err = c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", "", "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err = c.writer.WriteUint(6); err != nil {
+ return "", "", "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err = c.writer.WriteData([]byte(commitHex)); err != nil {
+ return "", "", "", fmt.Errorf("sending commit failed: %w", err)
+ }
+ status, err2 := c.reader.ReadUint()
+ if err2 != nil {
+ return "", "", "", fmt.Errorf("reading status failed: %w", err2)
+ }
+ if status != 0 {
+ return "", "", "", Perror(status)
+ }
+ id, err2 := c.reader.ReadData()
+ if err2 != nil {
+ return "", "", "", fmt.Errorf("reading parent oid failed: %w", err2)
+ }
+ statsBytes, err2 := c.reader.ReadData()
+ if err2 != nil {
+ return "", "", "", fmt.Errorf("reading stats failed: %w", err2)
+ }
+ patchBytes, err2 := c.reader.ReadData()
+ if err2 != nil {
+ return "", "", "", fmt.Errorf("reading patch failed: %w", err2)
+ }
+ return hex.EncodeToString(id), string(statsBytes), string(patchBytes), nil
+}
+
+func (c *Client) MergeBase(repoPath, hexA, hexB string) (string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(7); err != nil {
+ return "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(hexA)); err != nil {
+ return "", fmt.Errorf("sending oid A failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(hexB)); err != nil {
+ return "", fmt.Errorf("sending oid B failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return "", fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return "", Perror(status)
+ }
+ base, err := c.reader.ReadData()
+ if err != nil {
+ return "", fmt.Errorf("reading base oid failed: %w", err)
+ }
+ return hex.EncodeToString(base), nil
+}
+
+func (c *Client) Log(repoPath, refSpec string, n uint) ([]Commit, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return nil, fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(8); err != nil {
+ return nil, fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(refSpec)); err != nil {
+ return nil, fmt.Errorf("sending refspec failed: %w", err)
+ }
+ if err := c.writer.WriteUint(uint64(n)); err != nil {
+ return nil, fmt.Errorf("sending limit failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return nil, fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return nil, Perror(status)
+ }
+ var out []Commit
+ for {
+ id, err := c.reader.ReadData()
+ if err != nil {
+ break
+ }
+ title, _ := c.reader.ReadData()
+ authorName, _ := c.reader.ReadData()
+ authorEmail, _ := c.reader.ReadData()
+ date, _ := c.reader.ReadData()
+ out = append(out, Commit{
+ Hash: hex.EncodeToString(id),
+ Author: string(authorName),
+ Email: string(authorEmail),
+ Date: string(date),
+ Message: string(title),
+ })
+ }
+ return out, nil
+}
+
+func (c *Client) CommitTreeOID(repoPath, commitHex string) (string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(12); err != nil {
+ return "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(commitHex)); err != nil {
+ return "", fmt.Errorf("sending oid failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return "", fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return "", Perror(status)
+ }
+ id, err := c.reader.ReadData()
+ if err != nil {
+ return "", fmt.Errorf("reading tree oid failed: %w", err)
+ }
+ return hex.EncodeToString(id), nil
+}
+
+func (c *Client) CommitCreate(repoPath, treeHex string, parents []string, authorName, authorEmail string, when time.Time, message string) (string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(13); err != nil {
+ return "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(treeHex)); err != nil {
+ return "", fmt.Errorf("sending tree oid failed: %w", err)
+ }
+ if err := c.writer.WriteUint(uint64(len(parents))); err != nil {
+ return "", fmt.Errorf("sending parents count failed: %w", err)
+ }
+ for _, p := range parents {
+ if err := c.writer.WriteData([]byte(p)); err != nil {
+ return "", fmt.Errorf("sending parent oid failed: %w", err)
+ }
+ }
+ if err := c.writer.WriteData([]byte(authorName)); err != nil {
+ return "", fmt.Errorf("sending author name failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(authorEmail)); err != nil {
+ return "", fmt.Errorf("sending author email failed: %w", err)
+ }
+ if err := c.writer.WriteInt(when.Unix()); err != nil {
+ return "", fmt.Errorf("sending when failed: %w", err)
+ }
+ _, offset := when.Zone()
+ if err := c.writer.WriteInt(int64(offset / 60)); err != nil {
+ return "", fmt.Errorf("sending tz offset failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(message)); err != nil {
+ return "", fmt.Errorf("sending message failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return "", fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return "", Perror(status)
+ }
+ id, err := c.reader.ReadData()
+ if err != nil {
+ return "", fmt.Errorf("reading commit oid failed: %w", err)
+ }
+ return hex.EncodeToString(id), nil
+}
+
+func (c *Client) UpdateRef(repoPath, refName, commitHex string) error {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(14); err != nil {
+ return fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(refName)); err != nil {
+ return fmt.Errorf("sending ref name failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(commitHex)); err != nil {
+ return fmt.Errorf("sending commit oid failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return Perror(status)
+ }
+ return nil
+}
diff --git a/forged/internal/ipc/git2c/perror.go b/forged/internal/ipc/git2c/perror.go
index 6bc7595..4be2a07 100644
--- a/forged/internal/ipc/git2c/perror.go
+++ b/forged/internal/ipc/git2c/perror.go
@@ -8,19 +8,33 @@ package git2c
import "errors"
var (
- ErrUnknown = errors.New("git2c: unknown error")
- ErrPath = errors.New("git2c: get tree entry by path failed")
- ErrRevparse = errors.New("git2c: revparse failed")
- ErrReadme = errors.New("git2c: no readme")
- ErrBlobExpected = errors.New("git2c: blob expected")
- ErrEntryToObject = errors.New("git2c: tree entry to object conversion failed")
- ErrBlobRawContent = errors.New("git2c: get blob raw content failed")
- ErrRevwalk = errors.New("git2c: revwalk failed")
- ErrRevwalkPushHead = errors.New("git2c: revwalk push head failed")
- ErrBareProto = errors.New("git2c: bare protocol error")
+ ErrUnknown = errors.New("git2c: unknown error")
+ ErrPath = errors.New("git2c: get tree entry by path failed")
+ ErrRevparse = errors.New("git2c: revparse failed")
+ ErrReadme = errors.New("git2c: no readme")
+ ErrBlobExpected = errors.New("git2c: blob expected")
+ ErrEntryToObject = errors.New("git2c: tree entry to object conversion failed")
+ ErrBlobRawContent = errors.New("git2c: get blob raw content failed")
+ ErrRevwalk = errors.New("git2c: revwalk failed")
+ ErrRevwalkPushHead = errors.New("git2c: revwalk push head failed")
+ ErrBareProto = errors.New("git2c: bare protocol error")
+ ErrRefResolve = errors.New("git2c: ref resolve failed")
+ ErrBranches = errors.New("git2c: list branches failed")
+ ErrCommitLookup = errors.New("git2c: commit lookup failed")
+ ErrDiff = errors.New("git2c: diff failed")
+ ErrMergeBaseNone = errors.New("git2c: no merge base found")
+ ErrMergeBase = errors.New("git2c: merge base failed")
+ ErrCommitCreate = errors.New("git2c: commit create failed")
+ ErrUpdateRef = errors.New("git2c: update ref failed")
+ ErrCommitTree = errors.New("git2c: commit tree lookup failed")
+ ErrInitRepoCreate = errors.New("git2c: init repo: create failed")
+ ErrInitRepoConfig = errors.New("git2c: init repo: open config failed")
+ ErrInitRepoSetHooksPath = errors.New("git2c: init repo: set core.hooksPath failed")
+ ErrInitRepoSetAdvertisePushOptions = errors.New("git2c: init repo: set receive.advertisePushOptions failed")
+ ErrInitRepoMkdir = errors.New("git2c: init repo: create directory failed")
)
-func Perror(errno uint) error {
+func Perror(errno uint64) error {
switch errno {
case 0:
return nil
@@ -42,6 +56,32 @@ func Perror(errno uint) error {
return ErrRevwalkPushHead
case 11:
return ErrBareProto
+ case 12:
+ return ErrRefResolve
+ case 13:
+ return ErrBranches
+ case 14:
+ return ErrCommitLookup
+ case 15:
+ return ErrDiff
+ case 16:
+ return ErrMergeBaseNone
+ case 17:
+ return ErrMergeBase
+ case 18:
+ return ErrUpdateRef
+ case 19:
+ return ErrCommitCreate
+ case 20:
+ return ErrInitRepoCreate
+ case 21:
+ return ErrInitRepoConfig
+ case 22:
+ return ErrInitRepoSetHooksPath
+ case 23:
+ return ErrInitRepoSetAdvertisePushOptions
+ case 24:
+ return ErrInitRepoMkdir
}
return ErrUnknown
}
diff --git a/forged/internal/ipc/git2c/tree.go b/forged/internal/ipc/git2c/tree.go
new file mode 100644
index 0000000..f598e14
--- /dev/null
+++ b/forged/internal/ipc/git2c/tree.go
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package git2c
+
+import (
+ "encoding/hex"
+ "fmt"
+)
+
+type TreeEntryRaw struct {
+ Mode uint64
+ Name string
+ OID string // hex
+}
+
+func (c *Client) TreeListByOID(repoPath, treeHex string) ([]TreeEntryRaw, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return nil, fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(9); err != nil {
+ return nil, fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(treeHex)); err != nil {
+ return nil, fmt.Errorf("sending tree oid failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return nil, fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return nil, Perror(status)
+ }
+ count, err := c.reader.ReadUint()
+ if err != nil {
+ return nil, fmt.Errorf("reading count failed: %w", err)
+ }
+ entries := make([]TreeEntryRaw, 0, count)
+ for range count {
+ mode, err := c.reader.ReadUint()
+ if err != nil {
+ return nil, fmt.Errorf("reading mode failed: %w", err)
+ }
+ name, err := c.reader.ReadData()
+ if err != nil {
+ return nil, fmt.Errorf("reading name failed: %w", err)
+ }
+ id, err := c.reader.ReadData()
+ if err != nil {
+ return nil, fmt.Errorf("reading oid failed: %w", err)
+ }
+ entries = append(entries, TreeEntryRaw{Mode: mode, Name: string(name), OID: hex.EncodeToString(id)})
+ }
+ return entries, nil
+}
+
+func (c *Client) WriteTree(repoPath string, entries []TreeEntryRaw) (string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(10); err != nil {
+ return "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteUint(uint64(len(entries))); err != nil {
+ return "", fmt.Errorf("sending count failed: %w", err)
+ }
+ for _, e := range entries {
+ if err := c.writer.WriteUint(e.Mode); err != nil {
+ return "", fmt.Errorf("sending mode failed: %w", err)
+ }
+ if err := c.writer.WriteData([]byte(e.Name)); err != nil {
+ return "", fmt.Errorf("sending name failed: %w", err)
+ }
+ raw, err := hex.DecodeString(e.OID)
+ if err != nil {
+ return "", fmt.Errorf("decode oid hex: %w", err)
+ }
+ if err := c.writer.WriteDataFixed(raw); err != nil {
+ return "", fmt.Errorf("sending oid failed: %w", err)
+ }
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return "", fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return "", Perror(status)
+ }
+ id, err := c.reader.ReadData()
+ if err != nil {
+ return "", fmt.Errorf("reading oid failed: %w", err)
+ }
+ return hex.EncodeToString(id), nil
+}
+
+func (c *Client) WriteBlob(repoPath string, content []byte) (string, error) {
+ if err := c.writer.WriteData([]byte(repoPath)); err != nil {
+ return "", fmt.Errorf("sending repo path failed: %w", err)
+ }
+ if err := c.writer.WriteUint(11); err != nil {
+ return "", fmt.Errorf("sending command failed: %w", err)
+ }
+ if err := c.writer.WriteData(content); err != nil {
+ return "", fmt.Errorf("sending blob content failed: %w", err)
+ }
+ status, err := c.reader.ReadUint()
+ if err != nil {
+ return "", fmt.Errorf("reading status failed: %w", err)
+ }
+ if status != 0 {
+ return "", Perror(status)
+ }
+ id, err := c.reader.ReadData()
+ if err != nil {
+ return "", fmt.Errorf("reading oid failed: %w", err)
+ }
+ return hex.EncodeToString(id), nil
+}
diff --git a/forged/internal/ipc/irc/bot.go b/forged/internal/ipc/irc/bot.go
deleted file mode 100644
index 07008ae..0000000
--- a/forged/internal/ipc/irc/bot.go
+++ /dev/null
@@ -1,170 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package irc
-
-import (
- "context"
- "crypto/tls"
- "fmt"
- "log/slog"
- "net"
-
- "go.lindenii.runxiyu.org/forge/forged/internal/common/misc"
-)
-
-// Bot represents an IRC bot client that handles events and allows for sending messages.
-type Bot struct {
- // TODO: Use each config field instead of embedding Config here.
- config *Config
- ircSendBuffered chan string
- ircSendDirectChan chan misc.ErrorBack[string]
-}
-
-// NewBot creates a new Bot instance using the provided configuration.
-func NewBot(c *Config) (b *Bot) {
- b = &Bot{
- config: c,
- } //exhaustruct:ignore
- return
-}
-
-// Connect establishes a new IRC session and starts handling incoming and outgoing messages.
-// This method blocks until an error occurs or the connection is closed.
-func (b *Bot) Connect(ctx context.Context) error {
- var err error
- var underlyingConn net.Conn
- if b.config.TLS {
- dialer := tls.Dialer{} //exhaustruct:ignore
- underlyingConn, err = dialer.DialContext(ctx, b.config.Net, b.config.Addr)
- } else {
- dialer := net.Dialer{} //exhaustruct:ignore
- underlyingConn, err = dialer.DialContext(ctx, b.config.Net, b.config.Addr)
- }
- if err != nil {
- return fmt.Errorf("dialing irc: %w", err)
- }
- defer func() {
- _ = underlyingConn.Close()
- }()
-
- conn := NewConn(underlyingConn)
-
- logAndWriteLn := func(s string) (n int, err error) {
- slog.Debug("irc tx", "line", s)
- return conn.WriteString(s + "\r\n")
- }
-
- _, err = logAndWriteLn("NICK " + b.config.Nick)
- if err != nil {
- return err
- }
- _, err = logAndWriteLn("USER " + b.config.User + " 0 * :" + b.config.Gecos)
- if err != nil {
- return err
- }
-
- readLoopError := make(chan error)
- writeLoopAbort := make(chan struct{})
- go func() {
- for {
- select {
- case <-writeLoopAbort:
- return
- default:
- }
-
- msg, line, err := conn.ReadMessage()
- if err != nil {
- readLoopError <- err
- return
- }
-
- slog.Debug("irc rx", "line", line)
-
- switch msg.Command {
- case "001":
- _, err = logAndWriteLn("JOIN #chat")
- if err != nil {
- readLoopError <- err
- return
- }
- case "PING":
- _, err = logAndWriteLn("PONG :" + msg.Args[0])
- if err != nil {
- readLoopError <- err
- return
- }
- case "JOIN":
- c, ok := msg.Source.(Client)
- if !ok {
- slog.Error("unable to convert source of JOIN to client")
- }
- if c.Nick != b.config.Nick {
- continue
- }
- default:
- }
- }
- }()
-
- for {
- select {
- case err = <-readLoopError:
- return err
- case line := <-b.ircSendBuffered:
- _, err = logAndWriteLn(line)
- if err != nil {
- select {
- case b.ircSendBuffered <- line:
- default:
- slog.Error("unable to requeue message", "line", line)
- }
- writeLoopAbort <- struct{}{}
- return err
- }
- case lineErrorBack := <-b.ircSendDirectChan:
- _, err = logAndWriteLn(lineErrorBack.Content)
- lineErrorBack.ErrorChan <- err
- if err != nil {
- writeLoopAbort <- struct{}{}
- return err
- }
- }
- }
-}
-
-// SendDirect sends an IRC message directly to the connection and bypasses
-// the buffering system.
-func (b *Bot) SendDirect(line string) error {
- ech := make(chan error, 1)
-
- b.ircSendDirectChan <- misc.ErrorBack[string]{
- Content: line,
- ErrorChan: ech,
- }
-
- return <-ech
-}
-
-// Send queues a message to be sent asynchronously via the buffered send queue.
-// If the queue is full, the message is dropped and an error is logged.
-func (b *Bot) Send(line string) {
- select {
- case b.ircSendBuffered <- line:
- default:
- slog.Error("irc sendq full", "line", line)
- }
-}
-
-// ConnectLoop continuously attempts to maintain an IRC session.
-// If the connection drops, it automatically retries with no delay.
-func (b *Bot) ConnectLoop(ctx context.Context) {
- b.ircSendBuffered = make(chan string, b.config.SendQ)
- b.ircSendDirectChan = make(chan misc.ErrorBack[string])
-
- for {
- err := b.Connect(ctx)
- slog.Error("irc session error", "error", err)
- }
-}
diff --git a/forged/internal/ipc/irc/config.go b/forged/internal/ipc/irc/config.go
deleted file mode 100644
index b1b5703..0000000
--- a/forged/internal/ipc/irc/config.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package irc
-
-// Config contains IRC connection and identity settings for the bot.
-// This should usually be a part of the primary config struct.
-type Config struct {
- Net string `scfg:"net"`
- Addr string `scfg:"addr"`
- TLS bool `scfg:"tls"`
- SendQ uint `scfg:"sendq"`
- Nick string `scfg:"nick"`
- User string `scfg:"user"`
- Gecos string `scfg:"gecos"`
-}
diff --git a/forged/internal/ipc/irc/conn.go b/forged/internal/ipc/irc/conn.go
deleted file mode 100644
index b9b208c..0000000
--- a/forged/internal/ipc/irc/conn.go
+++ /dev/null
@@ -1,58 +0,0 @@
-package irc
-
-import (
- "bufio"
- "fmt"
- "net"
- "slices"
-
- "go.lindenii.runxiyu.org/forge/forged/internal/common/misc"
-)
-
-type Conn struct {
- netConn net.Conn
- bufReader *bufio.Reader
-}
-
-func NewConn(netConn net.Conn) Conn {
- return Conn{
- netConn: netConn,
- bufReader: bufio.NewReader(netConn),
- }
-}
-
-func (c *Conn) ReadMessage() (msg Message, line string, err error) {
- raw, err := c.bufReader.ReadSlice('\n')
- if err != nil {
- return
- }
-
- if raw[len(raw)-1] == '\n' {
- raw = raw[:len(raw)-1]
- }
- if raw[len(raw)-1] == '\r' {
- raw = raw[:len(raw)-1]
- }
-
- lineBytes := slices.Clone(raw)
- line = misc.BytesToString(lineBytes)
- msg, err = Parse(lineBytes)
-
- return
-}
-
-func (c *Conn) Write(p []byte) (n int, err error) {
- n, err = c.netConn.Write(p)
- if err != nil {
- err = fmt.Errorf("write to connection: %w", err)
- }
- return n, err
-}
-
-func (c *Conn) WriteString(s string) (n int, err error) {
- n, err = c.netConn.Write(misc.StringToBytes(s))
- if err != nil {
- err = fmt.Errorf("write to connection: %w", err)
- }
- return n, err
-}
diff --git a/forged/internal/ipc/irc/doc.go b/forged/internal/ipc/irc/doc.go
deleted file mode 100644
index dcfca82..0000000
--- a/forged/internal/ipc/irc/doc.go
+++ /dev/null
@@ -1,2 +0,0 @@
-// Package irc provides basic IRC bot functionality.
-package irc
diff --git a/forged/internal/ipc/irc/errors.go b/forged/internal/ipc/irc/errors.go
deleted file mode 100644
index 3506c70..0000000
--- a/forged/internal/ipc/irc/errors.go
+++ /dev/null
@@ -1,8 +0,0 @@
-package irc
-
-import "errors"
-
-var (
- ErrInvalidIRCv3Tag = errors.New("invalid ircv3 tag")
- ErrMalformedMsg = errors.New("malformed irc message")
-)
diff --git a/forged/internal/ipc/irc/message.go b/forged/internal/ipc/irc/message.go
deleted file mode 100644
index 3387bec..0000000
--- a/forged/internal/ipc/irc/message.go
+++ /dev/null
@@ -1,126 +0,0 @@
-// SPDX-License-Identifier: MIT
-// SPDX-FileCopyrightText: Copyright (c) 2018-2024 luk3yx <https://luk3yx.github.io>
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package irc
-
-import (
- "bytes"
-
- "go.lindenii.runxiyu.org/forge/forged/internal/common/misc"
-)
-
-type Message struct {
- Command string
- Source Source
- Tags map[string]string
- Args []string
-}
-
-// All strings returned are borrowed from the input byte slice.
-func Parse(raw []byte) (msg Message, err error) {
- sp := bytes.Split(raw, []byte{' '}) // TODO: Use bytes.Cut instead here
-
- if bytes.HasPrefix(sp[0], []byte{'@'}) { // TODO: Check size manually
- if len(sp[0]) < 2 {
- err = ErrMalformedMsg
- return msg, err
- }
- sp[0] = sp[0][1:]
-
- msg.Tags, err = tagsToMap(sp[0])
- if err != nil {
- return msg, err
- }
-
- if len(sp) < 2 {
- err = ErrMalformedMsg
- return msg, err
- }
- sp = sp[1:]
- } else {
- msg.Tags = nil // TODO: Is a nil map the correct thing to use here?
- }
-
- if bytes.HasPrefix(sp[0], []byte{':'}) { // TODO: Check size manually
- if len(sp[0]) < 2 {
- err = ErrMalformedMsg
- return msg, err
- }
- sp[0] = sp[0][1:]
-
- msg.Source = parseSource(sp[0])
-
- if len(sp) < 2 {
- err = ErrMalformedMsg
- return msg, err
- }
- sp = sp[1:]
- }
-
- msg.Command = misc.BytesToString(sp[0])
- if len(sp) < 2 {
- return msg, err
- }
- sp = sp[1:]
-
- for i := 0; i < len(sp); i++ {
- if len(sp[i]) == 0 {
- continue
- }
- if sp[i][0] == ':' {
- if len(sp[i]) < 2 {
- sp[i] = []byte{}
- } else {
- sp[i] = sp[i][1:]
- }
- msg.Args = append(msg.Args, misc.BytesToString(bytes.Join(sp[i:], []byte{' '})))
- // TODO: Avoid Join by not using sp in the first place
- break
- }
- msg.Args = append(msg.Args, misc.BytesToString(sp[i]))
- }
-
- return msg, err
-}
-
-var ircv3TagEscapes = map[byte]byte{ //nolint:gochecknoglobals
- ':': ';',
- 's': ' ',
- 'r': '\r',
- 'n': '\n',
-}
-
-func tagsToMap(raw []byte) (tags map[string]string, err error) {
- tags = make(map[string]string)
- for rawTag := range bytes.SplitSeq(raw, []byte{';'}) {
- key, value, found := bytes.Cut(rawTag, []byte{'='})
- if !found {
- err = ErrInvalidIRCv3Tag
- return tags, err
- }
- if len(value) == 0 {
- tags[misc.BytesToString(key)] = ""
- } else {
- if !bytes.Contains(value, []byte{'\\'}) {
- tags[misc.BytesToString(key)] = misc.BytesToString(value)
- } else {
- valueUnescaped := bytes.NewBuffer(make([]byte, 0, len(value)))
- for i := 0; i < len(value); i++ {
- if value[i] == '\\' {
- i++
- byteUnescaped, ok := ircv3TagEscapes[value[i]]
- if !ok {
- byteUnescaped = value[i]
- }
- valueUnescaped.WriteByte(byteUnescaped)
- } else {
- valueUnescaped.WriteByte(value[i])
- }
- }
- tags[misc.BytesToString(key)] = misc.BytesToString(valueUnescaped.Bytes())
- }
- }
- }
- return tags, err
-}
diff --git a/forged/internal/ipc/irc/source.go b/forged/internal/ipc/irc/source.go
deleted file mode 100644
index 938751f..0000000
--- a/forged/internal/ipc/irc/source.go
+++ /dev/null
@@ -1,51 +0,0 @@
-// SPDX-License-Identifier: MIT
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package irc
-
-import (
- "bytes"
-
- "go.lindenii.runxiyu.org/forge/forged/internal/common/misc"
-)
-
-type Source interface {
- AsSourceString() string
-}
-
-//nolint:ireturn
-func parseSource(s []byte) Source {
- nick, userhost, found := bytes.Cut(s, []byte{'!'})
- if !found {
- return Server{name: misc.BytesToString(s)}
- }
-
- user, host, found := bytes.Cut(userhost, []byte{'@'})
- if !found {
- return Server{name: misc.BytesToString(s)}
- }
-
- return Client{
- Nick: misc.BytesToString(nick),
- User: misc.BytesToString(user),
- Host: misc.BytesToString(host),
- }
-}
-
-type Server struct {
- name string
-}
-
-func (s Server) AsSourceString() string {
- return s.name
-}
-
-type Client struct {
- Nick string
- User string
- Host string
-}
-
-func (c Client) AsSourceString() string {
- return c.Nick + "!" + c.User + "@" + c.Host
-}