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

Commit bb0d6f1

Browse files
authored
Merge pull request #91 from github-for-unity/fixes/settings-view-git-remote-update
Updating display correctly when the remote is removed
2 parents a6c5049 + 0f83c8c commit bb0d6f1

File tree

21 files changed

+826
-610
lines changed

21 files changed

+826
-610
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,22 @@ public ITask InitializeRepository()
104104
}))
105105
.Then(GitClient.Add(filesForInitialCommit))
106106
.Then(GitClient.Commit("Initial commit", null))
107-
.Then(RestartRepository)
107+
.Then(_ =>
108+
{
109+
Environment.InitializeRepository();
110+
RestartRepository();
111+
})
108112
.ThenInUI(InitializeUI);
109113
return task;
110114
}
111115

112116
public void RestartRepository()
113117
{
114-
Environment.InitializeRepository();
115118
if (Environment.RepositoryPath != null)
116119
{
117120
repositoryManager = Unity.RepositoryManager.CreateInstance(Platform, TaskManager, UsageTracker, GitClient, Environment.RepositoryPath);
118-
Environment.Repository = new Repository(GitClient, repositoryManager, Environment.RepositoryPath);
119121
repositoryManager.Initialize();
120-
Environment.Repository.Initialize();
122+
Environment.Repository.Initialize(repositoryManager);
121123
repositoryManager.Start();
122124
Logger.Trace($"Got a repository? {Environment.Repository}");
123125
}

src/GitHub.Api/Git/GitConfig.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
54
using System.Text;
65
using System.Text.RegularExpressions;
76

87
namespace GitHub.Unity
98
{
9+
[Serializable]
1010
public struct ConfigRemote
1111
{
12-
public string Name { get; set; }
13-
public string Url { get; set; }
12+
public string Name;
13+
public string Url;
1414

1515
public override string ToString()
1616
{
1717
return String.Format("{{Remote {0} {1}}}", Name, Url);
1818
}
1919
}
2020

21+
[Serializable]
2122
public struct ConfigBranch
2223
{
23-
public string Name { get; set; }
24-
public ConfigRemote? Remote { get; set; }
24+
public string Name;
25+
public ConfigRemote? Remote;
2526
public bool IsTracking => Remote.HasValue;
2627

2728
public override string ToString()

src/GitHub.Api/Git/GitRemote.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public override string ToString()
2929
sb.AppendLine(String.Format("URL: {0}", Url));
3030
sb.AppendLine(String.Format("Login: {0}", Login));
3131
sb.AppendLine(String.Format("User: {0}", User));
32-
sb.AppendLine(String.Format("Token: {0}", Token));
3332
sb.AppendLine(String.Format("Host: {0}", Host));
3433
sb.AppendLine(String.Format("Function: {0}", Function));
3534
return sb.ToString();

src/GitHub.Api/Git/IRepository.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitHub.Unity
88
/// </summary>
99
interface IRepository : IEquatable<IRepository>
1010
{
11-
void Initialize();
11+
void Initialize(IRepositoryManager repositoryManager);
1212
void Refresh();
1313
ITask SetupRemote(string remoteName, string remoteUrl);
1414
ITask Pull();
@@ -42,16 +42,17 @@ interface IRepository : IEquatable<IRepository>
4242
/// <summary>
4343
/// Gets the current remote of the repository.
4444
/// </summary>
45-
ConfigRemote? CurrentRemote { get; }
45+
ConfigRemote? CurrentRemote { get; set; }
4646
/// <summary>
4747
/// Gets the current branch of the repository.
4848
/// </summary>
49-
string CurrentBranch { get; }
50-
GitStatus CurrentStatus { get; }
49+
ConfigBranch? CurrentBranch { get; set; }
50+
GitStatus CurrentStatus { get; set; }
5151
IEnumerable<GitBranch> LocalBranches { get; }
5252
IEnumerable<GitBranch> RemoteBranches { get; }
5353
IUser User { get; set; }
5454
IEnumerable<GitLock> CurrentLocks { get; }
55+
string CurrentBranchName { get; }
5556

5657
event Action<GitStatus> OnStatusUpdated;
5758
event Action<string> OnActiveBranchChanged;

src/GitHub.Api/Git/Repository.cs

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
using System.Diagnostics;
44
using System.Globalization;
55
using System.Linq;
6-
using System.Threading.Tasks;
76

87
namespace GitHub.Unity
98
{
109
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1110
class Repository : IRepository, IEquatable<Repository>
1211
{
13-
private readonly IGitClient gitClient;
14-
private readonly IRepositoryManager repositoryManager;
12+
private IRepositoryManager repositoryManager;
13+
private ConfigBranch? currentBranch;
14+
private ConfigRemote? currentRemote;
15+
private GitStatus currentStatus;
1516

1617
public event Action<GitStatus> OnStatusUpdated;
1718
public event Action<string> OnActiveBranchChanged;
@@ -22,54 +23,45 @@ class Repository : IRepository, IEquatable<Repository>
2223
public event Action OnRepositoryInfoChanged;
2324

2425
public IEnumerable<GitBranch> LocalBranches => repositoryManager.LocalBranches.Values.Select(
25-
x => new GitBranch(x.Name, (x.IsTracking ? (x.Remote.Value.Name + "/" + x.Name) : "[None]"), x.Name == CurrentBranch));
26+
x => new GitBranch(x.Name, (x.IsTracking ? (x.Remote.Value.Name + "/" + x.Name) : "[None]"), x.Name == CurrentBranch?.Name));
2627

2728
public IEnumerable<GitBranch> RemoteBranches => repositoryManager.RemoteBranches.Values.SelectMany(
2829
x => x.Values).Select(x => new GitBranch(x.Remote.Value.Name + "/" + x.Name, "[None]", false));
2930

3031
/// <summary>
3132
/// Initializes a new instance of the <see cref="Repository"/> class.
3233
/// </summary>
33-
/// <param name="gitClient"></param>
3434
/// <param name="repositoryManager"></param>
3535
/// <param name="name">The repository name.</param>
3636
/// <param name="cloneUrl">The repository's clone URL.</param>
3737
/// <param name="localPath"></param>
38-
public Repository(IGitClient gitClient, IRepositoryManager repositoryManager, NPath localPath)
38+
public Repository(string name, NPath localPath)
3939
{
40-
Guard.ArgumentNotNull(repositoryManager, nameof(repositoryManager));
40+
Guard.ArgumentNotNullOrWhiteSpace(name, nameof(name));
41+
Guard.ArgumentNotNull(localPath, nameof(localPath));
4142

42-
this.gitClient = gitClient;
43-
this.repositoryManager = repositoryManager;
43+
Name = name;
4444
LocalPath = localPath;
45-
if (repositoryManager.ActiveBranch.HasValue)
46-
RepositoryManager_OnActiveBranchChanged(repositoryManager.ActiveBranch?.Name);
47-
if (repositoryManager.ActiveRemote.HasValue)
48-
RepositoryManager_OnActiveRemoteChanged(repositoryManager.ActiveRemote);
49-
SetCloneUrl();
45+
this.User = new User();
46+
}
47+
48+
public void Initialize(IRepositoryManager repositoryManager)
49+
{
50+
Guard.ArgumentNotNull(repositoryManager, nameof(repositoryManager));
5051

52+
this.repositoryManager = repositoryManager;
53+
repositoryManager.OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged;
54+
repositoryManager.OnCommitChanged += RepositoryManager_OnHeadChanged;
55+
repositoryManager.OnLocksUpdated += RepositoryManager_OnLocksUpdated;
5156
repositoryManager.OnStatusUpdated += RepositoryManager_OnStatusUpdated;
5257
repositoryManager.OnActiveBranchChanged += RepositoryManager_OnActiveBranchChanged;
5358
repositoryManager.OnActiveRemoteChanged += RepositoryManager_OnActiveRemoteChanged;
54-
repositoryManager.OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged;
55-
repositoryManager.OnHeadChanged += RepositoryManager_OnHeadChanged;
56-
repositoryManager.OnLocksUpdated += RepositoryManager_OnLocksUpdated;
57-
repositoryManager.OnRemoteOrTrackingChanged += SetCloneUrl;
58-
}
59-
60-
public void Initialize()
61-
{
62-
User = new User();
63-
gitClient.GetConfig("user.name", GitConfigSource.User)
64-
.Then((s, x) => User.Name = x)
65-
.Then(gitClient.GetConfig("user.email", GitConfigSource.User))
66-
.Then((s, x) => User.Email = x)
67-
.Start();
59+
repositoryManager.OnGitUserLoaded += user => User = user;
6860
}
6961

7062
public void Refresh()
7163
{
72-
repositoryManager.Refresh();
64+
repositoryManager?.Refresh();
7365
}
7466

7567
public ITask SetupRemote(string remote, string remoteUrl)
@@ -88,12 +80,12 @@ public ITask SetupRemote(string remote, string remoteUrl)
8880

8981
public ITask Pull()
9082
{
91-
return repositoryManager.Pull(CurrentRemote.Value.Name, CurrentBranch);
83+
return repositoryManager.Pull(CurrentRemote.Value.Name, CurrentBranch?.Name);
9284
}
9385

9486
public ITask Push()
9587
{
96-
return repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch);
88+
return repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch?.Name);
9789
}
9890

9991
public ITask Fetch()
@@ -131,17 +123,19 @@ private void SetCloneUrl()
131123
OnRepositoryInfoChanged?.Invoke();
132124
}
133125

126+
private void RepositoryManager_OnStatusUpdated(GitStatus status)
127+
{
128+
CurrentStatus = status;
129+
}
130+
134131
private void RepositoryManager_OnActiveRemoteChanged(ConfigRemote? remote)
135132
{
136133
CurrentRemote = remote;
137-
SetCloneUrl();
138-
OnActiveRemoteChanged?.Invoke(CurrentRemote.HasValue ? CurrentRemote.Value.Name : null);
139134
}
140135

141-
private void RepositoryManager_OnActiveBranchChanged(string branch)
136+
private void RepositoryManager_OnActiveBranchChanged(ConfigBranch? branch)
142137
{
143138
CurrentBranch = branch;
144-
OnActiveBranchChanged?.Invoke(CurrentBranch);
145139
}
146140

147141
private void RepositoryManager_OnHeadChanged()
@@ -154,12 +148,6 @@ private void RepositoryManager_OnLocalBranchListChanged()
154148
OnLocalBranchListChanged?.Invoke();
155149
}
156150

157-
private void RepositoryManager_OnStatusUpdated(GitStatus status)
158-
{
159-
CurrentStatus = status;
160-
OnStatusUpdated?.Invoke(CurrentStatus);
161-
}
162-
163151
private void RepositoryManager_OnLocksUpdated(IEnumerable<GitLock> locks)
164152
{
165153
CurrentLocks = locks;
@@ -197,25 +185,51 @@ public bool Equals(IRepository other)
197185
object.Equals(LocalPath, other.LocalPath);
198186
}
199187

200-
public override string ToString()
188+
public ConfigBranch? CurrentBranch
201189
{
202-
return DebuggerDisplay;
190+
get { return currentBranch; }
191+
set
192+
{
193+
if (currentBranch.HasValue != value.HasValue || (currentBranch.HasValue && !currentBranch.Value.Equals(value.Value)))
194+
{
195+
currentBranch = value;
196+
Logger.Trace("OnActiveBranchChanged: {0}", value?.ToString() ?? "NULL");
197+
OnActiveBranchChanged?.Invoke(CurrentBranch.HasValue ? CurrentBranch.Value.Name : null);
198+
}
199+
}
203200
}
204-
205201
/// <summary>
206202
/// Gets the current branch of the repository.
207203
/// </summary>
208-
public string CurrentBranch { get; private set; }
204+
205+
public string CurrentBranchName => currentBranch?.Name;
209206

210207
/// <summary>
211208
/// Gets the current remote of the repository.
212209
/// </summary>
213-
public ConfigRemote? CurrentRemote { get; private set; }
210+
public ConfigRemote? CurrentRemote
211+
{
212+
get { return currentRemote; }
213+
set
214+
{
215+
if (currentRemote.HasValue != value.HasValue || (currentRemote.HasValue && !currentRemote.Value.Equals(value.Value)))
216+
{
217+
currentRemote = value;
218+
SetCloneUrl();
219+
Logger.Trace("OnActiveRemoteChanged: {0}", value?.ToString() ?? "NULL");
220+
OnActiveRemoteChanged?.Invoke(CurrentRemote.HasValue ? CurrentRemote.Value.Name : null);
221+
}
222+
}
223+
}
214224

215-
public string Name { get; private set; }
216225
public UriString CloneUrl { get; private set; }
226+
227+
public string Name { get; private set; }
228+
217229
public NPath LocalPath { get; private set; }
230+
218231
public string Owner => CloneUrl?.Owner ?? null;
232+
219233
public bool IsGitHub { get { return HostAddress.IsGitHubDotCom(CloneUrl); } }
220234

221235
internal string DebuggerDisplay => String.Format(
@@ -227,10 +241,19 @@ public override string ToString()
227241
CloneUrl,
228242
LocalPath,
229243
CurrentBranch,
230-
CurrentRemote?.Name
231-
);
244+
CurrentRemote);
245+
246+
public GitStatus CurrentStatus
247+
{
248+
get { return currentStatus; }
249+
set
250+
{
251+
Logger.Trace("OnStatusUpdated: {0}", value.ToString());
252+
currentStatus = value;
253+
OnStatusUpdated?.Invoke(CurrentStatus);
254+
}
255+
}
232256

233-
public GitStatus CurrentStatus { get; private set; }
234257
public IUser User { get; set; }
235258
public IEnumerable<GitLock> CurrentLocks { get; private set; }
236259
protected static ILogging Logger { get; } = Logging.GetLogger<Repository>();

0 commit comments

Comments
 (0)