Skip to content

Commit

Permalink
Close in-memory repo, fix diff when no changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Aug 23, 2016
1 parent 590e484 commit a0cab9b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
4 changes: 0 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ repositories {
antlr4.output = new File('src/main/java/com/netflix/java/refactor/aspectj')
antlr4.extraArgs = ['-package', 'com.netflix.java.refactor.aspectj']

// add antlr4 to classpath
configurations {
compile.extendsFrom antlr4
}

configurations {
compile.extendsFrom compileShaded
}

Expand Down
17 changes: 11 additions & 6 deletions src/main/kotlin/com/netflix/java/refactor/InMemoryDiffEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.io.ByteArrayOutputStream

class InMemoryDiffEntry(filePath: String, old: String, new: String): DiffEntry() {
private val repo = InMemoryRepository.Builder().build()

init {
changeType = DiffEntry.ChangeType.MODIFY
oldPath = filePath
Expand All @@ -22,13 +22,18 @@ class InMemoryDiffEntry(filePath: String, old: String, new: String): DiffEntry()

oldMode = FileMode.REGULAR_FILE
newMode = FileMode.REGULAR_FILE
repo.close()
}

val diff: String by lazy {
val patch = ByteArrayOutputStream()
val formatter = DiffFormatter(patch)
formatter.setRepository(repo)
formatter.format(this)
String(patch.toByteArray())
if(oldId == newId)
""
else {
val patch = ByteArrayOutputStream()
val formatter = DiffFormatter(patch)
formatter.setRepository(repo)
formatter.format(this)
String(patch.toByteArray())
}
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/com/netflix/java/refactor/JavaSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class JavaSource(internal val cu: CompilationUnit) {

fun file() = cu.source()
fun text() = String(Files.readAllBytes(cu.source()))

fun classes() = cu.jcCompilationUnit.defs
.filterIsInstance<JCTree.JCClassDecl>()
.map { it.sym }
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/com/netflix/java/refactor/JavaSourceDiff.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.netflix.java.refactor

class JavaSourceDiff(private val source: JavaSource) {
val before = source.text()
private val before = source.text()

fun gitStylePatch(): String {
val after = source.text()
return InMemoryDiffEntry(source.file().toString(), before, after).diff
}
fun gitStylePatch() = InMemoryDiffEntry(source.file().toString(), before, source.text()).diff
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class RefactorTransaction(val refactorer: JavaSource) {
}

Files.write(refactorer.file(), source.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)

// refactorer.file().toFile().writeText(source)
} catch(t: Throwable) {
// TODO how can we throw a better exception?
t.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import org.junit.Assert.assertEquals
class InMemoryDiffEntryTest {

@Test
fun diffTest() {
fun idempotent() {
val diff = InMemoryDiffEntry("com/netflix/MyJavaClass.java",
"public class A {}",
"public class A {}")

assertEquals("", diff.diff)
}

@Test
fun singleLineChange() {
val diff = InMemoryDiffEntry("com/netflix/MyJavaClass.java",
"""
|public void test() {
Expand Down

0 comments on commit a0cab9b

Please sign in to comment.