From 51ea07b6541e5667671480dd199cf86f33d1e3df Mon Sep 17 00:00:00 2001
From: Maximilian Betz <Maximilian.Betz@awi.de>
Date: Fri, 1 Apr 2022 10:51:02 +0200
Subject: [PATCH] included git commit hash in version information

---
 lib/datamodel.dart | 13 +++++++++++++
 lib/main.dart      | 15 +--------------
 lib/overview.dart  | 19 +++++++++++--------
 pubspec.yaml       |  2 +-
 4 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/lib/datamodel.dart b/lib/datamodel.dart
index dfa5a21..c209847 100644
--- a/lib/datamodel.dart
+++ b/lib/datamodel.dart
@@ -1,5 +1,6 @@
 import 'dart:convert';
 import 'package:flutter/cupertino.dart';
+import 'package:flutter/services.dart';
 import 'package:shared_preferences/shared_preferences.dart';
 
 class Collection {
@@ -457,3 +458,15 @@ class EventStoreInstance extends EventStoreBase {
     );
   }
 }
+
+String gitVersionHash = '';
+Future<void> loadGitInfo() async {
+  final commitHash = await rootBundle.loadString('.git/refs/heads/main');
+
+  gitVersionHash = commitHash.substring(0,8);  //Use the short version
+  debugPrint("Commit ID: $gitVersionHash");
+}
+
+String getGitHash(){
+  return gitVersionHash;
+}
\ No newline at end of file
diff --git a/lib/main.dart b/lib/main.dart
index f80e635..70016de 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -8,31 +8,18 @@ import 'overview.dart';
 import 'configuration.dart';
 import 'databaseconnector.dart';
 
-Future<String> getGitInfo() async {
-  final _head = await rootBundle.loadString('.git/HEAD');
-  final commitId = await rootBundle.loadString('.git/ORIG_HEAD');
-
-  final branch = _head.split('/').last;
-
-  print("Branch: $branch");
-  print("Commit ID: $commitId");
-
-  return "Branch: $branch,  Commit ID: $commitId";
-}
-
 
 Future<void> loadConfiguration() async {
   final ConfigurationStoreInstance configuration = ConfigurationStoreInstance();
   final EventStoreInstance event = EventStoreInstance();
   WidgetsFlutterBinding.ensureInitialized();
 
-  getGitInfo();
+  loadGitInfo();  //In oder to display the git commit version.
 
   // Load conf and last event fields from shared preferences to prefill UI
   await configuration.loadFromSharedPrefs();
   await event.loadFromSharedPrefs();
 
-
   //const storage = FlutterSecureStorage();   //TODO: move login information to secure storage
   //Map<String, String> allValues = await storage.readAll();
   //print('Secure Storage: ' + allValues.toString());
diff --git a/lib/overview.dart b/lib/overview.dart
index 4fc1235..819f180 100644
--- a/lib/overview.dart
+++ b/lib/overview.dart
@@ -1,4 +1,5 @@
 import 'package:flutter/material.dart';
+import 'package:mobileeventlog/datamodel.dart';
 
 class Overview extends StatelessWidget {
   const Overview({Key? key}) : super(key: key);
@@ -11,14 +12,16 @@ class Overview extends StatelessWidget {
       content: Column(
         mainAxisSize: MainAxisSize.min,
         crossAxisAlignment: CrossAxisAlignment.center,
-        children: const <Widget>[
-          Icon( Icons.copyright_outlined),
-          Text('Alfred Wegener Institute'),
-          Text('Data-Logistics-Support'),
-          SizedBox(height: 10),
-          Text('Maximilian Betz'),
-          SizedBox(height: 30),
-          Text('o2a-support@awi.de'),
+        children: <Widget>[
+          const Icon( Icons.copyright_outlined),
+          const Text('Alfred Wegener Institute'),
+          const Text('Data-Logistics-Support'),
+          const SizedBox(height: 10),
+          const Text('Maximilian Betz'),
+          const SizedBox(height: 30),
+          const Text('o2a-support@awi.de'),
+          const SizedBox(height: 30),
+          Text('Git-Commit-Hash: ' + getGitHash()),
         ],
       ),
       actions: <Widget>[
diff --git a/pubspec.yaml b/pubspec.yaml
index 0533534..66e8e91 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -71,7 +71,7 @@ flutter:
     - assets/awi_logo.png
     - .git/HEAD         # This file points out the current branch of the project.
     - .git/ORIG_HEAD    # This file points to the commit id at origin (last commit id of the remote repository).
-    - .git/refs/heads/
+    - .git/refs/heads/main
   #   - images/a_dot_burr.jpeg
   #   - images/a_dot_ham.jpeg
 
-- 
GitLab