diff options
author | Mohamed Zenadi <mohamed@zenadi.com> | 2015-04-19 20:27:30 +0200 |
---|---|---|
committer | Mohamed Zenadi <mohamed@zenadi.com> | 2015-04-19 20:27:30 +0200 |
commit | 771d8a9415831b5d6fe9cebf574fbf7e5eacd373 (patch) | |
tree | 3c74a84dce73e1e19b93839b803f509feabe36df /app/src/main/java/com/zeapo | |
parent | 6983ef4f82a85cae1e7f83638ce2f5ec37e96ef0 (diff) |
Add a Sync command that calls Pull then Push
Fixes #78
Diffstat (limited to 'app/src/main/java/com/zeapo')
5 files changed, 86 insertions, 36 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index 13ae41ae..b3d3beee 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -29,8 +29,6 @@ import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.Git; import java.io.File; -import java.io.IOException; -import java.util.Stack; public class PasswordStore extends ActionBarActivity { private static final String TAG = "PwdStrAct"; @@ -64,7 +62,7 @@ public class PasswordStore extends ActionBarActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.pwdstore, menu); + getMenuInflater().inflate(R.menu.main_menu, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); @@ -160,6 +158,17 @@ public class PasswordStore extends ActionBarActivity { startActivityForResult(intent, GitActivity.REQUEST_PULL); return true; + case R.id.git_sync: + if (!PasswordRepository.isInitialized()) { + initBefore.show(); + break; + } + + intent = new Intent(this, GitActivity.class); + intent.putExtra("Operation", GitActivity.REQUEST_SYNC); + startActivityForResult(intent, GitActivity.REQUEST_SYNC); + return true; + case R.id.refresh: updateListAdapter(); return true; diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java index 0db20811..76599c3b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java @@ -25,11 +25,7 @@ import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.UserPreference; import com.zeapo.pwdstore.utils.PasswordRepository; -import org.eclipse.jgit.api.CloneCommand; -import org.eclipse.jgit.api.Git; - import org.apache.commons.io.FileUtils; -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import java.io.File; import java.io.IOException; @@ -59,6 +55,7 @@ public class GitActivity extends ActionBarActivity { public static final int REQUEST_CLONE = 103; public static final int REQUEST_INIT = 104; public static final int EDIT_SERVER = 105; + public static final int REQUEST_SYNC = 106; @Override protected void onCreate(Bundle savedInstanceState) { @@ -260,11 +257,15 @@ public class GitActivity extends ActionBarActivity { break; case REQUEST_PULL: - pullFromRepository(); + syncRepository(REQUEST_PULL); break; case REQUEST_PUSH: - pushToRepository(); + syncRepository(REQUEST_PUSH); + break; + + case REQUEST_SYNC: + syncRepository(REQUEST_SYNC); break; } @@ -521,20 +522,6 @@ public class GitActivity extends ActionBarActivity { } /** - * Pull the latest changes from the remote repository and merges them locally - */ - public void pullFromRepository() { - syncRepository(REQUEST_PULL); - } - - /** - * Pushes the latest changes from the local repository to the remote one - */ - public void pushToRepository() { - syncRepository(REQUEST_PUSH); - } - - /** * Syncs the local repository with the remote one (either pull or push) * @param operation the operation to execute can be REQUEST_PULL or REQUEST_PUSH */ @@ -566,13 +553,19 @@ public class GitActivity extends ActionBarActivity { PasswordRepository.addRemote("origin", hostname, false); GitOperation op; - if (operation == REQUEST_PULL) { - op = new PullOperation(localDir, activity).setCommand(); - } else if (operation == REQUEST_PUSH) { - op = new PushOperation(localDir, activity).setCommand(); - } else { - Log.e(TAG, "Sync operation not recognized : " + operation); - return; + switch (operation) { + case REQUEST_PULL: + op = new PullOperation(localDir, activity).setCommand(); + break; + case REQUEST_PUSH: + op = new PushOperation(localDir, activity).setCommand(); + break; + case REQUEST_SYNC: + op = new SyncOperation(localDir, activity).setCommands(); + break; + default: + Log.e(TAG, "Sync operation not recognized : " + operation); + return; } try { diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java index add18278..1551012f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java @@ -39,10 +39,9 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { @Override protected String doInBackground(GitCommand... cmd) { - int count = cmd.length; - for (int i = 0; i < count; i++) { + for (GitCommand aCmd : cmd) { try { - cmd[i].call(); + aCmd.call(); } catch (Exception e) { e.printStackTrace(); return e.getMessage(); @@ -52,7 +51,8 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { } protected void onPostExecute(String result) { - this.dialog.dismiss(); + if (this.dialog != null) + this.dialog.dismiss(); if (result == null) result = "Unexpected error"; diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java index 60112c90..c7b70ca4 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java @@ -6,7 +6,6 @@ import android.content.DialogInterface; import android.content.Intent; import android.support.annotation.Nullable; import android.text.InputType; -import android.util.Log; import android.widget.EditText; import android.widget.LinearLayout; @@ -16,7 +15,6 @@ import com.zeapo.pwdstore.git.config.GitConfigSessionFactory; import com.zeapo.pwdstore.git.config.SshConfigSessionFactory; import com.zeapo.pwdstore.utils.PasswordRepository; -import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.transport.JschConfigSessionFactory; diff --git a/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java new file mode 100644 index 00000000..9351cc1b --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.java @@ -0,0 +1,50 @@ +package com.zeapo.pwdstore.git; + +import android.app.Activity; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.PushCommand; + +import java.io.File; + +public class SyncOperation extends GitOperation { + protected PullCommand pullCommand; + protected PushCommand pushCommand; + + /** + * Creates a new git operation + * + * @param fileDir the git working tree directory + * @param callingActivity the calling activity + */ + public SyncOperation(File fileDir, Activity callingActivity) { + super(fileDir, callingActivity); + } + + /** + * Sets the command + * @return the current object + */ + public SyncOperation setCommands() { + this.pullCommand = new Git(repository) + .pull() + .setRebase(true) + .setRemote("origin"); + this.pushCommand = new Git(repository) + .push() + .setPushAll() + .setRemote("origin"); + return this; + } + + @Override + public void execute() throws Exception { + + if (this.provider != null) { + this.pullCommand.setCredentialsProvider(this.provider); + this.pushCommand.setCredentialsProvider(this.provider); + } + new GitAsyncTask(callingActivity, true, false, PullCommand.class).execute(this.pullCommand, this.pushCommand); + } +} |