Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 1aaea46

Browse files
committed
Make sure the ActiveRepo data is always up to date
If the origin remote url changes, update ActiveRepo that as soon as possible.
1 parent 788e726 commit 1aaea46

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/GitHub.Exports/Services/ITeamExplorerServiceHolder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public interface ITeamExplorerServiceHolder
4343
void Unsubscribe(object who);
4444

4545
IGitAwareItem HomeSection { get; }
46+
47+
/// <summary>
48+
/// Refresh the information on the active repo (in case of remote url changes or other such things)
49+
/// </summary>
50+
void Refresh();
4651
}
4752

4853
public interface IGitAwareItem

src/GitHub.VisualStudio/Base/TeamExplorerServiceHolder.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,40 @@ public ISimpleRepositoryModel ActiveRepo
5959
[return: AllowNull] get { return activeRepo; }
6060
private set
6161
{
62-
if (Equals(activeRepo, value))
62+
if (activeRepo == value)
6363
return;
64+
if (activeRepo != null)
65+
activeRepo.PropertyChanged -= ActiveRepoPropertyChanged;
6466
activeRepo = value;
67+
if (activeRepo != null)
68+
activeRepo.PropertyChanged += ActiveRepoPropertyChanged;
6569
NotifyActiveRepo();
6670
}
6771
}
6872

6973
public void Subscribe(object who, Action<ISimpleRepositoryModel> handler)
7074
{
75+
bool notificationsExist;
76+
ISimpleRepositoryModel repo;
7177
lock(activeRepoHandlers)
7278
{
73-
var repo = ActiveRepo;
79+
repo = ActiveRepo;
80+
notificationsExist = activeRepoNotified;
7481
if (!activeRepoHandlers.ContainsKey(who))
7582
activeRepoHandlers.Add(who, handler);
7683
else
7784
activeRepoHandlers[who] = handler;
78-
if (activeRepoNotified)
79-
handler(repo);
8085
}
86+
87+
// the repo url might have changed and we don't get notifications
88+
// for that, so this is a good place to refresh it in case that happened
89+
repo?.Refresh();
90+
91+
// if the active repo hasn't changed and there's notifications queued up,
92+
// notify the subscriber. If the repo has changed, the set above will trigger
93+
// notifications so we don't have to do it here.
94+
if (repo == ActiveRepo && notificationsExist)
95+
handler(repo);
8196
}
8297

8398
public void Unsubscribe(object who)
@@ -101,6 +116,12 @@ public void ClearServiceProvider(IServiceProvider provider)
101116
ServiceProvider = null;
102117
}
103118

119+
public void Refresh()
120+
{
121+
GitUIContext = GitUIContext ?? UIContext.FromUIContextGuid(new Guid("11B8E6D7-C08B-4385-B321-321078CDD1F8"));
122+
UIContextChanged(GitUIContext?.IsActive ?? false, true);
123+
}
124+
104125
void NotifyActiveRepo()
105126
{
106127
lock (activeRepoHandlers)
@@ -114,10 +135,10 @@ void NotifyActiveRepo()
114135
void UIContextChanged(object sender, UIContextChangedEventArgs e)
115136
{
116137
ActiveRepo = null;
117-
UIContextChanged(e.Activated);
138+
UIContextChanged(e.Activated, false);
118139
}
119140

120-
async void UIContextChanged(bool active)
141+
async void UIContextChanged(bool active, bool refresh)
121142
{
122143
Debug.Assert(ServiceProvider != null, "UIContextChanged called before service provider is set");
123144
if (ServiceProvider == null)
@@ -126,7 +147,7 @@ async void UIContextChanged(bool active)
126147
if (active)
127148
{
128149
GitService = GitService ?? ServiceProvider.GetService<IGitExt>();
129-
if (ActiveRepo == null)
150+
if (ActiveRepo == null || refresh)
130151
ActiveRepo = await System.Threading.Tasks.Task.Run(() =>
131152
{
132153
var repos = GitService?.ActiveRepositories;
@@ -158,12 +179,17 @@ void CheckAndUpdate(object sender, System.ComponentModel.PropertyChangedEventArg
158179
return;
159180

160181
var repo = service.ActiveRepositories.FirstOrDefault()?.ToModel();
161-
// this comparison is safe, the extension method supports null instances
162-
if (!repo.Equals(ActiveRepo))
182+
if (repo != ActiveRepo)
163183
// so annoying that this is on the wrong thread
164184
syncContext.Post(r => ActiveRepo = r as ISimpleRepositoryModel, repo);
165185
}
166186

187+
void ActiveRepoPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
188+
{
189+
if (e.PropertyName == "CloneUrl")
190+
ActiveRepo = sender as ISimpleRepositoryModel;
191+
}
192+
167193
public IGitAwareItem HomeSection
168194
{
169195
[return:AllowNull]

0 commit comments

Comments
 (0)