Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/GitHub.TeamFoundation.14/Services/VSGitExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
using Task = System.Threading.Tasks.Task;
using static Microsoft.VisualStudio.VSConstants;

namespace GitHub.VisualStudio.Base
{
Expand Down Expand Up @@ -52,9 +51,15 @@ public VSGitExt(IAsyncServiceProvider asyncServiceProvider, IVSUIContextFactory
// Start with empty array until we have a chance to initialize.
ActiveRepositories = Array.Empty<ILocalRepositoryModel>();

// Initialize when we enter the context of a Git repository
var context = factory.GetUIContext(UICONTEXT.RepositoryOpen_guid);
context.WhenActivated(() => JoinableTaskFactory.RunAsync(InitializeAsync).Task.Forget(log));
// The IGitExt service isn't available when a TFS based solution is opened directly.
// It will become available when moving to a Git based solution (and cause a UIContext event to fire).
// NOTE: I tried using the RepositoryOpen context, but it didn't work consistently.
var context = factory.GetUIContext(new Guid(Guids.GitSccProviderId));
context.WhenActivated(() =>
{
log.Debug("WhenActivated");
JoinableTaskFactory.RunAsync(InitializeAsync).Task.Forget(log);
});
}

async Task InitializeAsync()
Expand Down
14 changes: 8 additions & 6 deletions test/GitHub.TeamFoundation.UnitTests/VSGitExtTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ public class VSGitExtTests
{
public class TheConstructor : TestBaseClass
{
[TestCase(true, 1)]
[TestCase(false, 0)]
public void GetServiceIGitExt_WhenRepositoryOpenIsActive(bool isActive, int expectCalls)
[TestCase(true, Guids.GitSccProviderId, 1)]
[TestCase(true, UICONTEXT.RepositoryOpen_string, 0, Description = "No longer using RepositoryOpen")]
[TestCase(false, Guids.GitSccProviderId, 0)]
public void GetServiceIGitExt_WhenGitSccProviderIsActive(bool isActive, string contextGuidString, int expectCalls)
{
var context = CreateVSUIContext(isActive);
var sp = Substitute.For<IAsyncServiceProvider>();

var target = CreateVSGitExt(context, sp: sp);
var target = CreateVSGitExt(context, sp: sp, contextGuidString: contextGuidString);

sp.Received(expectCalls).GetServiceAsync(typeof(IGitExt));
}
Expand Down Expand Up @@ -210,15 +211,16 @@ static IReadOnlyList<IGitRepositoryInfo> CreateActiveRepositories(params string[
}

static VSGitExt CreateVSGitExt(IVSUIContext context = null, IGitExt gitExt = null, IAsyncServiceProvider sp = null,
ILocalRepositoryModelFactory repoFactory = null, JoinableTaskContext joinableTaskContext = null)
ILocalRepositoryModelFactory repoFactory = null, JoinableTaskContext joinableTaskContext = null, string contextGuidString = null)
{
context = context ?? CreateVSUIContext(true);
gitExt = gitExt ?? CreateGitExt();
var contextGuid = new Guid(contextGuidString ?? Guids.GitSccProviderId);
sp = sp ?? Substitute.For<IAsyncServiceProvider>();
repoFactory = repoFactory ?? Substitute.For<ILocalRepositoryModelFactory>();
joinableTaskContext = joinableTaskContext ?? new JoinableTaskContext();
var factory = Substitute.For<IVSUIContextFactory>();
factory.GetUIContext(UICONTEXT.RepositoryOpen_guid).Returns(context);
factory.GetUIContext(contextGuid).Returns(context);
sp.GetServiceAsync(typeof(IGitExt)).Returns(gitExt);
var vsGitExt = new VSGitExt(sp, factory, repoFactory, joinableTaskContext);
vsGitExt.JoinTillEmpty();
Expand Down