diff --git a/src/main/kotlin/CommitChanges.kt b/src/main/kotlin/CommitChanges.kt index 4b77bb2..7b9c968 100644 --- a/src/main/kotlin/CommitChanges.kt +++ b/src/main/kotlin/CommitChanges.kt @@ -8,7 +8,6 @@ import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip -import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.res.useResource @@ -27,14 +26,19 @@ import theme.primaryTextColor import theme.secondaryTextColor import java.net.HttpURLConnection import java.net.URL -import java.text.DateFormat -import java.text.SimpleDateFormat -import java.util.* +import androidx.compose.runtime.getValue +import androidx.compose.runtime.setValue @Composable -fun CommitChanges(commitDiff: Pair>, onDiffSelected: (DiffEntry) -> Unit) { - val commit = commitDiff.first - val diff = commitDiff.second +fun CommitChanges( + gitManager: GitManager, + commit: RevCommit, + onDiffSelected: (DiffEntry) -> Unit +) { + var diff by remember { mutableStateOf(emptyList()) } + LaunchedEffect(commit) { + diff = gitManager.diffListFromCommit(commit) + } Column( modifier = Modifier diff --git a/src/main/kotlin/GitManager.kt b/src/main/kotlin/GitManager.kt index eacf747..25b482d 100644 --- a/src/main/kotlin/GitManager.kt +++ b/src/main/kotlin/GitManager.kt @@ -8,6 +8,7 @@ import org.eclipse.jgit.api.Git import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.lib.Ref import org.eclipse.jgit.lib.Repository +import org.eclipse.jgit.revwalk.RevCommit import org.eclipse.jgit.storage.file.FileRepositoryBuilder import java.io.File @@ -186,6 +187,10 @@ class GitManager { fun credentialsAccepted(user: String, password: String) { credentialsStateManager.updateState(CredentialsState.CredentialsAccepted(user, password)) } + + suspend fun diffListFromCommit(commit: RevCommit): List { + return diffManager.commitDiffEntries(safeGit, commit) + } } diff --git a/src/main/kotlin/RepositorySelected.kt b/src/main/kotlin/RepositorySelected.kt index b2f1000..562e253 100644 --- a/src/main/kotlin/RepositorySelected.kt +++ b/src/main/kotlin/RepositorySelected.kt @@ -22,18 +22,17 @@ import org.eclipse.jgit.treewalk.CanonicalTreeParser import java.io.IOException -@ExperimentalMaterialApi @Composable -fun RepositorySelected(gitManager: GitManager, repository: Repository) { +fun RepositorySelected(gitManager: GitManager) { var selectedRevCommit by remember { - mutableStateOf>?>(null) + mutableStateOf(null) } var diffSelected by remember { mutableStateOf(null) } var uncommitedChangesSelected by remember { - mutableStateOf(false) + mutableStateOf(false) } val selectedIndexCommitLog = remember { mutableStateOf(-1) } @@ -50,8 +49,8 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) { }, title = "", - ) { - Column ( + ) { + Column( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, @@ -76,7 +75,7 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) { }, visualTransformation = PasswordVisualTransformation() ) - Button(onClick = {gitManager.credentialsAccepted(userField, passwordField)}) { + Button(onClick = { gitManager.credentialsAccepted(userField, passwordField) }) { Text("Ok") } } @@ -109,29 +108,7 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) { onRevCommitSelected = { commit -> // TODO Move all this code to tree manager - val parent = if (commit.parentCount == 0) { - null - } else - commit.parents.first() - - val oldTreeParser = if (parent != null) - prepareTreeParser(repository, parent) - else { - CanonicalTreeParser() - } - - val newTreeParser = prepareTreeParser(repository, commit) - - Git(repository).use { git -> - val diffs = git.diff() - .setNewTree(newTreeParser) - .setOldTree(oldTreeParser) - .call() - - selectedRevCommit = commit to diffs - } - - + selectedRevCommit = commit uncommitedChangesSelected = false }, onUncommitedChangesSelected = { @@ -168,7 +145,8 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) { } else { selectedRevCommit?.let { CommitChanges( - commitDiff = it, + gitManager = gitManager, + commit = it, onDiffSelected = { diffEntry -> diffSelected = DiffEntryType.CommitDiff(diffEntry) } @@ -179,15 +157,3 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) { } } - -@Throws(IOException::class) -fun prepareTreeParser(repository: Repository, commit: RevCommit): AbstractTreeIterator? { - // from the commit we can build the tree which allows us to construct the TreeParser - RevWalk(repository).use { walk -> - val tree: RevTree = walk.parseTree(commit.tree.id) - val treeParser = CanonicalTreeParser() - repository.newObjectReader().use { reader -> treeParser.reset(reader, tree.id) } - walk.dispose() - return treeParser - } -} \ No newline at end of file diff --git a/src/main/kotlin/git/DiffManager.kt b/src/main/kotlin/git/DiffManager.kt index 3af4705..d62e151 100644 --- a/src/main/kotlin/git/DiffManager.kt +++ b/src/main/kotlin/git/DiffManager.kt @@ -4,10 +4,18 @@ import DiffEntryType import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git +import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.diff.DiffFormatter import org.eclipse.jgit.dircache.DirCacheIterator +import org.eclipse.jgit.lib.Repository +import org.eclipse.jgit.revwalk.RevCommit +import org.eclipse.jgit.revwalk.RevTree +import org.eclipse.jgit.revwalk.RevWalk +import org.eclipse.jgit.treewalk.AbstractTreeIterator +import org.eclipse.jgit.treewalk.CanonicalTreeParser import org.eclipse.jgit.treewalk.FileTreeIterator import java.io.ByteArrayOutputStream +import java.io.IOException class DiffManager { suspend fun diffFormat(git: Git, diffEntryType: DiffEntryType): List = withContext(Dispatchers.IO) { @@ -35,6 +43,37 @@ class DiffManager { return@withContext diff.split("\n", "\r\n").filterNot { it.startsWith("diff --git") } + } + suspend fun commitDiffEntries(git: Git, commit: RevCommit): List = withContext(Dispatchers.IO) { + val repository = git.repository + val parent = if (commit.parentCount == 0) { + null + } else + commit.parents.first() + + val oldTreeParser = if (parent != null) + prepareTreeParser(repository, parent) + else { + CanonicalTreeParser() + } + + val newTreeParser = prepareTreeParser(repository, commit) + + return@withContext git.diff() + .setNewTree(newTreeParser) + .setOldTree(oldTreeParser) + .call() + } +} + +fun prepareTreeParser(repository: Repository, commit: RevCommit): AbstractTreeIterator? { + // from the commit we can build the tree which allows us to construct the TreeParser + RevWalk(repository).use { walk -> + val tree: RevTree = walk.parseTree(commit.tree.id) + val treeParser = CanonicalTreeParser() + repository.newObjectReader().use { reader -> treeParser.reset(reader, tree.id) } + walk.dispose() + return treeParser } } \ No newline at end of file diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index e4c2610..18d3e8d 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -70,7 +70,7 @@ fun Gitnuro(gitManager: GitManager) { Crossfade(targetState = repositorySelectionStatus) { @Suppress("UnnecessaryVariable") // Don't inline it because smart cast won't work - when (val status = repositorySelectionStatus) { + when (repositorySelectionStatus) { RepositorySelectionStatus.None -> { NoneRepository() } @@ -78,7 +78,7 @@ fun Gitnuro(gitManager: GitManager) { LoadingRepository() } is RepositorySelectionStatus.Open -> { - RepositorySelected(gitManager = gitManager, repository = status.repository) + RepositorySelected(gitManager = gitManager) } } }