authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-07-05 17:54:31 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-07-05 17:54:31 -07:00
log75962d6240a3c8580c8a04262436a3caea793774
tree80e879f49da44b22503cb2fc9f07f84619f01459
parente5ed412abe39e3244f7c44c797c2acd6addcfd6a

getHEAD: return null when repo has no commits


1 files changed, 2 insertions(+), 2 deletions(-)

git.zig+2-2
...@@ -26,7 +26,7 @@ pub fn version(alloc: std.mem.Allocator) !string {...@@ -26,7 +26,7 @@ pub fn version(alloc: std.mem.Allocator) !string {
26/// Returns the result of running `git rev-parse HEAD`26/// Returns the result of running `git rev-parse HEAD`
27/// dir must already be pointing at the .git folder27/// dir must already be pointing at the .git folder
28// TODO this doesnt handle when there are 0 commits28// TODO this doesnt handle when there are 0 commits
29pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {29pub 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:")) {
...@@ -54,7 +54,7 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {...@@ -54,7 +54,7 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !CommitId {
54 if (std.mem.eql(u8, h[5..], ref)) return ensureObjId(CommitId, objid);54 if (std.mem.eql(u8, h[5..], ref)) return ensureObjId(CommitId, objid);
55 }55 }
56 }56 }
57 unreachable;57 return null;
58 }58 }
5959
60 return ensureObjId(CommitId, h);60 return ensureObjId(CommitId, h);