aboutsummaryrefslogtreecommitdiff
path: root/git_plumbing.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-02 08:49:03 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-02 09:24:37 +0800
commitc32389d7d54f3fe66d32f849c02c5e75b7d476c8 (patch)
tree6a28b81c35e6ac602cb10ca24869b8bc2342134c /git_plumbing.go
parentLMTP: Fix mistake in command arguments (diff)
downloadforge-c32389d7d54f3fe66d32f849c02c5e75b7d476c8.tar.gz
forge-c32389d7d54f3fe66d32f849c02c5e75b7d476c8.tar.zst
forge-c32389d7d54f3fe66d32f849c02c5e75b7d476c8.zip
LMTP: Actually apply patches from email
Diffstat (limited to 'git_plumbing.go')
-rw-r--r--git_plumbing.go173
1 files changed, 173 insertions, 0 deletions
diff --git a/git_plumbing.go b/git_plumbing.go
new file mode 100644
index 0000000..36acb90
--- /dev/null
+++ b/git_plumbing.go
@@ -0,0 +1,173 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package main
+
+import (
+ "bytes"
+ "context"
+ "encoding/hex"
+ "errors"
+ "os"
+ "os/exec"
+ "path"
+ "sort"
+ "strings"
+)
+
+func writeTree(ctx context.Context, repoPath string, entries []treeEntry) (string, error) {
+ var buf bytes.Buffer
+
+ // Must
+ sort.Slice(entries, func(i, j int) bool {
+ return entries[i].name < entries[j].name
+ })
+
+ for _, e := range entries {
+ buf.WriteString(e.mode)
+ buf.WriteByte(' ')
+ buf.WriteString(e.name)
+ buf.WriteByte(0)
+ buf.Write(e.sha)
+ }
+
+ cmd := exec.CommandContext(ctx, "git", "hash-object", "-w", "-t", "tree", "--stdin")
+ cmd.Env = append(os.Environ(), "GIT_DIR="+repoPath)
+ cmd.Stdin = &buf
+
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ if err := cmd.Run(); err != nil {
+ return "", err
+ }
+ return strings.TrimSpace(out.String()), nil
+}
+
+func buildTreeRecursive(ctx context.Context, repoPath string, baseTree string, updates map[string][]byte) (string, error) {
+ treeCache := make(map[string][]treeEntry)
+
+ var walk func(string, string) error
+ walk = func(prefix, sha string) error {
+ cmd := exec.CommandContext(ctx, "git", "cat-file", "tree", sha)
+ cmd.Env = append(os.Environ(), "GIT_DIR="+repoPath)
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ if err := cmd.Run(); err != nil {
+ return err
+ }
+ data := out.Bytes()
+ i := 0
+ var entries []treeEntry
+ for i < len(data) {
+ modeEnd := bytes.IndexByte(data[i:], ' ')
+ if modeEnd < 0 {
+ return errors.New("invalid tree format")
+ }
+ mode := string(data[i : i+modeEnd])
+ i += modeEnd + 1
+
+ nameEnd := bytes.IndexByte(data[i:], 0)
+ if nameEnd < 0 {
+ return errors.New("missing null after filename")
+ }
+ name := string(data[i : i+nameEnd])
+ i += nameEnd + 1
+
+ if i+20 > len(data) {
+ return errors.New("unexpected EOF in SHA")
+ }
+ shaBytes := data[i : i+20]
+ i += 20
+
+ entries = append(entries, treeEntry{
+ mode: mode,
+ name: name,
+ sha: shaBytes,
+ })
+
+ if mode == "40000" {
+ subPrefix := path.Join(prefix, name)
+ if err := walk(subPrefix, hex.EncodeToString(shaBytes)); err != nil {
+ return err
+ }
+ }
+ }
+ treeCache[prefix] = entries
+ return nil
+ }
+
+ if err := walk("", baseTree); err != nil {
+ return "", err
+ }
+
+ for filePath, blobSha := range updates {
+ parts := strings.Split(filePath, "/")
+ dir := strings.Join(parts[:len(parts)-1], "/")
+ name := parts[len(parts)-1]
+
+ entries := treeCache[dir]
+ found := false
+ for i, e := range entries {
+ if e.name == name {
+ if blobSha == nil {
+ // Remove TODO
+ entries = append(entries[:i], entries[i+1:]...)
+ } else {
+ entries[i].sha = blobSha
+ }
+ found = true
+ break
+ }
+ }
+ if !found && blobSha != nil {
+ entries = append(entries, treeEntry{
+ mode: "100644",
+ name: name,
+ sha: blobSha,
+ })
+ }
+ treeCache[dir] = entries
+ }
+
+ built := make(map[string][]byte)
+ var build func(string) ([]byte, error)
+ build = func(prefix string) ([]byte, error) {
+ entries := treeCache[prefix]
+ for i, e := range entries {
+ if e.mode == "40000" {
+ subPrefix := path.Join(prefix, e.name)
+ if sha, ok := built[subPrefix]; ok {
+ entries[i].sha = sha
+ continue
+ }
+ newShaStr, err := build(subPrefix)
+ if err != nil {
+ return nil, err
+ }
+ entries[i].sha = newShaStr
+ }
+ }
+ shaStr, err := writeTree(ctx, repoPath, entries)
+ if err != nil {
+ return nil, err
+ }
+ shaBytes, err := hex.DecodeString(shaStr)
+ if err != nil {
+ return nil, err
+ }
+ built[prefix] = shaBytes
+ return shaBytes, nil
+ }
+
+ rootShaBytes, err := build("")
+ if err != nil {
+ return "", err
+ }
+ return hex.EncodeToString(rootShaBytes), nil
+}
+
+type treeEntry struct {
+ mode string // like "100644"
+ name string // individual name
+ sha []byte
+}