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

Commit b9939d2

Browse files
authored
Merge pull request #1645 from github/fixes/1644-GitServiceHelper-avoid-MEF
Stop GitServiceHelper from depending on MEF
2 parents 6cf4a55 + 0fc4dc2 commit b9939d2

File tree

17 files changed

+140
-103
lines changed

17 files changed

+140
-103
lines changed

src/GitHub.App/GitHub.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<Compile Include="SampleData\ForkRepositoryExecuteViewModelDesigner.cs" />
215215
<Compile Include="SampleData\ForkRepositorySelectViewModelDesigner.cs" />
216216
<Compile Include="Models\PullRequestReviewModel.cs" />
217+
<Compile Include="SampleData\GitServiceDesigner.cs" />
217218
<Compile Include="SampleData\ForkRepositorySwitchViewModelDesigner.cs" />
218219
<Compile Include="SampleData\PullRequestFilesViewModelDesigner.cs" />
219220
<Compile Include="SampleData\PullRequestReviewAuthoringViewModelDesigner.cs" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Threading.Tasks;
2+
using GitHub.Primitives;
3+
using GitHub.Services;
4+
using LibGit2Sharp;
5+
6+
namespace GitHub.SampleData
7+
{
8+
class GitServiceDesigner : IGitService
9+
{
10+
public Task<string> GetLatestPushedSha(string path) => Task.FromResult<string>(null);
11+
public UriString GetRemoteUri(IRepository repo, string remote = "origin") => null;
12+
public IRepository GetRepository(string path) => null;
13+
public UriString GetUri(string path, string remote = "origin") => null;
14+
public UriString GetUri(IRepository repository, string remote = "origin") => null;
15+
}
16+
}

