Skip to content

Commit 08e99a3

Browse files
Merge pull request #445 from karlseguin/capture_git_commit
Make the the short git SHA available within the program
2 parents 488c7e6 + d0ba06c commit 08e99a3

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- uses: ./.github/actions/install
2929

3030
- name: zig build
31-
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8 -Dcpu=x86_64
31+
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8 -Dcpu=x86_64 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
3232

3333
- name: Rename binary
3434
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}
@@ -60,7 +60,7 @@ jobs:
6060
arch: ${{env.ARCH}}
6161

6262
- name: zig build
63-
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8
63+
run: zig build --release=safe -Doptimize=ReleaseSafe -Dengine=v8 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
6464

6565
- name: Rename binary
6666
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ download-zig:
5959
## Build in release-safe mode
6060
build:
6161
@printf "\e[36mBuilding (release safe)...\e[0m\n"
62-
@$(ZIG) build -Doptimize=ReleaseSafe -Dengine=v8 || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
62+
$(ZIG) build -Doptimize=ReleaseSafe -Dengine=v8 -Dgit_commit=$$(git rev-parse --short HEAD) || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
6363
@printf "\e[33mBuild OK\e[0m\n"
6464

6565
## Build in debug mode
6666
build-dev:
6767
@printf "\e[36mBuilding (debug)...\e[0m\n"
68-
@$(ZIG) build -Dengine=v8 || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
68+
@$(ZIG) build -Dengine=v8 -Dgit_commit=$$(git rev-parse --short HEAD) || (printf "\e[33mBuild ERROR\e[0m\n"; exit 1;)
6969
@printf "\e[33mBuild OK\e[0m\n"
7070

7171
## Run the server in debug mode

build.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ pub fn build(b: *std.Build) !void {
5858
.optimize = mode,
5959
});
6060
try common(b, exe, options);
61+
{
62+
var opt = b.addOptions();
63+
opt.addOption(
64+
[]const u8,
65+
"git_commit",
66+
b.option([]const u8, "git_commit", "Current git commit") orelse "dev",
67+
);
68+
exe.root_module.addImport("build_info", opt.createModule());
69+
}
6170
b.installArtifact(exe);
6271

6372
// run

src/main.zig

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ pub fn main() !void {
5959

6060
switch (args.mode) {
6161
.help => args.printUsageAndExit(args.mode.help),
62+
.version => {
63+
std.debug.print("{s}\n", .{@import("build_info").git_commit});
64+
return std.process.cleanExit();
65+
},
6266
.serve => |opts| {
6367
const address = std.net.Address.parseIp4(opts.host, opts.port) catch |err| {
6468
log.err("address (host:port) {any}\n", .{err});
@@ -125,12 +129,14 @@ const Command = struct {
125129
help,
126130
fetch,
127131
serve,
132+
version,
128133
};
129134

130135
const Mode = union(ModeType) {
131136
help: bool, // false when being printed because of an error
132137
fetch: Fetch,
133138
serve: Serve,
139+
version: void,
134140
};
135141

136142
const Serve = struct {
@@ -172,10 +178,13 @@ const Command = struct {
172178
\\--timeout Inactivity timeout in seconds before disconnecting clients
173179
\\ Defaults to 3 (seconds)
174180
\\
181+
\\version command
182+
\\Displays the version of {s}
183+
\\
175184
\\help command
176185
\\Displays this message
177186
;
178-
std.debug.print(usage, .{ self.exec_name, self.exec_name, self.exec_name });
187+
std.debug.print(usage, .{ self.exec_name, self.exec_name, self.exec_name, self.exec_name });
179188
if (success) {
180189
return std.process.cleanExit();
181190
}
@@ -210,9 +219,10 @@ fn parseArgs(allocator: Allocator) !Command {
210219
};
211220

212221
cmd.mode = switch (mode) {
213-
.help => Command.Mode{ .help = true },
214-
.serve => Command.Mode{ .serve = parseServeArgs(allocator, &args) catch return cmd },
215-
.fetch => Command.Mode{ .fetch = parseFetchArgs(allocator, &args) catch return cmd },
222+
.help => .{ .help = true },
223+
.serve => .{ .serve = parseServeArgs(allocator, &args) catch return cmd },
224+
.fetch => .{ .fetch = parseFetchArgs(allocator, &args) catch return cmd },
225+
.version => .{ .version = {} },
216226
};
217227
return cmd;
218228
}

0 commit comments

Comments
 (0)