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

Commit 9b5088a

Browse files
Properly updating repository info
1 parent 1756035 commit 9b5088a

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/GitHub.Api/Git/Repository.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,21 @@ public ITask ReleaseLock(string file, bool force)
145145
return repositoryManager.UnlockFile(file, force);
146146
}
147147

148-
private void SetCloneUrl()
148+
private void UpdateRepositoryInfo()
149149
{
150150
if (CurrentRemote.HasValue)
151+
{
151152
CloneUrl = new UriString(CurrentRemote.Value.Url);
153+
Name = CloneUrl.RepositoryName;
154+
Logger.Trace("CloneUrl: {0}", CloneUrl.ToString());
155+
}
152156
else
157+
{
153158
CloneUrl = null;
154-
Name = CloneUrl != null ? CloneUrl.RepositoryName : LocalPath.FileName;
159+
Name = LocalPath.FileName;
160+
Logger.Trace("CloneUrl: [NULL]");
161+
}
162+
155163
OnRepositoryInfoChanged?.Invoke();
156164
}
157165

@@ -204,7 +212,11 @@ private void UpdateCurrentBranchAndRemote()
204212

205213
if (!remote.HasValue)
206214
{
207-
remote = repositoryManager.Config.GetRemotes().FirstOrDefault();
215+
var configRemotes = repositoryManager.Config.GetRemotes().ToArray();
216+
if (configRemotes.Any())
217+
{
218+
remote = configRemotes.FirstOrDefault();
219+
}
208220
}
209221

210222
CurrentRemote = remote;
@@ -334,8 +346,8 @@ public ConfigBranch? CurrentBranch
334346
if (!Nullable.Equals(currentBranch, value))
335347
{
336348
currentBranch = value;
337-
Logger.Trace("OnCurrentBranchChanged: {0}", value?.ToString() ?? "NULL");
338-
OnCurrentBranchChanged?.Invoke(CurrentBranch.HasValue ? CurrentBranch.Value.Name : null);
349+
Logger.Trace("OnCurrentBranchChanged: {0}", currentBranch.HasValue ? currentBranch.ToString() : "[NULL]");
350+
OnCurrentBranchChanged?.Invoke(currentBranch.HasValue ? currentBranch.Value.Name : null);
339351
}
340352
}
341353
}
@@ -356,9 +368,9 @@ public ConfigRemote? CurrentRemote
356368
if (!Nullable.Equals(currentRemote, value))
357369
{
358370
currentRemote = value;
359-
SetCloneUrl();
360-
Logger.Trace("OnCurrentRemoteChanged: {0}", value?.ToString() ?? "NULL");
361-
OnCurrentRemoteChanged?.Invoke(CurrentRemote.HasValue ? CurrentRemote.Value.Name : null);
371+
Logger.Trace("OnCurrentRemoteChanged: {0}", currentRemote.HasValue ? currentRemote.Value.ToString() : "[NULL]");
372+
OnCurrentRemoteChanged?.Invoke(currentRemote.HasValue ? currentRemote.Value.Name : null);
373+
UpdateRepositoryInfo();
362374
}
363375
}
364376
}

src/tests/UnitTests/Git/RepositoryTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ private static Repository LoadRepository()
3131
}
3232

3333
private RepositoryEvents repositoryEvents;
34+
private TimeSpan repositoryEventsTimeout;
3435

3536
[SetUp]
3637
public void OnSetup()
3738
{
3839
repositoryEvents = new RepositoryEvents();
40+
repositoryEventsTimeout = TimeSpan.FromSeconds(0.5);
3941
}
4042

4143
[Test]
@@ -89,16 +91,17 @@ public void Repository()
8991

9092
repositoryManager.OnLocalBranchListUpdated += Raise.Event<Action<Dictionary<string, ConfigBranch>>>(branchDictionary);
9193

92-
repositoryEvents.OnLocalBranchListChanged.WaitOne().Should().BeTrue();
94+
repositoryEvents.OnLocalBranchListChanged.WaitOne(repositoryEventsTimeout).Should().BeTrue("OnLocalBranchListChanged not raised");
9395

9496
repositoryManager.OnRemoteBranchListUpdated += Raise.Event<Action<Dictionary<string, Dictionary<string, ConfigBranch>>>>(remoteBranchDictionary);
9597

96-
repositoryEvents.OnRemoteBranchListChanged.WaitOne().Should().BeTrue();
98+
repositoryEvents.OnRemoteBranchListChanged.WaitOne(repositoryEventsTimeout).Should().BeTrue("OnRemoteBranchListChanged not raised");
9799

98100
repositoryManager.OnHeadUpdated += Raise.Event<Action<string>>("ref:refs/heads/master");
99101

100-
repositoryEvents.OnCurrentBranchChanged.WaitOne().Should().BeTrue();
101-
repositoryEvents.OnCurrentRemoteChanged.WaitOne().Should().BeTrue();
102+
repositoryEvents.OnCurrentBranchChanged.WaitOne(repositoryEventsTimeout).Should().BeTrue("OnCurrentBranchChanged not raised");
103+
repositoryEvents.OnCurrentRemoteChanged.WaitOne(repositoryEventsTimeout).Should().BeTrue("OnCurrentRemoteChanged not raised");
104+
repositoryEvents.OnRepositoryInfoChanged.WaitOne(repositoryEventsTimeout).Should().BeTrue("OnRepositoryInfoChanged not raised");
102105

103106
expectedBranch.Should().Be("master");
104107
expectedRemote.Should().Be("origin");

0 commit comments

Comments
 (0)