src/GitHub.App/SampleData/PullRequestCreationViewModelDesigner.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public PullRequestCreationViewModelDesigner()
1616
{
1717
Branches = new List<IBranch>
1818
{
19-
new BranchModel("master", new LocalRepositoryModel("http://github.com/user/repo")),
20-
new BranchModel("don/stub-ui", new LocalRepositoryModel("http://github.com/user/repo")),
21-
new BranchModel("feature/pr/views", new LocalRepositoryModel("http://github.com/user/repo")),
22-
new BranchModel("release-1.0.17.0", new LocalRepositoryModel("http://github.com/user/repo")),
19+
new BranchModel("master", new LocalRepositoryModel("http://github.com/user/repo", new GitServiceDesigner())),
20+
new BranchModel("don/stub-ui", new LocalRepositoryModel("http://github.com/user/repo", new GitServiceDesigner())),
21+
new BranchModel("feature/pr/views", new LocalRepositoryModel("http://github.com/user/repo", new GitServiceDesigner())),
22+
new BranchModel("release-1.0.17.0", new LocalRepositoryModel("http://github.com/user/repo", new GitServiceDesigner())),
2323
}.AsReadOnly();
2424

25-
TargetBranch = new BranchModel("master", new LocalRepositoryModel("http://github.com/user/repo"));
25+
TargetBranch = new BranchModel("master", new LocalRepositoryModel("http://github.com/user/repo", new GitServiceDesigner()));
2626
SourceBranch = Branches[2];
2727

2828
SelectedAssignee = "Haacked (Phil Haack)";

src/GitHub.App/SampleData/SampleViewModels.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Conn : IConnection
197197
public HostAddress HostAddress { get; set; }
198198

199199
public string Username { get; set; }
200-
public ObservableCollection<ILocalRepositoryModel> Repositories { get; set; }
200+
public ObservableCollection<ILocalRepositoryModel> Repositories { get; set; }
201201

202202
public Octokit.User User => null;
203203
public bool IsLoggedIn => true;
@@ -396,12 +396,12 @@ public class GitHubConnectSectionDesigner : IGitHubConnectSection
396396
public GitHubConnectSectionDesigner()
397397
{
398398
Repositories = new ObservableCollection<ILocalRepositoryModel>();
399-
Repositories.Add(new LocalRepositoryModel("octokit", new UriString("https://github.com/octokit/octokit.net"), @"C:\Users\user\Source\Repos\octokit.net"));
400-
Repositories.Add(new LocalRepositoryModel("cefsharp", new UriString("https://github.com/cefsharp/cefsharp"), @"C:\Users\user\Source\Repos\cefsharp"));
401-
Repositories.Add(new LocalRepositoryModel("git-lfs", new UriString("https://github.com/github/git-lfs"), @"C:\Users\user\Source\Repos\git-lfs"));
402-
Repositories.Add(new LocalRepositoryModel("another octokit", new UriString("https://github.com/octokit/octokit.net"), @"C:\Users\user\Source\Repos\another-octokit.net"));
403-
Repositories.Add(new LocalRepositoryModel("some cefsharp", new UriString("https://github.com/cefsharp/cefsharp"), @"C:\Users\user\Source\Repos\something-else"));
404-
Repositories.Add(new LocalRepositoryModel("even more git-lfs", new UriString("https://github.com/github/git-lfs"), @"C:\Users\user\Source\Repos\A different path"));
399+
Repositories.Add(new LocalRepositoryModel("octokit", new UriString("https://github.com/octokit/octokit.net"), @"C:\Users\user\Source\Repos\octokit.net", new GitServiceDesigner()));
400+
Repositories.Add(new LocalRepositoryModel("cefsharp", new UriString("https://github.com/cefsharp/cefsharp"), @"C:\Users\user\Source\Repos\cefsharp", new GitServiceDesigner()));
401+
Repositories.Add(new LocalRepositoryModel("git-lfs", new UriString("https://github.com/github/git-lfs"), @"C:\Users\user\Source\Repos\git-lfs", new GitServiceDesigner()));
402+
Repositories.Add(new LocalRepositoryModel("another octokit", new UriString("https://github.com/octokit/octokit.net"), @"C:\Users\user\Source\Repos\another-octokit.net", new GitServiceDesigner()));
403+
Repositories.Add(new LocalRepositoryModel("some cefsharp", new UriString("https://github.com/cefsharp/cefsharp"), @"C:\Users\user\Source\Repos\something-else", new GitServiceDesigner()));
404+
Repositories.Add(new LocalRepositoryModel("even more git-lfs", new UriString("https://github.com/github/git-lfs"), @"C:\Users\user\Source\Repos\A different path", new GitServiceDesigner()));
405405
}
406406

407407
public ObservableCollection<ILocalRepositoryModel> Repositories

src/GitHub.App/Services/GitClient.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ public class GitClient : IGitClient
1919
{
2020
const string defaultOriginName = "origin";
2121
static readonly ILogger log = LogManager.ForContext<GitClient>();
22+
readonly IGitService gitService;
2223
readonly PullOptions pullOptions;
2324
readonly PushOptions pushOptions;
2425
readonly FetchOptions fetchOptions;
2526

2627
[ImportingConstructor]
27-
public GitClient(IGitHubCredentialProvider credentialProvider)
28+
public GitClient(IGitHubCredentialProvider credentialProvider, IGitService gitService)
2829
{
2930
Guard.ArgumentNotNull(credentialProvider, nameof(credentialProvider));
31+
Guard.ArgumentNotNull(gitService, nameof(gitService));
32+
33+
this.gitService = gitService;
3034

3135
pushOptions = new PushOptions { CredentialsProvider = credentialProvider.HandleCredentials };
3236
fetchOptions = new FetchOptions { CredentialsProvider = credentialProvider.HandleCredentials };
@@ -338,7 +342,7 @@ public Task<Remote> GetHttpRemote(IRepository repo, string remote)
338342

339343
return Task.Factory.StartNew(() =>
340344
{
341-
var uri = GitService.GitServiceHelper.GetRemoteUri(repo, remote);
345+
var uri = gitService.GetRemoteUri(repo, remote);
342346
var remoteName = uri.IsHypertextTransferProtocol ? remote : remote + "-http";
343347
var ret = repo.Network.Remotes[remoteName];
344348
if (ret == null)

src/GitHub.Exports/Models/BranchModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Globalization;
3+
using GitHub.Services;
34

45
namespace GitHub.Models
56
{
@@ -25,13 +26,13 @@ public BranchModel(Octokit.Branch branch, IRepositoryModel repo)
2526
Id = String.Format(CultureInfo.InvariantCulture, "{0}/{1}", Repository.Owner, Name);
2627
}
2728

28-
public BranchModel(LibGit2Sharp.Branch branch, IRepositoryModel repo)
29+
public BranchModel(LibGit2Sharp.Branch branch, IRepositoryModel repo, IGitService gitService)
2930
{
3031
Extensions.Guard.ArgumentNotNull(branch, nameof(branch));
3132
Extensions.Guard.ArgumentNotNull(repo, nameof(repo));
3233
Name = DisplayName = branch.FriendlyName;
3334
#pragma warning disable 0618 // TODO: Replace `Branch.Remote` with `Repository.Network.Remotes[branch.RemoteName]`.
34-
Repository = branch.IsRemote ? new LocalRepositoryModel(branch.Remote.Url) : repo;
35+
Repository = branch.IsRemote ? new LocalRepositoryModel(branch.Remote.Url, gitService) : repo;
3536
#pragma warning restore 0618
3637
IsTracking = branch.IsTracking;
3738
Sha = branch.Tip?.Sha;

src/GitHub.Exports/Models/LocalRepositoryModel.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using System.Linq;
66
using GitHub.Primitives;
77
using GitHub.UI;
8+
using GitHub.Exports;
89
using GitHub.Services;
910
using GitHub.Extensions;
1011
using System.Threading.Tasks;
11-
using GitHub.Exports;
1212

1313
namespace GitHub.Models
1414
{
@@ -18,17 +18,22 @@ namespace GitHub.Models
1818
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1919
public class LocalRepositoryModel : RepositoryModel, ILocalRepositoryModel, IEquatable<LocalRepositoryModel>
2020
{
21+
readonly IGitService gitService;
22+
2123
/// <summary>
2224
/// Initializes a new instance of the <see cref="LocalRepositoryModel"/> class.
2325
/// </summary>
2426
/// <param name="name">The repository name.</param>
2527
/// <param name="cloneUrl">The repository's clone URL.</param>
2628
/// <param name="localPath">The repository's local path.</param>
27-
public LocalRepositoryModel(string name, UriString cloneUrl, string localPath)
29+
/// <param name="gitService">The service used to refresh the repository's URL.</param>
30+
public LocalRepositoryModel(string name, UriString cloneUrl, string localPath, IGitService gitService)
2831
: base(name, cloneUrl)
2932
{
3033
Guard.ArgumentNotEmptyString(localPath, nameof(localPath));
34+
Guard.ArgumentNotNull(gitService, nameof(gitService));
3135

36+
this.gitService = gitService;
3237
LocalPath = localPath;
3338
Icon = Octicon.repo;
3439
}
@@ -37,9 +42,13 @@ public LocalRepositoryModel(string name, UriString cloneUrl, string localPath)
3742
/// Initializes a new instance of the <see cref="LocalRepositoryModel"/> class.
3843
/// </summary>
3944
/// <param name="path">The repository's local path.</param>
40-
public LocalRepositoryModel(string path)
41-
: base(path)
45+
/// <param name="gitService">The service used to find the repository's URL.</param>
46+
public LocalRepositoryModel(string path, IGitService gitService)
47+
: base(path, gitService)
4248
{
49+
Guard.ArgumentNotNull(gitService, nameof(gitService));
50+
51+
this.gitService = gitService;
4352
LocalPath = path;
4453
Icon = Octicon.repo;
4554
}
@@ -51,7 +60,7 @@ public void Refresh()
5160
{
5261
if (LocalPath == null)
5362
return;
54-
CloneUrl = GitService.GitServiceHelper.GetUri(LocalPath);
63+
CloneUrl = gitService.GetUri(LocalPath);
5564
}
5665

5766
/// <summary>
@@ -68,7 +77,7 @@ public async Task<UriString> GenerateUrl(LinkType linkType, string path = null,
6877
if (CloneUrl == null)
6978
return null;
7079

71-
var sha = await GitService.GitServiceHelper.GetLatestPushedSha(path ?? LocalPath);
80+
var sha = await gitService.GetLatestPushedSha(path ?? LocalPath);
7281
// this also incidentally checks whether the repo has a valid LocalPath
7382
if (String.IsNullOrEmpty(sha))
7483
return CloneUrl.ToRepositoryUrl().AbsoluteUri;
@@ -157,7 +166,7 @@ public string HeadSha
157166
{
158167
get
159168
{
160-
using (var repo = GitService.GitServiceHelper.GetRepository(LocalPath))
169+
using (var repo = gitService.GetRepository(LocalPath))
161170
{
162171
return repo?.Commits.FirstOrDefault()?.Sha ?? string.Empty;
163172
}
@@ -172,9 +181,9 @@ public IBranch CurrentBranch
172181
get
173182
{
174183
// BranchModel doesn't keep a reference to Repository
175-
using (var repo = GitService.GitServiceHelper.GetRepository(LocalPath))
184+
using (var repo = gitService.GetRepository(LocalPath))
176185
{
177-
return new BranchModel(repo?.Head, this);
186+
return new BranchModel(repo?.Head, this, gitService);
178187
}
179188
}
180189
}

src/GitHub.Exports/Models/RepositoryModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ public RepositoryModel(
4040
/// The path to the local repository from which repository name and clone URL will be
4141
/// extracted.
4242
/// </param>
43-
protected RepositoryModel(string path)
43+
/// <param name="gitService">The service used to find the repository's <see cref="Name"/> and <see cref="CloneUrl"/>.</param>
44+
protected RepositoryModel(string path, IGitService gitService)
4445
{
4546
Guard.ArgumentNotNull(path, nameof(path));
4647

4748
var dir = new DirectoryInfo(path);
4849
if (!dir.Exists)
4950
throw new ArgumentException("Path does not exist", nameof(path));
50-
var uri = GitService.GitServiceHelper.GetUri(path);
51+
var uri = gitService.GetUri(path);
5152
Name = uri?.RepositoryName ?? dir.Name;
52-
CloneUrl = GitService.GitServiceHelper.GetUri(path);
53+
CloneUrl = gitService.GetUri(path);
5354
}
5455

5556
/// <summary>

src/GitHub.Exports/Services/GitService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ public UriString GetRemoteUri(IRepository repo, string remote = "origin")
7474
?.Url;
7575
}
7676

77-
public static IGitService GitServiceHelper => VisualStudio.Services.DefaultExportProvider.GetExportedValueOrDefault<IGitService>() ?? new GitService();
77+
/// <summary>
78+
/// Get a new instance of <see cref="GitService"/>.
79+
/// </summary>
80+
/// <remarks>
81+
/// This is equivalent to creating it via MEF with <see cref="CreationPolicy.NonShared"/>
82+
/// </remarks>
83+
public static IGitService GitServiceHelper => new GitService();
7884

7985
/// <summary>
8086
/// Finds the latest pushed commit of a file and returns the sha of that commit. Returns null when no commits have

src/GitHub.TeamFoundation.14/RegistryHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using GitHub.Logging;
77
using GitHub.Models;
8+
using GitHub.Services;
89
using Microsoft.Win32;
910
using Serilog;
1011

@@ -39,7 +40,7 @@ internal static IEnumerable<ILocalRepositoryModel> PokeTheRegistryForRepositoryL
3940
{
4041
var path = subkey?.GetValue("Path") as string;
4142
if (path != null && Directory.Exists(path))
42-
return new LocalRepositoryModel(path);
43+
return new LocalRepositoryModel(path, GitService.GitServiceHelper);
4344
}
4445
catch (Exception)
4546
{

0 commit comments

Comments
 (0)