From c71f7555decd124c72d65ec32d79e38e80774c9c Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Thu, 7 Apr 2022 22:39:33 +0200 Subject: [PATCH] Fixed stage/unstage hunks not working on properly on specific line endings --- .../app/extensions/RawTextExtensions.kt | 2 +- .../kotlin/app/extensions/StringExtensions.kt | 5 ---- src/main/kotlin/app/git/StatusManager.kt | 29 ++++++++++++++----- .../kotlin/app/git/diff/HunkDiffGenerator.kt | 20 +++++++++++-- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/app/extensions/RawTextExtensions.kt b/src/main/kotlin/app/extensions/RawTextExtensions.kt index a1e655d..0abd2ea 100644 --- a/src/main/kotlin/app/extensions/RawTextExtensions.kt +++ b/src/main/kotlin/app/extensions/RawTextExtensions.kt @@ -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) } \ No newline at end of file diff --git a/src/main/kotlin/app/extensions/StringExtensions.kt b/src/main/kotlin/app/extensions/StringExtensions.kt index 146001e..9df07de 100644 --- a/src/main/kotlin/app/extensions/StringExtensions.kt +++ b/src/main/kotlin/app/extensions/StringExtensions.kt @@ -29,8 +29,3 @@ val String.dirPath: String } else this } - -val String.withoutLineEnding: String - get() = this - .removeSuffix("\n") - .removeSuffix("\r\n") \ No newline at end of file diff --git a/src/main/kotlin/app/git/StatusManager.kt b/src/main/kotlin/app/git/StatusManager.kt index 6a6c4ea..3c4042f 100644 --- a/src/main/kotlin/app/git/StatusManager.kt +++ b/src/main/kotlin/app/git/StatusManager.kt @@ -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 { - 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 = 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( diff --git a/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt b/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt index 3da8feb..01accf4 100644 --- a/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt +++ b/src/main/kotlin/app/git/diff/HunkDiffGenerator.kt @@ -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++