From 81c37043caac73d6564dac00748684f7a0446c74 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 11 Nov 2023 14:46:30 +0100 Subject: [PATCH] Discarding hunks now always uses UTF-8 Fixes #170 --- .../{ResetEntryUseCase.kt => DiscardEntryUseCase.kt} | 2 +- .../git/workspace/DiscardUnstagedHunkLineUseCase.kt | 10 ++-------- .../jetpackduba/gitnuro/viewmodels/StatusViewModel.kt | 2 +- 3 files changed, 4 insertions(+), 10 deletions(-) rename src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/{ResetEntryUseCase.kt => DiscardEntryUseCase.kt} (92%) diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/ResetEntryUseCase.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/DiscardEntryUseCase.kt similarity index 92% rename from src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/ResetEntryUseCase.kt rename to src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/DiscardEntryUseCase.kt index cbf1e57f..87747a89 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/ResetEntryUseCase.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/DiscardEntryUseCase.kt @@ -5,7 +5,7 @@ import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git import javax.inject.Inject -class ResetEntryUseCase @Inject constructor() { +class DiscardEntryUseCase @Inject constructor() { suspend operator fun invoke(git: Git, statusEntry: StatusEntry, staged: Boolean): Unit = withContext(Dispatchers.IO) { if (staged || statusEntry.statusType == StatusType.CONFLICTING) { diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/DiscardUnstagedHunkLineUseCase.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/DiscardUnstagedHunkLineUseCase.kt index d31787e5..f333059f 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/DiscardUnstagedHunkLineUseCase.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/DiscardUnstagedHunkLineUseCase.kt @@ -1,7 +1,6 @@ package com.jetpackduba.gitnuro.git.workspace import com.jetpackduba.gitnuro.extensions.filePath -import com.jetpackduba.gitnuro.extensions.lineDelimiter import com.jetpackduba.gitnuro.git.diff.Hunk import com.jetpackduba.gitnuro.git.diff.Line import com.jetpackduba.gitnuro.git.diff.LineType @@ -10,7 +9,6 @@ import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git import org.eclipse.jgit.diff.DiffEntry import java.io.File -import java.io.FileWriter import javax.inject.Inject class DiscardUnstagedHunkLineUseCase @Inject constructor( @@ -22,7 +20,7 @@ class DiscardUnstagedHunkLineUseCase @Inject constructor( try { val file = File(repository.workTree, diffEntry.filePath) - val content = file.readText() + val content = file.readText(Charsets.UTF_8) val textLines = getLinesFromTextUseCase(content).toMutableList() if (line.lineType == LineType.ADDED) { @@ -47,13 +45,9 @@ class DiscardUnstagedHunkLineUseCase @Inject constructor( } } - println(content) val resultText = textLines.joinToString("") + file.writeText(resultText, Charsets.UTF_8) - - FileWriter(file).use { fw -> - fw.write(resultText) - } } catch (ex: Exception) { throw Exception( "Discard hunk line failed. Check if the file still exists and has the write permissions set", diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/StatusViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/StatusViewModel.kt index 920ce17c..9613eec6 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/StatusViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/StatusViewModel.kt @@ -37,7 +37,7 @@ class StatusViewModel @Inject constructor( private val tabState: TabState, private val stageEntryUseCase: StageEntryUseCase, private val unstageEntryUseCase: UnstageEntryUseCase, - private val resetEntryUseCase: ResetEntryUseCase, + private val resetEntryUseCase: DiscardEntryUseCase, private val stageAllUseCase: StageAllUseCase, private val unstageAllUseCase: UnstageAllUseCase, private val checkHasPreviousCommitsUseCase: CheckHasPreviousCommitsUseCase,