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

Fixes to update name/email in Settings View #174

Merged
merged 7 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/GitHub.Api/Git/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public ITask Revert(string changeset)

public ITask ListLocks()
{
if (repositoryManager == null)
return new ActionTask(TaskExtensions.CompletedTask);
return repositoryManager.ListLocks(false);
}

Expand Down Expand Up @@ -264,9 +266,16 @@ interface IUser
string Name { get; set; }
string Email { get; set; }
}

[Serializable]
class User : IUser
{
public string Name { get; set; }
public string Email { get; set; }

public override string ToString()
{
return String.Format("Name: {0} Email: {1}", Name, Email);
}
}
}
3 changes: 2 additions & 1 deletion src/GitHub.Api/Git/RepositoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ private void LoadGitUser()
.Then((success, value) => user.Name = value).Then(
GitClient.GetConfig("user.email", GitConfigSource.User)
.Then((success, value) => user.Email = value))
.Then(() => OnGitUserLoaded?.Invoke(user));
.Then(() => OnGitUserLoaded?.Invoke(user))
.Start();
}

private void SetupWatcher()
Expand Down
14 changes: 14 additions & 0 deletions src/GitHub.Api/NewTaskSystem/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ namespace GitHub.Unity
{
static class TaskExtensions
{
private static Task completedTask;

public static Task CompletedTask
{
get
{
if (completedTask == null)
{
completedTask = TaskEx.FromResult(true);
}
return completedTask;
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "task")]
public static void Forget(this Task task)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public IEnvironment Environment
unityVersion = Application.unityVersion;
}
environment.Initialize(unityVersion, extensionInstallPath.ToNPath(), unityApplication.ToNPath(), unityAssetsPath.ToNPath());
environment.InitializeRepository(repositoryPath != null ? repositoryPath.ToNPath() : null);
environment.InitializeRepository(!String.IsNullOrEmpty(repositoryPath) ? repositoryPath.ToNPath() : null);
Flush();
}
return environment;
Expand Down
118 changes: 95 additions & 23 deletions src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ class SettingsView : Subview
[SerializeField] private int lockedFileSelection = -1;
[SerializeField] private bool hasRemote;
[SerializeField] private bool remoteHasChanged;
[SerializeField] private bool userDataHasChanged;
[NonSerialized] private bool userDataHasChanged;

[SerializeField] private string newGitName;
[SerializeField] private string newGitEmail;
[SerializeField] private string newRepositoryRemoteUrl;
[SerializeField] private User cachedUser;

public override void OnEnable()
{
Expand All @@ -113,13 +114,16 @@ public override void OnRepositoryChanged(IRepository oldRepository)

DetachHandlers(oldRepository);
AttachHandlers(Repository);
activeRemote = Repository.CurrentRemote;
remoteHasChanged = true;
Refresh();
}

public override void Refresh()
{
base.Refresh();
Repository.ListLocks().Start();
if (Repository != null)
Repository.ListLocks().Start();
}

