Moved commit's diffEntry list loading to DiffManager

This commit is contained in:
Abdelilah El Aissaoui 2021-10-03 00:44:10 +02:00
parent 408d3d6dce
commit 2f77127119
5 changed files with 66 additions and 52 deletions

View file

@ -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<RevCommit, List<DiffEntry>>, 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<DiffEntry>()) }
LaunchedEffect(commit) {
diff = gitManager.diffListFromCommit(commit)
}
Column(
modifier = Modifier

View file

@ -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<DiffEntry> {
return diffManager.commitDiffEntries(safeGit, commit)
}
}

View file

@ -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<Pair<RevCommit, List<DiffEntry>>?>(null)
mutableStateOf<RevCommit?>(null)
}
var diffSelected by remember {
mutableStateOf<DiffEntryType?>(null)
}
var uncommitedChangesSelected by remember {
mutableStateOf<Boolean>(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
}
}

View file

@ -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<String> = 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<DiffEntry> = 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
}
}

View file

@ -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)
}
}
}