authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-23 06:36:01 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-23 06:36:01 -07:00
logf06b9b6a9d5a3f467e68bf9a845e3f0df950baa0
tree4c28593bcb036c1e3ff4a6b59c2bb91eb0e9b8cc
parent8985b5209378008d783e72cd7e92c1680db536a6

getHEAD: refs folder takes priority over packed-refs file


1 files changed, 12 insertions(+), 6 deletions(-)

git.zig+12-6
......@@ -30,9 +30,16 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {
3030 const h = std.mem.trimRight(u8, try dir.readFileAlloc(alloc, "HEAD", 1024), "\n");
3131
3232 if (std.mem.startsWith(u8, h, "ref:")) {
33 const r = blk: {
33 blk: {
34 const reffile = dir.readFileAlloc(alloc, h[5..], 1024) catch |err| switch (err) {
35 error.FileNotFound => break :blk,
36 else => |e| return e,
37 };
38 return ensureObjId(CommitId, std.mem.trimRight(u8, reffile, "\n"));
39 }
40 blk: {
3441 const pckedrfs = dir.readFileAlloc(alloc, "packed-refs", 1024 * 1024 * 1024) catch |err| switch (err) {
35 error.FileNotFound => try std.fs.cwd().readFileAlloc(alloc, "/dev/null", 1024),
42 error.FileNotFound => break :blk,
3643 else => |e| return e,
3744 };
3845 var iter = std.mem.split(u8, pckedrfs, "\n");
......@@ -44,11 +51,10 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {
4451 const objid = jter.next().?;
4552 const ref = jter.next().?;
4653 std.debug.assert(jter.next() == null);
47 if (std.mem.eql(u8, h[5..], ref)) break :blk objid;
54 if (std.mem.eql(u8, h[5..], ref)) return ensureObjId(CommitId, objid);
4855 }
49 break :blk std.mem.trimRight(u8, try dir.readFileAlloc(alloc, h[5..], 1024), "\n");
50 };
51 return ensureObjId(CommitId, r);
56 }
57 unreachable;
5258 }
5359
5460 return ensureObjId(CommitId, h);