Skip to content

Commit 184101e

Browse files
committed
Added Git Config window
1 parent 98bf95f commit 184101e

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

Editor/GitConfigWindow.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Abuksigun.MRGitUI;
2+
using System;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using UnityEditor;
6+
using UnityEditor.PackageManager.UI;
7+
using UnityEngine;
8+
9+
public class GitConfigWindow : MonoBehaviour
10+
{
11+
record Setting(string name);
12+
13+
static Setting[] settingsList = { new ("user.name"), new("user.email") };
14+
15+
[MenuItem("Assets/Git/Config", true)]
16+
public static bool Check() => PackageShortcuts.GetSelectedGitModules().Count() == 1;
17+
18+
[MenuItem("Assets/Git/Config", priority = 100)]
19+
public static async void Invoke()
20+
{
21+
var module = PackageShortcuts.GetSelectedGitModules().FirstOrDefault();
22+
if (module == null)
23+
return;
24+
var columnWidth = GUILayout.Width(200);
25+
var valueWidth = GUILayout.Width(160);
26+
var buttonWidth = GUILayout.Width(20);
27+
28+
await GUIShortcuts.ShowModalWindow("Git Config", new Vector2Int(1000, 700), async window => {
29+
30+
using (new EditorGUILayout.HorizontalScope())
31+
{
32+
EditorGUILayout.SelectableLabel("Name", columnWidth);
33+
foreach (var scope in Enum.GetValues(typeof(ConfigScope)).Cast<ConfigScope>())
34+
EditorGUILayout.LabelField(scope.ToString(), columnWidth);
35+
}
36+
37+
foreach (var setting in settingsList)
38+
{
39+
using (new EditorGUILayout.HorizontalScope())
40+
{
41+
EditorGUILayout.SelectableLabel(setting.name, columnWidth);
42+
foreach (var scope in Enum.GetValues(typeof(ConfigScope)).Cast<ConfigScope>())
43+
{
44+
var config = module.GitConfigValue(setting.name, scope).GetResultOrDefault();
45+
46+
if (scope != ConfigScope.None)
47+
{
48+
if (!string.IsNullOrEmpty(config) && GUILayout.Button("X", buttonWidth))
49+
_ = module.UnsetConfig(setting.name, scope);
50+
if (string.IsNullOrEmpty(config) ? GUILayout.Button("Set value", columnWidth) : GUILayout.Button("E", buttonWidth))
51+
_ = ShowChangeSettingWindow(module, setting, scope);
52+
}
53+
if (!string.IsNullOrEmpty(config))
54+
EditorGUILayout.SelectableLabel(config, valueWidth);
55+
}
56+
}
57+
}
58+
});
59+
}
60+
61+
static async Task ShowChangeSettingWindow(Module module, Setting setting, ConfigScope scope)
62+
{
63+
string newValue = await module.GitConfigValue(setting.name, scope);
64+
await GUIShortcuts.ShowModalWindow("Set Value", new Vector2Int(300, 180), window => {
65+
newValue = GUILayout.TextField(newValue);
66+
using (new GUILayout.HorizontalScope())
67+
{
68+
if (GUILayout.Button("Close"))
69+
{
70+
window.Close();
71+
}
72+
if (GUILayout.Button("Apply"))
73+
{
74+
_ = module.SetConfig(setting.name, scope, newValue);
75+
window.Close();
76+
}
77+
}
78+
});
79+
}
80+
}

Editor/GitConfigWindow.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Module.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
using System.Text.RegularExpressions;
77
using System.Threading.Tasks;
88
using UnityEditor;
9+
using UnityEditor.Experimental.GraphView;
910
using UnityEngine;
1011
using PackageInfo = UnityEditor.PackageManager.PackageInfo;
1112

