|
| 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