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;...@@ -3,6 +3,9 @@ const string = []const u8;
33
4// 40 is length of sha1 hash4// 40 is length of sha1 hash
5pub const Id = *const [40]u8;5pub const Id = *const [40]u8;
6pub const TreeId = struct { id: Id };
7pub const CommitId = struct { id: Id };
8pub const BlobId = struct { id: Id };
69
7/// Returns the result of running `git rev-parse HEAD`10/// Returns the result of running `git rev-parse HEAD`
8/// dir must already be pointing at the .git folder11/// 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 {...@@ -35,7 +38,7 @@ pub fn getObject(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !string {
35pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {38pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
36 var iter = std.mem.split(u8, commitfile, "\n");39 var iter = std.mem.split(u8, commitfile, "\n");
37 var result: Commit = undefined;40 var result: Commit = undefined;
38 var parents = std.ArrayList(Id).init(alloc);41 var parents = std.ArrayList(CommitId).init(alloc);
39 errdefer parents.deinit();42 errdefer parents.deinit();
40 while (true) {43 while (true) {
41 const line = iter.next().?;44 const line = iter.next().?;
...@@ -43,10 +46,10 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {...@@ -43,10 +46,10 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
43 const space = std.mem.indexOfScalar(u8, line, ' ').?;46 const space = std.mem.indexOfScalar(u8, line, ' ').?;
44 const k = line[0..space];47 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] };
47 if (std.mem.eql(u8, k, "author")) result.author = line[space + 1 ..];50 if (std.mem.eql(u8, k, "author")) result.author = line[space + 1 ..];
48 if (std.mem.eql(u8, k, "committer")) result.committer = line[space + 1 ..];51 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] });
50 }53 }
51 result.parents = try parents.toOwnedSlice();54 result.parents = try parents.toOwnedSlice();
52 result.message = iter.rest();55 result.message = iter.rest();
...@@ -54,8 +57,8 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {...@@ -54,8 +57,8 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
54}57}
5558
56pub const Commit = struct {59pub const Commit = struct {
57 tree: Id,60 tree: TreeId,
58 parents: []const Id,61 parents: []const CommitId,
59 author: string,62 author: string,
60 committer: string,63 committer: string,
61 gpgsig: string,64 gpgsig: string,