Fast implementation of Git in pure Go codeberg.org/lindenii/furgit
git go
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/show-object: Split files

Runxi Yu ad880cd2 6cff7528

+141 -123
-123
cmd/show-object/main.go
··· 3 3 4 4 import ( 5 5 "flag" 6 - "fmt" 7 6 "log" 8 - "os" 9 - "strings" 10 - 11 - "codeberg.org/lindenii/furgit/object" 12 - "codeberg.org/lindenii/furgit/object/blob" 13 - "codeberg.org/lindenii/furgit/object/commit" 14 - objectid "codeberg.org/lindenii/furgit/object/id" 15 - "codeberg.org/lindenii/furgit/object/stored" 16 - "codeberg.org/lindenii/furgit/object/tag" 17 - "codeberg.org/lindenii/furgit/object/tree" 18 - "codeberg.org/lindenii/furgit/repository" 19 7 ) 20 8 21 9 func main() { ··· 33 21 log.Fatalf("run: %v", err) 34 22 } 35 23 } 36 - 37 - func run(repoPath, name *string) error { 38 - root, err := os.OpenRoot(*repoPath) 39 - if err != nil { 40 - return fmt.Errorf("open repo root: %w", err) 41 - } 42 - 43 - defer func() { _ = root.Close() }() 44 - 45 - repo, err := repository.Open(root) 46 - if err != nil { 47 - return fmt.Errorf("open repository: %w", err) 48 - } 49 - 50 - id, err := resolveInput(repo, *name) 51 - if err != nil { 52 - _ = repo.Close() 53 - 54 - return fmt.Errorf("resolve %q: %w", *name, err) 55 - } 56 - 57 - s, err := repo.Fetcher().ExactObject(id) 58 - if err != nil { 59 - _ = repo.Close() 60 - 61 - return fmt.Errorf("read object %s: %w", id, err) 62 - } 63 - 64 - printStored(s) 65 - 66 - err = repo.Close() 67 - if err != nil { 68 - return fmt.Errorf("close repository: %w", err) 69 - } 70 - 71 - return nil 72 - } 73 - 74 - func resolveInput(repo *repository.Repository, input string) (objectid.ObjectID, error) { 75 - id, err := objectid.ParseHex(repo.Algorithm(), strings.TrimSpace(input)) 76 - if err == nil { 77 - return id, nil 78 - } 79 - 80 - resolved, err := repo.Refs().ResolveToDetached(input) 81 - if err != nil { 82 - return objectid.ObjectID{}, err 83 - } 84 - 85 - return resolved.ID, nil 86 - } 87 - 88 - func printStored(s *stored.Stored[object.Object]) { 89 - var b strings.Builder 90 - 91 - id := s.ID() 92 - ty := s.Object().ObjectType() 93 - 94 - tyName, ok := ty.Name() 95 - if !ok { 96 - tyName = fmt.Sprintf("type %d", ty) 97 - } 98 - 99 - fmt.Fprintf(&b, "id: %s\n", id) 100 - fmt.Fprintf(&b, "type: %s\n", tyName) 101 - 102 - switch obj := s.Object().(type) { 103 - case *blob.Blob: 104 - blob := obj 105 - fmt.Fprintf(&b, "size: %d\n", len(blob.Data)) 106 - fmt.Fprintf(&b, "data: %q\n", string(blob.Data)) 107 - case *tree.Tree: 108 - tree := obj 109 - fmt.Fprintf(&b, "entries: %d\n", len(tree.Entries)) 110 - 111 - for _, entry := range tree.Entries { 112 - fmt.Fprintf(&b, "%06o %s\t%s\n", entry.Mode, entry.ID, entry.Name) 113 - } 114 - case *commit.Commit: 115 - commit := obj 116 - fmt.Fprintf(&b, "tree: %s\n", commit.Tree) 117 - 118 - for _, parent := range commit.Parents { 119 - fmt.Fprintf(&b, "parent: %s\n", parent) 120 - } 121 - 122 - fmt.Fprintf(&b, "author: %s <%s>\n", commit.Author.Name, commit.Author.Email) 123 - fmt.Fprintf(&b, "committer: %s <%s>\n", commit.Committer.Name, commit.Committer.Email) 124 - fmt.Fprintf(&b, "message:\n%s\n", string(commit.Message)) 125 - case *tag.Tag: 126 - tag := obj 127 - 128 - targetTy, ok := tag.TargetType.Name() 129 - if !ok { 130 - targetTy = fmt.Sprintf("type %d", tag.TargetType) 131 - } 132 - 133 - fmt.Fprintf(&b, "target: %s (%s)\n", tag.Target, targetTy) 134 - fmt.Fprintf(&b, "name: %s\n", tag.Name) 135 - 136 - if tag.Tagger != nil { 137 - fmt.Fprintf(&b, "tagger: %s <%s>\n", tag.Tagger.Name, tag.Tagger.Email) 138 - } 139 - 140 - fmt.Fprintf(&b, "message:\n%s\n", string(tag.Message)) 141 - default: 142 - fmt.Fprintf(&b, "%#v\n", obj) 143 - } 144 - 145 - _, _ = os.Stdout.WriteString(b.String()) 146 - }
+74
cmd/show-object/print.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "os" 6 + "strings" 7 + 8 + "codeberg.org/lindenii/furgit/object" 9 + "codeberg.org/lindenii/furgit/object/blob" 10 + "codeberg.org/lindenii/furgit/object/commit" 11 + "codeberg.org/lindenii/furgit/object/stored" 12 + "codeberg.org/lindenii/furgit/object/tag" 13 + "codeberg.org/lindenii/furgit/object/tree" 14 + ) 15 + 16 + func printStored(s *stored.Stored[object.Object]) { 17 + var b strings.Builder 18 + 19 + id := s.ID() 20 + ty := s.Object().ObjectType() 21 + 22 + tyName, ok := ty.Name() 23 + if !ok { 24 + tyName = fmt.Sprintf("type %d", ty) 25 + } 26 + 27 + fmt.Fprintf(&b, "id: %s\n", id) 28 + fmt.Fprintf(&b, "type: %s\n", tyName) 29 + 30 + switch obj := s.Object().(type) { 31 + case *blob.Blob: 32 + blob := obj 33 + fmt.Fprintf(&b, "size: %d\n", len(blob.Data)) 34 + fmt.Fprintf(&b, "data: %q\n", string(blob.Data)) 35 + case *tree.Tree: 36 + tree := obj 37 + fmt.Fprintf(&b, "entries: %d\n", len(tree.Entries)) 38 + 39 + for _, entry := range tree.Entries { 40 + fmt.Fprintf(&b, "%06o %s\t%s\n", entry.Mode, entry.ID, entry.Name) 41 + } 42 + case *commit.Commit: 43 + commit := obj 44 + fmt.Fprintf(&b, "tree: %s\n", commit.Tree) 45 + 46 + for _, parent := range commit.Parents { 47 + fmt.Fprintf(&b, "parent: %s\n", parent) 48 + } 49 + 50 + fmt.Fprintf(&b, "author: %s <%s>\n", commit.Author.Name, commit.Author.Email) 51 + fmt.Fprintf(&b, "committer: %s <%s>\n", commit.Committer.Name, commit.Committer.Email) 52 + fmt.Fprintf(&b, "message:\n%s\n", string(commit.Message)) 53 + case *tag.Tag: 54 + tag := obj 55 + 56 + targetTy, ok := tag.TargetType.Name() 57 + if !ok { 58 + targetTy = fmt.Sprintf("type %d", tag.TargetType) 59 + } 60 + 61 + fmt.Fprintf(&b, "target: %s (%s)\n", tag.Target, targetTy) 62 + fmt.Fprintf(&b, "name: %s\n", tag.Name) 63 + 64 + if tag.Tagger != nil { 65 + fmt.Fprintf(&b, "tagger: %s <%s>\n", tag.Tagger.Name, tag.Tagger.Email) 66 + } 67 + 68 + fmt.Fprintf(&b, "message:\n%s\n", string(tag.Message)) 69 + default: 70 + fmt.Fprintf(&b, "%#v\n", obj) 71 + } 72 + 73 + _, _ = os.Stdout.WriteString(b.String()) 74 + }
+22
cmd/show-object/resolve.go
··· 1 + package main 2 + 3 + import ( 4 + "strings" 5 + 6 + objectid "codeberg.org/lindenii/furgit/object/id" 7 + "codeberg.org/lindenii/furgit/repository" 8 + ) 9 + 10 + func resolveInput(repo *repository.Repository, input string) (objectid.ObjectID, error) { 11 + id, err := objectid.ParseHex(repo.Algorithm(), strings.TrimSpace(input)) 12 + if err == nil { 13 + return id, nil 14 + } 15 + 16 + resolved, err := repo.Refs().ResolveToDetached(input) 17 + if err != nil { 18 + return objectid.ObjectID{}, err 19 + } 20 + 21 + return resolved.ID, nil 22 + }
+45
cmd/show-object/run.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "os" 6 + 7 + "codeberg.org/lindenii/furgit/repository" 8 + ) 9 + 10 + func run(repoPath, name *string) error { 11 + root, err := os.OpenRoot(*repoPath) 12 + if err != nil { 13 + return fmt.Errorf("open repo root: %w", err) 14 + } 15 + 16 + defer func() { _ = root.Close() }() 17 + 18 + repo, err := repository.Open(root) 19 + if err != nil { 20 + return fmt.Errorf("open repository: %w", err) 21 + } 22 + 23 + id, err := resolveInput(repo, *name) 24 + if err != nil { 25 + _ = repo.Close() 26 + 27 + return fmt.Errorf("resolve %q: %w", *name, err) 28 + } 29 + 30 + s, err := repo.Fetcher().ExactObject(id) 31 + if err != nil { 32 + _ = repo.Close() 33 + 34 + return fmt.Errorf("read object %s: %w", id, err) 35 + } 36 + 37 + printStored(s) 38 + 39 + err = repo.Close() 40 + if err != nil { 41 + return fmt.Errorf("close repository: %w", err) 42 + } 43 + 44 + return nil 45 + }