fix git rm and sync

fixes #276
fixes #283
This commit is contained in:
Mohamed Zenadi 2017-03-27 22:47:33 +02:00
parent d888e5e2f2
commit 824845bf41
5 changed files with 38 additions and 22 deletions

View file

@ -403,9 +403,9 @@ public class PasswordStore extends AppCompatActivity {
// Adds shortcut // Adds shortcut
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, item.getFullPathName()) ShortcutInfo shortcut = new ShortcutInfo.Builder(this, item.getFullPathToParent())
.setShortLabel(item.toString()) .setShortLabel(item.toString())
.setLongLabel(item.getFullPathName() + item.toString()) .setLongLabel(item.getFullPathToParent() + item.toString())
.setIcon(Icon.createWithResource(this, R.drawable.ic_launcher)) .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher))
.setIntent(intent.setAction("DECRYPT_PASS")) // Needs action .setIntent(intent.setAction("DECRYPT_PASS")) // Needs action
.build(); .build();
@ -476,7 +476,7 @@ public class PasswordStore extends AppCompatActivity {
it.remove(); it.remove();
adapter.updateSelectedItems(position, selectedItems); adapter.updateSelectedItems(position, selectedItems);
commit("[ANDROID PwdStore] Remove " + item + " from store."); commitAdd("[ANDROID PwdStore] Remove " + item + " from store.");
deletePasswords(adapter, selectedItems); deletePasswords(adapter, selectedItems);
} }
}) })
@ -532,7 +532,7 @@ public class PasswordStore extends AppCompatActivity {
return PasswordRepository.getRepositoryDirectory(getApplicationContext()); return PasswordRepository.getRepositoryDirectory(getApplicationContext());
} }
private void commit(final String message) { private void commitAdd(final String message) {
new GitOperation(PasswordRepository.getRepositoryDirectory(activity), activity) { new GitOperation(PasswordRepository.getRepositoryDirectory(activity), activity) {
@Override @Override
public void execute() { public void execute() {
@ -540,7 +540,7 @@ public class PasswordStore extends AppCompatActivity {
Git git = new Git(this.repository); Git git = new Git(this.repository);
GitAsyncTask tasks = new GitAsyncTask(activity, false, true, this); GitAsyncTask tasks = new GitAsyncTask(activity, false, true, this);
tasks.execute( tasks.execute(
git.add().addFilepattern("."), git.add().setUpdate(true).addFilepattern("."),
git.commit().setMessage(message) git.commit().setMessage(message)
); );
} }
@ -556,18 +556,18 @@ public class PasswordStore extends AppCompatActivity {
settings.edit().putBoolean("repository_initialized", true).apply(); settings.edit().putBoolean("repository_initialized", true).apply();
break; break;
case PgpHandler.REQUEST_CODE_DECRYPT_AND_VERIFY: case PgpHandler.REQUEST_CODE_DECRYPT_AND_VERIFY:
// if went from decrypt->edit and user saved changes, we need to commit // if went from decrypt->edit and user saved changes, we need to commitAdd
if (data.getBooleanExtra("needCommit", false)) { if (data.getBooleanExtra("needCommit", false)) {
commit(this.getResources().getString(R.string.edit_commit_text) + data.getExtras().getString("NAME")); commitAdd(this.getResources().getString(R.string.edit_commit_text) + data.getExtras().getString("NAME"));
refreshListAdapter(); refreshListAdapter();
} }
break; break;
case PgpHandler.REQUEST_CODE_ENCRYPT: case PgpHandler.REQUEST_CODE_ENCRYPT:
commit(this.getResources().getString(R.string.add_commit_text) + data.getExtras().getString("NAME") + this.getResources().getString(R.string.from_store)); commitAdd(this.getResources().getString(R.string.add_commit_text) + data.getExtras().getString("NAME") + this.getResources().getString(R.string.from_store));
refreshListAdapter(); refreshListAdapter();
break; break;
case PgpHandler.REQUEST_CODE_EDIT: case PgpHandler.REQUEST_CODE_EDIT:
commit(this.getResources().getString(R.string.edit_commit_text) + data.getExtras().getString("NAME")); commitAdd(this.getResources().getString(R.string.edit_commit_text) + data.getExtras().getString("NAME"));
refreshListAdapter(); refreshListAdapter();
break; break;
case GitActivity.REQUEST_INIT: case GitActivity.REQUEST_INIT:
@ -622,7 +622,7 @@ public class PasswordStore extends AppCompatActivity {
// TODO this should show a warning to the user // TODO this should show a warning to the user
Log.e("Moving", "Something went wrong while moving."); Log.e("Moving", "Something went wrong while moving.");
} else { } else {
commit("[ANDROID PwdStore] Moved " commitAdd("[ANDROID PwdStore] Moved "
+ string.replace(PasswordRepository.getRepositoryDirectory(getApplicationContext()) + "/", "") + string.replace(PasswordRepository.getRepositoryDirectory(getApplicationContext()) + "/", "")
+ " to " + " to "
+ target.getAbsolutePath().replace(PasswordRepository.getRepositoryDirectory(getApplicationContext()) + "/", "") + target.getAbsolutePath().replace(PasswordRepository.getRepositoryDirectory(getApplicationContext()) + "/", "")

View file

@ -7,7 +7,9 @@ import android.os.AsyncTask;
import com.zeapo.pwdstore.PasswordStore; import com.zeapo.pwdstore.PasswordStore;
import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.R;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.StatusCommand;
public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
@ -33,10 +35,21 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
} }
@Override @Override
protected String doInBackground(GitCommand... cmd) { protected String doInBackground(GitCommand... commands) {
for (GitCommand aCmd : cmd) { Integer nbChanges = null;
for (GitCommand command : commands) {
try { try {
aCmd.call(); if (command instanceof StatusCommand) {
// in case we have changes, we want to keep track of it
nbChanges = ((StatusCommand) command).call().getChanged().size();
} else if (command instanceof CommitCommand) {
// the previous status will eventually be used to avoid a commit
if (nbChanges == null || nbChanges > 0)
command.call();
} else {
command.call();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return e.getMessage() + "\nCaused by:\n" + e.getCause(); return e.getMessage() + "\nCaused by:\n" + e.getCause();
@ -49,8 +62,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
if (this.dialog != null) if (this.dialog != null)
try { try {
this.dialog.dismiss(); this.dialog.dismiss();
} catch (Exception e) } catch (Exception e) {
{
// ignore // ignore
} }

View file

@ -11,11 +11,13 @@ import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PullCommand; import org.eclipse.jgit.api.PullCommand;
import org.eclipse.jgit.api.PushCommand; import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.StatusCommand;
import java.io.File; import java.io.File;
public class SyncOperation extends GitOperation { public class SyncOperation extends GitOperation {
protected AddCommand addCommand; protected AddCommand addCommand;
protected StatusCommand statusCommand;
protected CommitCommand commitCommand; protected CommitCommand commitCommand;
protected PullCommand pullCommand; protected PullCommand pullCommand;
protected PushCommand pushCommand; protected PushCommand pushCommand;
@ -32,11 +34,13 @@ public class SyncOperation extends GitOperation {
/** /**
* Sets the command * Sets the command
*
* @return the current object * @return the current object
*/ */
public SyncOperation setCommands() { public SyncOperation setCommands() {
Git git = new Git(repository); Git git = new Git(repository);
this.addCommand = git.add().addFilepattern("."); this.addCommand = git.add().setUpdate(true).addFilepattern(".");
this.statusCommand = git.status();
this.commitCommand = git.commit().setMessage("[Android Password Store] Sync"); this.commitCommand = git.commit().setMessage("[Android Password Store] Sync");
this.pullCommand = git.pull().setRebase(true).setRemote("origin"); this.pullCommand = git.pull().setRebase(true).setRemote("origin");
this.pushCommand = git.push().setPushAll().setRemote("origin"); this.pushCommand = git.push().setPushAll().setRemote("origin");
@ -49,7 +53,7 @@ public class SyncOperation extends GitOperation {
this.pullCommand.setCredentialsProvider(this.provider); this.pullCommand.setCredentialsProvider(this.provider);
this.pushCommand.setCredentialsProvider(this.provider); this.pushCommand.setCredentialsProvider(this.provider);
} }
new GitAsyncTask(callingActivity, true, false, this).execute(this.addCommand, this.commitCommand, this.pullCommand, this.pushCommand); new GitAsyncTask(callingActivity, true, false, this).execute(this.addCommand, this.statusCommand, this.commitCommand, this.pullCommand, this.pushCommand);
} }
@Override @Override

View file

@ -105,7 +105,7 @@ public abstract class EntryRecyclerAdapter extends RecyclerView.Adapter<EntryRec
holder.name.setText(pass.toString()); holder.name.setText(pass.toString());
} }
holder.type.setText(pass.getFullPathName()); holder.type.setText(pass.getFullPathToParent());
if (pass.getType() == PasswordItem.TYPE_CATEGORY) { if (pass.getType() == PasswordItem.TYPE_CATEGORY) {
// holder.card.setCardBackgroundColor(activity.getResources().getColor(R.color.blue_grey_200)); // holder.card.setCardBackgroundColor(activity.getResources().getColor(R.color.blue_grey_200));
} else { } else {

View file

@ -11,7 +11,7 @@ public class PasswordItem implements Comparable{
private String name; private String name;
private PasswordItem parent; private PasswordItem parent;
private File file; private File file;
private String fullPathName; private String fullPathToParent;
public boolean selected = false; public boolean selected = false;
/** Create a password item /** Create a password item
@ -26,7 +26,7 @@ public class PasswordItem implements Comparable{
this.parent = parent; this.parent = parent;
this.type = type; this.type = type;
this.file = file; this.file = file;
this.fullPathName = file.getAbsolutePath().replace(rootDir.getAbsolutePath(), "").replace(file.getName(), ""); this.fullPathToParent = file.getAbsolutePath().replace(rootDir.getAbsolutePath(), "").replace(file.getName(), "");
} }
/** Create a new Category item /** Create a new Category item
@ -83,8 +83,8 @@ public class PasswordItem implements Comparable{
return this.file; return this.file;
} }
public String getFullPathName() { public String getFullPathToParent() {
return this.fullPathName; return this.fullPathToParent;
} }
@Override @Override