diff options
Diffstat (limited to 'app/src/main/java')
5 files changed, 41 insertions, 25 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt index e42bfef3..1376dd04 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt @@ -44,7 +44,6 @@ import kotlinx.coroutines.launch import me.msfjarvis.openpgpktx.util.OpenPgpApi import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection import me.msfjarvis.openpgpktx.util.OpenPgpUtils -import org.eclipse.jgit.api.Git class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { @@ -328,16 +327,13 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB if (result.resultCode == RESULT_OK) { result.data?.getStringArrayExtra(OpenPgpApi.EXTRA_KEY_IDS)?.let { keyIds -> gpgIdentifierFile.writeText(keyIds.joinToString("\n")) - val repo = PasswordRepository.getRepository(null) - if (repo != null) { - lifecycleScope.launch { - commitChange( - getString( - R.string.git_commit_gpg_id, - getLongName(gpgIdentifierFile.parentFile!!.absolutePath, repoPath, gpgIdentifierFile.name) - ) + lifecycleScope.launch { + commitChange( + getString( + R.string.git_commit_gpg_id, + getLongName(gpgIdentifierFile.parentFile!!.absolutePath, repoPath, gpgIdentifierFile.name) ) - } + ) } encrypt(data) } @@ -430,19 +426,13 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB returnIntent.putExtra(RETURN_EXTRA_USERNAME, username) } - val repo = PasswordRepository.getRepository(null) - if (repo != null) { - val status = Git(repo).status().call() - if (status.modified.isNotEmpty()) { - lifecycleScope.launch { - commitChange( - getString( - R.string.git_commit_edit_text, - getLongName(fullPath, repoPath, editName) - ) - ) - } - } + lifecycleScope.launch { + commitChange( + getString( + R.string.git_commit_edit_text, + getLongName(fullPath, repoPath, editName) + ) + ) } if (directoryInputLayout.isVisible && directoryInputLayout.isEnabled && oldFileName != null) { diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt index 97ce6602..f06fc891 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitCommandExecutor.kt @@ -27,6 +27,7 @@ import org.eclipse.jgit.api.CommitCommand import org.eclipse.jgit.api.PullCommand import org.eclipse.jgit.api.PushCommand import org.eclipse.jgit.api.RebaseResult +import org.eclipse.jgit.api.StatusCommand import org.eclipse.jgit.transport.RemoteRefUpdate import org.eclipse.jgit.transport.SshSessionFactory @@ -43,14 +44,24 @@ class GitCommandExecutor( message = activity.resources.getString(R.string.git_operation_running), length = Snackbar.LENGTH_INDEFINITE, ) + // Count the number of uncommitted files + var nbChanges = 0 var operationResult: Result = Result.Ok try { for (command in operation.commands) { when (command) { + is StatusCommand -> { + val res = withContext(Dispatchers.IO) { + command.call() + } + nbChanges = res.uncommittedChanges.size + } is CommitCommand -> { // the previous status will eventually be used to avoid a commit - withContext(Dispatchers.IO) { - command.call() + if (nbChanges > 0) { + withContext(Dispatchers.IO) { + command.call() + } } } is PullCommand -> { diff --git a/app/src/main/java/com/zeapo/pwdstore/git/operation/ResetToRemoteOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/operation/ResetToRemoteOperation.kt index f0aee212..0d0dc019 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/operation/ResetToRemoteOperation.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/operation/ResetToRemoteOperation.kt @@ -18,9 +18,14 @@ import org.eclipse.jgit.api.ResetCommand class ResetToRemoteOperation(fileDir: File, callingActivity: AppCompatActivity) : GitOperation(fileDir, callingActivity) { override val commands = arrayOf( + // Stage all files git.add().addFilepattern("."), + // Fetch everything from the origin remote git.fetch().setRemote("origin"), + // Do a hard reset to the remote branch. Equivalent to git reset --hard origin/$remoteBranch git.reset().setRef("origin/$remoteBranch").setMode(ResetCommand.ResetType.HARD), + // Force-create $remoteBranch if it doesn't exist. This covers the case where you switched + // branches from 'master' to anything else. git.branchCreate().setName(remoteBranch).setForce(true), ) diff --git a/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt index 223f2cd8..35b12810 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/operation/SyncOperation.kt @@ -17,9 +17,15 @@ import java.io.File class SyncOperation(fileDir: File, callingActivity: AppCompatActivity) : GitOperation(fileDir, callingActivity) { override val commands = arrayOf( + // Stage all files git.add().addFilepattern("."), + // Populate the changed files count + git.status(), + // Commit everything! If needed, obviously. git.commit().setAll(true).setMessage("[Android Password Store] Sync"), + // Pull and rebase on top of the remote branch git.pull().setRebase(true).setRemote("origin"), + // Push it all back git.push().setPushAll().setRemote("origin"), ) diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt index da6be410..96bf1b7e 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -111,7 +111,11 @@ suspend fun FragmentActivity.commitChange( } object : GitOperation(getRepositoryDirectory(), this@commitChange) { override val commands = arrayOf( + // Stage all files git.add().addFilepattern("."), + // Populate the changed files count + git.status(), + // Commit everything! If anything changed, that is. git.commit().setAll(true).setMessage(message), ) |