From cd870a26189555cfa57dd40a95de5704a9fa1dce Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 25 Jun 2026 15:55:25 -0700 Subject: [PATCH] getHEAD: handle zero commits --- git.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/git.zig b/git.zig index 18431d0c642a4e9c318d8136326abc824e51c151..f592fef0082b87ccd1cf07c3de8e5c436fcedd08 100644 --- a/git.zig +++ b/git.zig @@ -71,12 +71,14 @@ pub fn version(alloc: std.mem.Allocator) !string { /// Returns the result of running `git rev-parse HEAD` /// dir must already be pointing at the .git folder -// TODO this doesnt handle when there are 0 commits pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { const t = tracer.trace(@src(), "", .{}); defer t.end(); - const headfile = try dir.readFileAlloc(alloc, "HEAD", 1024); + const headfile = dir.readFileAlloc(alloc, "HEAD", 1024) catch |err| switch (err) { + error.ENOENT => return null, + else => |e| return e, + }; defer alloc.free(headfile); const h = std.mem.trimEnd(u8, headfile, "\n"); -- 2.54.0