Skip to content

Commit

Permalink
feat(diff): add handling for delete files
Browse files Browse the repository at this point in the history
This commit adds handling for delete files in the DiffSimplifier class. It checks for lines starting with "deleted file mode" and extracts the file path. The file path is then added to the destination list. Additionally, a test case is added to verify the handling of delete files.
  • Loading branch information
phodal committed Jan 11, 2024
1 parent 6b599c4 commit 72724da
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/prompting/diff/DiffSimplifier.kt
Expand Up @@ -71,6 +71,7 @@ class DiffSimplifier(val project: Project) {
@NotNull
fun postProcess(@NotNull diffString: String): String {
val lines = diffString.lines()
val length = lines.size
val destination = ArrayList<String>()
var index = 0
while (true) {
Expand Down Expand Up @@ -101,6 +102,8 @@ class DiffSimplifier(val project: Project) {
continue
}


// todo: spike for handle for new file
if (line.startsWith("@@") && line.endsWith("@@")) {
index++
continue
Expand Down Expand Up @@ -133,6 +136,32 @@ class DiffSimplifier(val project: Project) {
}
}

// handle for delete
if (line.startsWith("deleted file mode")) {
val nextLine = lines[index + 1]
if (nextLine.startsWith("--- a/")) {
val withoutHead = nextLine.substring("--- a/".length)
// footer: (date 1704768267000)
val withoutFooter = withoutHead.substring(0, withoutHead.indexOf("\t"))
destination.add("delete file $withoutFooter")
// search for the next line starts with "Index:"
while (true) {
if (index + 2 >= length) {
break
}

val nextNextLine = lines[index + 2]
if (nextNextLine.startsWith("Index:")) {
index += 3
break
}
index++
}

continue
}
}

if (line.startsWith("---") || line.startsWith("+++")) {
// remove revision number with regex
val result = revisionRegex.replace(line, "")
Expand Down
Expand Up @@ -39,4 +39,25 @@ class DiffSimplifierTest {
"""rename file server/src/main/kotlin/com/thoughtworks/archguard/code/module/domain/model/LeafManger.kt server/metric-service/src/main/kotlin/org/archguard/arch/LeafManger.kt""".trimMargin()
)
}

@Test
fun testHandleForDelete() {
val diff = """
Index: server/src/main/kotlin/com/thoughtworks/archguard/metrics/domain/dfms/ModuleDfms.kt
deleted file mode 100644
--- a/server/src/main/kotlin/com/thoughtworks/archguard/metrics/domain/dfms/ModuleDfms.kt
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.thoughtworks.archguard.metrics.domain.dfms
-
Index: server/src/main/kotlin/com/thoughtworks/archguard/metrics/domain/dfms/ClassDfms.kt
""".trimIndent()

val postProcess = DiffSimplifier.postProcess(diff)
assertEquals(
postProcess,
"""delete file server/src/main/kotlin/com/thoughtworks/archguard/metrics/domain/dfms/ModuleDfms.kt""".trimMargin()
)
}
}

0 comments on commit 72724da

Please sign in to comment.