Skip to content

Commit 50eed96

Browse files
p12ticmrubens
andauthored
fix: Improve handling of escaped markers in apply_diff (#2274)
* fix: Improve handling of escaped markers in apply_diff unescapeMarkers() is supposed to fix escaped merge conflict markers so that they match the code. However the function has a bug which requires SEARCH and REPLACE strings in the markers to be replaced. This is not part of merge conflict markers, so many valid cases were previously missed. * Revert test changes * Add new test --------- Co-authored-by: Matt Rubens <[email protected]>
1 parent 7eea755 commit 50eed96

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/core/diff/strategies/__tests__/multi-search-replace.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,25 @@ function five() {
14361436
expect(result.success).toBe(true)
14371437
})
14381438

1439+
it("handles escaping of markers with custom suffixes", async () => {
1440+
const originalContent = "before\n<<<<<<< HEAD\nmiddle\n>>>>>>> feature-branch\nafter\n"
1441+
const diffContent =
1442+
"<<<<<<< SEARCH\n" +
1443+
"before\n" +
1444+
"\\<<<<<<< HEAD\n" +
1445+
"middle\n" +
1446+
"\\>>>>>>> feature-branch\n" +
1447+
"after\n" +
1448+
"=======\n" +
1449+
"replaced content\n" +
1450+
">>>>>>> REPLACE"
1451+
const result = await strategy.applyDiff(originalContent, diffContent)
1452+
expect(result.success).toBe(true)
1453+
if (result.success) {
1454+
expect(result.content).toBe("replaced content\n")
1455+
}
1456+
})
1457+
14391458
it("detects separator when expecting replace", () => {
14401459
const diff = "<<<<<<< SEARCH\n" + "content\n" + "=======\n" + "new content\n" + "======="
14411460
const result = strategy["validateMarkerSequencing"](diff)

src/core/diff/strategies/multi-search-replace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ Only use a single line of '=======' between search and replacement content, beca
143143

144144
private unescapeMarkers(content: string): string {
145145
return content
146-
.replace(/^\\<<<<<<< SEARCH/gm, "<<<<<<< SEARCH")
146+
.replace(/^\\<<<<<<</gm, "<<<<<<<")
147147
.replace(/^\\=======/gm, "=======")
148-
.replace(/^\\>>>>>>> REPLACE/gm, ">>>>>>> REPLACE")
148+
.replace(/^\\>>>>>>>/gm, ">>>>>>>")
149149
.replace(/^\\-------/gm, "-------")
150150
.replace(/^\\:end_line:/gm, ":end_line:")
151151
.replace(/^\\:start_line:/gm, ":start_line:")

0 commit comments

Comments
 (0)