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

Commit 090f950

Browse files
Merge pull request #319 from github-for-unity/fixes/initialize-project-requires-name-email
Preventing initialize when name & email is not present
2 parents 2987c08 + e19c891 commit 090f950

File tree

6 files changed

+148
-34
lines changed

6 files changed

+148
-34
lines changed

src/GitHub.Api/Git/GitClient.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ 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();
27+
2628
ITask<List<GitLock>> ListLocks(bool local,
2729
BaseOutputListProcessor<GitLock> processor = null);
2830

@@ -239,7 +241,7 @@ public ITask<Version> LfsVersion(IOutputProcessor<Version> processor = null)
239241

240242
public ITask<string> GetConfig(string key, GitConfigSource configSource, IOutputProcessor<string> processor = null)
241243
{
242-
Logger.Trace("GetConfig");
244+
Logger.Trace("GetConfig: {0}", key);
243245

244246
return new GitConfigGetTask(key, configSource, cancellationToken, processor)
245247
.Configure(processManager);
@@ -253,6 +255,28 @@ public ITask<string> SetConfig(string key, string value, GitConfigSource configS
253255
.Configure(processManager);
254256
}
255257

258+
public ITask<string[]> GetConfigUserAndEmail()
259+
{
260+
string username = null;
261+
string email = null;
262+
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+
});
278+
}
279+
256280
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)
257281
{
258282
Logger.Trace("ListLocks");

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,20 @@ public ITask UnlockFile(string file, bool force)
313313

314314
private void LoadGitUser()
315315
{
316-
var user = new User();
317-
GitClient.GetConfig("user.name", GitConfigSource.User)
318-
.Then((success, value) => user.Name = value).Then(
319-
GitClient.GetConfig("user.email", GitConfigSource.User)
320-
.Then((success, value) => user.Email = value))
321-
.Then(() => {
322-
Logger.Trace("OnGitUserLoaded: {0}", user);
323-
OnGitUserLoaded?.Invoke(user);
324-
})
325-
.Start();
316+
GitClient.GetConfigUserAndEmail()
317+
.Then((success, strings) => {
318+
var username = strings[0];
319+
var email = strings[1];
320+
321+
var user = new User {
322+
Name = username,
323+
Email = email
324+
};
325+
326+
Logger.Trace("OnGitUserLoaded: {0}", user);
327+
OnGitUserLoaded?.Invoke(user);
328+
329+
}).Start();
326330
}
327331

328332
private void SetupWatcher()

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Styles.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Styles
5151

5252
private static GUIStyle label,
5353
boldLabel,
54+
centeredErrorLabel,
5455
errorLabel,
5556
deletedFileLabel,
5657
longMessageStyle,
@@ -333,6 +334,22 @@ public static GUIStyle ErrorLabel
333334
}
334335
}
335336

