From 38ed0780f8ed9e05472a28846fbbdd7a5f638004 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Sat, 20 Jul 2024 01:31:04 +0200
Subject: [PATCH] Allow user-defined `git` config

---
 scripts/Common/OS/users.fish          |  2 ++
 scripts/Common/Software/git/main.fish | 38 ++++++++++++++++++++-------
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/scripts/Common/OS/users.fish b/scripts/Common/OS/users.fish
index 5e5f561b..c3b810f6 100644
--- a/scripts/Common/OS/users.fish
+++ b/scripts/Common/OS/users.fish
@@ -24,4 +24,6 @@ for name in (echo "$users" | jq '.[]' --raw-output0 | string split0)
     while ! sudo passwd "$name"
         echo "An error occurred! Please try again."
     end
+
+    source "$dir/../Software/git/main.fish" configure user "$name"
 end
diff --git a/scripts/Common/Software/git/main.fish b/scripts/Common/Software/git/main.fish
index d5211176..74c21b9d 100644
--- a/scripts/Common/Software/git/main.fish
+++ b/scripts/Common/Software/git/main.fish
@@ -7,21 +7,39 @@ begin
         echo "git"
     end
 
-    function configureSW -S -V dir
+    function configureSW -S -V dir -a scope name
         source "$dir/../../Scripts/config.fish"
-        set -l branch "valhalla.git.defaultBranch"
+        set -l root
+        set -l configArgs
 
         function setConfig
-            sudo git config --system $argv
+            sudo git config $configArgs $argv
         end
 
+        if [ -z "$scope" ] || [ "$scope" = "system" ]
+            set -l root "valhalla"
+            set -l configArgs "--system"
+        else
+            set -l root "valhalla.users.$name"
+            set -l configArgs "--global"
+            set -l displayName "$root.displayName"
+            set -l mailAddress "$root.mailAddress"
+
+            if isSet "$displayName"
+                setConfig user.name (getConfig "$displayName")
+            end
+
+            if isSet "$mailAddress"
+                setConfig user.email (getConfig "$mailAddress")
+            end
+        end
+
+        set -l branch "$root.git.defaultBranch"
+
         if isSet "$branch"
             setConfig init.defaultBranch (getConfig "$branch")
         end
 
-        setConfig user.name "Manuel Thalmann"
-        setConfig user.email "m@nuth.ch"
-
         begin # Git Flow
             set -l dir (mktemp -d)
 
@@ -39,13 +57,13 @@ begin
                 yes "" | git flow init &> /dev/null
 
                 if isSet "$mainKey"
-                    sudo git flow config set --system master (getConfig "$mainKey") > /dev/null
+                    sudo git flow config set $configArgs master (getConfig "$mainKey") > /dev/null
                 else
                     true
                 end
 
                 if isSet "$devKey"
-                    sudo git flow config set --system develop (getConfig "$devKey") > /dev/null
+                    sudo git flow config set $configArgs develop (getConfig "$devKey") > /dev/null
                 else
                     true
                 end
@@ -56,8 +74,8 @@ begin
         end
 
         begin # Aliases
-            set -l key valhalla.git.aliases
-            set -l aliases (getConfig "$key" --json)
+            set -l key git.aliases
+            set -l aliases (getConfig "$root.$key" --json)
 
             for name in (echo "$aliases" | jq 'keys[]' --raw-output0 | string split0)
                 setConfig "alias.$name" (getConfig "$key.$name")