authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-14 03:42:26 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-14 03:42:26 -07:00
logf1c193a7b0522bc268e0185b3e2b118ba90d0c95
treef1c960188fedb720109f71d183536d35532aae63
parentfb15bf5eb1ac5d4ced4717558401b27b481fa9a8
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

getObject: read .git/objects/ when available


2 files changed, 24 insertions(+), 0 deletions(-)

git.zig+23
......@@ -5,6 +5,7 @@ const time = @import("time");
55const extras = @import("extras");
66const tracer = @import("tracer");
77const nfs = @import("nfs");
8const nio = @import("nio");
89
910pub const Id = []const u8;
1011
......@@ -109,6 +110,28 @@ pub fn getObject(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !string {
109110 const t = tracer.trace(@src(), " {s}", .{obj});
110111 defer t.end();
111112
113 if (obj.len == 40) blk: { //sha1 object
114 var sub_path: [49:0]u8 = "objects/00/00000000000000000000000000000000000000".*;
115 @memcpy(sub_path[8..][0..2], obj[0..2]);
116 @memcpy(sub_path[11..], obj[2..]);
117 const objfile = dir.openFile(&sub_path, .{}) catch |err| switch (err) {
118 error.ENOENT => break :blk,
119 else => |e| return e,
120 };
121 defer objfile.close();
122 var bufr = nio.BufferedReader(4096, nfs.File).init(objfile);
123 var list = std.ArrayList(u8).init(alloc);
124 defer list.deinit();
125 try list.ensureUnusedCapacity(512);
126 try std.compress.flate.inflate.decompress(.zlib, bufr.anyReadable(), list.writer());
127 const header = list.items[0..std.mem.indexOfScalar(u8, list.items, 0).?];
128 const _type = header[0..std.mem.indexOfScalar(u8, header, ' ').?];
129 const content_len = try extras.parseDigits(u64, header[_type.len + 1 ..], 10);
130 std.debug.assert(list.items[header.len + 1 ..].len == content_len);
131 list.replaceRangeAssumeCapacity(0, header.len + 1, "");
132 return list.toOwnedSlice();
133 }
134
112135 const result = try std.process.Child.run(.{
113136 .allocator = alloc,
114137 .cwd_dir = dir.to_std(),
zig.mod+1
......@@ -8,6 +8,7 @@ dependencies:
88 - src: git https://github.com/nektro/zig-extras
99 - src: git https://github.com/nektro/zig-tracer
1010 - src: git https://github.com/nektro/zig-nfs
11 - src: git https://github.com/nektro/zig-nio
1112root_dependencies:
1213 - src: git https://github.com/nektro/zig-extras
1314 - src: git https://github.com/nektro/zig-expect