Skip to content

Commit 3472058

Browse files
init commit
0 parents  commit 3472058

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

commitinfo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7fc9f61f7badb3e37205316c9d6476aff612c71d

ext.manifest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name: "GitCommit"

gitrevision.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
local log = require("hg.log")
2+
3+
local M = {}
4+
5+
local COMMIT_INFO_PATH = "gitcommit/commitinfo"
6+
local REVISION_LENGTH = 10
7+
8+
local function read_commit_hash()
9+
local file, open_error = io.open(COMMIT_INFO_PATH, "r")
10+
11+
if not file then
12+
log.debug(
13+
string.format(
14+
"WARNING: Failed to open commit information file: %s (%s)",
15+
COMMIT_INFO_PATH,
16+
open_error or "unknown error"
17+
)
18+
)
19+
return nil
20+
end
21+
22+
local revision = file:read("*l")
23+
file:close()
24+
25+
if not revision or revision == "" then
26+
log.debug("WARNING: Commit information file is empty or unreadable: " .. COMMIT_INFO_PATH)
27+
return nil
28+
end
29+
30+
return revision:sub(1, REVISION_LENGTH)
31+
end
32+
33+
function M.apply_revision_override()
34+
local revision = read_commit_hash()
35+
36+
if not revision then
37+
log.debug("INFO: project.revision override will not be applied.")
38+
return
39+
end
40+
41+
log.debug("INFO: Detected commit hash: " .. revision .. ". Overriding sys.get_config.")
42+
43+
local original_get_config = sys.get_config
44+
45+
sys.get_config = function(key, default_value)
46+
if key == "project.revision" then
47+
return revision
48+
end
49+
return original_get_config(key, default_value)
50+
end
51+
52+
log.debug(
53+
"INFO: sys.get_config successfully overridden. project.revision is now: " .. sys.get_config("project.revision")
54+
)
55+
end
56+
57+
return M

src/gitcommit.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#define LIB_NAME "GitCommit"
2+
#define MODULE_NAME "gitcommit"
3+
4+
#include <dmsdk/sdk.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <assert.h>
9+
10+
static char s_commit_hash[11] = {0};
11+
static const char* COMMIT_FILE_PATH = "gitcommit/commitinfo";
12+
13+
static void RetrieveGitCommit() {
14+
#if !defined(_WIN32) && !defined(_WIN64)
15+
system("mkdir -p gitcommit");
16+
#endif
17+
char cmd[256];
18+
#if defined(_WIN32) || defined(_WIN64)
19+
snprintf(cmd, sizeof(cmd),
20+
"git rev-parse HEAD > \"%s\" 2>nul",
21+
COMMIT_FILE_PATH);
22+
#else
23+
snprintf(cmd, sizeof(cmd),
24+
"git rev-parse HEAD > \"%s\" 2>/dev/null",
25+
COMMIT_FILE_PATH);
26+
#endif
27+
28+
system(cmd);
29+
30+
FILE* file = fopen(COMMIT_FILE_PATH, "r");
31+
if (!file) {
32+
return;
33+
}
34+
if (fgets(s_commit_hash, sizeof(s_commit_hash), file)) {
35+
char* nl = strchr(s_commit_hash, '\n');
36+
if (nl) *nl = '\0';
37+
}
38+
fclose(file);
39+
}
40+
41+
static int LuaGetCommit(lua_State* L) {
42+
DM_LUA_STACK_CHECK(L, 1);
43+
lua_pushstring(L, s_commit_hash);
44+
return 1;
45+
}
46+
47+
static const luaL_Reg Module_methods[] = {
48+
{"get_commit", LuaGetCommit},
49+
{0,0}
50+
};
51+
52+
static void LuaInit(lua_State* L) {
53+
int top = lua_gettop(L);
54+
luaL_register(L, MODULE_NAME, Module_methods);
55+
lua_pop(L, 1);
56+
assert(top == lua_gettop(L));
57+
}
58+
59+
dmExtension::Result AppInitializeGitCommit(dmExtension::AppParams*) {
60+
return dmExtension::RESULT_OK;
61+
}
62+
63+
dmExtension::Result InitializeGitCommit(dmExtension::Params* params) {
64+
RetrieveGitCommit();
65+
LuaInit(params->m_L);
66+
return dmExtension::RESULT_OK;
67+
}
68+
69+
dmExtension::Result AppFinalizeGitCommit(dmExtension::AppParams*) {
70+
return dmExtension::RESULT_OK;
71+
}
72+
73+
dmExtension::Result FinalizeGitCommit(dmExtension::Params*) {
74+
return dmExtension::RESULT_OK;
75+
}
76+
77+
DM_DECLARE_EXTENSION(GitCommit, LIB_NAME,
78+
AppInitializeGitCommit, AppFinalizeGitCommit,
79+
InitializeGitCommit, 0, 0, FinalizeGitCommit)

0 commit comments

Comments
 (0)