Fixed crash when doing a diff in previously deleted file

Also diff was showing changes from the current workdir
This commit is contained in:
Abdelilah El Aissaoui 2021-09-27 01:23:08 +02:00
parent e3a2b0b31a
commit 58d3f06998
5 changed files with 27 additions and 17 deletions

View File

@ -14,13 +14,12 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import org.eclipse.jgit.diff.DiffEntry
import theme.primaryTextColor import theme.primaryTextColor
@Composable @Composable
fun Diff(gitManager: GitManager, diffEntry: DiffEntry, onCloseDiffView: () -> Unit) { fun Diff(gitManager: GitManager, diffEntryType: DiffEntryType, onCloseDiffView: () -> Unit) {
val text = remember(diffEntry) { val text = remember(diffEntryType.diffEntry) {
gitManager.diffFormat(diffEntry) gitManager.diffFormat(diffEntryType)
} }
Card( Card(

View File

@ -0,0 +1,7 @@
import org.eclipse.jgit.diff.DiffEntry
sealed class DiffEntryType(val diffEntry: DiffEntry) {
class CommitDiff(diffEntry: DiffEntry): DiffEntryType(diffEntry)
class UnstagedDiff(diffEntry: DiffEntry): DiffEntryType(diffEntry)
class StagedDiff(diffEntry: DiffEntry): DiffEntryType(diffEntry)
}

View File

@ -126,7 +126,8 @@ class GitManager {
val hasUncommitedChanges: StateFlow<Boolean> val hasUncommitedChanges: StateFlow<Boolean>
get() = statusManager.hasUncommitedChanges get() = statusManager.hasUncommitedChanges
fun diffFormat(diffEntry: DiffEntry): String { fun diffFormat(diffEntryType: DiffEntryType): String {
val diffEntry = diffEntryType.diffEntry
val byteArrayOutputStream = ByteArrayOutputStream() val byteArrayOutputStream = ByteArrayOutputStream()
DiffFormatter(byteArrayOutputStream).use { formatter -> DiffFormatter(byteArrayOutputStream).use { formatter ->
@ -136,9 +137,9 @@ class GitManager {
val oldTree = DirCacheIterator(repo.readDirCache()) val oldTree = DirCacheIterator(repo.readDirCache())
val newTree = FileTreeIterator(repo) val newTree = FileTreeIterator(repo)
println(diffEntry) if(diffEntryType is DiffEntryType.UnstagedDiff)
formatter.scan(oldTree, newTree) //TODO Should only be set when using diff for unstaged changes formatter.scan(oldTree, newTree)
// formatter.format(oldTree, newTree)
formatter.format(diffEntry) formatter.format(diffEntry)
formatter.flush() formatter.flush()
} }

View File

@ -20,7 +20,7 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
} }
var diffSelected by remember { var diffSelected by remember {
mutableStateOf<DiffEntry?>(null) mutableStateOf<DiffEntryType?>(null)
} }
var uncommitedChangesSelected by remember { var uncommitedChangesSelected by remember {
mutableStateOf<Boolean>(false) mutableStateOf<Boolean>(false)
@ -79,7 +79,7 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
else -> { else -> {
Diff( Diff(
gitManager = gitManager, gitManager = gitManager,
diffEntry = diffEntry, diffEntryType = diffEntry,
onCloseDiffView = { diffSelected = null }) onCloseDiffView = { diffSelected = null })
} }
} }
@ -94,9 +94,11 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
if (uncommitedChangesSelected) { if (uncommitedChangesSelected) {
UncommitedChanges( UncommitedChanges(
gitManager = gitManager, gitManager = gitManager,
onDiffEntrySelected = { diffEntry -> onStagedDiffEntrySelected = { diffEntry ->
println(diffEntry.filePath) diffSelected = DiffEntryType.StagedDiff(diffEntry)
diffSelected = diffEntry },
onUnstagedDiffEntrySelected = { diffEntry ->
diffSelected = DiffEntryType.UnstagedDiff(diffEntry)
} }
) )
} else { } else {
@ -104,7 +106,7 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
CommitChanges( CommitChanges(
commitDiff = it, commitDiff = it,
onDiffSelected = { diffEntry -> onDiffSelected = { diffEntry ->
diffSelected = diffEntry diffSelected = DiffEntryType.CommitDiff(diffEntry)
} }
) )
} }

View File

@ -33,7 +33,8 @@ import theme.headerBackground
@Composable @Composable
fun UncommitedChanges( fun UncommitedChanges(
gitManager: GitManager, gitManager: GitManager,
onDiffEntrySelected: (DiffEntry) -> Unit, onStagedDiffEntrySelected: (DiffEntry) -> Unit,
onUnstagedDiffEntrySelected: (DiffEntry) -> Unit,
) { ) {
val stageStatusState = gitManager.stageStatus.collectAsState() val stageStatusState = gitManager.stageStatus.collectAsState()
val stageStatus = stageStatusState.value val stageStatus = stageStatusState.value
@ -64,7 +65,7 @@ fun UncommitedChanges(
title = "Staged", title = "Staged",
optionIcon = Icons.Default.Close, optionIcon = Icons.Default.Close,
diffEntries = staged, diffEntries = staged,
onDiffEntrySelected = onDiffEntrySelected, onDiffEntrySelected = onStagedDiffEntrySelected,
onDiffEntryOptionSelected = { onDiffEntryOptionSelected = {
gitManager.unstage(it) gitManager.unstage(it)
} }
@ -78,7 +79,7 @@ fun UncommitedChanges(
title = "Unstaged", title = "Unstaged",
optionIcon = Icons.Default.Add, optionIcon = Icons.Default.Add,
diffEntries = unstaged, diffEntries = unstaged,
onDiffEntrySelected = onDiffEntrySelected, onDiffEntrySelected = onUnstagedDiffEntrySelected,
onDiffEntryOptionSelected = { onDiffEntryOptionSelected = {
gitManager.stage(it) gitManager.stage(it)
} }