authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 20:22:39 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 20:22:39 -07:00
logee237b0a004e5b4ef11235f27092e605e7dbb938
tree26d4b6eb53adad9a49bc825d39ddc229383ceb4d
parentcc714914f69269db0c569ea416fa87142b388557

add Id type to differentiate objects from strings


1 files changed, 14 insertions(+), 9 deletions(-)

git.zig+14-9
......@@ -1,24 +1,29 @@
11const std = @import("std");
22const string = []const u8;
33
4// 40 is length of sha1 hash
5pub const Id = *const [40]u8;
6
47/// Returns the result of running `git rev-parse HEAD`
58/// dir must already be pointing at the .git folder
6pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !string {
9pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !Id {
710 const h = std.mem.trimRight(u8, try dir.readFileAlloc(alloc, "HEAD", 1024), "\n");
811
912 if (std.mem.startsWith(u8, h, "ref:")) {
10 return std.mem.trimRight(u8, try dir.readFileAlloc(alloc, h[5..], 1024), "\n");
13 const r = std.mem.trimRight(u8, try dir.readFileAlloc(alloc, h[5..], 1024), "\n");
14 std.debug.assert(r.len == 40);
15 return r[0..40];
1116 }
1217
1318 // content should be 40-char sha1 hash
1419 std.debug.assert(h.len == 40);
15 return h;
20 return h[0..40];
1621}
1722
1823// TODO make this inspect .git/objects
1924// https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
2025// https://git-scm.com/book/en/v2/Git-Internals-Packfiles
21pub fn getObject(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: string) !string {
26pub fn getObject(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !string {
2227 const result = try std.ChildProcess.exec(.{
2328 .allocator = alloc,
2429 .cwd_dir = dir,
......@@ -30,7 +35,7 @@ pub fn getObject(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: string) !string
3035pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
3136 var iter = std.mem.split(u8, commitfile, "\n");
3237 var result: Commit = undefined;
33 var parents = std.ArrayList(string).init(alloc);
38 var parents = std.ArrayList(Id).init(alloc);
3439 errdefer parents.deinit();
3540 while (true) {
3641 const line = iter.next().?;
......@@ -38,10 +43,10 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
3843 const space = std.mem.indexOfScalar(u8, line, ' ').?;
3944 const k = line[0..space];
4045
41 if (std.mem.eql(u8, k, "tree")) result.tree = line[space + 1 ..];
46 if (std.mem.eql(u8, k, "tree")) result.tree = line[space + 1 ..][0..40];
4247 if (std.mem.eql(u8, k, "author")) result.author = line[space + 1 ..];
4348 if (std.mem.eql(u8, k, "committer")) result.committer = line[space + 1 ..];
44 if (std.mem.eql(u8, k, "parent")) try parents.append(line[space + 1 ..]);
49 if (std.mem.eql(u8, k, "parent")) try parents.append(line[space + 1 ..][0..40]);
4550 }
4651 result.parents = try parents.toOwnedSlice();
4752 result.message = iter.rest();
......@@ -49,8 +54,8 @@ pub fn parseCommit(alloc: std.mem.Allocator, commitfile: string) !Commit {
4954}
5055
5156pub const Commit = struct {
52 tree: string,
53 parents: []const string,
57 tree: Id,
58 parents: []const Id,
5459 author: string,
5560 committer: string,
5661 gpgsig: string,