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

Commit 058ed1b

Browse files
committed
Stop diff line matcher getting confused by partial match
1 parent 8cd4641 commit 058ed1b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/GitHub.Exports/Models/DiffUtilities.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ public static DiffLine Match(IEnumerable<DiffChunk> diff, IList<DiffLine> target
100100
return source.Lines[i + j - 1];
101101
}
102102
}
103-
else
104-
{
105-
j = 0;
106-
}
107103
}
108104
}
109105

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,25 @@ public class TheMatchMethod
193193
[InlineData("", " x", -1)]
194194
[InlineData(" x", "", -1)]
195195

196-
[InlineData(" 1. 2.", " 2. 1.", 1)] // matched full context
197-
[InlineData(" 1. 2.", " 2. 3.", -1)] // didn't match full context
198-
[InlineData(" 2.", " 2. 1.", 0)] // match if we run out of context lines
196+
[InlineData(" 1. 2.", " 1. 2.", 1)] // matched full context
197+
[InlineData(" 1. 2.", " 3. 2.", -1)] // didn't match full context
198+
[InlineData(" 2.", " 1. 2.", 0)] // match if we run out of context lines
199+
200+
// Tests for https://github.com/github/VisualStudio/issues/1149
201+
// Matching algorithm got confused when there was a partial match.
202+
[InlineData("+a.+x.+x.", "+a.+x.", 1)]
203+
[InlineData("+a.+x.+x.", "+a.+x.+x.", 2)]
204+
[InlineData("+a.+x.+x.+b.+x.+x.", "+a.+x.", 1)]
205+
[InlineData("+a.+x.+x.+b.+x.+x.", "+b.+x.", 4)]
199206
public void MatchLine(string lines1, string lines2, int skip /* -1 for no match */)
200207
{
201208
var header = "@@ -1 +1 @@";
202209
lines1 = lines1.Replace(".", "\r\n");
203210
lines2 = lines2.Replace(".", "\r\n");
204211
var chunks1 = DiffUtilities.ParseFragment(header + "\n" + lines1).ToList();
205212
var chunks2 = DiffUtilities.ParseFragment(header + "\n" + lines2).ToList();
206-
var expectLine = (skip != -1) ? chunks1.First().Lines.Skip(skip).First() : null;
207-
var targetLines = chunks2.First().Lines;
213+
var expectLine = (skip != -1) ? chunks1.First().Lines[skip] : null;
214+
var targetLines = chunks2.First().Lines.Reverse().ToList();
208215

209216
var line = DiffUtilities.Match(chunks1, targetLines);
210217

0 commit comments

Comments
 (0)