]> gitweb.ps.run Git - ziggit/commitdiff
refacor init
authorPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 25 May 2024 15:46:26 +0000 (17:46 +0200)
committerPatrick Schönberger <patrick.schoenberger@posteo.de>
Sat, 25 May 2024 15:46:26 +0000 (17:46 +0200)
git.zig

diff --git a/git.zig b/git.zig
index 3baa985c1a9abbe5534966fcf686815553367a53..fe828705d0dde459fcae8a930ddb47e18ef81783 100644 (file)
--- a/git.zig
+++ b/git.zig
@@ -21,15 +21,38 @@ const Object = struct {
 const PackFile = struct {
     alloc: Alloc,
 
-    pub fn init(alloc: Alloc) PackFile {
-        return .{
-            .alloc = alloc,
-        };
-    }
-    pub fn deinit(self: *PackFile) void {
-        _ = self;
+    pub fn open(alloc: Alloc, dir: std.fs.Dir) !PackFile {
+        var self = PackFile{ .alloc = alloc };
+
+        var packDir = try dir.openDir("objects/pack", .{ .iterate = true });
+        defer packDir.close();
+
+        var packIt = packDir.iterate();
+        while (try packIt.next()) |f| {
+            if (std.mem.endsWith(u8, f.name, ".idx")) {
+                const idxFilename = f.name;
+                var pckFilename = try std.BoundedArray(u8, std.fs.max_path_bytes).init(0);
+                try std.fmt.format(
+                    pckFilename.writer(),
+                    "{s}.pack",
+                    .{idxFilename[0 .. idxFilename.len - 4]},
+                );
+
+                const idxFile = try packDir.openFile(idxFilename, .{});
+                const pckFile = try packDir.openFile(pckFilename.constSlice(), .{});
+                defer idxFile.close();
+                defer pckFile.close();
+
+                const idxReader = idxFile.reader().any();
+                const pckReader = pckFile.reader().any();
+
+                try self.parse(idxReader, pckReader);
+            }
+        }
+
+        return self;
     }
-    pub fn parse(self: *PackFile, idxReader: std.io.AnyReader, pckReader: anytype) !void {
+    pub fn parse(self: *PackFile, idxReader: std.io.AnyReader, pckReader: std.io.AnyReader) !void {
         _ = self;
         var buffer: [16]u8 = undefined;
         _ = try idxReader.read(&buffer);
@@ -44,58 +67,35 @@ const PackFile = struct {
 };
 const Repo = struct {
     alloc: Alloc,
+    dir: std.fs.Dir,
     packfile: PackFile,
 
-    head: Id,
-
     pub fn open(alloc: Alloc, path: []const u8) !Repo {
         const dir = try std.fs.cwd().openDir(path, .{});
 
-        // read file HEAD
-        const head = try dir.readFileAlloc(alloc, "HEAD", 1024);
-        defer alloc.free(head);
-
-        // read file pointed at by HEAD
-        const headPath = head[5 .. head.len - 1];
-        var idBuffer: [40]u8 = undefined;
-        const idStr = try dir.readFile(headPath, &idBuffer);
-
-        // parse id from file
-        const id = try std.fmt.parseUnsigned(u160, idStr, 16);
-
-        // open any packfiles
-        var packfile = PackFile.init(alloc);
-        if (dir.openDir("objects/pack", .{ .iterate = true })) |packDir| {
-            var packIt = packDir.iterate();
-            while (try packIt.next()) |f| {
-                if (std.mem.endsWith(u8, f.name, ".idx")) {
-                    const idxFilename = f.name;
-                    var pckFilenameBuffer: [64]u8 = undefined;
-                    const pckFilename = try std.fmt.bufPrint(&pckFilenameBuffer, "{s}.pack", .{idxFilename[0 .. idxFilename.len - 4]});
-
-                    const idxFile = try packDir.openFile(idxFilename, .{});
-                    const pckFile = try packDir.openFile(pckFilename, .{});
-                    defer idxFile.close();
-                    defer pckFile.close();
-
-                    const idxReader = idxFile.reader().any();
-                    const pckReader = pckFile.reader().any();
-
-                    try packfile.parse(idxReader, pckReader);
-                }
-            }
-        } else |err| {
-            std.debug.print("{}\n", .{err});
-        }
+        const packfile = try PackFile.open(alloc, dir);
 
         return .{
             .alloc = alloc,
+            .dir = dir,
             .packfile = packfile,
-            .head = id,
         };
     }
     pub fn close(self: *Repo) void {
-        self.packfile.deinit();
+        self.dir.close();
+    }
+    pub fn getHead(self: *Repo) !Id {
+        // read file HEAD
+        const head = try self.dir.readFileAlloc(self.alloc, "HEAD", 1024);
+        defer self.alloc.free(head);
+
+        // read file pointed at by HEAD
+        const headPath = head[5 .. head.len - 1];
+        var idBuffer: [40]u8 = undefined;
+        const idStr = try self.dir.readFile(headPath, &idBuffer);
+
+        // parse id from file
+        return try std.fmt.parseUnsigned(u160, idStr, 16);
     }
     // pub fn getObject(self: *Repo, id: Id) Object {}
 };
@@ -104,7 +104,9 @@ test "print HEAD" {
     var repo = try Repo.open(std.testing.allocator, "../microwindows/.git");
     defer repo.close();
 
-    std.debug.print("HEAD: {}\n", .{repo.head});
+    const head = try repo.getHead();
+
+    std.debug.print("HEAD: {}\n", .{head});
 }
 
 // test "list commits" {