authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 23:22:51 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 23:22:51 -07:00
logef17e36eaa82709593a80634cd345ebfa6fac1c8
treebd72685e91bd1b30354f96fdb2f2acd2e82d02fc
parentdf4919e65aef4364e714ccb38177717186f5917f

add extra Id types for more type safety


1 files changed, 8 insertions(+), 5 deletions(-)

git.zig+8-5
......@@ -3,6 +3,9 @@ const string = []const u8;
33
44// 40 is length of sha1 hash
55pub const Id = *const [40]u8;
6pub const TreeId = struct { id: Id };
7pub const CommitId = struct { id: Id };
8pub const BlobId = struct { id: Id };
69
710/// Returns the result of running `git rev-parse HEAD`
811/// dir must already be pointing at the .git folder
......@@ -35,7 +38,7 @@ pub fn getObject(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !string {
3538pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
3639 var iter = std.mem.split(u8, commitfile, "\n");
3740 var result: Commit = undefined;
38 var parents = std.ArrayList(Id).init(alloc);
41 var parents = std.ArrayList(CommitId).init(alloc);
3942 errdefer parents.deinit();
4043 while (true) {
4144 const line = iter.next().?;
......@@ -43,10 +46,10 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
4346 const space = std.mem.indexOfScalar(u8, line, ' ').?;
4447 const k = line[0..space];
4548
46 if (std.mem.eql(u8, k, "tree")) result.tree = line[space + 1 ..][0..40];
49 if (std.mem.eql(u8, k, "tree")) result.tree = .{ .id = line[space + 1 ..][0..40] };
4750 if (std.mem.eql(u8, k, "author")) result.author = line[space + 1 ..];
4851 if (std.mem.eql(u8, k, "committer")) result.committer = line[space + 1 ..];
49 if (std.mem.eql(u8, k, "parent")) try parents.append(line[space + 1 ..][0..40]);
52 if (std.mem.eql(u8, k, "parent")) try parents.append(.{ .id = line[space + 1 ..][0..40] });
5053 }
5154 result.parents = try parents.toOwnedSlice();
5255 result.message = iter.rest();
......@@ -54,8 +57,8 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
5457}
5558
5659pub const Commit = struct {
57 tree: Id,
58 parents: []const Id,
60 tree: TreeId,
61 parents: []const CommitId,
5962 author: string,
6063 committer: string,
6164 gpgsig: string,