Moved repository opening to repository manager
This commit is contained in:
parent
6a49048d9a
commit
d544cc4e94
@ -3,10 +3,31 @@ package app.git
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
|
import org.eclipse.jgit.lib.Repository
|
||||||
|
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
|
||||||
|
import java.io.File
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class RepositoryManager @Inject constructor() {
|
class RepositoryManager @Inject constructor() {
|
||||||
suspend fun getRepositoryState(git: Git) = withContext(Dispatchers.IO) {
|
suspend fun getRepositoryState(git: Git) = withContext(Dispatchers.IO) {
|
||||||
return@withContext git.repository.repositoryState
|
return@withContext git.repository.repositoryState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun openRepository(directory: File): Repository {
|
||||||
|
val gitDirectory = if (directory.name == ".git") {
|
||||||
|
directory
|
||||||
|
} else {
|
||||||
|
val gitDir = File(directory, ".git")
|
||||||
|
if (gitDir.exists() && gitDir.isDirectory) {
|
||||||
|
gitDir
|
||||||
|
} else
|
||||||
|
directory
|
||||||
|
}
|
||||||
|
|
||||||
|
val builder = FileRepositoryBuilder()
|
||||||
|
return builder.setGitDir(gitDirectory)
|
||||||
|
.readEnvironment() // scan environment GIT_* variables
|
||||||
|
.findGitDir() // scan up the file system tree
|
||||||
|
.build()
|
||||||
|
}
|
||||||
}
|
}
|
@ -106,21 +106,7 @@ class TabViewModel @Inject constructor(
|
|||||||
fun openRepository(directory: File) = tabState.safeProcessingWihoutGit {
|
fun openRepository(directory: File) = tabState.safeProcessingWihoutGit {
|
||||||
println("Trying to open repository ${directory.absoluteFile}")
|
println("Trying to open repository ${directory.absoluteFile}")
|
||||||
|
|
||||||
val gitDirectory = if (directory.name == ".git") {
|
val repository: Repository = repositoryManager.openRepository(directory)
|
||||||
directory
|
|
||||||
} else {
|
|
||||||
val gitDir = File(directory, ".git")
|
|
||||||
if (gitDir.exists() && gitDir.isDirectory) {
|
|
||||||
gitDir
|
|
||||||
} else
|
|
||||||
directory
|
|
||||||
}
|
|
||||||
|
|
||||||
val builder = FileRepositoryBuilder()
|
|
||||||
val repository: Repository = builder.setGitDir(gitDirectory)
|
|
||||||
.readEnvironment() // scan environment GIT_* variables
|
|
||||||
.findGitDir() // scan up the file system tree
|
|
||||||
.build()
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
repository.workTree // test if repository is valid
|
repository.workTree // test if repository is valid
|
||||||
|
Loading…
Reference in New Issue
Block a user