From a8599c3bdc5986cbf0f66f116723694b79241c26 Mon Sep 17 00:00:00 2001 From: Matt Robenolt Date: Thu, 12 Sep 2024 13:06:16 -0700 Subject: [PATCH] build: fix version detection if there is no .git folder available This path already handles if `git`, the binary, is missing. But we should also check if this is building from a git checkout, hence having a .git/ folder in the root. This would happen when building from a source tarball for example. --- src/build/Version.zig | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/build/Version.zig b/src/build/Version.zig index df6585a8e..42467a68b 100644 --- a/src/build/Version.zig +++ b/src/build/Version.zig @@ -19,8 +19,18 @@ branch: []const u8, pub fn detect(b: *std.Build) !Version { // Execute a bunch of git commands to determine the automatic version. var code: u8 = 0; + + const root_path = b.build_root.path orelse "."; + + // Check that we're within a git checkout with a .git folder, if not, bail early. + const git_path = try std.fs.path.join(b.allocator, &[_][]const u8{ root_path, ".git" }); + std.fs.cwd().access(git_path, .{}) catch |err| switch (err) { + error.FileNotFound => return error.GitNotFound, + else => return err, + }; + const branch: []const u8 = b.runAllowFail( - &[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "rev-parse", "--abbrev-ref", "HEAD" }, + &[_][]const u8{ "git", "-C", root_path, "rev-parse", "--abbrev-ref", "HEAD" }, &code, .Ignore, ) catch |err| switch (err) { @@ -30,7 +40,7 @@ pub fn detect(b: *std.Build) !Version { const short_hash = short_hash: { const output = b.runAllowFail( - &[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "log", "--pretty=format:%h", "-n", "1" }, + &[_][]const u8{ "git", "-C", root_path, "log", "--pretty=format:%h", "-n", "1" }, &code, .Ignore, ) catch |err| switch (err) { @@ -42,7 +52,7 @@ pub fn detect(b: *std.Build) !Version { }; const tag = b.runAllowFail( - &[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "describe", "--exact-match", "--tags" }, + &[_][]const u8{ "git", "-C", root_path, "describe", "--exact-match", "--tags" }, &code, .Ignore, ) catch |err| switch (err) { @@ -54,7 +64,7 @@ pub fn detect(b: *std.Build) !Version { _ = b.runAllowFail(&[_][]const u8{ "git", "-C", - b.build_root.path orelse ".", + root_path, "diff", "--quiet", "--exit-code",