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

Commit 66a7ad7

Browse files
committed
Pass IGitService into LocalRepositoryModel
Make LocalRepositoryModel's dependency on IGitService explicit.
1 parent 37ab7f6 commit 66a7ad7

File tree

13 files changed

+64
-44
lines changed

13 files changed

+64
-44
lines changed

src/GitHub.App/GitHub.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
<Compile Include="Models\PullRequestReviewCommentModel.cs" />
212212
<Compile Include="Models\PullRequestDetailArgument.cs" />
213213
<Compile Include="Models\PullRequestReviewModel.cs" />
214+
<Compile Include="SampleData\GitServiceDesigner.cs" />
214215
<Compile Include="SampleData\PullRequestFilesViewModelDesigner.cs" />
215216
<Compile Include="SampleData\PullRequestReviewAuthoringViewModelDesigner.cs" />
216217
<Compile Include="SampleData\PullRequestReviewFileCommentViewModelDesigner.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.Exports/Models/BranchModel.cs

Lines changed: 2 additions & 1 deletion
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
{
@@ -31,7 +32,7 @@ public BranchModel(LibGit2Sharp.Branch branch, IRepositoryModel repo)
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.GitServiceHelper) : repo;
3536
#pragma warning restore 0618
3637
IsTracking = branch.IsTracking;
3738
Sha = branch.Tip?.Sha;

src/GitHub.Exports/Models/LocalRepositoryModel.cs

Lines changed: 15 additions & 8 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,21 @@ 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, IGitService gitService = null)
28-
: base(name, cloneUrl, gitService)
29+
public LocalRepositoryModel(string name, UriString cloneUrl, string localPath, IGitService gitService)
30+
: base(name, cloneUrl)
2931
{
3032
Guard.ArgumentNotEmptyString(localPath, nameof(localPath));
33+
Guard.ArgumentNotNull(gitService, nameof(gitService));
3134

35+
this.gitService = gitService;
3236
LocalPath = localPath;
3337
Icon = Octicon.repo;
3438
}
@@ -37,9 +41,12 @@ public LocalRepositoryModel(string name, UriString cloneUrl, string localPath, I
3741
/// Initializes a new instance of the <see cref="LocalRepositoryModel"/> class.
3842
/// </summary>
3943
/// <param name="path">The repository's local path.</param>
40-
public LocalRepositoryModel(string path, IGitService gitService = null)
44+
public LocalRepositoryModel(string path, IGitService gitService)
4145
: base(path, gitService)
4246
{
47+
Guard.ArgumentNotNull(gitService, nameof(gitService));
48+
49+
this.gitService = gitService;
4350
LocalPath = path;
4451
Icon = Octicon.repo;
4552
}
@@ -51,7 +58,7 @@ public void Refresh()
5158
{
5259
if (LocalPath == null)
5360
return;
54-
CloneUrl = GitService.GetUri(LocalPath);
61+
CloneUrl = gitService.GetUri(LocalPath);
5562
}
5663

5764
/// <summary>
@@ -68,7 +75,7 @@ public async Task<UriString> GenerateUrl(LinkType linkType, string path = null,
6875
if (CloneUrl == null)
6976
return null;
7077

71-
var sha = await GitService.GetLatestPushedSha(path ?? LocalPath);
78+
var sha = await gitService.GetLatestPushedSha(path ?? LocalPath);
7279
// this also incidentally checks whether the repo has a valid LocalPath
7380
if (String.IsNullOrEmpty(sha))
7481
return CloneUrl.ToRepositoryUrl().AbsoluteUri;
@@ -157,7 +164,7 @@ public string HeadSha
157164
{
158165
get
159166
{
160-
using (var repo = GitService.GetRepository(LocalPath))
167+
using (var repo = gitService.GetRepository(LocalPath))
161168
{
162169
return repo?.Commits.FirstOrDefault()?.Sha ?? string.Empty;
163170
}
@@ -172,7 +179,7 @@ public IBranch CurrentBranch
172179
get
173180
{
174181
// BranchModel doesn't keep a reference to Repository
175-
using (var repo = GitService.GetRepository(LocalPath))
182+
using (var repo = gitService.GetRepository(LocalPath))
176183
{
177184
return new BranchModel(repo?.Head, this);
178185
}

src/GitHub.Exports/Models/RepositoryModel.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public class RepositoryModel : NotificationAwareObject, IRepositoryModel
2424
/// <param name="cloneUrl">The repository's clone URL.</param>
2525
public RepositoryModel(
2626
string name,
27-
UriString cloneUrl,
28-
IGitService gitService = null) : this(gitService)
27+
UriString cloneUrl)
2928
{
3029
Guard.ArgumentNotEmptyString(name, nameof(name));
3130
Guard.ArgumentNotNull(cloneUrl, nameof(cloneUrl));
@@ -41,7 +40,7 @@ public RepositoryModel(
4140
/// The path to the local repository from which repository name and clone URL will be
4241
/// extracted.
4342
/// </param>
44-
protected RepositoryModel(string path, IGitService gitService = null) : this(gitService)
43+
protected RepositoryModel(string path, IGitService gitService)
4544
{
4645
Guard.ArgumentNotNull(path, nameof(path));
4746

@@ -53,11 +52,6 @@ protected RepositoryModel(string path, IGitService gitService = null) : this(git
5352
CloneUrl = gitService.GetUri(path);
5453
}
5554

56-
RepositoryModel(IGitService gitService = null)
57-
{
58-
GitService = gitService ?? Services.GitService.GitServiceHelper;
59-
}
60-
6155
/// <summary>
6256
/// Gets the name of the repository.
6357
/// </summary>
@@ -113,7 +107,5 @@ public void SetIcon(bool isPrivate, bool isFork)
113107
? Octicon.repo_forked
114108
: Octicon.repo;
115109
}
116-
117-
protected IGitService GitService { get; }
118110
}
119111
}

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
{
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using System.ComponentModel.Composition;
2-
using GitHub.Models;
1+
using GitHub.Models;
2+
using GitHub.Services;
33

44
namespace GitHub.TeamFoundation.Services
55
{
66
class LocalRepositoryModelFactory : ILocalRepositoryModelFactory
77
{
88
public ILocalRepositoryModel Create(string localPath)
99
{
10-
return new LocalRepositoryModel(localPath);
10+
return new LocalRepositoryModel(localPath, GitService.GitServiceHelper);
1111
}
1212
}
1313
}

src/GitHub.TeamFoundation.15/RegistryHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Threading.Tasks;
88
using GitHub.Logging;
99
using GitHub.Models;
10-
using GitHub.VisualStudio;
10+
using GitHub.Services;
1111
using Microsoft.Win32;
1212
using Serilog;
1313

@@ -34,7 +34,7 @@ internal static IEnumerable<ILocalRepositoryModel> PokeTheRegistryForRepositoryL
3434
{
3535
var path = subkey?.GetValue("Path") as string;
3636
if (path != null)
37-
return new LocalRepositoryModel(path);
37+
return new LocalRepositoryModel(path, GitService.GitServiceHelper);
3838
}
3939
catch (Exception)
4040
{

0 commit comments

Comments
 (0)