]> gitweb.ps.run Git - ziggit/commitdiff
Implement Object parsing
authorpatrick-scho <patrick.schoenberger@posteo.de>
Mon, 12 Aug 2024 12:32:59 +0000 (14:32 +0200)
committerpatrick-scho <patrick.schoenberger@posteo.de>
Mon, 12 Aug 2024 12:32:59 +0000 (14:32 +0200)
git.zig

diff --git a/git.zig b/git.zig
index 8c44fb6bd891daf0c2bc30c5e2444e85f2732365..0d42c2afe50e6cc448b412a30caa4559ebdde242 100644 (file)
--- a/git.zig
+++ b/git.zig
@@ -33,6 +33,55 @@ const Object = struct {
             .data = data,
         };
     }
+    pub fn parse(self: Object, alloc: Alloc) !union(enum) { c: Commit, t: Tree, b: Blob } {
+        switch (self.kind) {
+            1 => {
+                const authorOffset = std.mem.indexOf(u8, self.data, "author ") orelse return error.InvalidCommitFormat;
+                const authorNewline = std.mem.indexOfScalarPos(u8, self.data, authorOffset, '\n') orelse return error.InvalidCommitFormat;
+                const committerOffset = std.mem.indexOf(u8, self.data, "committer ") orelse return error.InvalidCommitFormat;
+                const committerNewline = std.mem.indexOfScalarPos(u8, self.data, committerOffset, '\n') orelse return error.InvalidCommitFormat;
+
+                return .{
+                    .c = Commit{
+                        .tree = try std.fmt.parseUnsigned(Id, self.data[5..45], 16),
+                        .parent = try std.fmt.parseUnsigned(Id, self.data[53..93], 16),
+                        .author = self.data[authorOffset..authorNewline],
+                        .committer = self.data[committerOffset..committerNewline],
+                        .message = self.data[committerNewline + 1 .. self.data.len],
+                    },
+                };
+            },
+            2 => {
+                var t = Tree.init(alloc);
+
+                var offset: usize = 0;
+
+                while (offset < self.data.len - 1) {
+                    const spaceOffset = std.mem.indexOfScalarPos(u8, self.data, offset, ' ') orelse return error.InvalidTreeFormat;
+                    const zeroOffset = std.mem.indexOfScalarPos(u8, self.data, spaceOffset, 0) orelse return error.InvalidTreeFormat;
+
+                    try t.append(.{
+                        .permissions = self.data[offset..spaceOffset],
+                        .name = self.data[spaceOffset + 1 .. zeroOffset],
+                        .id = std.mem.readVarInt(Id, self.data[zeroOffset + 1 .. zeroOffset + 21], .big),
+                    });
+
+                    offset = zeroOffset + 21;
+                }
+
+                return .{ .t = t };
+            },
+            3 => {
+                return .{
+                    .b = Blob{ .data = self.data },
+                };
+            },
+            4 => {
+                return error.TagNotImplemented;
+            },
+            else => return error.UnknownGitObjectType,
+        }
+    }
     // pub fn getCommit(self: *Object) Commit {}
     // pub fn getBlob(self: *Object) Blob {}
 };