1213
namespace Abuksigun.MRGitUI
1314
{
1415
using static Const;
1516

17+
public enum ConfigScope { Global, Local, None }
18+
19+
public record ConfigRef(string key, ConfigScope scope);
1620
public record BlameLine(string Hash, string Author, DateTime Date, string Text, int Line);
1721
public record Reference(string Name, string QualifiedName, string Hash);
1822
public record Tag(string Name, string Hash) : Reference(Name, Name, Hash);
@@ -67,6 +71,7 @@ public class Module
6771
Dictionary<int, Task<GitStatus>> diffCache;
6872
Dictionary<int, Task<string[]>> fileLogCache;
6973
Dictionary<string, Task<BlameLine[]>> fileBlameCache;
74+
Dictionary<ConfigRef, Task<string>> configCache;
7075

7176
List<IOData> processLog = new();
7277
List<IOData> processLogConcurent = new();
@@ -166,6 +171,17 @@ public Task<BlameLine[]> BlameFile(string filePath)
166171
fileBlameCache ??= new();
167172
return fileBlameCache.TryGetValue(filePath, out var blame) ? blame : fileBlameCache[filePath] = GetBlame(filePath);
168173
}
174+
public Task<string> GitConfigValue(string key, ConfigScope scope)
175+
{
176+
configCache ??= new();
177+
var configRef = new ConfigRef(key, scope);
178+
return configCache.TryGetValue(configRef, out var blame) ? blame : configCache[configRef] = GetGitConfigValue(key, scope);
179+
}
180+
async Task<string> GetGitConfigValue(string key, ConfigScope scope)
181+
{
182+
var result = await RunGit($"config {ScopeToString(scope)} --get {key}");
183+
return result.Output.Trim();
184+
}
169185
async Task<string> GetRepoPath()
170186
{
171187
return (await RunGit("rev-parse --show-toplevel")).Output.Trim();
@@ -405,6 +421,8 @@ public void RefreshFilesStatus()
405421
gitStatus = null;
406422
fileDiffCache = null;
407423
diffCache = null;
424+
configCache = null;
425+
AssetDatabase.Refresh();
408426
}
409427

410428
#region Remotes Managment
@@ -511,5 +529,21 @@ public Task<CommandResult> Stash(string commitMessage, IEnumerable<string> files
511529
return RunGit($"stash push -m {commitMessage.WrapUp()} -- {PackageShortcuts.JoinFileNames(files)}").AfterCompletion(RefreshFilesStatus, RefreshReferences);
512530
}
513531
#endregion
532+
533+
#region Config
534+
public Task<CommandResult> UnsetConfig(string key, ConfigScope scope)
535+
{
536+
return RunGit($"config --unset {ScopeToString(scope)} {key}").AfterCompletion(RefreshFilesStatus, RefreshReferences);
537+
}
538+
public Task<CommandResult> SetConfig(string key, ConfigScope scope, string newValue)
539+
{
540+
return RunGit($"config {ScopeToString(scope)} {key} \"{newValue}\"").AfterCompletion(RefreshFilesStatus, RefreshReferences);
541+
}
542+
#endregion
543+
544+
static string ScopeToString(ConfigScope scope)
545+
{
546+
return scope != ConfigScope.None ? "--" + scope.ToString().ToLower() : null;
547+
}
514548
}
515549
}

Editor/PluginSettingsProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public static class PluginSettingsProvider
1919
private static Task<string> userEmailTask;
2020

2121
public static string LocalRepoPaths => PlayerPrefs.GetString(LocalRepoPathsKey, "../");
22-
22+
2323
[SettingsProvider]
2424
public static SettingsProvider CreateMyCustomSettingsProvider() => new("Preferences/External Tools/MR Unity Git UI", SettingsScope.User)
2525
{
2626
activateHandler = (_, rootElement) =>
2727
{
2828
userNameTask = GetGitConfigValue("user.name");
2929
userEmailTask = GetGitConfigValue("user.email");
30-
userName = null;
30+
userName = null;
3131
userEmail = null;
3232

3333
rootElement.Add(new IMGUIContainer(() =>

0 commit comments

Comments
 (0)