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

Commit a62b02c

Browse files
authored
Merge pull request #1275 from github/fixes/1274-ref-null-PR-details
Fix possible null ref on PR details window
2 parents 43ff384 + 4f98855 commit a62b02c

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

src/GitHub.App/Services/GitClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,13 @@ public Task<bool> IsModified(IRepository repository, string path, byte[] content
405405
{
406406
if (repository.RetrieveStatus(path) == FileStatus.Unaltered)
407407
{
408-
var head = repository.Head[path];
409-
if (head.TargetType != TreeEntryTargetType.Blob)
408+
var treeEntry = repository.Head[path];
409+
if (treeEntry?.TargetType != TreeEntryTargetType.Blob)
410410
{
411411
return false;
412412
}
413413

414-
var blob1 = (Blob)head.Target;
414+
var blob1 = (Blob)treeEntry.Target;
415415
using (var s = contents != null ? new MemoryStream(contents) : new MemoryStream())
416416
{
417417
var blob2 = repository.ObjectDatabase.CreateBlob(s, path);

src/UnitTests/GitHub.App/Services/GitClientTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,88 @@
1111

1212
public class GitClientTests
1313
{
14+
public class TheIsModifiedMethod
15+
{
16+
[Theory]
17+
[InlineData(FileStatus.Unaltered, false)]
18+
[InlineData(FileStatus.ModifiedInIndex, true)]
19+
[InlineData(FileStatus.ModifiedInWorkdir, true)]
20+
public async Task RetrieveStatus(FileStatus fileStatus, bool expect)
21+
{
22+
var path = "path";
23+
var repo = Substitute.For<IRepository>();
24+
repo.RetrieveStatus(path).Returns(fileStatus);
25+
repo.Head.Returns(Substitute.For<Branch>());
26+
var treeEntry = null as TreeEntry;
27+
repo.Head[path].Returns(treeEntry);
28+
var gitClient = new GitClient(Substitute.For<IGitHubCredentialProvider>());
29+
30+
var modified = await gitClient.IsModified(repo, path, null);
31+
32+
Assert.Equal(expect, modified);
33+
}
34+
35+
[Fact]
36+
public async Task TreeEntry_Null_False()
37+
{
38+
var path = "path";
39+
var repo = Substitute.For<IRepository>();
40+
repo.RetrieveStatus(path).Returns(FileStatus.Unaltered);
41+
repo.Head.Returns(Substitute.For<Branch>());
42+
var treeEntry = null as TreeEntry;
43+
repo.Head[path].Returns(treeEntry);
44+
var gitClient = new GitClient(Substitute.For<IGitHubCredentialProvider>());
45+
46+
var modified = await gitClient.IsModified(repo, path, null);
47+
48+
Assert.False(modified);
49+
}
50+
51+
[Fact]
52+
public async Task TreeEntryTarget_GitLink_False()
53+
{
54+
var path = "path";
55+
var repo = Substitute.For<IRepository>();
56+
repo.RetrieveStatus(path).Returns(FileStatus.Unaltered);
57+
repo.Head.Returns(Substitute.For<Branch>());
58+
var treeEntry = Substitute.For<TreeEntry>();
59+
treeEntry.TargetType.Returns(TreeEntryTargetType.GitLink);
60+
treeEntry.Target.Returns(Substitute.For<GitLink>());
61+
repo.Head[path].Returns(treeEntry);
62+
var gitClient = new GitClient(Substitute.For<IGitHubCredentialProvider>());
63+
64+
var modified = await gitClient.IsModified(repo, path, null);
65+
66+
Assert.False(modified);
67+
}
68+
69+
[Theory]
70+
[InlineData(0, 0, false)]
71+
[InlineData(1, 0, true)]
72+
[InlineData(0, 1, true)]
73+
[InlineData(1, 1, true)]
74+
public async Task ContentChanges(int linesAdded, int linesDeleted, bool expected)
75+
{
76+
var path = "path";
77+
var repo = Substitute.For<IRepository>();
78+
repo.RetrieveStatus(path).Returns(FileStatus.Unaltered);
79+
repo.Head.Returns(Substitute.For<Branch>());
80+
var treeEntry = Substitute.For<TreeEntry>();
81+
treeEntry.TargetType.Returns(TreeEntryTargetType.Blob);
82+
treeEntry.Target.Returns(Substitute.For<Blob>());
83+
repo.Head[path].Returns(treeEntry);
84+
var changes = Substitute.For<ContentChanges>();
85+
changes.LinesAdded.Returns(linesAdded);
86+
changes.LinesDeleted.Returns(linesDeleted);
87+
repo.Diff.Compare(null, null).ReturnsForAnyArgs(changes);
88+
var gitClient = new GitClient(Substitute.For<IGitHubCredentialProvider>());
89+
90+
var modified = await gitClient.IsModified(repo, path, null);
91+
92+
Assert.Equal(expected, modified);
93+
}
94+
}
95+
1496
public class TheIsHeadPushedMethod : TestBaseClass
1597
{
1698
[Theory]

0 commit comments

Comments
 (0)