Completed images & binaries diff
This commit is contained in:
parent
27f216aa5d
commit
ef5bd9a254
@ -30,7 +30,6 @@ class RawFileManager @AssistedInject constructor(
|
|||||||
"png",
|
"png",
|
||||||
"jpg",
|
"jpg",
|
||||||
"jpeg",
|
"jpeg",
|
||||||
"svg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@ -72,7 +71,7 @@ class RawFileManager @AssistedInject constructor(
|
|||||||
println("Data's size is ${ldr.size}")
|
println("Data's size is ${ldr.size}")
|
||||||
|
|
||||||
val tempDir = createTempDirectory("gitnuro${repository.directory.absolutePath.replace("/", "_")}")
|
val tempDir = createTempDirectory("gitnuro${repository.directory.absolutePath.replace("/", "_")}")
|
||||||
val tempFile = createTempFile(tempDir, prefix = "${entry.newPath}_${side.name}")
|
val tempFile = createTempFile(tempDir, prefix = "${entry.newPath.replace("/", "_")}_${side.name}")
|
||||||
println("Temp file generated: ${tempFile.absolutePathString()}")
|
println("Temp file generated: ${tempFile.absolutePathString()}")
|
||||||
|
|
||||||
val out = FileOutputStream(tempFile.toFile())
|
val out = FileOutputStream(tempFile.toFile())
|
||||||
@ -102,7 +101,8 @@ sealed class EntryContent {
|
|||||||
object Missing: EntryContent()
|
object Missing: EntryContent()
|
||||||
object InvalidObjectBlob: EntryContent()
|
object InvalidObjectBlob: EntryContent()
|
||||||
data class Text(val rawText: RawText): EntryContent()
|
data class Text(val rawText: RawText): EntryContent()
|
||||||
data class ImageBinary(val tempFilePath: Path): EntryContent()
|
sealed class BinaryContent() : EntryContent()
|
||||||
object Binary: EntryContent()
|
data class ImageBinary(val tempFilePath: Path): BinaryContent()
|
||||||
|
object Binary: BinaryContent()
|
||||||
object TooLargeEntry: EntryContent()
|
object TooLargeEntry: EntryContent()
|
||||||
}
|
}
|
@ -12,11 +12,14 @@ import org.eclipse.jgit.patch.FileHeader.PatchType
|
|||||||
import org.eclipse.jgit.treewalk.AbstractTreeIterator
|
import org.eclipse.jgit.treewalk.AbstractTreeIterator
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.nio.file.Path
|
import java.io.InvalidObjectException
|
||||||
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
import kotlin.contracts.contract
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
private const val CONTEXT_LINES = 3
|
private const val CONTEXT_LINES = 3
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generator of [Hunk] lists from [DiffEntry]
|
* Generator of [Hunk] lists from [DiffEntry]
|
||||||
*/
|
*/
|
||||||
@ -45,13 +48,47 @@ class HunkDiffGenerator @AssistedInject constructor(
|
|||||||
val rawOld = rawFileManager.getRawContent(DiffEntry.Side.OLD, ent)
|
val rawOld = rawFileManager.getRawContent(DiffEntry.Side.OLD, ent)
|
||||||
val rawNew = rawFileManager.getRawContent(DiffEntry.Side.NEW, ent)
|
val rawNew = rawFileManager.getRawContent(DiffEntry.Side.NEW, ent)
|
||||||
|
|
||||||
// todo won't work for new files
|
if(rawOld == EntryContent.InvalidObjectBlob || rawNew == EntryContent.InvalidObjectBlob)
|
||||||
return if(rawOld is EntryContent.Text && rawNew is EntryContent.Text)
|
throw InvalidObjectException("Invalid object in diff format")
|
||||||
DiffResult.Text(format(fileHeader, rawOld.rawText, rawNew.rawText))
|
|
||||||
else if(rawOld is EntryContent.ImageBinary && rawNew is EntryContent.ImageBinary)
|
var diffResult: DiffResult = DiffResult.Text(emptyList())
|
||||||
DiffResult.Images(rawOld.tempFilePath, rawNew.tempFilePath)
|
|
||||||
else
|
// If we can, generate text diff (if one of the files has never been a binary file)
|
||||||
DiffResult.Text(emptyList())
|
val hasGeneratedTextDiff = canGenerateTextDiff(rawOld, rawNew) { oldRawText, newRawText ->
|
||||||
|
diffResult = DiffResult.Text(format(fileHeader, oldRawText, newRawText))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasGeneratedTextDiff) {
|
||||||
|
diffResult = DiffResult.NonText(rawOld, rawNew)
|
||||||
|
}
|
||||||
|
|
||||||
|
return diffResult
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
private fun canGenerateTextDiff(
|
||||||
|
rawOld: EntryContent,
|
||||||
|
rawNew: EntryContent,
|
||||||
|
onText: (oldRawText: RawText, newRawText: RawText) -> Unit
|
||||||
|
): Boolean {
|
||||||
|
|
||||||
|
val rawOldText = when (rawOld) {
|
||||||
|
is EntryContent.Text -> rawOld.rawText
|
||||||
|
EntryContent.Missing -> RawText.EMPTY_TEXT
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
val newOldText = when (rawNew) {
|
||||||
|
is EntryContent.Text -> rawNew.rawText
|
||||||
|
EntryContent.Missing -> RawText.EMPTY_TEXT
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
return if(rawOldText != null && newOldText != null) {
|
||||||
|
onText(rawOldText, newOldText)
|
||||||
|
true
|
||||||
|
} else
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,5 +193,8 @@ class HunkDiffGenerator @AssistedInject constructor(
|
|||||||
|
|
||||||
sealed class DiffResult {
|
sealed class DiffResult {
|
||||||
data class Text(val hunks: List<Hunk>) : DiffResult()
|
data class Text(val hunks: List<Hunk>) : DiffResult()
|
||||||
data class Images(val oldTempFile: Path, val newTempsFile: Path): DiffResult()
|
data class NonText(
|
||||||
|
val oldBinaryContent: EntryContent,
|
||||||
|
val newBinaryContent: EntryContent,
|
||||||
|
) : DiffResult()
|
||||||
}
|
}
|
@ -18,6 +18,7 @@ import androidx.compose.ui.text.font.FontFamily
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import app.git.DiffEntryType
|
import app.git.DiffEntryType
|
||||||
|
import app.git.EntryContent
|
||||||
import app.git.diff.DiffResult
|
import app.git.diff.DiffResult
|
||||||
import app.git.diff.Hunk
|
import app.git.diff.Hunk
|
||||||
import app.git.diff.Line
|
import app.git.diff.Line
|
||||||
@ -28,6 +29,7 @@ import app.ui.components.SecondaryButton
|
|||||||
import app.viewmodels.DiffViewModel
|
import app.viewmodels.DiffViewModel
|
||||||
import org.eclipse.jgit.diff.DiffEntry
|
import org.eclipse.jgit.diff.DiffEntry
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
|
import java.nio.file.Path
|
||||||
import kotlin.io.path.absolutePathString
|
import kotlin.io.path.absolutePathString
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
@ -52,39 +54,101 @@ fun Diff(
|
|||||||
DiffHeader(diffEntry, onCloseDiffView)
|
DiffHeader(diffEntry, onCloseDiffView)
|
||||||
if (diffResult is DiffResult.Text) {
|
if (diffResult is DiffResult.Text) {
|
||||||
TextDiff(diffEntryType, diffViewModel, diffResult)
|
TextDiff(diffEntryType, diffViewModel, diffResult)
|
||||||
} else if (diffResult is DiffResult.Images) {
|
} else if (diffResult is DiffResult.NonText) {
|
||||||
ImagesDiff(diffResult)
|
NonTextDiff(diffResult)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ImagesDiff(diffResult: DiffResult.Images) {
|
fun NonTextDiff(diffResult: DiffResult.NonText) {
|
||||||
val oldImagePath = diffResult.oldTempFile
|
val oldBinaryContent = diffResult.oldBinaryContent
|
||||||
val newImagePath = diffResult.newTempsFile
|
val newBinaryContent = diffResult.newBinaryContent
|
||||||
|
|
||||||
|
val showOldAndNew = oldBinaryContent != EntryContent.Missing && newBinaryContent != EntryContent.Missing
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize(),
|
||||||
.background(Color.Red),
|
|
||||||
verticalAlignment = Alignment.CenterVertically
|
verticalAlignment = Alignment.CenterVertically
|
||||||
) {
|
) {
|
||||||
Image(
|
|
||||||
bitmap = loadImageBitmap(inputStream = FileInputStream(oldImagePath.absolutePathString())),
|
if (showOldAndNew) {
|
||||||
contentDescription = null,
|
Column(
|
||||||
modifier = Modifier.fillMaxWidth(0.5f)
|
modifier = Modifier.weight(0.5f)
|
||||||
.background(Color.Yellow),
|
.padding(start = 24.dp, end = 8.dp, top = 24.dp, bottom = 24.dp),
|
||||||
)
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
Spacer(
|
) {
|
||||||
modifier = Modifier.fillMaxWidth(0.1f)
|
SideTitle("Old")
|
||||||
.background(Color.Green),
|
SideDiff(oldBinaryContent)
|
||||||
)
|
}
|
||||||
Image(
|
Column(
|
||||||
bitmap = loadImageBitmap(inputStream = FileInputStream(newImagePath.absolutePathString())),
|
modifier = Modifier.weight(0.5f)
|
||||||
contentDescription = null,
|
.padding(start = 8.dp, end = 24.dp, top = 24.dp, bottom = 24.dp),
|
||||||
modifier = Modifier.fillMaxWidth()
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
.background(Color.Blue),
|
) {
|
||||||
|
SideTitle("New")
|
||||||
|
SideDiff(newBinaryContent)
|
||||||
|
}
|
||||||
|
} else if(oldBinaryContent != EntryContent.Missing) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.fillMaxSize()
|
||||||
|
.padding(all = 24.dp),
|
||||||
|
) {
|
||||||
|
SideDiff(oldBinaryContent)
|
||||||
|
}
|
||||||
|
} else if(newBinaryContent != EntryContent.Missing) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier.fillMaxSize()
|
||||||
|
.padding(all = 24.dp),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center,
|
||||||
|
) {
|
||||||
|
SideTitle("Binary file")
|
||||||
|
Spacer(modifier = Modifier.height(24.dp))
|
||||||
|
SideDiff(newBinaryContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SideTitle(text: String) {
|
||||||
|
Text(
|
||||||
|
text = text,
|
||||||
|
fontSize = 20.sp,
|
||||||
|
color = MaterialTheme.colors.primaryTextColor,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SideDiff(entryContent: EntryContent) {
|
||||||
|
when (entryContent) {
|
||||||
|
EntryContent.Binary -> BinaryDiff()
|
||||||
|
is EntryContent.ImageBinary -> ImageDiff(entryContent.tempFilePath)
|
||||||
|
else -> {}
|
||||||
|
// is EntryContent.Text -> //TODO maybe have a text view if the file was a binary before?
|
||||||
|
// TODO Show some info about this EntryContent.TooLargeEntry -> TODO()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ImageDiff(tempImagePath: Path) {
|
||||||
|
Image(
|
||||||
|
bitmap = loadImageBitmap(inputStream = FileInputStream(tempImagePath.absolutePathString())),
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier.fillMaxSize()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun BinaryDiff() {
|
||||||
|
Image(
|
||||||
|
painter = painterResource("binary.svg"),
|
||||||
|
contentDescription = null,
|
||||||
|
modifier = Modifier.width(400.dp),
|
||||||
|
colorFilter = ColorFilter.tint(MaterialTheme.colors.primary)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
3
src/main/resources/binary.svg
Normal file
3
src/main/resources/binary.svg
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<svg width="120" height="120" viewBox="0 0 120 120" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path fill-rule="evenodd" clip-rule="evenodd" d="M10 0C4.47715 0 0 4.47715 0 10V110C0 115.523 4.47715 120 10 120H110C115.523 120 120 115.523 120 110V10C120 4.47715 115.523 0 110 0H10ZM59.666 55.0039V69.2773C59.666 73.1328 59.2148 76.5371 58.3125 79.4902C57.4375 82.416 56.166 84.8633 54.498 86.832C52.8301 88.8281 50.793 90.332 48.3867 91.3438C45.9805 92.3555 43.2734 92.8613 40.2656 92.8613C37.2852 92.8613 34.5918 92.3555 32.1855 91.3438C29.7793 90.332 27.7422 88.8281 26.0742 86.832C24.3789 84.8633 23.0664 82.416 22.1367 79.4902C21.2344 76.5371 20.7832 73.1328 20.7832 69.2773V55.0039C20.7832 51.1484 21.2344 47.7578 22.1367 44.832C23.0391 41.8789 24.3379 39.4043 26.0332 37.4082C27.7012 35.4395 29.7246 33.9492 32.1035 32.9375C34.5098 31.9258 37.2031 31.4199 40.1836 31.4199C43.1914 31.4199 45.8984 31.9258 48.3047 32.9375C50.7109 33.9492 52.7617 35.4395 54.457 37.4082C56.125 39.4043 57.4102 41.8789 58.3125 44.832C59.2148 47.7578 59.666 51.1484 59.666 55.0039ZM32.2676 62.4277V64.3555L48.1816 52.502C48.127 50.2598 47.8809 48.3457 47.4434 46.7598C47.0332 45.1465 46.418 43.8613 45.5977 42.9043C44.9688 42.1387 44.1895 41.5781 43.2598 41.2227C42.3574 40.8398 41.332 40.6484 40.1836 40.6484C38.8984 40.6484 37.7637 40.8945 36.7793 41.3867C35.8223 41.8516 35.0156 42.5625 34.3594 43.5195C33.6484 44.5312 33.1152 45.8438 32.7598 47.457C32.4316 49.0703 32.2676 50.9844 32.2676 53.1992V59.8027V62.4277ZM47.6484 76.7832C48.0039 75.1426 48.1816 73.2148 48.1816 71V64.0684V62.0996V60.2539L32.2676 72.0664C32.3223 73.8711 32.5 75.4707 32.8008 76.8652C33.1289 78.2598 33.5801 79.4219 34.1543 80.3516C34.8105 81.4727 35.6445 82.3066 36.6562 82.8535C37.668 83.4004 38.8711 83.6738 40.2656 83.6738C41.5508 83.6738 42.6855 83.4414 43.6699 82.9766C44.6543 82.4844 45.4746 81.7461 46.1309 80.7617C46.8145 79.7227 47.3203 78.3965 47.6484 76.7832ZM99.1641 32.2812V92H87.6387V46.1855L72.5449 51.5996V41.6738L98.5488 32.2812H99.1641Z" fill="black"/>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 2.0 KiB |
Loading…
Reference in New Issue
Block a user