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");...@@ -5,6 +5,7 @@ const time = @import("time");
5const extras = @import("extras");5const extras = @import("extras");
6const tracer = @import("tracer");6const tracer = @import("tracer");
7const nfs = @import("nfs");7const nfs = @import("nfs");
8const nio = @import("nio");
89
9pub const Id = []const u8;10pub const Id = []const u8;
1011
...@@ -109,6 +110,28 @@ pub fn getObject(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !string {...@@ -109,6 +110,28 @@ pub fn getObject(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !string {
109 const t = tracer.trace(@src(), " {s}", .{obj});110 const t = tracer.trace(@src(), " {s}", .{obj});
110 defer t.end();111 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
112 const result = try std.process.Child.run(.{135 const result = try std.process.Child.run(.{
113 .allocator = alloc,136 .allocator = alloc,
114 .cwd_dir = dir.to_std(),137 .cwd_dir = dir.to_std(),
zig.mod+1
...@@ -8,6 +8,7 @@ dependencies:...@@ -8,6 +8,7 @@ dependencies:
8 - src: git https://github.com/nektro/zig-extras8 - src: git https://github.com/nektro/zig-extras
9 - src: git https://github.com/nektro/zig-tracer9 - src: git https://github.com/nektro/zig-tracer
10 - src: git https://github.com/nektro/zig-nfs10 - src: git https://github.com/nektro/zig-nfs
11 - src: git https://github.com/nektro/zig-nio
11root_dependencies:12root_dependencies:
12 - src: git https://github.com/nektro/zig-extras13 - src: git https://github.com/nektro/zig-extras
13 - src: git https://github.com/nektro/zig-expect14 - src: git https://github.com/nektro/zig-expect