| ... | @@ -84,7 +84,9 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { | ... | @@ -84,7 +84,9 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { |
| 84 | const t = tracer.trace(@src(), "", .{}); | 84 | const t = tracer.trace(@src(), "", .{}); |
| 85 | defer t.end(); | 85 | defer t.end(); |
| 86 | | 86 | |
| 87 | const h = std.mem.trimEnd(u8, try dir.readFileAlloc(alloc, "HEAD", 1024), "\n"); | 87 | const headfile = try dir.readFileAlloc(alloc, "HEAD", 1024); |
| | 88 | defer alloc.free(headfile); |
| | 89 | const h = std.mem.trimEnd(u8, headfile, "\n"); |
| 88 | | 90 | |
| 89 | if (std.mem.startsWith(u8, h, "ref:")) { | 91 | if (std.mem.startsWith(u8, h, "ref:")) { |
| 90 | blk: { | 92 | blk: { |
| ... | @@ -95,13 +97,15 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { | ... | @@ -95,13 +97,15 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { |
| 95 | error.ENOENT => break :blk, | 97 | error.ENOENT => break :blk, |
| 96 | else => |e| return e, | 98 | else => |e| return e, |
| 97 | }; | 99 | }; |
| 98 | return ensureObjId(CommitId, std.mem.trimEnd(u8, reffile, "\n")); | 100 | defer alloc.free(reffile); |
| | 101 | return ensureObjId(CommitId, try alloc.dupe(u8, std.mem.trimEnd(u8, reffile, "\n"))); |
| 99 | } | 102 | } |
| 100 | blk: { | 103 | blk: { |
| 101 | const pckedrfs = dir.readFileAlloc(alloc, "packed-refs", 1024 * 1024 * 1024) catch |err| switch (err) { | 104 | const pckedrfs = dir.readFileAlloc(alloc, "packed-refs", 1024 * 1024 * 1024) catch |err| switch (err) { |
| 102 | error.ENOENT => break :blk, | 105 | error.ENOENT => break :blk, |
| 103 | else => |e| return e, | 106 | else => |e| return e, |
| 104 | }; | 107 | }; |
| | 108 | defer alloc.free(pckedrfs); |
| 105 | var iter = std.mem.splitScalar(u8, pckedrfs, '\n'); | 109 | var iter = std.mem.splitScalar(u8, pckedrfs, '\n'); |
| 106 | while (iter.next()) |line| { | 110 | while (iter.next()) |line| { |
| 107 | if (std.mem.startsWith(u8, line, "#")) continue; | 111 | if (std.mem.startsWith(u8, line, "#")) continue; |
| ... | @@ -111,13 +115,13 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { | ... | @@ -111,13 +115,13 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { |
| 111 | const objid = jter.next().?; | 115 | const objid = jter.next().?; |
| 112 | const ref = jter.next().?; | 116 | const ref = jter.next().?; |
| 113 | std.debug.assert(jter.next() == null); | 117 | std.debug.assert(jter.next() == null); |
| 114 | if (std.mem.eql(u8, h[5..], ref)) return ensureObjId(CommitId, objid); | 118 | if (std.mem.eql(u8, h[5..], ref)) return ensureObjId(CommitId, try alloc.dupe(u8, objid)); |
| 115 | } | 119 | } |
| 116 | } | 120 | } |
| 117 | return null; | 121 | return null; |
| 118 | } | 122 | } |
| 119 | | 123 | |
| 120 | return ensureObjId(CommitId, h); | 124 | return ensureObjId(CommitId, try alloc.dupe(u8, h)); |
| 121 | } | 125 | } |
| 122 | | 126 | |
| 123 | // 40 is length of sha1 hash | 127 | // 40 is length of sha1 hash |