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

Commit d3f6af9

Browse files
Making Repository return GitBranch and GitRemote instead of ConfigBranch and ConfigRemote
1 parent 6f0d6ab commit d3f6af9

File tree

3 files changed

+102
-119
lines changed

3 files changed

+102
-119
lines changed

src/GitHub.Api/Git/IRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public interface IRepository : IEquatable<IRepository>
4545
/// <summary>
4646
/// Gets the current remote of the repository.
4747
/// </summary>
48-
ConfigRemote? CurrentRemote { get; set; }
48+
GitRemote? CurrentRemote { get; }
4949
/// <summary>
5050
/// Gets the current branch of the repository.
5151
/// </summary>
52-
ConfigBranch? CurrentBranch { get; set; }
53-
GitStatus CurrentStatus { get; set; }
52+
GitBranch? CurrentBranch { get; }
53+
GitStatus CurrentStatus { get; }
5454
IList<GitRemote> Remotes { get; }
5555
IEnumerable<GitBranch> LocalBranches { get; }
5656
IEnumerable<GitBranch> RemoteBranches { get; }

src/GitHub.Api/Git/Repository.cs

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void Initialize(IRepositoryManager repositoryManager)
4949

5050
this.repositoryManager = repositoryManager;
5151

52-
repositoryManager.OnCurrentBranchUpdated += branch => CurrentBranch = branch;
53-
repositoryManager.OnCurrentRemoteUpdated += remote => CurrentRemote = remote;
52+
repositoryManager.OnCurrentBranchUpdated += OnRepositoryManager_OnCurrentBranchUpdated;
53+
repositoryManager.OnCurrentRemoteUpdated += OnRepositoryManager_OnCurrentRemoteUpdated;
5454
repositoryManager.OnStatusUpdated += status => CurrentStatus = status;
5555
repositoryManager.OnLocksUpdated += locks => CurrentLocks = locks;
5656
repositoryManager.OnLocalBranchListUpdated += OnRepositoryManager_OnLocalBranchListUpdated;
@@ -168,6 +168,27 @@ public bool Equals(IRepository other)
168168
object.Equals(LocalPath, other.LocalPath);
169169
}
170170

171+
private void OnRepositoryManager_OnCurrentRemoteUpdated(ConfigRemote? remote)
172+
{
173+
if (!Nullable.Equals(currentRemote, remote))
174+
{
175+
currentRemote = remote;
176+
Logger.Trace("OnCurrentRemoteChanged: {0}", currentRemote.HasValue ? currentRemote.Value.ToString() : "[NULL]");
177+
OnCurrentRemoteChanged?.Invoke(currentRemote.HasValue ? currentRemote.Value.Name : null);
178+
UpdateRepositoryInfo();
179+
}
180+
}
181+
182+
private void OnRepositoryManager_OnCurrentBranchUpdated(ConfigBranch? branch)
183+
{
184+
if (!Nullable.Equals(currentBranch, branch))
185+
{
186+
currentBranch = branch;
187+
Logger.Trace("OnCurrentBranchChanged: {0}", currentBranch.HasValue ? currentBranch.ToString() : "[NULL]");
188+
OnCurrentBranchChanged?.Invoke(currentBranch.HasValue ? currentBranch.Value.Name : null);
189+
}
190+
}
191+
171192
private void OnRepositoryManager_OnLocalBranchUpdated(string name)
172193
{
173194
if (name == currentBranch?.Name)
@@ -181,10 +202,7 @@ private void OnRepositoryManager_OnRemoteBranchListUpdated(Dictionary<string, Co
181202
{
182203
remotes = updatedRemotes;
183204

184-
Remotes = remotes.Select(pair => new GitRemote {
185-
Name = pair.Value.Name,
186-
Url = pair.Value.Url
187-
}).ToArray();
205+
Remotes = remotes.Select(pair => GetGitRemote(pair.Value)).ToArray();
188206

189207
remoteBranches = branches;
190208

@@ -291,58 +309,59 @@ private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
291309
}
292310
}
293311

294-
public IList<GitRemote> Remotes { get; private set; }
295-
296-
public IEnumerable<GitBranch> LocalBranches => localBranches.Values.Select(x => {
312+
private GitBranch GetLocalGitBranch(ConfigBranch x)
313+
{
297314
var name = x.Name;
298315
var trackingName = x.IsTracking ? x.Remote.Value.Name + "/" + name : "[None]";
299316
var isActive = name == CurrentBranch?.Name;
300317

301318
return new GitBranch(name, trackingName, isActive);
302-
});
319+
}
303320

304-
public IEnumerable<GitBranch> RemoteBranches => remoteBranches.Values.SelectMany(x => x.Values).Select(x => {
321+
private GitBranch GetRemoteGitBranch(ConfigBranch x)
322+
{
305323
var name = x.Remote.Value.Name + "/" + x.Name;
306324
var trackingName = "[None]";
307325

308326
return new GitBranch(name, trackingName, false);
309-
});
327+
}
310328

311-
public ConfigBranch? CurrentBranch
329+
private GitRemote GetGitRemote(ConfigRemote configRemote)
312330
{
313-
get { return currentBranch; }
314-
set
331+
return new GitRemote { Name = configRemote.Name, Url = configRemote.Url };
332+
}
333+
334+
public IList<GitRemote> Remotes { get; private set; }
335+
336+
public IEnumerable<GitBranch> LocalBranches => localBranches.Values.Select(GetLocalGitBranch);
337+
338+
public IEnumerable<GitBranch> RemoteBranches => remoteBranches.Values.SelectMany(x => x.Values).Select(GetRemoteGitBranch);
339+
340+
public GitBranch? CurrentBranch
341+
{
342+
get
315343
{
316-
if (!Nullable.Equals(currentBranch, value))
344+
if (currentBranch != null)
317345
{
318-
currentBranch = value;
319-
Logger.Trace("OnCurrentBranchChanged: {0}", currentBranch.HasValue ? currentBranch.ToString() : "[NULL]");
320-
OnCurrentBranchChanged?.Invoke(currentBranch.HasValue ? currentBranch.Value.Name : null);
346+
return GetLocalGitBranch(currentBranch.Value);
321347
}
348+
349+
return null;
322350
}
323351
}
324352

325-
/// <summary>
326-
/// Gets the current branch of the repository.
327-
/// </summary>
328-
329353
public string CurrentBranchName => currentBranch?.Name;
330354

331-
/// <summary>
332-
/// Gets the current remote of the repository.
333-
/// </summary>
334-
public ConfigRemote? CurrentRemote
355+
public GitRemote? CurrentRemote
335356
{
336-
get { return currentRemote; }
337-
set
357+
get
338358
{
339-
if (!Nullable.Equals(currentRemote, value))
359+
if (currentRemote != null)
340360
{
341-
currentRemote = value;
342-
Logger.Trace("OnCurrentRemoteChanged: {0}", currentRemote.HasValue ? currentRemote.Value.ToString() : "[NULL]");
343-
OnCurrentRemoteChanged?.Invoke(currentRemote.HasValue ? currentRemote.Value.Name : null);
344-
UpdateRepositoryInfo();
361+
return GetGitRemote(currentRemote.Value);
345362
}
363+
364+
return null;
346365
}
347366
}
348367

@@ -370,7 +389,7 @@ public ConfigRemote? CurrentRemote
370389
public GitStatus CurrentStatus
371390
{
372391
get { return currentStatus; }
373-
set
392+
private set
374393
{
375394
currentStatus = value;
376395
Logger.Trace("OnStatusChanged: {0}", value.ToString());

0 commit comments

Comments
 (0)