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

Commit 974b6b1

Browse files
Merge pull request #426 from github-for-unity/fixes/git-client-set-user
Adding functionality to GitClient to set user info
2 parents 63ff89b + 157f11f commit 974b6b1

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/GitHub.Api/Git/GitClient.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -260,23 +264,30 @@ public ITask<User> GetConfigUserAndEmail()
260264
string username = null;
261265
string email = null;
262266

263-
return GetConfig("user.name", GitConfigSource.User)
267+
return GetConfig(UserNameConfigKey, GitConfigSource.User)
264268
.Then((success, value) => {
265269
if (success)
266270
{
267271
username = value;
268272
}
269273
})
270-
.Then(GetConfig("user.email", GitConfigSource.User)
274+
.Then(GetConfig(UserEmailConfigKey, GitConfigSource.User)
271275
.Then((success, value) => {
272276
if (success)
273277
{
274278
email = value;
275279
}
276280
})).Then(success => {
277-
Logger.Trace("{0}:{1} {2}:{3}", "user.name", username, "user.email", email);
278-
return new User { Name= username, Email = email };
279-
});
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 });
280291
}
281292

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

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

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class UserSettingsView : Subview
2222
[SerializeField] private string gitEmail;
2323
[SerializeField] private string newGitName;
2424
[SerializeField] private string newGitEmail;
25+
[SerializeField] private bool needsSaving;
2526

2627
public override void InitializeView(IView parent)
2728
{
@@ -42,11 +43,17 @@ public override void OnGUI()
4243

4344
EditorGUI.BeginDisabledGroup(IsBusy || Parent.IsBusy);
4445
{
45-
newGitName = EditorGUILayout.TextField(GitConfigNameLabel, newGitName);
46-
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);
46+
EditorGUI.BeginChangeCheck();
47+
{
48+
newGitName = EditorGUILayout.TextField(GitConfigNameLabel, newGitName);
49+
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);
50+
}
4751

48-
var needsSaving = (newGitName != gitName || newGitEmail != gitEmail)
49-
&& !(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+
}
5057

5158
EditorGUI.BeginDisabledGroup(!needsSaving);
5259
{
@@ -55,45 +62,28 @@ public override void OnGUI()
5562
GUI.FocusControl(null);
5663
isBusy = true;
5764

58-
GitClient.SetConfig("user.name", newGitName, GitConfigSource.User)
59-
.Then((success, value) =>
60-
{
65+
GitClient.SetConfigUserAndEmail(newGitName, newGitEmail)
66+
.FinallyInUI((success, exception, user) => {
67+
isBusy = false;
6168
if (success)
6269
{
6370
if (Repository != null)
6471
{
6572
Repository.User.Name = gitName = newGitName;
73+
Repository.User.Email = gitEmail = newGitEmail;
6674
}
6775
else
6876
{
6977
gitName = newGitName;
78+
gitEmail = newGitEmail;
7079
}
80+
81+
needsSaving = false;
82+
83+
Redraw();
84+
Finish(true);
7185
}
7286
})
73-
.Then(
74-
GitClient.SetConfig("user.email", newGitEmail, GitConfigSource.User)
75-
.Then((success, value) =>
76-
{
77-
if (success)
78-
{
79-
if (Repository != null)
80-
{
81-
Repository.User.Email = gitEmail = newGitEmail;
82-
}
83-
else
84-
{
85-
gitEmail = newGitEmail;
86-
}
87-
88-
userDataHasChanged = true;
89-
}
90-
}))
91-
.FinallyInUI((_, __) =>
92-
{
93-
isBusy = false;
94-
Redraw();
95-
Finish(true);
96-
})
9787
.Start();
9888
}
9989
}
@@ -122,6 +112,7 @@ private void MaybeUpdateData()
122112
{
123113
newGitName = gitName = Repository.User.Name;
124114
newGitEmail = gitEmail = Repository.User.Email;
115+
needsSaving = false;
125116
}
126117
}
127118
}
@@ -146,6 +137,7 @@ private void UpdateUserDataFromClient()
146137
{
147138
newGitName = gitName = user.Name;
148139
newGitEmail = gitEmail = user.Email;
140+
needsSaving = false;
149141
Redraw();
150142
}
151143
}).Start();

0 commit comments

Comments
 (0)