From 73baabfa826a612db5ede26ed9eea099c3efdf90 Mon Sep 17 00:00:00 2001
From: EliteMasterEric <ericmyllyoja@gmail.com>
Date: Wed, 1 May 2024 13:29:54 -0400
Subject: [PATCH] Detect if your local Git has been modified and display it.

---
 source/funkin/util/Constants.hx            |  4 +++-
 source/funkin/util/logging/CrashHandler.hx |  2 +-
 source/funkin/util/macro/GitCommit.hx      | 27 ++++++++++++++++++++++
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/source/funkin/util/Constants.hx b/source/funkin/util/Constants.hx
index f79c99db9..c50f17697 100644
--- a/source/funkin/util/Constants.hx
+++ b/source/funkin/util/Constants.hx
@@ -46,7 +46,7 @@ class Constants
   #if (debug || FORCE_DEBUG_VERSION)
   static function get_VERSION():String
   {
-    return 'v${Application.current.meta.get('version')} (${GIT_BRANCH} : ${GIT_HASH})' + VERSION_SUFFIX;
+    return 'v${Application.current.meta.get('version')} (${GIT_BRANCH} : ${GIT_HASH}${GIT_HAS_LOCAL_CHANGES ? ' : MODIFIED' : ''})' + VERSION_SUFFIX;
   }
   #else
   static function get_VERSION():String
@@ -100,6 +100,8 @@ class Constants
    */
   public static final GIT_HASH:String = funkin.util.macro.GitCommit.getGitCommitHash();
 
+  public static final GIT_HAS_LOCAL_CHANGES:Bool = funkin.util.macro.GitCommit.getGitHasLocalChanges();
+
   /**
    * The current library versions, as provided by hmm.
    */
diff --git a/source/funkin/util/logging/CrashHandler.hx b/source/funkin/util/logging/CrashHandler.hx
index 240ad6fcf..610247e5b 100644
--- a/source/funkin/util/logging/CrashHandler.hx
+++ b/source/funkin/util/logging/CrashHandler.hx
@@ -120,7 +120,7 @@ class CrashHandler
     fullContents += '\n';
 
     fullContents += 'Generated by: ${Constants.GENERATED_BY}\n';
-    fullContents += '  Git hash: ${Constants.GIT_HASH}\n';
+    fullContents += '  Git hash: ${Constants.GIT_HASH} (${Constants.GIT_HAS_LOCAL_CHANGES ? 'MODIFIED' : 'CLEAN'})\n';
     fullContents += 'System timestamp: ${DateUtil.generateTimestamp()}\n';
     var driverInfo = FlxG?.stage?.context3D?.driverInfo ?? 'N/A';
     fullContents += 'Driver info: ${driverInfo}\n';
diff --git a/source/funkin/util/macro/GitCommit.hx b/source/funkin/util/macro/GitCommit.hx
index 86b8ebe68..5ca16e030 100644
--- a/source/funkin/util/macro/GitCommit.hx
+++ b/source/funkin/util/macro/GitCommit.hx
@@ -63,5 +63,32 @@ class GitCommit
     return macro $v{branchName};
     #end
   }
+
+  /**
+   * Get whether the local Git repository is dirty or not.
+   */
+  public static macro function getGitHasLocalChanges():haxe.macro.Expr.ExprOf<Bool>
+  {
+    #if !display
+    // Get the current line number.
+    var pos = haxe.macro.Context.currentPos();
+    var branchProcess = new sys.io.Process('git', ['status', '--porcelain']);
+
+    if (branchProcess.exitCode() != 0)
+    {
+      var message = branchProcess.stderr.readAll().toString();
+      haxe.macro.Context.info('[WARN] Could not determine current git commit; is this a proper Git repository?', pos);
+    }
+
+    var output:String = branchProcess.stdout.readLine();
+    trace('Git Status Output: ${output}');
+
+    // Generates a string expression
+    return macro $v{output.length > 0};
+    #else
+    // `#if display` is used for code completion. In this case we just assume true.
+    return macro $v{true};
+    #end
+  }
 }
 #end