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

Commit aa210b8

Browse files
committed
Support backtracking when there is a failed match
The prevents gaps in matched lines being accepted.
1 parent 058ed1b commit aa210b8

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/GitHub.Exports/Models/DiffUtilities.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,29 @@ public static IEnumerable<DiffChunk> ParseFragment(string diff)
8282

8383
public static DiffLine Match(IEnumerable<DiffChunk> diff, IList<DiffLine> target)
8484
{
85-
int j = 0;
86-
8785
if (target.Count == 0)
8886
{
8987
return null; // no lines to match
9088
}
9189

9290
foreach (var source in diff)
9391
{
92+
var matches = 0;
9493
for (var i = source.Lines.Count - 1; i >= 0; --i)
9594
{
96-
if (source.Lines[i].Content == target[j].Content)
95+
if (source.Lines[i].Content == target[matches].Content)
9796
{
98-
if (++j == target.Count || i == 0)
97+
matches++;
98+
if (matches == target.Count || i == 0)
9999
{
100-
return source.Lines[i + j - 1];
100+
return source.Lines[i + matches - 1];
101101
}
102102
}
103+
else
104+
{
105+
i += matches;
106+
matches = 0;
107+
}
103108
}
104109
}
105110

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public class TheMatchMethod
203203
[InlineData("+a.+x.+x.", "+a.+x.+x.", 2)]
204204
[InlineData("+a.+x.+x.+b.+x.+x.", "+a.+x.", 1)]
205205
[InlineData("+a.+x.+x.+b.+x.+x.", "+b.+x.", 4)]
206+
[InlineData("+a.+b.+x", "+a.+x.", -1)] // backtrack when there is a failed match
206207
public void MatchLine(string lines1, string lines2, int skip /* -1 for no match */)
207208
{
208209
var header = "@@ -1 +1 @@";

0 commit comments

Comments
 (0)