Fixed stage/unstage hunks not working on properly on specific line endings

This commit is contained in:
Abdelilah El Aissaoui 2022-04-07 22:39:33 +02:00
parent 5982c65c79
commit c71f7555de
4 changed files with 41 additions and 15 deletions

View file

@ -6,5 +6,5 @@ import java.io.ByteArrayOutputStream
fun RawText.lineAt(line: Int): String {
val outputStream = ByteArrayOutputStream()
this.writeLine(outputStream, line)
return outputStream.toString(Charsets.UTF_8) + "\n"
return outputStream.toString(Charsets.UTF_8)
}

View file

@ -29,8 +29,3 @@ val String.dirPath: String
} else
this
}
val String.withoutLineEnding: String
get() = this
.removeSuffix("\n")
.removeSuffix("\r\n")

View file

@ -71,7 +71,7 @@ class StatusManager @Inject constructor(
for (line in hunkLines) {
when (line.lineType) {
LineType.ADDED -> {
textLines.add(line.oldLineNumber + linesAdded, line.text.withoutLineEnding)
textLines.add(line.oldLineNumber + linesAdded, line.text)
linesAdded++
}
LineType.REMOVED -> {
@ -82,7 +82,7 @@ class StatusManager @Inject constructor(
}
}
val stagedFileText = textLines.joinToString(entryContent.rawText.lineDelimiter)
val stagedFileText = textLines.joinToString("")
dirCacheEditor.add(HunkEdit(diffEntry.newPath, repository, ByteBuffer.wrap(stagedFileText.toByteArray())))
dirCacheEditor.commit()
@ -100,7 +100,7 @@ class StatusManager @Inject constructor(
var completedWithErrors = true
try {
val rawFileManager = rawFileManagerFactory.create(git.repository)
val rawFileManager = rawFileManagerFactory.create(repository)
val entryContent = rawFileManager.getRawContent(DiffEntry.Side.NEW, diffEntry)
if (entryContent !is EntryContent.Text)
@ -131,11 +131,11 @@ class StatusManager @Inject constructor(
for (line in removedLines) {
// Check how many lines before this one have been deleted
val previouslyRemovedLines = addedLines.count { it.newLineNumber < line.newLineNumber }
textLines.add(line.newLineNumber + linesAdded - previouslyRemovedLines, line.text.withoutLineEnding)
textLines.add(line.newLineNumber + linesAdded - previouslyRemovedLines, line.text)
linesAdded++
}
val stagedFileText = textLines.joinToString(entryContent.rawText.lineDelimiter)
val stagedFileText = textLines.joinToString("")
dirCacheEditor.add(HunkEdit(diffEntry.newPath, repository, ByteBuffer.wrap(stagedFileText.toByteArray())))
dirCacheEditor.commit()
@ -147,8 +147,23 @@ class StatusManager @Inject constructor(
}
private fun getTextLines(rawFile: RawText): List<String> {
val content = rawFile.rawContent.toString(Charsets.UTF_8)
return content.split(rawFile.lineDelimiter).toMutableList()
val content = rawFile.rawContent.toString(Charsets.UTF_8)//.removeSuffix(rawFile.lineDelimiter)
var splitted: List<String> = content.split(rawFile.lineDelimiter).toMutableList().apply {
if(this.last() == "")
removeLast()
}
splitted = splitted.mapIndexed { index, line ->
val lineWithBreak = line + rawFile.lineDelimiter
if(index == splitted.count() - 1 && !content.endsWith(lineWithBreak)) {
line
} else
lineWithBreak
}
return splitted
}
private class HunkEdit(

View file

@ -123,12 +123,28 @@ class HunkDiffGenerator @AssistedInject constructor(
oldCurrentLine++
newCurrentLine++
} else if (oldCurrentLine < curEdit.endA) {
val lineText = oldRawText.lineAt(oldCurrentLine)
var lineText = oldRawText.lineAt(oldCurrentLine)
if (
oldCurrentLine < oldRawText.size() - 1 || // If it's not the last
(oldCurrentLine == oldRawText.size() - 1 && !oldRawText.isMissingNewlineAtEnd) // Or is the last and contains new line at the end
) {
lineText += oldRawText.lineDelimiter
}
lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.REMOVED))
oldCurrentLine++
} else if (newCurrentLine < curEdit.endB) {
val lineText = newRawText.lineAt(newCurrentLine)
var lineText = newRawText.lineAt(newCurrentLine)
if (
newCurrentLine < newRawText.size() - 1 || // If it's not the last
(newCurrentLine == newRawText.size() - 1 && !newRawText.isMissingNewlineAtEnd) // Or is the last and contains new line at the end
) {
lineText += newRawText.lineDelimiter
}
lines.add(Line(lineText, oldCurrentLine, newCurrentLine, LineType.ADDED))
newCurrentLine++