337+
public static GUIStyle CenteredErrorLabel
338+
{
339+
get
340+
{
341+
if (centeredErrorLabel == null)
342+
{
343+
centeredErrorLabel = new GUIStyle(EditorStyles.label);
344+
centeredErrorLabel.alignment = TextAnchor.MiddleCenter;
345+
centeredErrorLabel.name = "CenteredErrorLabel";
346+
centeredErrorLabel.wordWrap = true;
347+
centeredErrorLabel.normal.textColor = Color.red;
348+
}
349+
return centeredErrorLabel;
350+
}
351+
}
352+
336353
public static GUIStyle LongMessageStyle
337354
{
338355
get

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

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,41 @@ class InitProjectView : Subview
99
{
1010
private const string NoRepoTitle = "No Git repository found for this project";
1111
private const string NoRepoDescription = "Initialize a Git repository to track changes and collaborate with others.";
12+
private const string NoUserOrEmailError = "Name and Email must be configured in Settings";
1213

1314
[SerializeField] private UserSettingsView userSettingsView = new UserSettingsView();
1415
[SerializeField] private GitPathView gitPathView = new GitPathView();
1516
[SerializeField] private bool isBusy;
1617

18+
[NonSerialized] private string errorMessage;
19+
[NonSerialized] private bool isUserDataPresent;
20+
[NonSerialized] private bool userDataHasChanged;
21+
1722
public override void InitializeView(IView parent)
1823
{
1924
base.InitializeView(parent);
25+
2026
userSettingsView.InitializeView(this);
2127
gitPathView.InitializeView(this);
22-
}
2328

24-
public override void OnDataUpdate()
25-
{
26-
base.OnDataUpdate();
27-
userSettingsView.OnDataUpdate();
28-
gitPathView.OnDataUpdate();
29+
if (!string.IsNullOrEmpty(Environment.GitExecutablePath))
30+
{
31+
CheckForUser();
32+
}
2933
}
3034

3135
public override void OnEnable()
3236
{
3337
base.OnEnable();
3438
gitPathView.OnEnable();
39+
userDataHasChanged = Environment.GitExecutablePath != null;
40+
}
41+
42+
public override void OnDataUpdate()
43+
{
44+
base.OnDataUpdate();
45+
userSettingsView.OnDataUpdate();
46+
gitPathView.OnDataUpdate();
3547
}
3648

3749
public override void OnGUI()
@@ -78,7 +90,7 @@ public override void OnGUI()
7890
GUILayout.BeginHorizontal();
7991
GUILayout.FlexibleSpace();
8092

81-
EditorGUI.BeginDisabledGroup(IsBusy);
93+
EditorGUI.BeginDisabledGroup(IsBusy || !isUserDataPresent);
8294
{
8395
if (GUILayout.Button(Localization.InitializeRepositoryButtonText, "Button"))
8496
{
@@ -93,11 +105,54 @@ public override void OnGUI()
93105
GUILayout.FlexibleSpace();
94106
GUILayout.EndHorizontal();
95107

108+
ShowErrorMessage();
109+
96110
GUILayout.FlexibleSpace();
97111
}
98112
GUILayout.EndVertical();
99113
}
100114

115+
private void ShowErrorMessage()
116+
{
117+
if (errorMessage != null)
118+
{
119+
GUILayout.Space(Styles.BaseSpacing);
120+
GUILayout.BeginHorizontal();
121+
{
122+
GUILayout.Label(errorMessage, Styles.CenteredErrorLabel);
123+
}
124+
GUILayout.EndHorizontal();
125+
}
126+
}
127+
128+
private void MaybeUpdateData()
129+
{
130+
if (userDataHasChanged)
131+
{
132+
userDataHasChanged = false;
133+
CheckForUser();
134+
}
135+
}
136+
137+
private void CheckForUser()
138+
{
139+
isBusy = true;
140+
141+
GitClient.GetConfigUserAndEmail().FinallyInUI((success, ex, strings) => {
142+
var username = strings[0];
143+
var email = strings[1];
144+
145+
146+
isBusy = false;
147+
isUserDataPresent = success && !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(email);
148+
errorMessage = isUserDataPresent ? null : NoUserOrEmailError;
149+
150+
Logger.Trace("Finally: {0}", isUserDataPresent);
151+
152+
Redraw();
153+
}).Start();
154+
}
155+
101156
public override bool IsBusy
102157
{
103158
get { return isBusy || userSettingsView.IsBusy || gitPathView.IsBusy; }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public enum PopupViewType
1111
{
1212
None,
1313
PublishView,
14-
AuthenticationView
14+
AuthenticationView,
1515
}
1616

1717
[SerializeField] private PopupViewType activeViewType;

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ namespace GitHub.Unity
77
[Serializable]
88
class UserSettingsView : Subview
99
{
10+
private static readonly Vector2 viewSize = new Vector2(325, 125);
11+
private const string WindowTitle = "User Settings";
12+
1013
private const string GitConfigTitle = "Git Configuration";
1114
private const string GitConfigNameLabel = "Name";
1215
private const string GitConfigEmailLabel = "Email";
@@ -21,6 +24,13 @@ class UserSettingsView : Subview
2124
[SerializeField] private string newGitEmail;
2225
[SerializeField] private User cachedUser;
2326

27+
public override void InitializeView(IView parent)
28+
{
29+
base.InitializeView(parent);
30+
Title = WindowTitle;
31+
Size = viewSize;
32+
}
33+
2434
public override void OnDataUpdate()
2535
{
2636
base.OnDataUpdate();
@@ -87,6 +97,7 @@ public override void OnGUI()
8797
{
8898
isBusy = false;
8999
Redraw();
100+
Finish(true);
90101
})
91102
.Start();
92103
}
@@ -100,23 +111,26 @@ private void MaybeUpdateData()
100111
{
101112
if (Repository == null)
102113
{
103-
if ((cachedUser == null || String.IsNullOrEmpty(cachedUser.Name)) && GitClient != null)
114+
if (!String.IsNullOrEmpty(EntryPoint.Environment.GitExecutablePath))
104115
{
105-
var user = new User();
106-
GitClient.GetConfig("user.name", GitConfigSource.User)
107-
.Then((success, value) => user.Name = value).Then(
108-
GitClient.GetConfig("user.email", GitConfigSource.User)
109-
.Then((success, value) => user.Email = value))
110-
.FinallyInUI((success, ex) =>
116+
if ((cachedUser == null || String.IsNullOrEmpty(cachedUser.Name)) && GitClient != null)
111117
{
112-
if (success && !String.IsNullOrEmpty(user.Name))
113-
{
114-
cachedUser = user;
115-
userDataHasChanged = true;
116-
Redraw();
117-
}
118-
})
119-
.Start();
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+
}
120134
}
121135

122136
if (userDataHasChanged)

0 commit comments

Comments
 (0)