1 const std = @import("std");
3 const Alloc = std.mem.Allocator;
6 const Commit = struct {
7 author: std.BoundedArray(u8, 64),
8 message: std.BoundedArray(u8, 1024),
13 data: std.BoundedArray(u8, 1024),
15 const Object = struct {
18 // pub fn getCommit(self: *Object) Commit {}
19 // pub fn getBlob(self: *Object) Blob {}
21 const PackFile = struct {
24 pub fn init(alloc: Alloc) PackFile {
29 pub fn deinit(self: *PackFile) void {
32 pub fn parse(self: *PackFile, idxReader: std.io.AnyReader, pckReader: anytype) !void {
34 var buffer: [16]u8 = undefined;
35 _ = try idxReader.read(&buffer);
36 std.debug.print("{s}\n", .{&buffer});
37 _ = try pckReader.read(&buffer);
38 std.debug.print("{s}\n", .{&buffer});
41 // pub fn init(alloc: Alloc, path: []const u8) PackFile {}
42 // pub fn deinit(self: *PackFile) void {}
43 // pub fn getObject(self: *PackFile, id: Id) Object {}
51 pub fn open(alloc: Alloc, path: []const u8) !Repo {
52 const dir = try std.fs.cwd().openDir(path, .{});
55 const head = try dir.readFileAlloc(alloc, "HEAD", 1024);
56 defer alloc.free(head);
58 // read file pointed at by HEAD
59 const headPath = head[5 .. head.len - 1];
60 var idBuffer: [40]u8 = undefined;
61 const idStr = try dir.readFile(headPath, &idBuffer);
64 const id = try std.fmt.parseUnsigned(u160, idStr, 16);
67 var packfile = PackFile.init(alloc);
68 if (dir.openDir("objects/pack", .{ .iterate = true })) |packDir| {
69 var packIt = packDir.iterate();
70 while (try packIt.next()) |f| {
71 if (std.mem.endsWith(u8, f.name, ".idx")) {
72 const idxFilename = f.name;
73 var pckFilenameBuffer: [64]u8 = undefined;
74 const pckFilename = try std.fmt.bufPrint(&pckFilenameBuffer, "{s}.pack", .{idxFilename[0 .. idxFilename.len - 4]});
76 const idxFile = try packDir.openFile(idxFilename, .{});
77 const pckFile = try packDir.openFile(pckFilename, .{});
78 defer idxFile.close();
79 defer pckFile.close();
81 const idxReader = idxFile.reader().any();
82 const pckReader = pckFile.reader().any();
84 try packfile.parse(idxReader, pckReader);
88 std.debug.print("{}\n", .{err});
97 pub fn close(self: *Repo) void {
98 self.packfile.deinit();
100 // pub fn getObject(self: *Repo, id: Id) Object {}
104 var repo = try Repo.open(std.testing.allocator, "../microwindows/.git");
107 std.debug.print("HEAD: {}\n", .{repo.head});
110 // test "list commits" {
111 // var repo = Repo.open(std.testing.allocator, "../microwindows/.git");
112 // defer repo.close();
114 // const head = repo.getObject(repo.head);
115 // defer head.deinit();
117 // var c = head.getCommit();
119 // std.debug.print("{}\n", .{c});
125 // var repo = Repo.open(std.testing.allocator, "../microwindows/.git");
126 // defer repo.close();
128 // const head = repo.getObject(repo.head);
129 // defer head.deinit();
131 // const commit = head.getCommit();
133 // std.debug.print("{}\n", .{commit.tree});
137 // var repo = Repo.open(std.testing.allocator, "../microwindows/.git");
138 // defer repo.close();
140 // const head = repo.getObject(repo.head);
141 // defer head.deinit();
143 // const commit = head.getCommit();
144 // const blob = repo.getBlob(commit.files[0].id);
146 // std.debug.print("{}\n", .{blob});