Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit f1d3698

Browse files
committed
Fixes for loading and saving user data and git remotes in settings
1 parent 1abab56 commit f1d3698

File tree

3 files changed

+121
-89
lines changed

3 files changed

+121
-89
lines changed

src/GitHub.Api/Git/Repository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public Repository(string name, NPath localPath)
4242

4343
Name = name;
4444
LocalPath = localPath;
45+
this.User = new User();
4546
}
4647

4748
public void Initialize(IRepositoryManager repositoryManager)
@@ -55,6 +56,7 @@ public void Initialize(IRepositoryManager repositoryManager)
5556
repositoryManager.OnStatusUpdated += RepositoryManager_OnStatusUpdated;
5657
repositoryManager.OnActiveBranchChanged += RepositoryManager_OnActiveBranchChanged;
5758
repositoryManager.OnActiveRemoteChanged += RepositoryManager_OnActiveRemoteChanged;
59+
repositoryManager.OnGitUserLoaded += user => User = user;
5860
}
5961

6062
public void Refresh()

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface IRepositoryManager : IDisposable
1616
event Action<GitStatus> OnStatusUpdated;
1717
event Action<ConfigBranch?> OnActiveBranchChanged;
1818
event Action<ConfigRemote?> OnActiveRemoteChanged;
19+
event Action<IUser> OnGitUserLoaded;
1920

2021
event Action<IEnumerable<GitLock>> OnLocksUpdated;
2122
Dictionary<string, ConfigBranch> LocalBranches { get; }
@@ -120,6 +121,7 @@ class RepositoryManager : IRepositoryManager
120121
public event Action<GitStatus> OnStatusUpdated;
121122
public event Action<ConfigBranch?> OnActiveBranchChanged;
122123
public event Action<ConfigRemote?> OnActiveRemoteChanged;
124+
public event Action<IUser> OnGitUserLoaded;
123125

124126
public static RepositoryManager CreateInstance(IPlatform platform, ITaskManager taskManager, IUsageTracker usageTracker,
125127
IGitClient gitClient, NPath repositoryRoot)
@@ -162,7 +164,7 @@ public void Start()
162164

163165
ReadHead();
164166
RefreshConfigData();
165-
167+
LoadGitUser();
166168
watcher.Start();
167169
}
168170

@@ -304,6 +306,16 @@ public ITask UnlockFile(string file, bool force)
304306
return task;
305307
}
306308

309+
private void LoadGitUser()
310+
{
311+
var user = new User();
312+
GitClient.GetConfig("user.name", GitConfigSource.User)
313+
.Then((success, value) => user.Name = value).Then(
314+
GitClient.GetConfig("user.email", GitConfigSource.User)
315+
.Then((success, value) => user.Email = value))
316+
.Then(() => OnGitUserLoaded?.Invoke(user));
317+
}
318+
307319
private void SetupWatcher()
308320
{
309321
watcher.HeadChanged += Watcher_OnHeadChanged;

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs

Lines changed: 106 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,9 @@ class SettingsView : Subview
6666
private const string MetricsOptInLabel = "Help us improve by sending anonymous usage data";
6767
private const string DefaultRepositoryRemoteName = "origin";
6868

69-
[NonSerialized] private bool busy = false;
70-
[NonSerialized] private int lockedFileSelection = -1;
7169
[NonSerialized] private int newGitIgnoreRulesSelection = -1;
72-
[NonSerialized] private bool hasRemote;
7370
[NonSerialized] private ConfigRemote? activeRemote;
7471

75-
// TODO: Replace me with the real values
7672
[SerializeField] private string gitName;
7773
[SerializeField] private string gitEmail;
7874

@@ -83,58 +79,65 @@ class SettingsView : Subview
8379
[SerializeField] private string repositoryRemoteName;
8480
[SerializeField] private string repositoryRemoteUrl;
8581
[SerializeField] private Vector2 scroll;
82+
[SerializeField] private bool isBusy;
83+
[SerializeField] private int lockedFileSelection = -1;
84+
[SerializeField] private bool hasRemote;
8685
[SerializeField] private bool remoteHasChanged;
86+
[SerializeField] private bool userDataHasChanged;
87+
88+
[SerializeField] private string newGitName;
89+
[SerializeField] private string newGitEmail;
90+
[SerializeField] private string newRepositoryRemoteUrl;
8791

8892
public override void OnEnable()
8993
{
9094
base.OnEnable();
91-
if (lockedFiles == null)
92-
lockedFiles = new List<GitLock>();
93-
94-
if (Repository != null)
95-
Repository.OnActiveRemoteChanged += Repository_OnActiveRemoteChanged;
95+
AttachHandlers(Repository);
9696
}
9797

9898
public override void OnDisable()
9999
{
100100
base.OnDisable();
101-
102-
if (Repository != null)
103-
{
104-
Repository.OnActiveRemoteChanged -= Repository_OnActiveRemoteChanged;
105-
Repository.OnLocksUpdated -= RunLocksUpdateOnMainThread;
106-
}
101+
DetachHandlers(Repository);
107102
}
108103

109104
public override void OnDataUpdate()
110105
{
111106
base.OnDataUpdate();
112-
113-
if (Repository == null)
114-
return;
115-
116-
MaybeUpdateRemote();
107+
MaybeUpdateData();
117108
}
118109

119110
public override void OnRepositoryChanged(IRepository oldRepository)
120111
{
121112
base.OnRepositoryChanged(oldRepository);
122113

123-
if (oldRepository != null)
124-
{
125-
oldRepository.OnActiveRemoteChanged -= Repository_OnActiveRemoteChanged;
126-
oldRepository.OnLocksUpdated -= RunLocksUpdateOnMainThread;
127-
}
114+
DetachHandlers(oldRepository);
115+
AttachHandlers(Repository);
116+
Refresh();
117+
}
128118

129-
if (Repository != null)
130-
{
131-
gitName = Repository.User.Name;
132-
gitEmail = Repository.User.Email;
119+
public override void Refresh()
120+
{
121+
base.Refresh();
122+
Repository.ListLocks().Start();
123+
}
133124

134-
Repository.OnActiveRemoteChanged += Repository_OnActiveRemoteChanged;
135-
Repository.OnLocksUpdated += RunLocksUpdateOnMainThread;
136-
Repository.ListLocks().Start();
137-
}
125+
private void AttachHandlers(IRepository repository)
126+
{
127+
if (repository == null)
128+
return;
129+
130+
repository.OnActiveRemoteChanged += Repository_OnActiveRemoteChanged;
131+
repository.OnLocksUpdated += RunLocksUpdateOnMainThread;
132+
}
133+
134+
private void DetachHandlers(IRepository repository)
135+
{
136+
if (repository == null)
137+
return;
138+
139+
repository.OnActiveRemoteChanged -= Repository_OnActiveRemoteChanged;
140+
repository.OnLocksUpdated -= RunLocksUpdateOnMainThread;
138141
}
139142

140143
public override void OnGUI()
@@ -164,28 +167,46 @@ public override void OnGUI()
164167
GUILayout.EndScrollView();
165168
}
166169

