Skip to content

Commit 689e67f

Browse files
mini cleanup
1 parent 769a449 commit 689e67f

File tree

3 files changed

+138
-141
lines changed

3 files changed

+138
-141
lines changed

src/ConnectedMode/ServerBranchProvider.cs

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,88 +24,87 @@
2424
using SonarLint.VisualStudio.Core.Binding;
2525
using SonarLint.VisualStudio.SLCore.Listener.Branch;
2626

27-
namespace SonarLint.VisualStudio.ConnectedMode
27+
namespace SonarLint.VisualStudio.ConnectedMode;
28+
29+
[Export(typeof(IServerBranchProvider))]
30+
internal class ServerBranchProvider : IServerBranchProvider
2831
{
29-
[Export(typeof(IServerBranchProvider))]
30-
internal class ServerBranchProvider : IServerBranchProvider
32+
/// <summary>
33+
/// Factory method to create and return an <see cref="IRepository"/> instance.
34+
/// </summary>
35+
/// <remarks>Only used for testing</remarks>
36+
internal delegate IRepository CreateRepositoryObject(string repoRootPath);
37+
38+
private readonly IConfigurationProvider configurationProvider;
39+
private readonly IGitWorkspaceService gitWorkspaceService;
40+
private readonly IBranchMatcher branchMatcher;
41+
private readonly ILogger logger;
42+
private readonly CreateRepositoryObject createRepo;
43+
44+
[ImportingConstructor]
45+
public ServerBranchProvider(
46+
IConfigurationProvider configurationProvider,
47+
IGitWorkspaceService gitWorkspaceService,
48+
IBranchMatcher branchMatcher,
49+
ILogger logger)
50+
: this(configurationProvider, gitWorkspaceService, branchMatcher, logger, DoCreateRepo)
3151
{
32-
/// <summary>
33-
/// Factory method to create and return an <see cref="IRepository"/> instance.
34-
/// </summary>
35-
/// <remarks>Only used for testing</remarks>
36-
internal delegate IRepository CreateRepositoryObject(string repoRootPath);
37-
38-
private readonly IConfigurationProvider configurationProvider;
39-
private readonly IGitWorkspaceService gitWorkspaceService;
40-
private readonly IBranchMatcher branchMatcher;
41-
private readonly ILogger logger;
42-
private readonly CreateRepositoryObject createRepo;
43-
44-
[ImportingConstructor]
45-
public ServerBranchProvider(
46-
IConfigurationProvider configurationProvider,
47-
IGitWorkspaceService gitWorkspaceService,
48-
IBranchMatcher branchMatcher,
49-
ILogger logger)
50-
: this(configurationProvider, gitWorkspaceService, branchMatcher, logger, DoCreateRepo)
51-
{
52-
}
53-
54-
internal /* for testing */ ServerBranchProvider(
55-
IConfigurationProvider configurationProvider,
56-
IGitWorkspaceService gitWorkspaceService,
57-
IBranchMatcher branchMatcher,
58-
ILogger logger,
59-
CreateRepositoryObject createRepo)
60-
{
61-
this.configurationProvider = configurationProvider;
62-
this.gitWorkspaceService = gitWorkspaceService;
63-
this.branchMatcher = branchMatcher;
64-
this.logger = logger;
65-
this.createRepo = createRepo;
66-
}
67-
68-
public string GetServerBranchName(List<RemoteBranch> branches)
69-
{
70-
var config = configurationProvider.GetConfiguration();
52+
}
7153

72-
if (config.Mode == SonarLintMode.Standalone)
73-
{
74-
logger.LogVerbose(Resources.BranchProvider_NotInConnectedMode);
75-
return null;
76-
}
54+
internal /* for testing */ ServerBranchProvider(
55+
IConfigurationProvider configurationProvider,
56+
IGitWorkspaceService gitWorkspaceService,
57+
IBranchMatcher branchMatcher,
58+
ILogger logger,
59+
CreateRepositoryObject createRepo)
60+
{
61+
this.configurationProvider = configurationProvider;
62+
this.gitWorkspaceService = gitWorkspaceService;
63+
this.branchMatcher = branchMatcher;
64+
this.logger = logger;
65+
this.createRepo = createRepo;
66+
}
7767

78-
var matchingBranchName = CalculateMatchingBranch(config, branches);
68+
public string GetServerBranchName(List<RemoteBranch> branches)
69+
{
70+
var config = configurationProvider.GetConfiguration();
7971

80-
if (matchingBranchName == null)
81-
{
82-
logger.LogVerbose(Resources.BranchProvider_FailedToCalculateMatchingBranch);
72+
if (config.Mode == SonarLintMode.Standalone)
73+
{
74+
logger.LogVerbose(Resources.BranchProvider_NotInConnectedMode);
75+
return null;
76+
}
8377

84-
matchingBranchName = branches.First(rb => rb.IsMain).Name;
85-
}
78+
var matchingBranchName = CalculateMatchingBranch(config, branches);
8679

87-
Debug.Assert(matchingBranchName != null);
80+
if (matchingBranchName == null)
81+
{
82+
logger.LogVerbose(Resources.BranchProvider_FailedToCalculateMatchingBranch);
8883

89-
logger.WriteLine(Resources.BranchProvider_MatchingServerBranchName, matchingBranchName);
90-
return matchingBranchName;
84+
matchingBranchName = branches.First(rb => rb.IsMain).Name;
9185
}
9286

93-
private string CalculateMatchingBranch(BindingConfiguration config, List<RemoteBranch> branches)
94-
{
95-
var gitRepoRoot = gitWorkspaceService.GetRepoRoot();
87+
Debug.Assert(matchingBranchName != null);
9688

97-
if (gitRepoRoot == null)
98-
{
99-
logger.LogVerbose(Resources.BranchProvider_CouldNotDetectGitRepo);
100-
return null;
101-
}
89+
logger.WriteLine(Resources.BranchProvider_MatchingServerBranchName, matchingBranchName);
90+
return matchingBranchName;
91+
}
10292

103-
var repo = createRepo(gitRepoRoot);
104-
var branchName = branchMatcher.GetMatchingBranch(config.Project.ServerProjectKey, repo, branches);
93+
private string CalculateMatchingBranch(BindingConfiguration config, List<RemoteBranch> branches)
94+
{
95+
var gitRepoRoot = gitWorkspaceService.GetRepoRoot();
10596

106-
return branchName;
97+
if (gitRepoRoot == null)
98+
{
99+
logger.LogVerbose(Resources.BranchProvider_CouldNotDetectGitRepo);
100+
return null;
107101
}
108102

109-
private static IRepository DoCreateRepo(string repoRootPath) => new Repository(repoRootPath);
103+
var repo = createRepo(gitRepoRoot);
104+
var branchName = branchMatcher.GetMatchingBranch(config.Project.ServerProjectKey, repo, branches);
105+
106+
return branchName;
110107
}
108+
109+
private static IRepository DoCreateRepo(string repoRootPath) => new Repository(repoRootPath);
111110
}

src/ConnectedMode/SlCoreGitChangeNotifier.cs

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,36 @@
2828
using SonarLint.VisualStudio.SLCore.Service.Branch;
2929
using SonarLint.VisualStudio.SLCore.State;
3030

31-
namespace SonarLint.VisualStudio.ConnectedMode
31+
namespace SonarLint.VisualStudio.ConnectedMode;
32+
33+
[Export(typeof(ISlCoreGitChangeNotifier))]
34+
[PartCreationPolicy(CreationPolicy.Shared)]
35+
internal sealed class SlCoreGitChangeNotifier : ISlCoreGitChangeNotifier
3236
{
33-
[Export(typeof(ISlCoreGitChangeNotifier))]
34-
[PartCreationPolicy(CreationPolicy.Shared)]
35-
internal sealed class SlCoreGitChangeNotifier : ISlCoreGitChangeNotifier
36-
{
37-
private readonly IActiveConfigScopeTracker activeConfigScopeTracker;
38-
private readonly ISLCoreServiceProvider serviceProvider;
39-
private readonly IBoundSolutionGitMonitor gitMonitor;
40-
private readonly ILogger logger;
41-
private readonly IThreadHandling threadHandling;
42-
private bool disposed;
37+
private readonly IActiveConfigScopeTracker activeConfigScopeTracker;
38+
private readonly ISLCoreServiceProvider serviceProvider;
39+
private readonly IBoundSolutionGitMonitor gitMonitor;
40+
private readonly ILogger logger;
41+
private readonly IThreadHandling threadHandling;
42+
private bool disposed;
4343

44-
[ImportingConstructor]
45-
public SlCoreGitChangeNotifier(
46-
IActiveConfigScopeTracker activeConfigScopeTracker,
47-
ISLCoreServiceProvider serviceProvider,
48-
IBoundSolutionGitMonitor gitMonitor,
49-
ILogger logger,
50-
IThreadHandling threadHandling,
51-
IInitializationProcessorFactory initializationProcessorFactory)
52-
{
53-
this.activeConfigScopeTracker = activeConfigScopeTracker;
54-
this.serviceProvider = serviceProvider;
55-
this.gitMonitor = gitMonitor;
56-
this.logger = logger;
57-
this.threadHandling = threadHandling;
44+
[ImportingConstructor]
45+
public SlCoreGitChangeNotifier(
46+
IActiveConfigScopeTracker activeConfigScopeTracker,
47+
ISLCoreServiceProvider serviceProvider,
48+
IBoundSolutionGitMonitor gitMonitor,
49+
ILogger logger,
50+
IThreadHandling threadHandling,
51+
IInitializationProcessorFactory initializationProcessorFactory)
52+
{
53+
this.activeConfigScopeTracker = activeConfigScopeTracker;
54+
this.serviceProvider = serviceProvider;
55+
this.gitMonitor = gitMonitor;
56+
this.logger = logger;
57+
this.threadHandling = threadHandling;
5858

59-
InitializationProcessor = initializationProcessorFactory.Create<SlCoreGitChangeNotifier>([gitMonitor],
60-
_ => threadHandling.RunOnUIThreadAsync(() =>
59+
InitializationProcessor = initializationProcessorFactory.Create<SlCoreGitChangeNotifier>([gitMonitor],
60+
_ => threadHandling.RunOnUIThreadAsync(() =>
6161
{
6262
if (disposed)
6363
{
@@ -66,46 +66,45 @@ public SlCoreGitChangeNotifier(
6666
gitMonitor.HeadChanged += GitMonitor_OnHeadChanged;
6767
activeConfigScopeTracker.CurrentConfigurationScopeChanged += ActiveConfigScopeTracker_OnCurrentConfigurationScopeChanged;
6868
}));
69-
}
69+
}
7070

71-
private void ActiveConfigScopeTracker_OnCurrentConfigurationScopeChanged(object sender, ConfigurationScopeChangedEventArgs e)
71+
private void ActiveConfigScopeTracker_OnCurrentConfigurationScopeChanged(object sender, ConfigurationScopeChangedEventArgs e)
72+
{
73+
if (e.DefinitionChanged)
7274
{
73-
if (e.DefinitionChanged)
74-
{
75-
gitMonitor.Refresh();
76-
}
75+
gitMonitor.Refresh();
7776
}
77+
}
7878

79-
private void GitMonitor_OnHeadChanged(object sender, EventArgs e) =>
80-
threadHandling.RunOnBackgroundThread(() =>
81-
{
82-
if (!serviceProvider.TryGetTransientService(out ISonarProjectBranchSlCoreService sonarProjectBranchSlCoreService))
83-
{
84-
logger.LogVerbose(SLCoreStrings.ServiceProviderNotInitialized);
85-
return;
86-
}
87-
if (activeConfigScopeTracker.Current == null)
88-
{
89-
return;
90-
}
91-
sonarProjectBranchSlCoreService.DidVcsRepositoryChange(new DidVcsRepositoryChangeParams(activeConfigScopeTracker.Current.Id));
92-
}).Forget();
93-
94-
public void Dispose()
79+
private void GitMonitor_OnHeadChanged(object sender, EventArgs e) =>
80+
threadHandling.RunOnBackgroundThread(() =>
9581
{
96-
if (disposed)
82+
if (!serviceProvider.TryGetTransientService(out ISonarProjectBranchSlCoreService sonarProjectBranchSlCoreService))
9783
{
84+
logger.LogVerbose(SLCoreStrings.ServiceProviderNotInitialized);
9885
return;
9986
}
100-
101-
if (InitializationProcessor.IsFinalized)
87+
if (activeConfigScopeTracker.Current == null)
10288
{
103-
gitMonitor.HeadChanged -= GitMonitor_OnHeadChanged;
104-
activeConfigScopeTracker.CurrentConfigurationScopeChanged -= ActiveConfigScopeTracker_OnCurrentConfigurationScopeChanged;
89+
return;
10590
}
106-
disposed = true;
91+
sonarProjectBranchSlCoreService.DidVcsRepositoryChange(new DidVcsRepositoryChangeParams(activeConfigScopeTracker.Current.Id));
92+
}).Forget();
93+
94+
public void Dispose()
95+
{
96+
if (disposed)
97+
{
98+
return;
10799
}
108100

109-
public IInitializationProcessor InitializationProcessor { get; }
101+
if (InitializationProcessor.IsFinalized)
102+
{
103+
gitMonitor.HeadChanged -= GitMonitor_OnHeadChanged;
104+
activeConfigScopeTracker.CurrentConfigurationScopeChanged -= ActiveConfigScopeTracker_OnCurrentConfigurationScopeChanged;
105+
}
106+
disposed = true;
110107
}
108+
109+
public IInitializationProcessor InitializationProcessor { get; }
111110
}

src/SLCore/Listener/Branch/IServerBranchProvider.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
2020

21-
namespace SonarLint.VisualStudio.SLCore.Listener.Branch
22-
{
23-
public record struct RemoteBranch(string Name, bool IsMain);
21+
namespace SonarLint.VisualStudio.SLCore.Listener.Branch;
22+
23+
public record struct RemoteBranch(string Name, bool IsMain);
2424

25-
public interface IServerBranchProvider
26-
{
27-
/// <summary>
28-
/// Returns the Sonar server branch to use when requesting data
29-
/// </summary>
30-
/// <returns>The Sonar server branch name,
31-
/// or the name of the Sonar server branch marked as "Main" if the branch cannot be determined,
32-
/// or null if we are not in connected mode.
33-
/// </returns>
34-
/// <remarks>
35-
/// Only applies in connected mode.
36-
/// </remarks>
37-
string? GetServerBranchName(List<RemoteBranch> branches);
38-
}
25+
public interface IServerBranchProvider
26+
{
27+
/// <summary>
28+
/// Returns the Sonar server branch to use when requesting data
29+
/// </summary>
30+
/// <returns>The Sonar server branch name,
31+
/// or the name of the Sonar server branch marked as "Main" if the branch cannot be determined,
32+
/// or null if we are not in connected mode.
33+
/// </returns>
34+
/// <remarks>
35+
/// Only applies in connected mode.
36+
/// </remarks>
37+
string? GetServerBranchName(List<RemoteBranch> branches);
3938
}

0 commit comments

Comments
 (0)