diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2022-01-27 09:22:15 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-27 09:22:15 +0530 |
commit | 2d607c78e79fb42eb10803fd5ed412cb74442807 (patch) | |
tree | 60bf859ee9281e98a46162f2301b32d58611bd1d /app/src/main/java | |
parent | d89d5003192cf11bd9701dddce63d5782b40a071 (diff) |
Unwind PasswordRepository's confusing control flow (#1686)
* Cleanup PasswordRepository
* Tear out nonsensical logic for getRepository
Diffstat (limited to 'app/src/main/java')
6 files changed, 26 insertions, 75 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/data/repo/PasswordRepository.kt b/app/src/main/java/dev/msfjarvis/aps/data/repo/PasswordRepository.kt index feaca889..a3cf2dda 100644 --- a/app/src/main/java/dev/msfjarvis/aps/data/repo/PasswordRepository.kt +++ b/app/src/main/java/dev/msfjarvis/aps/data/repo/PasswordRepository.kt @@ -4,8 +4,6 @@ */ package dev.msfjarvis.aps.data.repo -import android.os.Build -import androidx.annotation.RequiresApi import androidx.core.content.edit import com.github.michaelbull.result.getOrElse import com.github.michaelbull.result.onFailure @@ -18,77 +16,19 @@ import dev.msfjarvis.aps.util.extensions.unsafeLazy import dev.msfjarvis.aps.util.settings.PasswordSortOrder import dev.msfjarvis.aps.util.settings.PreferenceKeys import java.io.File -import java.nio.file.Files -import java.nio.file.LinkOption import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.Repository import org.eclipse.jgit.storage.file.FileRepositoryBuilder import org.eclipse.jgit.transport.RefSpec import org.eclipse.jgit.transport.RemoteConfig import org.eclipse.jgit.transport.URIish -import org.eclipse.jgit.util.FS -import org.eclipse.jgit.util.FS_POSIX_Java6 object PasswordRepository { - @RequiresApi(Build.VERSION_CODES.O) - private class FS_POSIX_Java6_with_optional_symlinks : FS_POSIX_Java6() { - - override fun supportsSymlinks() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - - override fun isSymLink(file: File) = Files.isSymbolicLink(file.toPath()) - - override fun readSymLink(file: File) = Files.readSymbolicLink(file.toPath()).toString() - - override fun createSymLink(source: File, target: String) { - val sourcePath = source.toPath() - if (Files.exists(sourcePath, LinkOption.NOFOLLOW_LINKS)) Files.delete(sourcePath) - Files.createSymbolicLink(sourcePath, File(target).toPath()) - } - } - - @RequiresApi(Build.VERSION_CODES.O) - private class Java7FSFactory : FS.FSFactory() { - - override fun detect(cygwinUsed: Boolean?): FS { - return FS_POSIX_Java6_with_optional_symlinks() - } - } - - private var repository: Repository? = null + var repository: Repository? = null private val settings by unsafeLazy { Application.instance.sharedPrefs } private val filesDir get() = Application.instance.filesDir - - /** - * Returns the git repository - * - * @param localDir needed only on the creation - * @return the git repository - */ - fun getRepository(localDir: File?): Repository? { - if (repository == null && localDir != null) { - val builder = FileRepositoryBuilder() - repository = - runCatching { - builder - .run { - gitDir = localDir - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - fs = Java7FSFactory().detect(null) - } - readEnvironment() - } - .build() - } - .getOrElse { e -> - e.printStackTrace() - null - } - } - return repository - } - val isInitialized: Boolean get() = repository != null @@ -96,11 +36,23 @@ object PasswordRepository { return repository?.objectDatabase?.exists() ?: false } - fun createRepository(localDir: File) { - localDir.delete() + /** + * Takes in a [repositoryDir] to initialize a Git repository with, and assigns it to [repository] + * as static state. + */ + private fun initializeRepository(repositoryDir: File) { + val builder = FileRepositoryBuilder() + repository = + runCatching { builder.setGitDir(repositoryDir).build() }.getOrElse { e -> + e.printStackTrace() + null + } + } - Git.init().setDirectory(localDir).call() - getRepository(localDir) + fun createRepository(repositoryDir: File) { + repositoryDir.delete() + Git.init().setDirectory(repositoryDir).call() + initializeRepository(repositoryDir) } // TODO add multiple remotes support for pull/push @@ -164,7 +116,7 @@ object PasswordRepository { fun initialize(): Repository? { val dir = getRepositoryDirectory() - // uninitialize the repo if the dir does not exist or is absolutely empty + // Un-initialize the repo if the dir does not exist or is absolutely empty settings.edit { if (!dir.exists() || !dir.isDirectory || requireNotNull(dir.listFiles()).isEmpty()) { putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false) @@ -172,9 +124,10 @@ object PasswordRepository { putBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, true) } } + // Create the repository static variable in PasswordRepository + initializeRepository(dir.resolve(".git")) - // create the repository static variable in PasswordRepository - return getRepository(File(dir.absolutePath + "/.git")) + return repository } /** diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt index e8111552..9b7b3e21 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt @@ -38,8 +38,7 @@ class FolderCreationDialogFragment : DialogFragment() { result.data?.getStringArrayExtra(OpenPgpApi.EXTRA_KEY_IDS)?.let { keyIds -> val gpgIdentifierFile = File(newFolder, ".gpg-id") gpgIdentifierFile.writeText(keyIds.joinToString("\n")) - val repo = PasswordRepository.getRepository(null) - if (repo != null) { + if (PasswordRepository.repository != null) { lifecycleScope.launch { val repoPath = PasswordRepository.getRepositoryDirectory().absolutePath requireActivity() diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt index 4fed7b99..959d9325 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitConfigActivity.kt @@ -78,7 +78,7 @@ class GitConfigActivity : BaseGitActivity() { /** Sets up the UI components of the tools section. */ private fun setupTools() { - val repo = PasswordRepository.getRepository(null) + val repo = PasswordRepository.repository if (repo != null) { binding.gitHeadStatus.text = headStatusMsg(repo) // enable the abort button only if we're rebasing or merging diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitServerConfigActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitServerConfigActivity.kt index 4b82ee81..6b1f30ea 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitServerConfigActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/git/config/GitServerConfigActivity.kt @@ -174,8 +174,7 @@ class GitServerConfigActivity : BaseGitActivity() { } } GitSettings.UpdateConnectionSettingsResult.Valid -> { - if (isClone && PasswordRepository.getRepository(null) == null) - PasswordRepository.initialize() + if (isClone && PasswordRepository.repository == null) PasswordRepository.initialize() if (!isClone) { Snackbar.make( binding.root, diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/GitLogModel.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/GitLogModel.kt index 4753bcd6..444a9fee 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/git/GitLogModel.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/git/GitLogModel.kt @@ -20,7 +20,7 @@ import org.eclipse.jgit.revwalk.RevCommit private val TAG = GitLogModel::class.java.simpleName private fun commits(): Iterable<RevCommit> { - val repo = PasswordRepository.getRepository(null) + val repo = PasswordRepository.repository if (repo == null) { logcat(TAG, ERROR) { "Could not access git repository" } return listOf() diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/GitOperation.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/GitOperation.kt index fe464069..f487b195 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/GitOperation.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/GitOperation.kt @@ -70,7 +70,7 @@ abstract class GitOperation(protected val callingActivity: FragmentActivity) { GitOperationEntryPoint::class.java ) - protected val repository = PasswordRepository.getRepository(null)!! + protected val repository = PasswordRepository.repository!! protected val git = Git(repository) protected val remoteBranch = hiltEntryPoint.gitSettings().branch private val authActivity |