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 {...@@ -30,9 +30,16 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {
30 const h = std.mem.trimRight(u8, try dir.readFileAlloc(alloc, "HEAD", 1024), "\n");30 const h = std.mem.trimRight(u8, try dir.readFileAlloc(alloc, "HEAD", 1024), "\n");
3131
32 if (std.mem.startsWith(u8, h, "ref:")) {32 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: {
34 const pckedrfs = dir.readFileAlloc(alloc, "packed-refs", 1024 * 1024 * 1024) catch |err| switch (err) {41 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,
36 else => |e| return e,43 else => |e| return e,
37 };44 };
38 var iter = std.mem.split(u8, pckedrfs, "\n");45 var iter = std.mem.split(u8, pckedrfs, "\n");
...@@ -44,11 +51,10 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {...@@ -44,11 +51,10 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {
44 const objid = jter.next().?;51 const objid = jter.next().?;
45 const ref = jter.next().?;52 const ref = jter.next().?;
46 std.debug.assert(jter.next() == null);53 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);
48 }55 }
49 break :blk std.mem.trimRight(u8, try dir.readFileAlloc(alloc, h[5..], 1024), "\n");56 }
50 };57 unreachable;
51 return ensureObjId(CommitId, r);
52 }58 }
5359
54 return ensureObjId(CommitId, h);60 return ensureObjId(CommitId, h);