Skip to content

Commit 4393411

Browse files
committed
fix(diff-simplifier): improve handling of real-world diffs #51
The diff simplifier now correctly handles diffs with multiple changes on the same line, including those with comments and blank lines.
1 parent e94b191 commit 4393411

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/main/kotlin/cc/unitmesh/devti/vcs/DiffSimplifier.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,15 @@ class DiffSimplifier(val project: Project) {
247247

248248
val startLine = substringBefore
249249
.substring("--- a/".length).trim()
250-
val withoutEnd = nextLine.substring("+++ b/".length, nextLine.indexOf("(date")).trim()
250+
var endIndex = nextLine.indexOf("(date")
251+
if (endIndex == -1) {
252+
endIndex = nextLine.indexOf("(revision")
253+
}
254+
if (endIndex == -1) {
255+
endIndex = nextLine.length
256+
}
257+
258+
val withoutEnd = nextLine.substring("+++ b/".length, endIndex).trim()
251259

252260
if (startLine == withoutEnd) {
253261
index += 2

src/test/kotlin/cc/unitmesh/devti/prompting/diff/DiffSimplifierTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,33 @@ change import from com.thoughtworks.archguard.code.module.domain.dubbo.ServiceCo
123123
"""modify file server/src/test/kotlin/com/thoughtworks/archguard/code/clazz/domain/CodeTreeTest.kt"""
124124
)
125125
}
126+
127+
@Test
128+
fun testHandleForRealWorld() {
129+
val code = """Index: src/main/java/cc/unitmesh/untitled/demo/controller/BlogCategoryController.java
130+
===================================================================
131+
diff --git a/src/main/java/cc/unitmesh/untitled/demo/controller/BlogCategoryController.java b/src/main/java/cc/unitmesh/untitled/demo/controller/BlogCategoryController.java
132+
--- a/src/main/java/cc/unitmesh/untitled/demo/controller/BlogCategoryController.java (revision 9b4b04de55fe5be5372eae67987b27f7d329e1f3)
133+
+++ b/src/main/java/cc/unitmesh/untitled/demo/controller/BlogCategoryController.java (revision 768efa678ecb9c044aea7c5c4873ed218357773b)
134+
@@ -6,7 +6,5 @@
135+
public class BlogCategoryController {
136+
// devti://story/github/1
137+
138+
- // 银行账户管理
139+
-
140+
// Close a bank account
141+
}"""
142+
143+
val postProcess = DiffSimplifier.postProcess(code)
144+
assertEquals(
145+
postProcess,
146+
"""modify file src/main/java/cc/unitmesh/untitled/demo/controller/BlogCategoryController.java
147+
public class BlogCategoryController {
148+
// devti://story/github/1
149+
- // 银行账户管理
150+
-
151+
// Close a bank account
152+
}"""
153+
)
154+
}
126155
}

0 commit comments

Comments
 (0)