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

Commit 21c38f1

Browse files
authored
Merge pull request #1068 from github/fixes/1052-added-comment-not-found
Make diff line matching more robust
2 parents 3654a52 + 077d55e commit 21c38f1

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/GitHub.Exports/Models/DiffUtilities.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public static DiffLine Match(IEnumerable<DiffChunk> diff, IList<DiffLine> target
9696
{
9797
if (source.Lines[i].Content == target[j].Content)
9898
{
99-
if (++j == target.Count) return source.Lines[i + j - 1];
99+
if (++j == target.Count || i == 0)
100+
{
101+
return source.Lines[i + j - 1];
102+
}
100103
}
101104
else
102105
{

test/GitHub.InlineReviews.UnitTests/Models/DiffUtilitiesTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,20 @@ public class TheMatchMethod
173173
{
174174
[Theory]
175175
[InlineData(" 1", " 1", 0)]
176-
[InlineData(" 1\n 2", " 2", 1)]
177-
[InlineData(" 1\n 1", " 1", 1)] // match the later line
176+
[InlineData(" 1. 2", " 2", 1)]
177+
[InlineData(" 1. 1", " 1", 1)] // match the later line
178178
[InlineData("+x", "-x", -1)]
179179
[InlineData("", " x", -1)]
180180
[InlineData(" x", "", -1)]
181+
182+
[InlineData(" 1. 2.", " 2. 1.", 1)] // matched full context
183+
[InlineData(" 1. 2.", " 2. 3.", -1)] // didn't match full context
184+
[InlineData(" 2.", " 2. 1.", 0)] // match if we run out of context lines
181185
public void MatchLine(string lines1, string lines2, int skip /* -1 for no match */)
182186
{
183187
var header = "@@ -1 +1 @@";
188+
lines1 = lines1.Replace(".", "\r\n");
189+
lines2 = lines2.Replace(".", "\r\n");
184190
var chunks1 = DiffUtilities.ParseFragment(header + "\n" + lines1).ToList();
185191
var chunks2 = DiffUtilities.ParseFragment(header + "\n" + lines2).ToList();
186192
var expectLine = (skip != -1) ? chunks1.First().Lines.Skip(skip).First() : null;

0 commit comments

Comments
 (0)