167-
private void MaybeUpdateRemote()
170+
private void MaybeUpdateData()
168171
{
169-
if (!remoteHasChanged)
172+
if (lockedFiles == null)
173+
lockedFiles = new List<GitLock>();
174+
175+
userDataHasChanged = Repository == null || (Repository.User.Name == gitName && Repository.User.Email == gitEmail);
176+
177+
if (Repository == null)
178+
return;
179+
180+
if (!remoteHasChanged && !userDataHasChanged)
170181
return;
171182

172-
remoteHasChanged = false;
173-
hasRemote = activeRemote.HasValue && !String.IsNullOrEmpty(activeRemote.Value.Url);
174-
if (!hasRemote)
183+
if (remoteHasChanged)
175184
{
176-
ResetToDefaults();
185+
remoteHasChanged = false;
186+
hasRemote = activeRemote.HasValue && !String.IsNullOrEmpty(activeRemote.Value.Url);
187+
if (!hasRemote)
188+
{
189+
repositoryRemoteName = DefaultRepositoryRemoteName;
190+
repositoryRemoteUrl = string.Empty;
191+
}
192+
else
193+
{
194+
repositoryRemoteName = activeRemote.Value.Name;
195+
repositoryRemoteUrl = activeRemote.Value.Url;
196+
}
177197
}
178-
else
198+
199+
if (userDataHasChanged && Repository != null)
179200
{
180-
repositoryRemoteName = activeRemote.Value.Name;
181-
repositoryRemoteUrl = activeRemote.Value.Url;
201+
gitName = Repository.User.Name;
202+
gitEmail = Repository.User.Email;
182203
}
183204
}
184205

185206
private void ResetToDefaults()
186207
{
187-
gitName = String.Empty;
188-
gitEmail = String.Empty;
208+
gitName = Repository != null ? Repository.User.Name : String.Empty;
209+
gitEmail = Repository != null ? Repository.User.Email : String.Empty;
189210
repositoryRemoteName = DefaultRepositoryRemoteName;
190211
repositoryRemoteUrl = string.Empty;
191212
}
@@ -219,62 +240,59 @@ private void OnLocksUpdate(IEnumerable<GitLock> update)
219240
private void OnUserSettingsGUI()
220241
{
221242
GUILayout.Label(GitConfigTitle, EditorStyles.boldLabel);
222-
GUI.enabled = !busy && Repository != null;
223-
224-
gitName = EditorGUILayout.TextField(GitConfigNameLabel, gitName);
225-
gitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, gitEmail);
226243

