git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commit28c2d907abb66eba11e64e7e63aaa250b1432652
parent6814af9345
authorIsaac Freund <[email protected]>
date2022-05-16 22:34
build: follow semantic versioning for -dev versions

The new version format is 0.2.0-dev.76+d1cf95b which is compliant to the
semantic versioning 2.0.0 spec and sorts better than the old format.

 build.zig | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/build.zig b/build.zig
index f8cf3bf..5de182b 100644
--- a/build.zig
+++ b/build.zig
@@ -55,15 +55,24 @@ pub fn build(b: *zbs.Builder) !void {
     const full_version = blk: {
         if (mem.endsWith(u8, version, "-dev")) {
             var ret: u8 = undefined;
-            const git_dir = try fs.path.join(b.allocator, &[_][]const u8{ b.build_root, ".git" });
-            const git_commit_hash = b.execAllowFail(
-                &[_][]const u8{ "git", "--git-dir", git_dir, "--work-tree", b.build_root, "rev-parse", "--short", "HEAD" },
+
+            const git_describe_long = b.execAllowFail(
+                &[_][]const u8{ "git", "-C", b.build_root, "describe", "--long" },
                 &ret,
                 .Inherit,
             ) catch break :blk version;
-            break :blk try std.fmt.allocPrintZ(b.allocator, "{s}-{s}", .{
-                version,
-                mem.trim(u8, git_commit_hash, &std.ascii.spaces),
+
+            var it = mem.split(u8, mem.trim(u8, git_describe_long, &std.ascii.spaces), "-");
+            _ = it.next().?; // previous tag
+            const commit_count = it.next().?;
+            const commit_hash = it.next().?;
+            assert(it.next() == null);
+            assert(commit_hash[0] == 'g');
+
+            // Follow semantic versioning, e.g. 0.2.0-dev.42+d1cf95b
+            break :blk try std.fmt.allocPrintZ(b.allocator, version ++ ".{s}+{s}", .{
+                commit_count,
+                commit_hash[1..],
             });
         } else {
             break :blk version;