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

Commit 2d0b24b

Browse files
Merge branch 'fixes/git-client-set-user' into enhancements/git-client-cache
2 parents 985c390 + 8b57bb9 commit 2d0b24b

File tree

4 files changed

+93
-101
lines changed

4 files changed

+93
-101
lines changed

src/GitHub.Api/Git/GitClient.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ITask<string> GetConfig(string key, GitConfigSource configSource,
2323
ITask<string> SetConfig(string key, string value, GitConfigSource configSource,
2424
IOutputProcessor<string> processor = null);
2525

26-
ITask<string[]> GetConfigUserAndEmail();
26+
ITask<User> GetConfigUserAndEmail();
2727

2828
ITask<List<GitLock>> ListLocks(bool local,
2929
BaseOutputListProcessor<GitLock> processor = null);
@@ -83,10 +83,14 @@ ITask<string> Unlock(string file, bool force,
8383
ITask<Version> Version(IOutputProcessor<Version> processor = null);
8484

8585
ITask<Version> LfsVersion(IOutputProcessor<Version> processor = null);
86+
87+
ITask<User> SetConfigUserAndEmail(string username, string email);
8688
}
8789

8890
class GitClient : IGitClient
8991
{
92+
private const string UserNameConfigKey = "user.name";
93+
private const string UserEmailConfigKey = "user.email";
9094
private readonly IEnvironment environment;
9195
private readonly IProcessManager processManager;
9296
private readonly ITaskManager taskManager;
@@ -255,26 +259,35 @@ public ITask<string> SetConfig(string key, string value, GitConfigSource configS
255259
.Configure(processManager);
256260
}
257261

258-
public ITask<string[]> GetConfigUserAndEmail()
262+
public ITask<User> GetConfigUserAndEmail()
259263
{
260264
string username = null;
261265
string email = null;
262266

263-
return GetConfig("user.name", GitConfigSource.User).Then((success, value) => {
264-
if (success)
265-
{
266-
username = value;
267-
}
268-
269-
}).Then(GetConfig("user.email", GitConfigSource.User).Then((success, value) => {
270-
if (success)
271-
{
272-
email = value;
273-
}
274-
})).Then(success => {
275-
Logger.Trace("user.name:{1} user.email:{2}", success, username, email);
276-
return new[] { username, email };
277-
});
267+
return GetConfig(UserNameConfigKey, GitConfigSource.User)
268+
.Then((success, value) => {
269+
if (success)
270+
{
271+
username = value;
272+
}
273+
})
274+
.Then(GetConfig(UserEmailConfigKey, GitConfigSource.User)
275+
.Then((success, value) => {
276+
if (success)
277+
{
278+
email = value;
279+
}
280+
})).Then(success => {
281+
Logger.Trace("{0}:{1} {2}:{3}", UserNameConfigKey, username, UserEmailConfigKey, email);
282+
return new User { Name = username, Email = email };
283+
});
284+
}
285+
286+
public ITask<User> SetConfigUserAndEmail(string username, string email)
287+
{
288+
return SetConfig(UserNameConfigKey, username, GitConfigSource.User)
289+
.Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User))
290+
.Then(b => new User { Name = username, Email = email });
278291
}
279292

