Merge pull request #2172 from ghostty-org/push-kpnkvvmzysom

build: build without Git, tag version as dev with 0 SHA
This commit is contained in:
Mitchell Hashimoto
2024-08-31 09:59:15 -07:00
committed by GitHub
2 changed files with 36 additions and 4 deletions

View File

@ -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")) {

View File

@ -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,
};