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

Commit 3d0a43c

Browse files
haackedshana
authored andcommitted
Merge pull request #441 from github/shana/431-fix-vs-hang-some-more
Avoid calling ActiveRepositories in the main thread, it blocks on startup
2 parents 6cb70e4 + 06562db commit 3d0a43c

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/GitHub.App/Services/RepositoryPublishService.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reactive.Linq;
55
using GitHub.Api;
66
using GitHub.Models;
7+
using LibGit2Sharp;
78

89
namespace GitHub.Services
910
{
@@ -12,24 +13,21 @@ namespace GitHub.Services
1213
public class RepositoryPublishService : IRepositoryPublishService
1314
{
1415
readonly IGitClient gitClient;
15-
readonly IVSServices services;
16+
readonly Repository activeRepository;
1617

1718
[ImportingConstructor]
1819
public RepositoryPublishService(IGitClient gitClient, IVSServices services)
1920
{
2021
this.gitClient = gitClient;
21-
this.services = services;
22+
this.activeRepository = services.GetActiveRepo();
2223
}
2324

2425
public string LocalRepositoryName
2526
{
2627
get
2728
{
28-
var repo = services.GetActiveRepo();
29-
if (repo != null && repo.Info != null && !string.IsNullOrEmpty(repo.Info.WorkingDirectory))
30-
{
31-
return new DirectoryInfo(repo.Info.WorkingDirectory).Name ?? "";
32-
}
29+
if (!string.IsNullOrEmpty(activeRepository?.Info?.WorkingDirectory))
30+
return new DirectoryInfo(activeRepository.Info.WorkingDirectory).Name ?? "";
3331
return string.Empty;
3432
}
3533
}
@@ -39,7 +37,7 @@ public string LocalRepositoryName
3937
IAccount account,
4038
IApiClient apiClient)
4139
{
42-
return Observable.Defer(() => Observable.Return(services.GetActiveRepo()))
40+
return Observable.Defer(() => Observable.Return(activeRepository))
4341
.SelectMany(r => apiClient.CreateRepository(newRepository, account.Login, account.IsUser)
4442
.Select(gitHubRepo => Tuple.Create(gitHubRepo, r)))
4543
.SelectMany(repo => gitClient.SetRemote(repo.Item2, "origin", new Uri(repo.Item1.CloneUrl)).Select(_ => repo))

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,8 @@ public string GetActiveRepoPath()
8282
var gitExt = serviceProvider.GetService<IGitExt>();
8383
if (gitExt.ActiveRepositories.Count > 0)
8484
return gitExt.ActiveRepositories.First().RepositoryPath;
85-
else
86-
{
87-
var repo = serviceProvider.GetSolution().GetRepoFromSolution();
88-
if (repo != null)
89-
return repo.Info.Path;
90-
}
91-
return string.Empty;
85+
var repo = serviceProvider.GetSolution().GetRepoFromSolution();
86+
return repo?.Info?.Path ?? string.Empty;
9287
}
9388

9489
static string PokeTheRegistryForLocalClonePath()

src/GitHub.VisualStudio/Base/TeamExplorerServiceHolder.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace GitHub.VisualStudio.Base
1717
[PartCreationPolicy(CreationPolicy.Shared)]
1818
public class TeamExplorerServiceHolder : ITeamExplorerServiceHolder
1919
{
20-
Dictionary<object, Action<IGitRepositoryInfo>> activeRepoHandlers = new Dictionary<object, Action<IGitRepositoryInfo>>();
20+
readonly Dictionary<object, Action<IGitRepositoryInfo>> activeRepoHandlers = new Dictionary<object, Action<IGitRepositoryInfo>>();
2121
IGitRepositoryInfo activeRepo;
2222
bool activeRepoNotified = false;
2323

@@ -115,7 +115,7 @@ void UIContextChanged(object sender, UIContextChangedEventArgs e)
115115
UIContextChanged(e.Activated);
116116
}
117117

118-
void UIContextChanged(bool active)
118+
async void UIContextChanged(bool active)
119119
{
120120
Debug.Assert(ServiceProvider != null, "UIContextChanged called before service provider is set");
121121
if (ServiceProvider == null)
@@ -125,25 +125,26 @@ void UIContextChanged(bool active)
125125
{
126126
GitService = GitService ?? ServiceProvider.GetService<IGitExt>();
127127
if (ActiveRepo == null)
128-
ActiveRepo = gitService.ActiveRepositories.FirstOrDefault();
128+
ActiveRepo = await System.Threading.Tasks.Task.Run(() => GitService.ActiveRepositories.FirstOrDefault());
129129
}
130130
else
131131
ActiveRepo = null;
132132
}
133133

134134
void CheckAndUpdate(object sender, System.ComponentModel.PropertyChangedEventArgs e)
135135
{
136+
if (e.PropertyName != "ActiveRepositories")
137+
return;
138+
136139
var service = GitService;
137140
if (service == null)
138141
return;
139142

140-
if (e.PropertyName == "ActiveRepositories")
141-
{
142-
var repo = service.ActiveRepositories.FirstOrDefault();
143-
if (!repo.Compare(ActiveRepo))
144-
// so annoying that this is on the wrong thread
145-
syncContext.Post(r => ActiveRepo = r as IGitRepositoryInfo, repo);
146-
}
143+
var repo = service.ActiveRepositories.FirstOrDefault();
144+
// this comparison is safe, the extension method supports null instances
145+
if (!repo.Compare(ActiveRepo))
146+
// so annoying that this is on the wrong thread
147+
syncContext.Post(r => ActiveRepo = r as IGitRepositoryInfo, repo);
147148
}
148149

149150
public IGitAwareItem HomeSection

0 commit comments

Comments
 (0)