private void AttachHandlers(IRepository repository)
Expand All @@ -144,12 +148,12 @@ public override void OnGUI()
{
scroll = GUILayout.BeginScrollView(scroll);
{
if (Repository != null)
{
OnUserSettingsGUI();
OnUserSettingsGUI();

GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);

if (Repository != null)
{
OnRepositorySettingsGUI();

GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
Expand All @@ -172,35 +176,63 @@ private void MaybeUpdateData()
if (lockedFiles == null)
lockedFiles = new List<GitLock>();

userDataHasChanged = Repository == null || (Repository.User.Name == gitName && Repository.User.Email == gitEmail);

if (Repository == null)
{
if (cachedUser == null || String.IsNullOrEmpty(cachedUser.Name))
{
var user = new User();
GitClient.GetConfig("user.name", GitConfigSource.User)
.Then((success, value) => user.Name = value).Then(
GitClient.GetConfig("user.email", GitConfigSource.User)
.Then((success, value) => user.Email = value))
.FinallyInUI((success, ex) =>
{
if (success && !String.IsNullOrEmpty(user.Name))
{
cachedUser = user;
userDataHasChanged = true;
Redraw();
}
})
.Start();
}

if (userDataHasChanged)
{
newGitName = gitName = cachedUser.Name;
newGitEmail = gitEmail = cachedUser.Email;
userDataHasChanged = false;
}
return;
}

userDataHasChanged = Repository.User.Name != gitName || Repository.User.Email != gitEmail;

if (!remoteHasChanged && !userDataHasChanged)
return;

if (userDataHasChanged)
{
userDataHasChanged = false;
newGitName = gitName = Repository.User.Name;
newGitEmail = gitEmail = Repository.User.Email;
}

if (remoteHasChanged)
{
remoteHasChanged = false;
hasRemote = activeRemote.HasValue && !String.IsNullOrEmpty(activeRemote.Value.Url);
if (!hasRemote)
{
repositoryRemoteName = DefaultRepositoryRemoteName;
repositoryRemoteUrl = string.Empty;
newRepositoryRemoteUrl = repositoryRemoteUrl = string.Empty;
}
else
{
repositoryRemoteName = activeRemote.Value.Name;
repositoryRemoteUrl = activeRemote.Value.Url;
newRepositoryRemoteUrl = repositoryRemoteUrl = activeRemote.Value.Url;
}
}

if (userDataHasChanged && Repository != null)
{
gitName = Repository.User.Name;
gitEmail = Repository.User.Email;
}
}

private void ResetToDefaults()
Expand Down Expand Up @@ -247,38 +279,76 @@ private void OnUserSettingsGUI()
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);

var needsSaving = newGitName != gitName || newGitEmail != gitEmail;
EditorGUI.BeginDisabledGroup(needsSaving);
EditorGUI.BeginDisabledGroup(!needsSaving);
{
if (GUILayout.Button(GitConfigUserSave, GUILayout.ExpandWidth(false)))
{
GitClient.SetConfig("user.name", newGitName, GitConfigSource.User)
.Then((success, value) => { if (success) Repository.User.Name = value; })
.Then((success, value) =>
{
if (success)
{
if (Repository != null)
{
Repository.User.Name = value;
}
else
{
if (cachedUser == null)
{
cachedUser = new User();
}
cachedUser.Name = value;
}
}
})
.Then(
GitClient.SetConfig("user.email", newGitEmail, GitConfigSource.User)
.Then((success, value) => { if (success) Repository.User.Email = value; }))
.FinallyInUI((_, __) => isBusy = false)
.Then((success, value) =>
{
if (success)
{
if (Repository != null)
{
Repository.User.Email = value;
}
else
{
cachedUser.Email = value;
userDataHasChanged = true;
}
}
}))
.FinallyInUI((_, __) =>
{
isBusy = false;
Redraw();
})
.Start();
isBusy = true;
}
}
EditorGUI.EndDisabledGroup();
}
EditorGUI.EndDisabledGroup();
}

private void OnRepositorySettingsGUI()
{
GUILayout.Label(GitRepositoryTitle, EditorStyles.boldLabel);

EditorGUI.BeginDisabledGroup(isBusy);
{
newRepositoryRemoteUrl = EditorGUILayout.TextField(GitRepositoryRemoteLabel + ": " + repositoryRemoteName, newRepositoryRemoteUrl);
var needsSaving = newRepositoryRemoteUrl != repositoryRemoteUrl;
EditorGUI.BeginDisabledGroup(needsSaving);
var needsSaving = newRepositoryRemoteUrl != repositoryRemoteUrl && !String.IsNullOrEmpty(newRepositoryRemoteUrl);
EditorGUI.BeginDisabledGroup(!needsSaving);
{
if (GUILayout.Button(GitRepositorySave, GUILayout.ExpandWidth(false)))
{
try
{
isBusy = true;
Repository.SetupRemote(repositoryRemoteName, repositoryRemoteUrl)
Repository.SetupRemote(repositoryRemoteName, newRepositoryRemoteUrl)
.FinallyInUI((_, __) =>
{
isBusy = false;
Expand All @@ -292,7 +362,9 @@ private void OnRepositorySettingsGUI()
}
}
}
EditorGUI.EndDisabledGroup();
}
EditorGUI.EndDisabledGroup();
}

private bool ValidateGitInstall(string path)
Expand Down