aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-04-02 06:23:30 +0000
committerGravatar Runxi Yu2026-04-02 06:28:39 +0000
commita041d523de389b65b98a5373a8034041db2a8d83 (patch)
tree7b423dc735f463be616045f2c3c2095a7737aca7 /cmd
parentresearch: Add dynamic pack resources (diff)
signatureNo signature
*: Remove
Diffstat (limited to 'cmd')
-rw-r--r--cmd/doc.go2
-rw-r--r--cmd/receivepack9418/conn.go122
-rw-r--r--cmd/receivepack9418/errpkt.go18
-rw-r--r--cmd/receivepack9418/gitproto.go23
-rw-r--r--cmd/receivepack9418/main.go61
-rw-r--r--cmd/receivepack9418/profile.go58
-rw-r--r--cmd/receivepack9418/request.go61
-rw-r--r--cmd/receivepack9418/run.go70
-rw-r--r--cmd/receivepack9418/server.go12
-rw-r--r--cmd/show-object/main.go23
-rw-r--r--cmd/show-object/print.go74
-rw-r--r--cmd/show-object/resolve.go22
-rw-r--r--cmd/show-object/run.go45
13 files changed, 0 insertions, 591 deletions
diff --git a/cmd/doc.go b/cmd/doc.go
deleted file mode 100644
index cdc58288..00000000
--- a/cmd/doc.go
+++ /dev/null
@@ -1,2 +0,0 @@
-// Package cmd encapsulates various commands provided by this module.
-package cmd
diff --git a/cmd/receivepack9418/conn.go b/cmd/receivepack9418/conn.go
deleted file mode 100644
index 755cf022..00000000
--- a/cmd/receivepack9418/conn.go
+++ /dev/null
@@ -1,122 +0,0 @@
-package main
-
-import (
- "bufio"
- "context"
- "fmt"
- "log"
- "net"
- "os"
- "strings"
-
- "codeberg.org/lindenii/furgit/network/receivepack"
- objectdual "codeberg.org/lindenii/furgit/object/store/dual"
- objectloose "codeberg.org/lindenii/furgit/object/store/loose"
- objectpacked "codeberg.org/lindenii/furgit/object/store/packed"
-)
-
-func (srv *server) handleConn(conn net.Conn) {
- defer func() { _ = conn.Close() }()
-
- reader := bufio.NewReader(conn)
- writer := bufio.NewWriter(conn)
-
- req, err := readGitProtoRequest(reader)
- if err != nil {
- writeErrPkt(writer, fmt.Sprintf("invalid initial request: %v", err))
- _ = writer.Flush()
-
- log.Printf("receivepack9418: %s: invalid initial request: %v", conn.RemoteAddr(), err)
-
- return
- }
-
- if req.Command != "git-receive-pack" {
- writeErrPkt(writer, fmt.Sprintf("unsupported command %q", req.Command))
- _ = writer.Flush()
-
- log.Printf("receivepack9418: %s: unsupported command %q", conn.RemoteAddr(), req.Command)
-
- return
- }
-
- gitProtocol := strings.Join(req.ExtraParameters, ":")
-
- objectIngress, cleanupObjectIngress, err := srv.openObjectIngress()
- if err != nil {
- writeErrPkt(writer, fmt.Sprintf("object ingress unavailable: %v", err))
- _ = writer.Flush()
-
- log.Printf("receivepack9418: %s: object ingress unavailable: %v", conn.RemoteAddr(), err)
-
- return
- }
-
- defer cleanupObjectIngress()
-
- opts := receivepack.Options{
- GitProtocol: gitProtocol,
- Algorithm: srv.repo.Algorithm(),
- Refs: srv.repo.Refs(),
- ExistingObjects: srv.repo.Objects(),
- ObjectIngress: objectIngress,
- }
-
- err = receivepack.ReceivePack(context.Background(), writer, reader, opts)
- if err != nil {
- _ = writer.Flush()
-
- log.Printf(
- "receivepack9418: %s: receive-pack failed (path=%q host=%q extras=%v): %v",
- conn.RemoteAddr(),
- req.Pathname,
- req.Host,
- req.ExtraParameters,
- err,
- )
-
- return
- }
-
- err = writer.Flush()
- if err != nil {
- log.Printf("receivepack9418: %s: flush failed: %v", conn.RemoteAddr(), err)
-
- return
- }
-}
-
-func (srv *server) openObjectIngress() (*objectdual.Dual, func(), error) {
- err := srv.objectsRoot.Mkdir("pack", 0o755)
- if err != nil && !os.IsExist(err) {
- return nil, nil, err
- }
-
- packRoot, err := srv.objectsRoot.OpenRoot("pack")
- if err != nil {
- return nil, nil, err
- }
-
- looseStore, err := objectloose.New(srv.objectsRoot, srv.repo.Algorithm())
- if err != nil {
- _ = packRoot.Close()
-
- return nil, nil, err
- }
-
- packedStore, err := objectpacked.New(packRoot, srv.repo.Algorithm(), objectpacked.Options{WriteRev: true})
- if err != nil {
- _ = looseStore.Close()
- _ = packRoot.Close()
-
- return nil, nil, err
- }
-
- cleanup := func() {
- _ = packedStore.Close()
- _ = looseStore.Close()
- _ = packRoot.Close()
- }
-
- return objectdual.New(looseStore, packedStore), cleanup, nil
-}
diff --git a/cmd/receivepack9418/errpkt.go b/cmd/receivepack9418/errpkt.go
deleted file mode 100644
index 743811aa..00000000
--- a/cmd/receivepack9418/errpkt.go
+++ /dev/null
@@ -1,18 +0,0 @@
-package main
-
-import (
- "io"
-
- "codeberg.org/lindenii/furgit/network/protocol/pktline"
-)
-
-func writeErrPkt(w io.Writer, message string) {
- payload := []byte("ERR " + message + "\n")
-
- frame, err := pktline.AppendData(nil, payload)
- if err != nil {
- return
- }
-
- _, _ = w.Write(frame)
-}
diff --git a/cmd/receivepack9418/gitproto.go b/cmd/receivepack9418/gitproto.go
deleted file mode 100644
index 28c192d4..00000000
--- a/cmd/receivepack9418/gitproto.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package main
-
-import (
- "fmt"
- "io"
-
- "codeberg.org/lindenii/furgit/network/protocol/pktline"
-)
-
-func readGitProtoRequest(r io.Reader) (gitProtoRequest, error) {
- dec := pktline.NewDecoder(r, pktline.ReadOptions{})
-
- frame, err := dec.ReadFrame()
- if err != nil {
- return gitProtoRequest{}, err
- }
-
- if frame.Type != pktline.PacketData {
- return gitProtoRequest{}, fmt.Errorf("expected initial pkt-line data, got %v", frame.Type)
- }
-
- return parseGitProtoRequestPayload(frame.Payload)
-}
diff --git a/cmd/receivepack9418/main.go b/cmd/receivepack9418/main.go
deleted file mode 100644
index 6884f326..00000000
--- a/cmd/receivepack9418/main.go
+++ /dev/null
@@ -1,61 +0,0 @@
-// Command receivepack9418 serves one fixed repository over git:// receive-pack on TCP 9418.
-package main
-
-import (
- "flag"
- "log"
- "os"
-)
-
-func main() {
- os.Exit(runMain())
-}
-
-func runMain() int {
- listenAddr := flag.String("listen", ":9418", "listen address")
- repoPath := flag.String("repo", "", "path to git dir (.git or bare repo root)")
- cpuProfilePath := flag.String("cpuprofile", "", "write CPU profile to file")
- memProfilePath := flag.String("memprofile", "", "write heap profile to file at exit")
-
- flag.Parse()
-
- if *repoPath == "" {
- log.Print("must provide -repo <path-to-git-dir>")
-
- return 2
- }
-
- if *cpuProfilePath != "" {
- stopCPUProfile, err := startCPUProfile(*cpuProfilePath)
- if err != nil {
- log.Printf("cpuprofile: %v", err)
-
- return 1
- }
-
- defer func() {
- stopErr := stopCPUProfile()
- if stopErr != nil {
- log.Printf("cpuprofile: %v", stopErr)
- }
- }()
- }
-
- if *memProfilePath != "" {
- defer func() {
- memErr := writeMemProfile(*memProfilePath)
- if memErr != nil {
- log.Printf("memprofile: %v", memErr)
- }
- }()
- }
-
- err := run(*listenAddr, *repoPath)
- if err != nil {
- log.Printf("run: %v", err)
-
- return 1
- }
-
- return 0
-}
diff --git a/cmd/receivepack9418/profile.go b/cmd/receivepack9418/profile.go
deleted file mode 100644
index 40ba1d56..00000000
--- a/cmd/receivepack9418/profile.go
+++ /dev/null
@@ -1,58 +0,0 @@
-package main
-
-import (
- "fmt"
- "os"
- "runtime"
- "runtime/pprof"
-)
-
-func startCPUProfile(path string) (func() error, error) {
- //#nosec G304
- file, err := os.Create(path)
- if err != nil {
- return nil, fmt.Errorf("create %q: %w", path, err)
- }
-
- err = pprof.StartCPUProfile(file)
- if err != nil {
- _ = file.Close()
-
- return nil, fmt.Errorf("start cpu profile %q: %w", path, err)
- }
-
- return func() error {
- pprof.StopCPUProfile()
-
- err := file.Close()
- if err != nil {
- return fmt.Errorf("close cpu profile %q: %w", path, err)
- }
-
- return nil
- }, nil
-}
-
-func writeMemProfile(path string) error {
- //#nosec G304
- file, err := os.Create(path)
- if err != nil {
- return fmt.Errorf("create %q: %w", path, err)
- }
-
- runtime.GC()
-
- err = pprof.WriteHeapProfile(file)
- if err != nil {
- _ = file.Close()
-
- return fmt.Errorf("write heap profile %q: %w", path, err)
- }
-
- err = file.Close()
- if err != nil {
- return fmt.Errorf("close heap profile %q: %w", path, err)
- }
-
- return nil
-}
diff --git a/cmd/receivepack9418/request.go b/cmd/receivepack9418/request.go
deleted file mode 100644
index 57b55e30..00000000
--- a/cmd/receivepack9418/request.go
+++ /dev/null
@@ -1,61 +0,0 @@
-package main
-
-import (
- "bytes"
- "errors"
- "fmt"
- "strings"
-)
-
-type gitProtoRequest struct {
- Command string
- Pathname string
- Host string
- ExtraParameters []string
-}
-
-func parseGitProtoRequestPayload(payload []byte) (gitProtoRequest, error) {
- parts := bytes.Split(payload, []byte{0})
- if len(parts) == 0 || len(parts[0]) == 0 {
- return gitProtoRequest{}, errors.New("missing command/path segment")
- }
-
- commandPath := string(parts[0])
-
- command, pathname, ok := strings.Cut(commandPath, " ")
- if !ok || command == "" || pathname == "" {
- return gitProtoRequest{}, fmt.Errorf("malformed command/path segment %q", commandPath)
- }
-
- req := gitProtoRequest{
- Command: command,
- Pathname: pathname,
- }
-
- i := 1
- if i < len(parts) && strings.HasPrefix(string(parts[i]), "host=") {
- req.Host = strings.TrimPrefix(string(parts[i]), "host=")
- i++
- }
-
- // No tail left.
- if i >= len(parts) {
- return req, nil
- }
-
- // If there is tail, grammar requires one empty field before extras.
- if len(parts[i]) != 0 {
- return gitProtoRequest{}, fmt.Errorf("unexpected token %q after host/path", string(parts[i]))
- }
-
- i++
- for ; i < len(parts); i++ {
- if len(parts[i]) == 0 {
- continue
- }
-
- req.ExtraParameters = append(req.ExtraParameters, string(parts[i]))
- }
-
- return req, nil
-}
diff --git a/cmd/receivepack9418/run.go b/cmd/receivepack9418/run.go
deleted file mode 100644
index 2932459d..00000000
--- a/cmd/receivepack9418/run.go
+++ /dev/null
@@ -1,70 +0,0 @@
-package main
-
-import (
- "context"
- "errors"
- "fmt"
- "log"
- "net"
- "os"
-
- "codeberg.org/lindenii/furgit/repository"
-)
-
-func run(listenAddr, repoPath string) error {
- repoRoot, err := os.OpenRoot(repoPath)
- if err != nil {
- return fmt.Errorf("open repo root: %w", err)
- }
-
- defer func() { _ = repoRoot.Close() }()
-
- repo, err := repository.Open(repoRoot)
- if err != nil {
- return fmt.Errorf("open repository: %w", err)
- }
-
- defer func() { _ = repo.Close() }()
-
- objectsRoot, err := repoRoot.OpenRoot("objects")
- if err != nil {
- return fmt.Errorf("open objects root: %w", err)
- }
-
- defer func() { _ = objectsRoot.Close() }()
-
- srv := &server{
- repo: repo,
- objectsRoot: objectsRoot,
- }
-
- ln, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", listenAddr)
- if err != nil {
- return fmt.Errorf("listen %q: %w", listenAddr, err)
- }
-
- defer func() { _ = ln.Close() }()
-
- log.Printf("receivepack9418: listening on %s", listenAddr)
- log.Printf("receivepack9418: repository=%s algorithm=%s", repoPath, repo.Algorithm())
-
- for {
- conn, err := ln.Accept()
- if err != nil {
- if errors.Is(err, net.ErrClosed) {
- return nil
- }
-
- nerr, ok := errors.AsType[net.Error](err)
- if ok && nerr.Timeout() {
- log.Printf("receivepack9418: timeout accept error: %v", err)
-
- continue
- }
-
- return fmt.Errorf("accept: %w", err)
- }
-
- go srv.handleConn(conn)
- }
-}
diff --git a/cmd/receivepack9418/server.go b/cmd/receivepack9418/server.go
deleted file mode 100644
index 74793712..00000000
--- a/cmd/receivepack9418/server.go
+++ /dev/null
@@ -1,12 +0,0 @@
-package main
-
-import (
- "os"
-
- "codeberg.org/lindenii/furgit/repository"
-)
-
-type server struct {
- repo *repository.Repository
- objectsRoot *os.Root
-}
diff --git a/cmd/show-object/main.go b/cmd/show-object/main.go
deleted file mode 100644
index 8fdffac8..00000000
--- a/cmd/show-object/main.go
+++ /dev/null
@@ -1,23 +0,0 @@
-// Command show-object provides a small command line utility to show the details of a specified Git object.
-package main
-
-import (
- "flag"
- "log"
-)
-
-func main() {
- repoPath := flag.String("r", "", "path to git dir (.git or bare repo root)")
- name := flag.String("h", "", "reference name or object id")
-
- flag.Parse()
-
- if *repoPath == "" || *name == "" {
- log.Fatal("must provide -r <repo> and -h <ref-or-object-id>")
- }
-
- err := run(repoPath, name)
- if err != nil {
- log.Fatalf("run: %v", err)
- }
-}
diff --git a/cmd/show-object/print.go b/cmd/show-object/print.go
deleted file mode 100644
index 75484f73..00000000
--- a/cmd/show-object/print.go
+++ /dev/null
@@ -1,74 +0,0 @@
-package main
-
-import (
- "fmt"
- "os"
- "strings"
-
- "codeberg.org/lindenii/furgit/object"
- "codeberg.org/lindenii/furgit/object/blob"
- "codeberg.org/lindenii/furgit/object/commit"
- "codeberg.org/lindenii/furgit/object/stored"
- "codeberg.org/lindenii/furgit/object/tag"
- "codeberg.org/lindenii/furgit/object/tree"
-)
-
-func printStored(s *stored.Stored[object.Object]) {
- var b strings.Builder
-
- id := s.ID()
- ty := s.Object().ObjectType()
-
- tyName, ok := ty.Name()
- if !ok {
- tyName = fmt.Sprintf("type %d", ty)
- }
-
- fmt.Fprintf(&b, "id: %s\n", id)
- fmt.Fprintf(&b, "type: %s\n", tyName)
-
- switch obj := s.Object().(type) {
- case *blob.Blob:
- blob := obj
- fmt.Fprintf(&b, "size: %d\n", len(blob.Data))
- fmt.Fprintf(&b, "data: %q\n", string(blob.Data))
- case *tree.Tree:
- tree := obj
- fmt.Fprintf(&b, "entries: %d\n", len(tree.Entries))
-
- for _, entry := range tree.Entries {
- fmt.Fprintf(&b, "%06o %s\t%s\n", entry.Mode, entry.ID, entry.Name)
- }
- case *commit.Commit:
- commit := obj
- fmt.Fprintf(&b, "tree: %s\n", commit.Tree)
-
- for _, parent := range commit.Parents {
- fmt.Fprintf(&b, "parent: %s\n", parent)
- }
-
- fmt.Fprintf(&b, "author: %s <%s>\n", commit.Author.Name, commit.Author.Email)
- fmt.Fprintf(&b, "committer: %s <%s>\n", commit.Committer.Name, commit.Committer.Email)
- fmt.Fprintf(&b, "message:\n%s\n", string(commit.Message))
- case *tag.Tag:
- tag := obj
-
- targetTy, ok := tag.TargetType.Name()
- if !ok {
- targetTy = fmt.Sprintf("type %d", tag.TargetType)
- }
-
- fmt.Fprintf(&b, "target: %s (%s)\n", tag.Target, targetTy)
- fmt.Fprintf(&b, "name: %s\n", tag.Name)
-
- if tag.Tagger != nil {
- fmt.Fprintf(&b, "tagger: %s <%s>\n", tag.Tagger.Name, tag.Tagger.Email)
- }
-
- fmt.Fprintf(&b, "message:\n%s\n", string(tag.Message))
- default:
- fmt.Fprintf(&b, "%#v\n", obj)
- }
-
- _, _ = os.Stdout.WriteString(b.String())
-}
diff --git a/cmd/show-object/resolve.go b/cmd/show-object/resolve.go
deleted file mode 100644
index eaf2c102..00000000
--- a/cmd/show-object/resolve.go
+++ /dev/null
@@ -1,22 +0,0 @@
-package main
-
-import (
- "strings"
-
- objectid "codeberg.org/lindenii/furgit/object/id"
- "codeberg.org/lindenii/furgit/repository"
-)
-
-func resolveInput(repo *repository.Repository, input string) (objectid.ObjectID, error) {
- id, err := objectid.ParseHex(repo.Algorithm(), strings.TrimSpace(input))
- if err == nil {
- return id, nil
- }
-
- resolved, err := repo.Refs().ResolveToDetached(input)
- if err != nil {
- return objectid.ObjectID{}, err
- }
-
- return resolved.ID, nil
-}
diff --git a/cmd/show-object/run.go b/cmd/show-object/run.go
deleted file mode 100644
index f1a6fc6d..00000000
--- a/cmd/show-object/run.go
+++ /dev/null
@@ -1,45 +0,0 @@
-package main
-
-import (
- "fmt"
- "os"
-
- "codeberg.org/lindenii/furgit/repository"
-)
-
-func run(repoPath, name *string) error {
- root, err := os.OpenRoot(*repoPath)
- if err != nil {
- return fmt.Errorf("open repo root: %w", err)
- }
-
- defer func() { _ = root.Close() }()
-
- repo, err := repository.Open(root)
- if err != nil {
- return fmt.Errorf("open repository: %w", err)
- }
-
- id, err := resolveInput(repo, *name)
- if err != nil {
- _ = repo.Close()
-
- return fmt.Errorf("resolve %q: %w", *name, err)
- }
-
- s, err := repo.Fetcher().ExactObject(id)
- if err != nil {
- _ = repo.Close()
-
- return fmt.Errorf("read object %s: %w", id, err)
- }
-
- printStored(s)
-
- err = repo.Close()
- if err != nil {
- return fmt.Errorf("close repository: %w", err)
- }
-
- return nil
-}