280293
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,9 @@ public ITask UnlockFile(string file, bool force)
299299
private void LoadGitUser()
300300
{
301301
GitClient.GetConfigUserAndEmail()
302-
.Then((success, strings) => {
303-
var username = strings[0];
304-
var email = strings[1];
305-
306-
var user = new User {
307-
Name = username,
308-
Email = email
309-
};
310-
302+
.Then((success, user) => {
311303
Logger.Trace("OnGitUserLoaded: {0}", user);
312304
OnGitUserLoaded?.Invoke(user);
313-
314305
}).Start();
315306
}
316307

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,9 @@ private void CheckForUser()
9696
Logger.Trace("Checking for user");
9797
isBusy = true;
9898

99-
GitClient.GetConfigUserAndEmail().FinallyInUI((success, ex, strings) => {
100-
var username = strings[0];
101-
var email = strings[1];
102-
99+
GitClient.GetConfigUserAndEmail().FinallyInUI((success, ex, user) => {
103100
isBusy = false;
104-
isUserDataPresent = success && !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(email);
101+
isUserDataPresent = success && !String.IsNullOrEmpty(user.Name) && !String.IsNullOrEmpty(user.Email);
105102
hasCompletedInitialCheck = true;
106103

107104
Logger.Trace("User Present: {0}", isUserDataPresent);

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

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class UserSettingsView : Subview
2222
[SerializeField] private string gitEmail;
2323
[SerializeField] private string newGitName;
2424
[SerializeField] private string newGitEmail;
25-
[SerializeField] private User cachedUser;
25+
[SerializeField] private bool needsSaving;
2626

2727
public override void InitializeView(IView parent)
2828
{
@@ -43,11 +43,17 @@ public override void OnGUI()
4343

4444
EditorGUI.BeginDisabledGroup(IsBusy || Parent.IsBusy);
4545
{
46-
newGitName = EditorGUILayout.TextField(GitConfigNameLabel, newGitName);
47-
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);
46+
EditorGUI.BeginChangeCheck();
47+
{
48+
newGitName = EditorGUILayout.TextField(GitConfigNameLabel, newGitName);
49+
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);
50+
}
4851

49-
var needsSaving = (newGitName != gitName || newGitEmail != gitEmail)
50-
&& !(string.IsNullOrEmpty(newGitName) || string.IsNullOrEmpty(newGitEmail));
52+
if (EditorGUI.EndChangeCheck())
53+
{
54+
needsSaving = !(string.IsNullOrEmpty(newGitName) || string.IsNullOrEmpty(newGitEmail))
55+
&& (newGitName != gitName || newGitEmail != gitEmail);
56+
}
5157

5258
EditorGUI.BeginDisabledGroup(!needsSaving);
5359
{
@@ -56,49 +62,28 @@ public override void OnGUI()
5662
GUI.FocusControl(null);
5763
isBusy = true;
5864

59-
GitClient.SetConfig("user.name", newGitName, GitConfigSource.User)
60-
.Then((success, value) =>
61-
{
65+
GitClient.SetConfigUserAndEmail(newGitName, newGitEmail)
66+
.FinallyInUI((success, exception, user) => {
67+
isBusy = false;
6268
if (success)
6369
{
6470
if (Repository != null)
6571
{
66-
Repository.User.Name = newGitName;
72+
Repository.User.Name = gitName = newGitName;
73+
Repository.User.Email = gitEmail = newGitEmail;
6774
}
6875
else
6976
{
70-
if (cachedUser == null)
71-
{
72-
cachedUser = new User();
73-
}
74-
cachedUser.Name = newGitName;
77+
gitName = newGitName;
78+
gitEmail = newGitEmail;
7579
}
80+
81+
needsSaving = false;
82+
83+
Redraw();
84+
Finish(true);
7685
}
7786
})
78-
.Then(
79-
GitClient.SetConfig("user.email", newGitEmail, GitConfigSource.User)
80-
.Then((success, value) =>
81-
{
82-
if (success)
83-
{
84-
if (Repository != null)
85-
{
86-
Repository.User.Email = newGitEmail;
87-
}
88-
else
89-
{
90-
cachedUser.Email = newGitEmail;
91-
}
92-
93-
userDataHasChanged = true;
94-
}
95-
}))
96-
.FinallyInUI((_, __) =>
97-
{
98-
isBusy = false;
99-
Redraw();
100-
Finish(true);
101-
})
10287
.Start();
10388
}
10489
}
@@ -107,49 +92,55 @@ public override void OnGUI()
10792
EditorGUI.EndDisabledGroup();
10893
}
10994

95+
public override void OnEnable()
96+
{
97+
base.OnEnable();
98+
userDataHasChanged = true;
99+
}
100+
110101
private void MaybeUpdateData()
111102
{
112-
if (Repository == null)
103+
if (userDataHasChanged)
113104
{
114-
if (!String.IsNullOrEmpty(EntryPoint.Environment.GitExecutablePath))
105+
userDataHasChanged = false;
106+
107+
if (Repository == null)
115108
{
116-
if ((cachedUser == null || String.IsNullOrEmpty(cachedUser.Name)) && GitClient != null)
117-
{
118-
GitClient.GetConfigUserAndEmail().FinallyInUI((success, ex, strings) => {
119-
var username = strings[0];
120-
var email = strings[1];
121-
122-
if (success && !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(email))
123-
{
124-
cachedUser = new User {
125-
Name = username,
126-
Email = email
127-
};
128-
129-
userDataHasChanged = true;
130-
Redraw();
131-
}
132-
}).Start();
133-
}
109+
UpdateUserDataFromClient();
134110
}
135-
136-
if (userDataHasChanged)
111+
else
137112
{
138-
newGitName = gitName = cachedUser.Name;
139-
newGitEmail = gitEmail = cachedUser.Email;
140-
userDataHasChanged = false;
113+
newGitName = gitName = Repository.User.Name;
114+
newGitEmail = gitEmail = Repository.User.Email;
115+
needsSaving = false;
141116
}
142-
return;
143117
}
118+
}
144119

145-
userDataHasChanged = Repository.User.Name != gitName || Repository.User.Email != gitEmail;
120+
private void UpdateUserDataFromClient()
121+
{
122+
if (String.IsNullOrEmpty(EntryPoint.Environment.GitExecutablePath))
123+
{
124+
return;
125+
}
146126

147-
if (!userDataHasChanged)
127+
if (GitClient == null)
128+
{
148129
return;
130+
}
149131

150-
userDataHasChanged = false;
151-
newGitName = gitName = Repository.User.Name;
152-
newGitEmail = gitEmail = Repository.User.Email;
132+
Logger.Trace("Update user data from GitClient");
133+
134+
GitClient.GetConfigUserAndEmail()
135+
.ThenInUI((success, user) => {
136+
if (success && !String.IsNullOrEmpty(user.Name) && !String.IsNullOrEmpty(user.Email))
137+
{
138+
newGitName = gitName = user.Name;
139+
newGitEmail = gitEmail = user.Email;
140+
needsSaving = false;
141+
Redraw();
142+
}
143+
}).Start();
153144
}
154145

155146
public override bool IsBusy

0 commit comments

Comments
 (0)