Skip to content

Commit 8066d38

Browse files
committed
fix(vcs): handle exceptions and improve diff processing
- Add try-catch block to handle exceptions in `DiffSimplifier.postProcess`. - Reduce the number of changes processed in `limitedChanges` from 500 to 100. - Replace `logger.info` with `logger.warn` for error logging. - Use `AutoDevNotifications.error` for error notifications. - Safely handle potential null values with `getOrNull` in diff processing.
1 parent 81fb820 commit 8066d38

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.devti.vcs
22

3+
import cc.unitmesh.devti.AutoDevNotifications
34
import com.intellij.openapi.components.Service
45
import com.intellij.openapi.diagnostic.logger
56
import com.intellij.openapi.diff.impl.patch.IdeaTextPatchBuilder
@@ -8,7 +9,6 @@ import com.intellij.openapi.project.Project
89
import com.intellij.openapi.util.io.FileUtilRt
910
import com.intellij.openapi.vcs.changes.*
1011
import com.intellij.project.stateStore
11-
import git4idea.config.GitExecutableManager
1212
import org.jetbrains.annotations.NotNull
1313
import java.io.StringWriter
1414
import java.nio.file.Path
@@ -63,11 +63,10 @@ class DiffSimplifier(val project: Project) {
6363
return ""
6464
}
6565

66-
67-
val limitedChnages = filteredChanges.subList(0, min(filteredChanges.size, 500))
66+
val limitedChanges = filteredChanges.subList(0, min(filteredChanges.size, 100))
6867

6968
val patches = IdeaTextPatchBuilder.buildPatch(
70-
project, limitedChnages, Path.of(basePath), false, true
69+
project, limitedChanges, Path.of(basePath), false, true
7170
)
7271

7372
UnifiedDiffWriter.write(
@@ -85,10 +84,11 @@ class DiffSimplifier(val project: Project) {
8584
return postProcess(originChanges)
8685
} catch (e: Exception) {
8786
if (originChanges.isNotEmpty()) {
88-
logger.info("Error calculating diff: $originChanges", e)
87+
logger.warn("Error calculating diff: $originChanges", e)
8988
}
9089

91-
throw RuntimeException("Error calculating diff: ${e.message}", e)
90+
AutoDevNotifications.error(project, "Error calculating diff: ${e.message}")
91+
return originChanges
9292
}
9393
}
9494

@@ -224,8 +224,8 @@ class DiffSimplifier(val project: Project) {
224224

225225
// handle for delete
226226
if (line.startsWith("deleted file mode")) {
227-
val nextLine = lines[index + 1]
228-
if (nextLine.startsWith("--- a/")) {
227+
val nextLine = lines.getOrNull(index + 1)
228+
if (nextLine?.startsWith("--- a/") == true) {
229229
val withoutHead = nextLine.substring("--- a/".length)
230230
// footer: (date 1704768267000)
231231
val withoutFooter = withoutHead.substring(0, withoutHead.indexOf("\t"))
@@ -250,8 +250,8 @@ class DiffSimplifier(val project: Project) {
250250

251251
if (line.startsWith("---") || line.startsWith("+++")) {
252252
// next line
253-
val nextLine = lines[index + 1]
254-
if (nextLine.startsWith("+++")) {
253+
val nextLine = lines.getOrNull(index + 1)
254+
if (nextLine?.startsWith("+++") == true) {
255255
// remove end date
256256
val substringBefore = line.substringBefore("(revision")
257257

core/src/main/kotlin/cc/unitmesh/devti/vcs/VcsPrompting.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ class VcsPrompting(private val project: Project) {
6868
return null
6969
}
7070

71-
val processedText = DiffSimplifier.postProcess(changeText)
71+
val processedText = try {
72+
DiffSimplifier.postProcess(changeText)
73+
} catch (e: Exception) {
74+
changeText
75+
}
7276

7377
val writer = StringWriter()
7478
if (details.isNotEmpty()) {

0 commit comments

Comments
 (0)