Search in sources :

Example 1 with GitCommand

use of org.eclipse.jgit.api.GitCommand in project Android-Password-Store by zeapo.

the class GitAsyncTask method doInBackground.

@Override
protected String doInBackground(GitCommand... commands) {
    Integer nbChanges = null;
    for (GitCommand command : commands) {
        Log.d("doInBackground", "Executing the command <" + command.toString() + ">");
        try {
            if (command instanceof StatusCommand) {
                // in case we have changes, we want to keep track of it
                org.eclipse.jgit.api.Status status = ((StatusCommand) command).call();
                nbChanges = status.getChanged().size() + status.getMissing().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 if (command instanceof PushCommand) {
                for (final PushResult result : ((PushCommand) command).call()) {
                    // Code imported (modified) from Gerrit PushOp, license Apache v2
                    for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
                        switch(rru.getStatus()) {
                            case REJECTED_NONFASTFORWARD:
                                return activity.getString(R.string.git_push_nff_error);
                            case REJECTED_NODELETE:
                            case REJECTED_REMOTE_CHANGED:
                            case NON_EXISTING:
                            case NOT_ATTEMPTED:
                                return activity.getString(R.string.git_push_generic_error) + rru.getStatus().name();
                            case REJECTED_OTHER_REASON:
                                if ("non-fast-forward".equals(rru.getMessage())) {
                                    return activity.getString(R.string.git_push_other_error);
                                } else {
                                    return activity.getString(R.string.git_push_generic_error) + rru.getMessage();
                                }
                            default:
                                break;
                        }
                    }
                }
            } else {
                command.call();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage() + "\nCaused by:\n" + e.getCause();
        }
    }
    return "";
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) CommitCommand(org.eclipse.jgit.api.CommitCommand) PushResult(org.eclipse.jgit.transport.PushResult) StatusCommand(org.eclipse.jgit.api.StatusCommand) PushCommand(org.eclipse.jgit.api.PushCommand) GitCommand(org.eclipse.jgit.api.GitCommand)

Aggregations

CommitCommand (org.eclipse.jgit.api.CommitCommand)1 GitCommand (org.eclipse.jgit.api.GitCommand)1 PushCommand (org.eclipse.jgit.api.PushCommand)1 StatusCommand (org.eclipse.jgit.api.StatusCommand)1 PushResult (org.eclipse.jgit.transport.PushResult)1 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)1