Add a Sync command that calls Pull then Push

Fixes #78
This commit is contained in:
Mohamed Zenadi 2015-04-19 20:27:30 +02:00
parent 6983ef4f82
commit 771d8a9415
9 changed files with 101 additions and 43 deletions

View file

@ -3,12 +3,12 @@ apply from: 'copyLibs.gradle' // enable 'copyLibs.gradle' script plugin
apply plugin: 'eclipse' apply plugin: 'eclipse'
android { android {
compileSdkVersion 21 compileSdkVersion 22
buildToolsVersion "21.1.2" buildToolsVersion "22.0.0"
defaultConfig { defaultConfig {
applicationId "com.zeapo.pwdstore" applicationId "com.zeapo.pwdstore"
minSdkVersion 15 minSdkVersion 15
targetSdkVersion 21 targetSdkVersion 22
versionCode 35 versionCode 35
versionName "1.2.0.15" versionName "1.2.0.15"
} }
@ -25,8 +25,8 @@ android {
} }
dependencies { dependencies {
compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:21.0.3' compile 'com.android.support:recyclerview-v7:22.0.0'
//compile fileTree(dir: 'libs', include: ['*.jar']) //compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':libraries:openpgp-api-lib') compile project(':libraries:openpgp-api-lib')
compile 'org.eclipse.jgit:org.eclipse.jgit:3.7.0.201502260915-r' compile 'org.eclipse.jgit:org.eclipse.jgit:3.7.0.201502260915-r'

View file

@ -29,8 +29,6 @@ import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Stack;
public class PasswordStore extends ActionBarActivity { public class PasswordStore extends ActionBarActivity {
private static final String TAG = "PwdStrAct"; private static final String TAG = "PwdStrAct";
@ -64,7 +62,7 @@ public class PasswordStore extends ActionBarActivity {
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. // 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); MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
@ -160,6 +158,17 @@ public class PasswordStore extends ActionBarActivity {
startActivityForResult(intent, GitActivity.REQUEST_PULL); startActivityForResult(intent, GitActivity.REQUEST_PULL);
return true; 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: case R.id.refresh:
updateListAdapter(); updateListAdapter();
return true; return true;

View file

@ -25,11 +25,7 @@ import com.zeapo.pwdstore.R;
import com.zeapo.pwdstore.UserPreference; import com.zeapo.pwdstore.UserPreference;
import com.zeapo.pwdstore.utils.PasswordRepository; 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.apache.commons.io.FileUtils;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.File; import java.io.File;
import java.io.IOException; 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_CLONE = 103;
public static final int REQUEST_INIT = 104; public static final int REQUEST_INIT = 104;
public static final int EDIT_SERVER = 105; public static final int EDIT_SERVER = 105;
public static final int REQUEST_SYNC = 106;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -260,11 +257,15 @@ public class GitActivity extends ActionBarActivity {
break; break;
case REQUEST_PULL: case REQUEST_PULL:
pullFromRepository(); syncRepository(REQUEST_PULL);
break; break;
case REQUEST_PUSH: case REQUEST_PUSH:
pushToRepository(); syncRepository(REQUEST_PUSH);
break;
case REQUEST_SYNC:
syncRepository(REQUEST_SYNC);
break; break;
} }
@ -520,20 +521,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) * 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 * @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); PasswordRepository.addRemote("origin", hostname, false);
GitOperation op; GitOperation op;
if (operation == REQUEST_PULL) { switch (operation) {
op = new PullOperation(localDir, activity).setCommand(); case REQUEST_PULL:
} else if (operation == REQUEST_PUSH) { op = new PullOperation(localDir, activity).setCommand();
op = new PushOperation(localDir, activity).setCommand(); break;
} else { case REQUEST_PUSH:
Log.e(TAG, "Sync operation not recognized : " + operation); op = new PushOperation(localDir, activity).setCommand();
return; break;
case REQUEST_SYNC:
op = new SyncOperation(localDir, activity).setCommands();
break;
default:
Log.e(TAG, "Sync operation not recognized : " + operation);
return;
} }
try { try {

View file

@ -39,10 +39,9 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
@Override @Override
protected String doInBackground(GitCommand... cmd) { protected String doInBackground(GitCommand... cmd) {
int count = cmd.length; for (GitCommand aCmd : cmd) {
for (int i = 0; i < count; i++) {
try { try {
cmd[i].call(); aCmd.call();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return e.getMessage(); return e.getMessage();
@ -52,7 +51,8 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
} }
protected void onPostExecute(String result) { protected void onPostExecute(String result) {
this.dialog.dismiss(); if (this.dialog != null)
this.dialog.dismiss();
if (result == null) if (result == null)
result = "Unexpected error"; result = "Unexpected error";

View file

@ -6,7 +6,6 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.text.InputType; import android.text.InputType;
import android.util.Log;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; 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.git.config.SshConfigSessionFactory;
import com.zeapo.pwdstore.utils.PasswordRepository; import com.zeapo.pwdstore.utils.PasswordRepository;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.JschConfigSessionFactory;

View file

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

View file

@ -17,10 +17,12 @@
<!--<item android:id="@+id/menu_add_category"--> <!--<item android:id="@+id/menu_add_category"-->
<!--android:title="New category"/>--> <!--android:title="New category"/>-->
<item android:id="@+id/git_sync"
android:title="@string/git_sync"/>
<item android:id="@+id/git_pull" <item android:id="@+id/git_pull"
android:title="Pull from remote"/> android:title="@string/git_pull"/>
<item android:id="@+id/git_push" <item android:id="@+id/git_push"
android:title="Push to remote"/> android:title="@string/git_push"/>
<item android:id="@+id/refresh" <item android:id="@+id/refresh"
android:title="Refresh list" android:title="Refresh list"

View file

@ -121,5 +121,8 @@
<string name="dialog_negative">Ne... později</string> <string name="dialog_negative">Ne... později</string>
<string name="dialog_oops">Ajaj...</string> <string name="dialog_oops">Ajaj...</string>
<string name="dialog_cancel">Zrušit</string> <string name="dialog_cancel">Zrušit</string>
<string name="git_sync">Syncronize repository</string>
<string name="git_pull">Pull from remote</string>
<string name="git_push">Push to remote</string>
</resources> </resources>

View file

@ -122,5 +122,8 @@
<string name="dialog_negative">Nah... later</string> <string name="dialog_negative">Nah... later</string>
<string name="dialog_oops">Oops...</string> <string name="dialog_oops">Oops...</string>
<string name="dialog_cancel">Cancel</string> <string name="dialog_cancel">Cancel</string>
<string name="git_sync">Syncronize repository</string>
<string name="git_pull">Pull from remote</string>
<string name="git_push">Push to remote</string>
</resources> </resources>