From 17db3179169f9733d0050054fcb37b6525c217f4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 31 Aug 2024 09:42:39 -0700 Subject: [PATCH] build: build without Git, tag version as dev with 0 SHA Fixes #2170 Example now: `info: ghostty version=0.1.0-dev+0000000` --- build.zig | 13 ++++++++++++- src/build/Version.zig | 27 ++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index bc829d59e..b448e583a 100644 --- a/build.zig +++ b/build.zig @@ -177,7 +177,18 @@ pub fn build(b: *std.Build) !void { config.version = if (version_string) |v| try std.SemanticVersion.parse(v) else version: { - const vsn = try Version.detect(b); + const vsn = Version.detect(b) catch |err| switch (err) { + // If Git isn't available we just make an unknown dev version. + error.GitNotFound => break :version .{ + .major = app_version.major, + .minor = app_version.minor, + .patch = app_version.patch, + .pre = "dev", + .build = "0000000", + }, + + else => return err, + }; if (vsn.tag) |tag| { // Tip releases behave just like any other pre-release so we skip. if (!std.mem.eql(u8, tag, "tip")) { diff --git a/src/build/Version.zig b/src/build/Version.zig index 6c1bbc717..df6585a8e 100644 --- a/src/build/Version.zig +++ b/src/build/Version.zig @@ -19,14 +19,34 @@ 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 branch = try b.runAllowFail(&[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "rev-parse", "--abbrev-ref", "HEAD" }, &code, .Ignore); + const branch: []const u8 = b.runAllowFail( + &[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "rev-parse", "--abbrev-ref", "HEAD" }, + &code, + .Ignore, + ) catch |err| switch (err) { + error.FileNotFound => return error.GitNotFound, + else => return err, + }; const short_hash = short_hash: { - const output = try b.runAllowFail(&[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "log", "--pretty=format:%h", "-n", "1" }, &code, .Ignore); + const output = b.runAllowFail( + &[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "log", "--pretty=format:%h", "-n", "1" }, + &code, + .Ignore, + ) catch |err| switch (err) { + error.FileNotFound => return error.GitNotFound, + else => return err, + }; + break :short_hash std.mem.trimRight(u8, output, "\r\n "); }; - const tag = b.runAllowFail(&[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "describe", "--exact-match", "--tags" }, &code, .Ignore) catch |err| switch (err) { + const tag = b.runAllowFail( + &[_][]const u8{ "git", "-C", b.build_root.path orelse ".", "describe", "--exact-match", "--tags" }, + &code, + .Ignore, + ) catch |err| switch (err) { + error.FileNotFound => return error.GitNotFound, error.ExitCodeFailure => "", // expected else => return err, }; @@ -39,6 +59,7 @@ pub fn detect(b: *std.Build) !Version { "--quiet", "--exit-code", }, &code, .Ignore) catch |err| switch (err) { + error.FileNotFound => return error.GitNotFound, error.ExitCodeFailure => {}, // expected else => return err, };