]> gitweb.ps.run Git - ziggit/blob - git.zig
refacor init
[ziggit] / git.zig
1 const std = @import("std");
2
3 const Alloc = std.mem.Allocator;
4
5 const Id = u160;
6 const Commit = struct {
7     author: std.BoundedArray(u8, 64),
8     message: std.BoundedArray(u8, 1024),
9     parent: Id,
10     tree: Id,
11 };
12 const Blob = struct {
13     data: std.BoundedArray(u8, 1024),
14 };
15 const Object = struct {
16     alloc: Alloc,
17
18     // pub fn getCommit(self: *Object) Commit {}
19     // pub fn getBlob(self: *Object) Blob {}
20 };
21 const PackFile = struct {
22     alloc: Alloc,
23
24     pub fn open(alloc: Alloc, dir: std.fs.Dir) !PackFile {
25         var self = PackFile{ .alloc = alloc };
26
27         var packDir = try dir.openDir("objects/pack", .{ .iterate = true });
28         defer packDir.close();
29
30         var packIt = packDir.iterate();
31         while (try packIt.next()) |f| {
32             if (std.mem.endsWith(u8, f.name, ".idx")) {
33                 const idxFilename = f.name;
34                 var pckFilename = try std.BoundedArray(u8, std.fs.max_path_bytes).init(0);
35                 try std.fmt.format(
36                     pckFilename.writer(),
37                     "{s}.pack",
38                     .{idxFilename[0 .. idxFilename.len - 4]},
39                 );
40
41                 const idxFile = try packDir.openFile(idxFilename, .{});
42                 const pckFile = try packDir.openFile(pckFilename.constSlice(), .{});
43                 defer idxFile.close();
44                 defer pckFile.close();
45
46                 const idxReader = idxFile.reader().any();
47                 const pckReader = pckFile.reader().any();
48
49                 try self.parse(idxReader, pckReader);
50             }
51         }
52
53         return self;
54     }
55     pub fn parse(self: *PackFile, idxReader: std.io.AnyReader, pckReader: std.io.AnyReader) !void {
56         _ = self;
57         var buffer: [16]u8 = undefined;
58         _ = try idxReader.read(&buffer);
59         std.debug.print("{s}\n", .{&buffer});
60         _ = try pckReader.read(&buffer);
61         std.debug.print("{s}\n", .{&buffer});
62     }
63
64     // pub fn init(alloc: Alloc, path: []const u8) PackFile {}
65     // pub fn deinit(self: *PackFile) void {}
66     // pub fn getObject(self: *PackFile, id: Id) Object {}
67 };
68 const Repo = struct {
69     alloc: Alloc,
70     dir: std.fs.Dir,
71     packfile: PackFile,
72
73     pub fn open(alloc: Alloc, path: []const u8) !Repo {
74         const dir = try std.fs.cwd().openDir(path, .{});
75
76         const packfile = try PackFile.open(alloc, dir);
77
78         return .{
79             .alloc = alloc,
80             .dir = dir,
81             .packfile = packfile,
82         };
83     }
84     pub fn close(self: *Repo) void {
85         self.dir.close();
86     }
87     pub fn getHead(self: *Repo) !Id {
88         // read file HEAD
89         const head = try self.dir.readFileAlloc(self.alloc, "HEAD", 1024);
90         defer self.alloc.free(head);
91
92         // read file pointed at by HEAD
93         const headPath = head[5 .. head.len - 1];
94         var idBuffer: [40]u8 = undefined;
95         const idStr = try self.dir.readFile(headPath, &idBuffer);
96
97         // parse id from file
98         return try std.fmt.parseUnsigned(u160, idStr, 16);
99     }
100     // pub fn getObject(self: *Repo, id: Id) Object {}
101 };
102
103 test "print HEAD" {
104     var repo = try Repo.open(std.testing.allocator, "../microwindows/.git");
105     defer repo.close();
106
107     const head = try repo.getHead();
108
109     std.debug.print("HEAD: {}\n", .{head});
110 }
111
112 // test "list commits" {
113 //     var repo = Repo.open(std.testing.allocator, "../microwindows/.git");
114 //     defer repo.close();
115
116 //     const head = repo.getObject(repo.head);
117 //     defer head.deinit();
118
119 //     var c = head.getCommit();
120 //     for (0..3) |_| {
121 //         std.debug.print("{}\n", .{c});
122 //         c = c.parent;
123 //     }
124 // }
125
126 // test "tree" {
127 //     var repo = Repo.open(std.testing.allocator, "../microwindows/.git");
128 //     defer repo.close();
129
130 //     const head = repo.getObject(repo.head);
131 //     defer head.deinit();
132
133 //     const commit = head.getCommit();
134
135 //     std.debug.print("{}\n", .{commit.tree});
136 // }
137
138 // test "blob" {
139 //     var repo = Repo.open(std.testing.allocator, "../microwindows/.git");
140 //     defer repo.close();
141
142 //     const head = repo.getObject(repo.head);
143 //     defer head.deinit();
144
145 //     const commit = head.getCommit();
146 //     const blob = repo.getBlob(commit.files[0].id);
147
148 //     std.debug.print("{}\n", .{blob});
149 // }