Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 737d281

Browse files
authored
Handle jgit errors (#243)
* initial work on the git error handling * remove throws exception and handle the jsch one correctly * move the commit task into its own operation * get rid of the interface and rely on the abstract class GitOperation * add error message to the pull command * add error message to the push command * add error message to the sync operationˆ
1 parent fd9e958 commit 737d281

File tree

8 files changed

+190
-111
lines changed

8 files changed

+190
-111
lines changed

app/src/main/java/com/zeapo/pwdstore/PasswordStore.java

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
import com.zeapo.pwdstore.crypto.PgpHandler;
2929
import com.zeapo.pwdstore.git.GitActivity;
3030
import com.zeapo.pwdstore.git.GitAsyncTask;
31+
import com.zeapo.pwdstore.git.GitOperation;
3132
import com.zeapo.pwdstore.pwgen.PRNGFixes;
3233
import com.zeapo.pwdstore.utils.PasswordItem;
3334
import com.zeapo.pwdstore.utils.PasswordRecyclerAdapter;
3435
import com.zeapo.pwdstore.utils.PasswordRepository;
3536

3637
import org.apache.commons.io.FileUtils;
37-
import org.eclipse.jgit.api.CommitCommand;
3838
import org.eclipse.jgit.api.Git;
3939
import org.eclipse.jgit.lib.Repository;
4040

@@ -57,6 +57,7 @@ public class PasswordStore extends AppCompatActivity {
5757
private final static int HOME = 403;
5858

5959
private final static int REQUEST_EXTERNAL_STORAGE = 50;
60+
6061
@Override
6162
protected void onCreate(Bundle savedInstanceState) {
6263
settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
@@ -74,7 +75,7 @@ protected void onCreate(Bundle savedInstanceState) {
7475
}
7576

7677
@Override
77-
public void onResume(){
78+
public void onResume() {
7879
super.onResume();
7980
// do not attempt to checkLocalRepository() if no storage permission: immediate crash
8081
if (settings.getBoolean("git_external", false)) {
@@ -373,10 +374,9 @@ private void checkLocalRepository(File localDir) {
373374
}
374375

375376

376-
377377
@Override
378378
public void onBackPressed() {
379-
if ((null != plist) && plist.isNotEmpty()) {
379+
if ((null != plist) && plist.isNotEmpty()) {
380380
plist.popBack();
381381
} else {
382382
super.onBackPressed();
@@ -438,20 +438,12 @@ public void deletePasswords(final PasswordRecyclerAdapter adapter, final Set<Int
438438
.setPositiveButton(this.getResources().getString(R.string.dialog_yes), new DialogInterface.OnClickListener() {
439439
@Override
440440
public void onClick(DialogInterface dialogInterface, int i) {
441-
String path = item.getFile().getAbsolutePath();
442441
item.getFile().delete();
443442
adapter.remove(position);
444443
it.remove();
445444
adapter.updateSelectedItems(position, selectedItems);
446445

447-
setResult(RESULT_CANCELED);
448-
Repository repo = PasswordRepository.getRepository(PasswordRepository.getRepositoryDirectory(activity));
449-
Git git = new Git(repo);
450-
GitAsyncTask tasks = new GitAsyncTask(activity, false, true, CommitCommand.class);
451-
tasks.execute(
452-
git.rm().addFilepattern(path.replace(PasswordRepository.getWorkTree() + "/", "")),
453-
git.commit().setMessage("[ANDROID PwdStore] Remove " + item + " from store.")
454-
);
446+
commit("[ANDROID PwdStore] Remove " + item + " from store.");
455447
deletePasswords(adapter, selectedItems);
456448
}
457449
})
@@ -468,10 +460,10 @@ public void onClick(DialogInterface dialogInterface, int i) {
468460
public void movePasswords(ArrayList<PasswordItem> values) {
469461
Intent intent = new Intent(this, PgpHandler.class);
470462
ArrayList<String> fileLocations = new ArrayList<>();
471-
for (PasswordItem passwordItem : values){
463+
for (PasswordItem passwordItem : values) {
472464
fileLocations.add(passwordItem.getFile().getAbsolutePath());
473465
}
474-
intent.putExtra("Files",fileLocations);
466+
intent.putExtra("Files", fileLocations);
475467
intent.putExtra("Operation", "SELECTFOLDER");
476468
startActivityForResult(intent, PgpHandler.REQUEST_CODE_SELECT_FOLDER);
477469
}
@@ -480,7 +472,7 @@ public void movePasswords(ArrayList<PasswordItem> values) {
480472
* clears adapter's content and updates it with a fresh list of passwords from the root
481473
*/
482474
public void updateListAdapter() {
483-
if ((null != plist)) {
475+
if ((null != plist)) {
484476
plist.updateAdapter();
485477
}
486478
}
@@ -489,31 +481,36 @@ public void updateListAdapter() {
489481
* Updates the adapter with the current view of passwords
490482
*/
491483
public void refreshListAdapter() {
492-
if ((null != plist)) {
484+
if ((null != plist)) {
493485
plist.refreshAdapter();
494486
}
495487
}
496488

497489
public void filterListAdapter(String filter) {
498-
if ((null != plist)) {
490+
if ((null != plist)) {
499491
plist.filterAdapter(filter);
500492
}
501493
}
502494

503495
private File getCurrentDir() {
504-
if ((null != plist)) {
496+
if ((null != plist)) {
505497
return plist.getCurrentDir();
506498
}
507499
return PasswordRepository.getWorkTree();
508500
}
509501

510-
private void commit(String message) {
511-
Git git = new Git(PasswordRepository.getRepository(new File("")));
512-
GitAsyncTask tasks = new GitAsyncTask(this, false, false, CommitCommand.class);
513-
tasks.execute(
514-
git.add().addFilepattern("."),
515-
git.commit().setMessage(message)
516-
);
502+
private void commit(final String message) {
503+
new GitOperation(PasswordRepository.getRepositoryDirectory(activity), activity) {
504+
@Override
505+
public void execute() {
506+
Git git = new Git(this.repository);
507+
GitAsyncTask tasks = new GitAsyncTask(activity, false, true, this);
508+
tasks.execute(
509+
git.add().addFilepattern("."),
510+
git.commit().setMessage(message)
511+
);
512+
}
513+
};
517514
}
518515

519516
protected void onActivityResult(int requestCode, int resultCode,
@@ -573,31 +570,29 @@ protected void onActivityResult(int requestCode, int resultCode,
573570
startActivityForResult(intent, GitActivity.REQUEST_CLONE);
574571
break;
575572
case PgpHandler.REQUEST_CODE_SELECT_FOLDER:
576-
Log.d("Moving","Moving passwords to "+data.getStringExtra("SELECTED_FOLDER_PATH"));
573+
Log.d("Moving", "Moving passwords to " + data.getStringExtra("SELECTED_FOLDER_PATH"));
577574
Log.d("Moving", TextUtils.join(", ", data.getStringArrayListExtra("Files")));
578575
File target = new File(data.getStringExtra("SELECTED_FOLDER_PATH"));
579-
if (!target.isDirectory()){
580-
Log.e("Moving","Tried moving passwords to a non-existing folder.");
576+
if (!target.isDirectory()) {
577+
Log.e("Moving", "Tried moving passwords to a non-existing folder.");
581578
break;
582579
}
583580

584-
Repository repo = PasswordRepository.getRepository(PasswordRepository.getRepositoryDirectory(activity));
585-
Git git = new Git(repo);
586-
GitAsyncTask tasks = new GitAsyncTask(activity, false, true, CommitCommand.class);
587-
588-
for (String string : data.getStringArrayListExtra("Files")){
581+
for (String string : data.getStringArrayListExtra("Files")) {
589582
File source = new File(string);
590-
if (!source.exists()){
591-
Log.e("Moving","Tried moving something that appears non-existent.");
583+
if (!source.exists()) {
584+
Log.e("Moving", "Tried moving something that appears non-existent.");
592585
continue;
593586
}
594-
if (!source.renameTo(new File(target.getAbsolutePath()+"/"+source.getName()))){
595-
Log.e("Moving","Something went wrong while moving.");
596-
}else{
597-
tasks.execute(
598-
git.add().addFilepattern(source.getAbsolutePath().replace(PasswordRepository.getWorkTree() + "/", "")),
599-
git.commit().setMessage("[ANDROID PwdStore] Moved "+string.replace(PasswordRepository.getWorkTree() + "/", "")+" to "+target.getAbsolutePath().replace(PasswordRepository.getWorkTree() + "/","")+target.getAbsolutePath()+"/"+source.getName()+".")
600-
);
587+
if (!source.renameTo(new File(target.getAbsolutePath() + "/" + source.getName()))) {
588+
// TODO this should show a warning to the user
589+
Log.e("Moving", "Something went wrong while moving.");
590+
} else {
591+
commit("[ANDROID PwdStore] Moved "
592+
+ string.replace(PasswordRepository.getWorkTree() + "/", "")
593+
+ " to "
594+
+ target.getAbsolutePath().replace(PasswordRepository.getWorkTree() + "/", "")
595+
+ target.getAbsolutePath() + "/" + source.getName() + ".");
601596
}
602597
}
603598
updateListAdapter();

app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.zeapo.pwdstore.git;
22

33
import android.app.Activity;
4+
import android.app.AlertDialog;
5+
import android.content.DialogInterface;
46

7+
import com.zeapo.pwdstore.R;
8+
import com.zeapo.pwdstore.utils.PasswordRepository;
9+
10+
import org.apache.commons.io.FileUtils;
511
import org.eclipse.jgit.api.CloneCommand;
612
import org.eclipse.jgit.api.Git;
713

@@ -12,7 +18,8 @@ public class CloneOperation extends GitOperation {
1218

1319
/**
1420
* Creates a new clone operation
15-
* @param fileDir the git working tree directory
21+
*
22+
* @param fileDir the git working tree directory
1623
* @param callingActivity the calling activity
1724
*/
1825
public CloneOperation(File fileDir, Activity callingActivity) {
@@ -21,6 +28,7 @@ public CloneOperation(File fileDir, Activity callingActivity) {
2128

2229
/**
2330
* Sets the command using the repository uri
31+
*
2432
* @param uri the uri of the repository
2533
* @return the current object
2634
*/
@@ -34,6 +42,7 @@ public CloneOperation setCommand(String uri) {
3442

3543
/**
3644
* sets the authentication for user/pwd scheme
45+
*
3746
* @param username the username
3847
* @param password the password
3948
* @return the current object
@@ -46,6 +55,7 @@ public CloneOperation setAuthentication(String username, String password) {
4655

4756
/**
4857
* sets the authentication for the ssh-key scheme
58+
*
4959
* @param sshKey the ssh-key file
5060
* @param username the username
5161
* @param passphrase the passphrase
@@ -58,10 +68,31 @@ public CloneOperation setAuthentication(File sshKey, String username, String pas
5868
}
5969

6070
@Override
61-
public void execute() throws Exception {
71+
public void execute() {
6272
if (this.provider != null) {
6373
((CloneCommand) this.command).setCredentialsProvider(this.provider);
6474
}
65-
new GitAsyncTask(callingActivity, true, false, CloneCommand.class).execute(this.command);
75+
new GitAsyncTask(callingActivity, true, false, this).execute(this.command);
76+
}
77+
78+
@Override
79+
public void onTaskEnded(String result) {
80+
new AlertDialog.Builder(callingActivity).
81+
setTitle(callingActivity.getResources().getString(R.string.jgit_error_dialog_title)).
82+
setMessage("Error occured during the clone operation, "
83+
+ callingActivity.getResources().getString(R.string.jgit_error_dialog_text)
84+
+ result
85+
+ "\nPlease check the FAQ for possible reasons why this error might occur.").
86+
setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
87+
@Override
88+
public void onClick(DialogInterface dialogInterface, int i) {
89+
// if we were unable to finish the job
90+
try {
91+
FileUtils.deleteDirectory(PasswordRepository.getWorkTree());
92+
} catch (Exception e) {
93+
e.printStackTrace();
94+
}
95+
}
96+
}).show();
6697
}
6798
}

app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
import android.app.Activity;
44
import android.app.ProgressDialog;
5-
import android.content.DialogInterface;
65
import android.os.AsyncTask;
7-
import android.support.v7.app.AlertDialog;
86

97
import com.zeapo.pwdstore.PasswordStore;
108
import com.zeapo.pwdstore.R;
11-
import com.zeapo.pwdstore.utils.PasswordRepository;
129

13-
import org.apache.commons.io.FileUtils;
14-
import org.eclipse.jgit.api.CloneCommand;
1510
import org.eclipse.jgit.api.GitCommand;
1611

1712

@@ -20,9 +15,9 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
2015
private boolean finishOnEnd;
2116
private boolean refreshListOnEnd;
2217
private ProgressDialog dialog;
23-
private Class operation;
18+
private GitOperation operation;
2419

25-
public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, Class operation) {
20+
public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, GitOperation operation) {
2621
this.activity = activity;
2722
this.finishOnEnd = finishOnEnd;
2823
this.refreshListOnEnd = refreshListOnEnd;
@@ -63,26 +58,7 @@ protected void onPostExecute(String result) {
6358
result = "Unexpected error";
6459

6560
if (!result.isEmpty()) {
66-
new AlertDialog.Builder(activity).
67-
setTitle(activity.getResources().getString(R.string.jgit_error_dialog_title)).
68-
setMessage(activity.getResources().getString(R.string.jgit_error_dialog_text) + result).
69-
setPositiveButton(activity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
70-
@Override
71-
public void onClick(DialogInterface dialogInterface, int i) {
72-
if (operation.equals(CloneCommand.class)) {
73-
// if we were unable to finish the job
74-
try {
75-
FileUtils.deleteDirectory(PasswordRepository.getWorkTree());
76-
} catch (Exception e) {
77-
e.printStackTrace();
78-
}
79-
} else {
80-
activity.setResult(Activity.RESULT_CANCELED);
81-
activity.finish();
82-
}
83-
}
84-
}).show();
85-
61+
this.operation.onTaskEnded(result);
8662
} else {
8763
if (finishOnEnd) {
8864
this.activity.setResult(Activity.RESULT_OK);

0 commit comments

Comments
 (0)