authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-09 13:41:04 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-09 13:41:04 -07:00
logbc26c340f602dd61cf3e84b3ba84e8d9f22ae327
treebfc2fb943ee4eb2022195885e52181774c33d406
parent1fc4dfc12f7cd3315cb4947608e62a1e63651e8f
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add OidKind and AnyId


1 files changed, 28 insertions(+), 23 deletions(-)

git.zig+28-23
......@@ -41,6 +41,26 @@ pub const TagId = struct {
4141 }
4242};
4343
44pub const OidKind = enum {
45 blob,
46 tree,
47 commit,
48 tag,
49};
50
51pub const AnyId = union(OidKind) {
52 blob: BlobId,
53 tree: TreeId,
54 commit: CommitId,
55 tag: TagId,
56
57 pub fn erase(self: AnyId) Id {
58 return switch (self) {
59 inline else => |v| v.id,
60 };
61 }
62};
63
4464pub fn version(alloc: std.mem.Allocator) !string {
4565 const result = try std.process.Child.run(.{
4666 .allocator = alloc,
......@@ -139,7 +159,7 @@ pub fn getObjectSize(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !u64 {
139159// TODO make this inspect .git manually
140160// https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
141161// https://git-scm.com/book/en/v2/Git-Internals-Packfiles
142pub fn isType(alloc: std.mem.Allocator, dir: nfs.Dir, maybeobj: Id, typ: Tree.Object.Id.Tag) !bool {
162pub fn isType(alloc: std.mem.Allocator, dir: nfs.Dir, maybeobj: Id, typ: OidKind) !bool {
143163 const t = tracer.trace(@src(), " {s} = {s} ?", .{ maybeobj, @tagName(typ) });
144164 defer t.end();
145165
......@@ -150,13 +170,13 @@ pub fn isType(alloc: std.mem.Allocator, dir: nfs.Dir, maybeobj: Id, typ: Tree.Ob
150170 });
151171 std.debug.assert(result.term == .Exited and result.term.Exited == 0);
152172 const output = std.mem.trimRight(u8, result.stdout, "\n");
153 return std.meta.stringToEnum(Tree.Object.Id.Tag, output).? == typ;
173 return std.meta.stringToEnum(OidKind, output).? == typ;
154174}
155175
156176// TODO make this inspect .git manually
157177// https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
158178// https://git-scm.com/book/en/v2/Git-Internals-Packfiles
159pub fn getType(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !Tree.Object.Id.Tag {
179pub fn getType(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !OidKind {
160180 const t = tracer.trace(@src(), " {s}", .{obj});
161181 defer t.end();
162182
......@@ -167,7 +187,7 @@ pub fn getType(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !Tree.Object.Id.
167187 });
168188 std.debug.assert(result.term == .Exited and result.term.Exited == 0);
169189 const output = std.mem.trimRight(u8, result.stdout, "\n");
170 return std.meta.stringToEnum(Tree.Object.Id.Tag, output) orelse @panic(output);
190 return std.meta.stringToEnum(OidKind, output) orelse @panic(output);
171191}
172192
173193// TODO make this inspect .git/objects manually
......@@ -311,18 +331,18 @@ pub fn parseTree(alloc: std.mem.Allocator, treefile: string) !Tree {
311331 }
312332 var jter = std.mem.splitScalar(u8, line, ' ');
313333 const mode = jter.next().?;
314 const otype = std.meta.stringToEnum(Tree.Object.Id.Tag, jter.next().?).?;
334 const otype = std.meta.stringToEnum(OidKind, jter.next().?).?;
315335 const id_and_name = jter.rest();
316336 const tab_pos = std.mem.indexOfScalar(u8, id_and_name, '\t').?; // why git. why.
317337 std.debug.assert(tab_pos == 40);
318338 const id = id_and_name[0..tab_pos][0..40];
319339 const name = id_and_name[tab_pos + 1 ..];
320340
321 inline for (std.meta.fields(Tree.Object.Id)) |item| {
341 inline for (std.meta.fields(AnyId)) |item| {
322342 if (std.mem.eql(u8, item.name, @tagName(otype))) {
323343 try children.append(.{
324344 .mode = try parseTreeMode(mode),
325 .id = @unionInit(Tree.Object.Id, item.name, item.type{ .id = id }),
345 .id = @unionInit(AnyId, item.name, item.type{ .id = id }),
326346 .name = name,
327347 });
328348 }
......@@ -381,24 +401,9 @@ pub const Tree = struct {
381401
382402 pub const Object = struct {
383403 mode: Mode,
384 id: @This().Id,
404 id: AnyId,
385405 name: string,
386406
387 pub const Id = union(enum) {
388 blob: BlobId,
389 tree: TreeId,
390 commit: CommitId,
391 tag: TagId,
392
393 pub const Tag = std.meta.Tag(@This());
394
395 pub fn erase(self: @This()) top.Id {
396 return switch (self) {
397 inline else => |v| v.id,
398 };
399 }
400 };
401
402407 pub const Mode = struct {
403408 type: Type,
404409 perm_user: Perm,