227-
GUI.enabled = !busy;
228-
if (GUILayout.Button(GitConfigUserSave, GUILayout.ExpandWidth(false)))
244+
EditorGUI.BeginDisabledGroup(isBusy);
229245
{
230-
var needsSaving = gitName != Repository.User.Name || gitEmail != Repository.User.Email;
231-
if (needsSaving)
246+
newGitName = EditorGUILayout.TextField(GitConfigNameLabel, newGitName);
247+
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);
248+
249+
var needsSaving = newGitName != gitName || newGitEmail != gitEmail;
250+
EditorGUI.BeginDisabledGroup(needsSaving);
232251
{
233-
GitClient.SetConfig("user.name", gitName, GitConfigSource.User)
234-
.Then((success, value) => { if (success) Repository.User.Name = value; })
235-
.Then(
236-
GitClient.SetConfig("user.email", gitEmail, GitConfigSource.User)
237-
.Then((success, value) => { if (success) Repository.User.Email = value; }))
238-
.FinallyInUI((_, __) => busy = false);
239-
busy = true;
252+
if (GUILayout.Button(GitConfigUserSave, GUILayout.ExpandWidth(false)))
253+
{
254+
GitClient.SetConfig("user.name", newGitName, GitConfigSource.User)
255+
.Then((success, value) => { if (success) Repository.User.Name = value; })
256+
.Then(
257+
GitClient.SetConfig("user.email", newGitEmail, GitConfigSource.User)
258+
.Then((success, value) => { if (success) Repository.User.Email = value; }))
259+
.FinallyInUI((_, __) => isBusy = false)
260+
.Start();
261+
isBusy = true;
262+
}
240263
}
241264
}
242-
GUI.enabled = true;
243265
}
244266

245267
private void OnRepositorySettingsGUI()
246268
{
247269
GUILayout.Label(GitRepositoryTitle, EditorStyles.boldLabel);
248-
GUI.enabled = !busy && Repository != null && !String.IsNullOrEmpty(repositoryRemoteName);
249-
250-
repositoryRemoteUrl = EditorGUILayout.TextField(GitRepositoryRemoteLabel + ": " + repositoryRemoteName, repositoryRemoteUrl);
251-
252-
if (GUILayout.Button(GitRepositorySave, GUILayout.ExpandWidth(false)))
270+
EditorGUI.BeginDisabledGroup(isBusy);
253271
{
254-
try
272+
newRepositoryRemoteUrl = EditorGUILayout.TextField(GitRepositoryRemoteLabel + ": " + repositoryRemoteName, newRepositoryRemoteUrl);
273+
var needsSaving = newRepositoryRemoteUrl != repositoryRemoteUrl;
274+
EditorGUI.BeginDisabledGroup(needsSaving);
255275
{
256-
busy = true;
257-
var needsSaving = !Repository.CurrentRemote.HasValue ||
258-
(!String.IsNullOrEmpty(repositoryRemoteUrl) && repositoryRemoteUrl != Repository.CurrentRemote.Value.Name);
259-
if (needsSaving)
276+
if (GUILayout.Button(GitRepositorySave, GUILayout.ExpandWidth(false)))
260277
{
261-
Repository.SetupRemote(repositoryRemoteName, repositoryRemoteUrl)
262-
.FinallyInUI((_, __) =>
263-
{
264-
busy = false;
265-
Redraw();
266-
})
267-
.Start();
278+
try
279+
{
280+
isBusy = true;
281+
Repository.SetupRemote(repositoryRemoteName, repositoryRemoteUrl)
282+
.FinallyInUI((_, __) =>
283+
{
284+
isBusy = false;
285+
Redraw();
286+
})
287+
.Start();
288+
}
289+
catch (Exception ex)
290+
{
291+
Logger.Error(ex);
292+
}
268293
}
269-
else
270-
busy = false;
271-
}
272-
catch (Exception ex)
273-
{
274-
Logger.Error(ex);
275294
}
276295
}
277-
GUI.enabled = true;
278296
}
279297

280298
private bool ValidateGitInstall(string path)
@@ -524,7 +542,7 @@ private void OnGitIgnoreRulesGUI()
524542

525543
private void OnGitLfsLocksGUI()
526544
{
527-
GUI.enabled = !busy && Repository != null;
545+
GUI.enabled = !isBusy && Repository != null;
528546
GUILayout.BeginVertical();
529547
{
530548
GUILayout.Label("Locked files", EditorStyles.boldLabel);
@@ -610,7 +628,7 @@ private void OnInstallPathGUI()
610628
// Install path
611629
GUILayout.Label(GitInstallTitle, EditorStyles.boldLabel);
612630

613-
GUI.enabled = !busy && gitExecPath != null;
631+
GUI.enabled = !isBusy && gitExecPath != null;
614632

615633
// Install path field
616634
EditorGUI.BeginChangeCheck();
@@ -659,7 +677,7 @@ private void OnPrivacyGui()
659677

660678
GUILayout.Label(PrivacyTitle, EditorStyles.boldLabel);
661679

662-
GUI.enabled = !busy && service != null;
680+
GUI.enabled = !isBusy && service != null;
663681

664682
var metricsEnabled = service != null ? service.Enabled : false;
665683
EditorGUI.BeginChangeCheck();
@@ -678,7 +696,7 @@ private void OnLoggingSettingsGui()
678696
{
679697
GUILayout.Label(DebugSettingsTitle, EditorStyles.boldLabel);
680698

681-
GUI.enabled = !busy;
699+
GUI.enabled = !isBusy;
682700

683701
var traceLogging = Logging.TracingEnabled;
684702

0 commit comments

Comments
 (0)