aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorjet2tlf <jet2tlf@gmail.com>2024-06-01 04:00:14 +0000
committerjet2tlf <jet2tlf@gmail.com>2024-06-01 04:00:14 +0000
commit4fcb99ab9a5fa08116f7bce0499f123c39facc32 (patch)
treeea93ddff36bf0e726082750ccf7d1ac95b09f361 /cmd
parent798ce32511e1ff7053e39b948e7ce5ce74cacca9 (diff)
downloadgit-go-4fcb99ab9a5fa08116f7bce0499f123c39facc32.tar.gz
git-go-4fcb99ab9a5fa08116f7bce0499f123c39facc32.zip
codecrafters submit [skip ci]
Diffstat (limited to 'cmd')
-rw-r--r--cmd/mygit/main.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/cmd/mygit/main.go b/cmd/mygit/main.go
index 2abec34..bbdb4a9 100644
--- a/cmd/mygit/main.go
+++ b/cmd/mygit/main.go
@@ -1,7 +1,10 @@
package main
import (
+ "bytes"
+ "compress/zlib"
"fmt"
+ "io"
"os"
)
@@ -26,6 +29,37 @@ func main() {
fmt.Println("Initialized git directory")
+ case "cat-file":
+ object := os.Args[3]
+ filePath := fmt.Sprintf(".git/objects/%s/%s", object[:2], object[2:])
+ file, err := os.Open(filePath)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error opening file: %s\n", err)
+ os.Exit(1)
+ }
+ defer file.Close()
+
+ r, err := zlib.NewReader(file)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error creating zlib reader: %s\n", err)
+ os.Exit(1)
+ }
+ defer r.Close()
+
+ w, err := io.ReadAll(r)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error reading zlib data: %s\n", err)
+ os.Exit(1)
+ }
+
+ parts := bytes.Split(w, []byte("\x00"))
+ if len(parts) < 2 {
+ fmt.Fprintf(os.Stderr, "Invalid zlib data\n")
+ os.Exit(1)
+ }
+
+ fmt.Print(string(parts[1]))
+
default:
fmt.Fprintf(os.Stderr, "Unknown command %s\n", command)
os.Exit(1)