Skip to content

Commit

Permalink
build: build without Git, tag version as dev with 0 SHA
Browse files Browse the repository at this point in the history
Fixes #2170

Example now: `info: ghostty version=0.1.0-dev+0000000`
  • Loading branch information
mitchellh committed Aug 31, 2024
1 parent 602bf6b commit c99b0c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
13 changes: 12 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
27 changes: 24 additions & 3 deletions src/build/Version.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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{ "gitt", "-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,
};
Expand All @@ -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,
};
Expand Down

0 comments on commit c99b0c6

Please sign in to comment.