Skip to content

Commit

Permalink
fix(GitUtil): push失败显示错误信息
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzbw committed Mar 8, 2018
1 parent 3dec809 commit 4c80e2a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -41,6 +41,7 @@ target/
*.iml
*.ipr
out/
classes/

### NetBeans ###
nbproject/private/
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/zbw/gitpic/controller/MainController.java
Expand Up @@ -170,14 +170,17 @@ protected void chooseUploadImg() {
@FXML
protected void commitAndPush() {
commitButton.setDisable(true);
try {
GitUtils.commitAll(repository);
} catch (TipException e) {
showErrorMessage(e.getMessage());
commitButton.setDisable(false);
return;
}
showNormalMessage("push到github中...");
ThreadPool.getInstance().execute(() -> {
try {
showNormalMessage("commit中...");
GitUtils.commitAll(repository);
showSuccessMessage("commit成功!");
} catch (TipException e) {
showErrorMessage(e.getMessage());
commitButton.setDisable(false);
return;
}
});
pushGit();
}

Expand All @@ -193,7 +196,6 @@ protected void dialogAccept() {
String password = this.passwordTextField.getText();
Preference.getInstance().saveGitUsername(username);
Preference.getInstance().saveGitPassword(password);
showNormalMessage("push到github中...");
pushGit();
dialog.close();
}
Expand Down Expand Up @@ -304,6 +306,7 @@ private void generateGitRawPath() {
*/
private void pushGit() {
ThreadPool.getInstance().execute(() -> {
showNormalMessage("push到github中...");
try {
String uri = GitUtils.getRemoteUri(repository);
if (Constants.GIT_SSH.equals(GitUtils.authType(uri))) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/zbw/gitpic/utils/Constants.java
Expand Up @@ -15,4 +15,5 @@ public interface Constants {
String GIT_DEFAULT_COMMIT_MESSAGE = "update";
String GIT_SSH = "git@";
String GIT_HTTPS = "https://";
String GIT_MASTER_HEAD = "refs/heads/master";
}
44 changes: 40 additions & 4 deletions src/main/java/com/zbw/gitpic/utils/GitUtils.java
Expand Up @@ -7,9 +7,7 @@
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.transport.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -135,7 +133,45 @@ public static void commitAll(Repository repository, String commitMsg) {
public static void push(Repository repository) {
Git git = new Git(repository);
try {
git.push().call();
Iterable<PushResult> results = git.push().call();
PushResult result = results.iterator().next();
String msg = "未知原因";
if (null == result) {
throw new TipException(("push失败: " + msg));
}
RemoteRefUpdate.Status status = result.getRemoteUpdate(Constants.GIT_MASTER_HEAD).getStatus();
switch (status) {
case OK:
return;
case NOT_ATTEMPTED:
msg = "Push process hasn't yet attempted to update this ref. This is the default status, prior to push process execution.";
break;
case UP_TO_DATE:
msg = "Remote ref was up to date, there was no need to update anything.";
break;
case REJECTED_NONFASTFORWARD:
msg = "Remote ref update was rejected, as it would cause non fast-forward update.";
break;
case REJECTED_NODELETE:
msg = "Remote ref update was rejected, because remote side doesn't support/allow deleting refs.";
break;
case REJECTED_REMOTE_CHANGED:
msg = "Remote ref update was rejected, because old object id on remote repository wasn't the same as defined expected old object.";
break;
case REJECTED_OTHER_REASON:
msg = "Remote ref update was rejected for other reason";
break;
case NON_EXISTING:
msg = "Remote ref didn't exist. Can occur on delete request of a non existing ref.";
break;
case AWAITING_REPORT:
msg = "Push process is awaiting update report from remote repository. This is a temporary state or state after critical error in push process.";
break;
default:
msg = "未知原因";
break;
}
throw new TipException("push失败: " + msg);
} catch (GitAPIException e) {
e.printStackTrace();
logger.error(e.getMessage());
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/fxml/main.fxml
Expand Up @@ -81,7 +81,8 @@
</HBox>
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="600.0" spacing="10.0">
<children>
<Label fx:id="promptLabel" styleClass="text-dark"/>
<Label fx:id="promptLabel" prefWidth="300.0" styleClass="text-dark" wrapText="true"
alignment="CENTER"/>
</children>
</HBox>
</children>
Expand Down

0 comments on commit 4c80e2a

Please sign in to comment.