From 96b98513b322003058dff06b6d23c11b92e02840 Mon Sep 17 00:00:00 2001 From: Ne_Eo Date: Sat, 30 Nov 2024 00:34:16 +0100 Subject: [PATCH] Added more stuff to git commit macro --- .../backend/system/macros/GitCommitMacro.hx | 101 +++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/source/funkin/backend/system/macros/GitCommitMacro.hx b/source/funkin/backend/system/macros/GitCommitMacro.hx index c03988ce..11f32152 100644 --- a/source/funkin/backend/system/macros/GitCommitMacro.hx +++ b/source/funkin/backend/system/macros/GitCommitMacro.hx @@ -8,11 +8,27 @@ class GitCommitMacro { /** * Returns the current commit number */ - public static var commitNumber(get, null):Int; + public static var commitNumber(get, never):Int; /** * Returns the current commit hash */ - public static var commitHash(get, null):String; + public static var commitHash(get, never):String; + /** + * Returns the current commit hash in long format + */ + public static var commitHashLong(get, never):String; + /** + * Returns the current commit message + */ + public static var commitMessage(get, never):String; + /** + * Returns the current commit author + */ + public static var commitAuthor(get, never):String; + /** + * Returns the current commit date + time + **/ + public static var commitDate(get, never):Date; // GETTERS #if REGION @@ -21,6 +37,15 @@ class GitCommitMacro { private static inline function get_commitHash() return __getCommitHash(); + private static inline function get_commitHashLong() + return __getCommitHashLong(); + + private static inline function get_commitMessage() + return __getCommitMessage(); + private static inline function get_commitAuthor() + return __getCommitAuthor(); + private static inline function get_commitDate() + return __getCommitDate(); #end // INTERNAL MACROS @@ -52,5 +77,77 @@ class GitCommitMacro { return macro $v{0} #end } + + private static macro function __getCommitHashLong() { + #if display + return macro $v{"-"}; + #else + try { + var proc = new Process('git', ['rev-parse', 'HEAD'], false); + proc.exitCode(true); + + return macro $v{proc.stdout.readLine()}; + } catch(e) {} + return macro $v{"-"} + #end + } + + private static macro function __getCommitMessage() { + #if display + return macro $v{"-"}; + #else + try { + var proc = new Process('git', ['log', '-1', '--pretty=%B'], false); + proc.exitCode(true); + var all = proc.stdout.readAll().toString(); + while(StringTools.endsWith(all, "\r\n")) all = all.substr(0, all.length - 2); + while(StringTools.endsWith(all, "\n")) all = all.substr(0, all.length - 1); + while(StringTools.endsWith(all, "\r")) all = all.substr(0, all.length - 1); + + return macro $v{all}; + } catch(e) {} + return macro $v{"-"} + #end + } + + private static macro function __getCommitAuthor() { + #if display + return macro $v{"-"}; + #else + try { + var proc = new Process('git', ['log', '-1', '--pretty=%an'], false); + proc.exitCode(true); + + return macro $v{StringTools.trim(proc.stdout.readLine())}; + } catch(e) {} + return macro $v{"-"} + #end + } + + private static macro function __getCommitDate() { + #if display + return macro $v{Date.now()}; + #else + try { + // get the time in UTC format + var oldTz = Sys.getEnv("TZ"); + Sys.putEnv("TZ", "UTC0"); + var proc = new Process('git', ['log', "--date='format-local:%Y-%m-%dT%H:%M:%SZ'", '-1', '--pretty=%cd'], false); + proc.exitCode(true); + Sys.putEnv("TZ", oldTz); + + var rawDate = StringTools.trim(proc.stdout.readLine()); + + try { + Date.fromString(rawDate); + } catch(e) { + throw 'Invalid date format: $rawDate'; + } + + return macro Date.fromString($v{rawDate}); + } catch(e) {} + return macro Date.fromString("1970-01-01T00:00:00"); // failed to parse date + #end + } #end } \ No newline at end of file