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

Commit f531eb1

Browse files
committed
Use the Repository as the data container
Given that the repository object is created and managed by the RepositoryManager, there's no reason to keep data in the repository manager object. We can keep all the data in the repository object and have it trigger events for data updates, while events that are not directly related to data changes in the repository data model are triggered directly from the repository manager. This way, the manager manages things and the repository models things, which feels like a nicer separation of duties. The repository listens for events from the repository manager in order to update its data, so the repository manager doesn't have to have a reference to the repository (breaking the circular dependency)
1 parent 6e6b9aa commit f531eb1

File tree

12 files changed

+179
-200
lines changed

12 files changed

+179
-200
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,11 @@ public ITask InitializeRepository()
109109

110110
public void RestartRepository()
111111
{
112-
Environment.InitializeRepository();
113112
if (Environment.RepositoryPath != null)
114113
{
115114
repositoryManager = Unity.RepositoryManager.CreateInstance(Platform, TaskManager, UsageTracker, GitClient, Environment.RepositoryPath);
116-
Environment.Repository = new Repository(GitClient, repositoryManager, Environment.RepositoryPath);
117115
repositoryManager.Initialize();
118-
Environment.Repository.Initialize();
116+
Environment.Repository.Initialize(repositoryManager);
119117
repositoryManager.Start();
120118
Logger.Trace($"Got a repository? {Environment.Repository}");
121119
}

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: 72 additions & 48 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,43 @@ 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();
50-
51-
repositoryManager.OnStatusUpdated += RepositoryManager_OnStatusUpdated;
52-
repositoryManager.OnActiveBranchChanged += RepositoryManager_OnActiveBranchChanged;
53-
repositoryManager.OnActiveRemoteChanged += RepositoryManager_OnActiveRemoteChanged;
54-
repositoryManager.OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged;
55-
repositoryManager.OnHeadChanged += RepositoryManager_OnHeadChanged;
56-
repositoryManager.OnLocksUpdated += RepositoryManager_OnLocksUpdated;
57-
repositoryManager.OnRemoteOrTrackingChanged += SetCloneUrl;
5845
}
5946

60-
public void Initialize()
47+
public void Initialize(IRepositoryManager repositoryManager)
6148
{
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();
49+
Guard.ArgumentNotNull(repositoryManager, nameof(repositoryManager));
50+
51+
this.repositoryManager = repositoryManager;
52+
repositoryManager.OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged;
53+
repositoryManager.OnCommitChanged += RepositoryManager_OnHeadChanged;
54+
repositoryManager.OnLocksUpdated += RepositoryManager_OnLocksUpdated;
55+
repositoryManager.OnStatusUpdated += status => CurrentStatus = status;
56+
repositoryManager.OnActiveBranchChanged += branch => CurrentBranch = branch;
57+
repositoryManager.OnActiveRemoteChanged += remote => CurrentRemote = remote;
6858
}
6959

7060
public void Refresh()
7161
{
72-
repositoryManager.Refresh();
62+
repositoryManager?.Refresh();
7363
}
7464

7565
public ITask SetupRemote(string remote, string remoteUrl)
@@ -88,12 +78,12 @@ public ITask SetupRemote(string remote, string remoteUrl)
8878

8979
public ITask Pull()
9080
{
91-
return repositoryManager.Pull(CurrentRemote.Value.Name, CurrentBranch);
81+
return repositoryManager.Pull(CurrentRemote.Value.Name, CurrentBranch?.Name);
9282
}
9383

9484
public ITask Push()
9585
{
96-
return repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch);
86+
return repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch?.Name);
9787
}
9888

9989
public ITask Fetch()
@@ -154,12 +144,6 @@ private void RepositoryManager_OnLocalBranchListChanged()
154144
OnLocalBranchListChanged?.Invoke();
155145
}
156146

157-
private void RepositoryManager_OnStatusUpdated(GitStatus status)
158-
{
159-
CurrentStatus = status;
160-
OnStatusUpdated?.Invoke(CurrentStatus);
161-
}
162-
163147
private void RepositoryManager_OnLocksUpdated(IEnumerable<GitLock> locks)
164148
{
165149
CurrentLocks = locks;
@@ -197,23 +181,55 @@ public bool Equals(IRepository other)
197181
object.Equals(LocalPath, other.LocalPath);
198182
}
199183

200-
public override string ToString()
184+
public ConfigBranch? CurrentBranch
201185
{
202-
return DebuggerDisplay;
186+
get { return currentBranch; }
187+
set
188+
{
189+
if (currentBranch.HasValue != value.HasValue || (currentBranch.HasValue && !currentBranch.Value.Equals(value.Value)))
190+
{
191+
currentBranch = value;
192+
Logger.Trace("OnActiveBranchChanged: {0}", value?.ToString() ?? "NULL");
193+
OnActiveBranchChanged?.Invoke(currentBranch?.Name);
194+
}
195+
}
203196
}
204-
205197
/// <summary>
206198
/// Gets the current branch of the repository.
207199
/// </summary>
208-
public string CurrentBranch { get; private set; }
200+
201+
public string CurrentBranchName => currentBranch?.Name;
209202

210203
/// <summary>
211204
/// Gets the current remote of the repository.
212205
/// </summary>
213-
public ConfigRemote? CurrentRemote { get; private set; }
206+
public ConfigRemote? CurrentRemote
207+
{
208+
get { return currentRemote; }
209+
set
210+
{
211+
if (currentRemote.HasValue != value.HasValue || (currentRemote.HasValue && !currentRemote.Value.Equals(value.Value)))
212+
{
213+
currentRemote = value;
214+
Logger.Trace("OnActiveRemoteChanged: {0}", value?.ToString() ?? "NULL");
215+
OnActiveRemoteChanged?.Invoke(value?.Name);
216+
}
217+
}
218+
}
219+
220+
public UriString CloneUrl
221+
{
222+
get
223+
{
224+
if (CurrentRemote.HasValue && CurrentRemote.Value.Url != null)
225+
return new UriString(CurrentRemote.Value.Url).ToRepositoryUrl();
226+
227+
return null;
228+
}
229+
}
230+
214231

215232
public string Name { get; private set; }
216-
public UriString CloneUrl { get; private set; }
217233
public NPath LocalPath { get; private set; }
218234
public string Owner => CloneUrl?.Owner ?? null;
219235
public bool IsGitHub { get { return HostAddress.IsGitHubDotCom(CloneUrl); } }
@@ -226,11 +242,19 @@ public override string ToString()
226242
Name,
227243
CloneUrl,
228244
LocalPath,
229-
CurrentBranch,
230-
CurrentRemote?.Name
231-
);
245+
GetHashCode());
246+
247+
public GitStatus CurrentStatus
248+
{
249+
get { return currentStatus; }
250+
set
251+
{
252+
Logger.Trace("OnStatusUpdated: {0}", value.ToString());
253+
currentStatus = value;
254+
OnStatusUpdated?.Invoke(value);
255+
}
256+
}
232257

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

0 commit comments

Comments
 (0)