Skip to content

Commit 75d22c7

Browse files
committed
feat: version info
1 parent 9062d05 commit 75d22c7

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/lipc/StringHandler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22
#include "lipc.h"
33
#include "lipc/IHandler.h"
4-
#include <cstdint>
54
#include <cstdio>
65
#include <cstdlib>
76
#include <cstring>
@@ -16,7 +15,7 @@ class LIPCString {
1615
LIPCString(char *value, void *data) : value(value), data(data) {
1716
}
1817
LIPCcode check(std::string value) {
19-
if (value.length() + 1 > *(int *)data) {
18+
if (value.length() + 1 > *(size_t *)data) {
2019
*(int *)data = value.length() + 1;
2120
return LIPC_ERROR_BUFFER_TOO_SMALL;
2221
}

src/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "lipc.h"
22
#include "lipc/StringHandler.h"
3+
#include <format>
34
#include <getopt.h>
45
#include <signal.h>
56
#include <stdio.h>
@@ -14,6 +15,7 @@
1415
#include <stdexcept>
1516
#include <array>
1617
#include <cstdio>
18+
#include "version.h"
1719

1820
#define APP_PREFIX "com.kindlemodding.utild"
1921
namespace utild {
@@ -142,6 +144,29 @@ int main(int argc, char *argv[]) {
142144
return value->set(_this->getData().empty() ? "No output yet." : _this->getData().c_str());
143145
})->subscribe(handle);
144146

147+
utild::lipc::StringHandler<std::nullptr_t> info_handler("info");
148+
149+
info_handler
150+
.setGetter([](utild::lipc::StringHandler<std::nullptr_t> *_this, LIPC *_lipc, utild::lipc::LIPCString* value) -> LIPCcode {
151+
152+
std::string dirty_status = std::string(GIT_IS_DIRTY).empty() ? "" : " (dirty)";
153+
154+
// The std::format string combining all the information
155+
// Note: We combine GIT_CURRENT_SHORT and dirty_status before passing to format
156+
// because std::format doesn't directly support conditional formatting within the string.
157+
std::string formatted_info = std::format(
158+
"Build Info: Branch: {}, Commit: {}{}, Built On: {}",
159+
GIT_BRANCH_NAME,
160+
GIT_CURRENT_SHORT,
161+
dirty_status,
162+
BUILD_TIME
163+
);
164+
return value->set(formatted_info);
165+
})->subscribe(handle);
166+
167+
168+
169+
145170
while (utild::keep_running) {
146171
sleep(1);
147172
}

src/meson.build

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
11
sources = files('main.cpp')
22

3+
git_current_short = run_command(['git', 'rev-parse', '--short', 'HEAD'], check : false)
4+
if git_current_short.returncode() == 0
5+
git_current_short = git_current_short.stdout().strip()
6+
else
7+
message('Warning: Git current short could not be determined. Using "unknown".')
8+
git_current_short = 'unknown'
9+
endif
10+
11+
git_branch_name = run_command(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], check : false)
12+
if git_branch_name.returncode() == 0
13+
git_branch_name = git_branch_name.stdout().strip()
14+
else
15+
# Handle cases where git is not available or not a git repository
16+
message('Warning: Git branch could not be determined. Using "unknown".')
17+
git_branch_name = 'unknown'
18+
endif
19+
20+
build_time = run_command(['date', '+%Y-%m-%d %H:%M:%S'], check : false)
21+
if build_time.returncode() == 0
22+
build_time = build_time.stdout().strip()
23+
else
24+
message('Warning: Build time could not be determined. Using "unknown".')
25+
build_time = 'unknown'
26+
endif
27+
28+
git_is_dirty = run_command(['git', 'status', '--porcelain'], check : false)
29+
if git_is_dirty.returncode() == 0
30+
# Command ran successfully. Check if its standard output is empty.
31+
if git_is_dirty.stdout().strip() == ''
32+
# Output is empty, meaning the Git repository is clean.
33+
git_is_dirty = ''
34+
else
35+
# Output is NOT empty, meaning there are uncommitted changes (dirty).
36+
git_is_dirty = '1'
37+
endif
38+
else
39+
message('Warning: Git is not available or not a git repository. Assuming clean.')
40+
git_is_dirty = ''
41+
endif
42+
43+
configure_file(
44+
input : 'version.h.in',
45+
output : 'version.h',
46+
configuration : {'GIT_BRANCH_NAME': git_branch_name, 'GIT_CURRENT_SHORT': git_current_short, 'BUILD_TIME': build_time, 'GIT_IS_DIRTY': git_is_dirty}
47+
)
348
executable('utild', sources, dependencies: [lipc_dep], cpp_args: ['-static-libstdc++'], link_args: ['-static-libstdc++'])

src/version.h.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
#pragma once
3+
4+
#define GIT_BRANCH_NAME "@GIT_BRANCH_NAME@"
5+
6+
#define GIT_CURRENT_SHORT "@GIT_CURRENT_SHORT@"
7+
8+
#define BUILD_TIME "@BUILD_TIME@"
9+
10+
#define GIT_IS_DIRTY "@GIT_IS_DIRTY@"

0 commit comments

Comments
 (0)