From c7ce82590f63bf2d4a605b264815141bd70bc90b Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Tue, 16 Jun 2020 17:19:30 -0700 Subject: [PATCH] Fix #11 by asserting the git result that we expect. --- CHANGELOG.md | 4 ++- CONTRIBUTING.md | 4 +-- .../spotless/changelog/GitActions.java | 27 ++++++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea56664..2cfc4be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed -- Bug in `PoolString.concat(String)`. +- Better error message for cases where the `spotlessChangelog` block is too low. ([#6](https://github.com/diffplug/spotless-changelog/issues/6)) +- Bug in `PoolString.concat(String)` ([1f6da65](https://github.com/diffplug/spotless-changelog/commit/1f6da65b51c5ee7af847dc0e427fe685fbd3d43c)). +- No longer accepts git failures silently (they were always printed, but did not properly kill the build). ([#11](https://github.com/diffplug/spotless-changelog/issues/11)) ## [1.1.0] - 2020-01-13 ### Added diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4f791f1..097c608 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -42,12 +42,12 @@ We publish tagged releases to mavenCentral, jcenter, and the gradle plugin porta ## License -By contributing your code, you agree to license your contribution under the terms of the APLv2: https://github.com/diffplug/blowdryer/blob/master/LICENSE +By contributing your code, you agree to license your contribution under the terms of the APLv2: https://github.com/diffplug/spotless-changelog/blob/main/LICENSE All files are released with the Apache 2.0 license as such: ``` -Copyright 2019 DiffPlug +Copyright (C) 2019-2020 DiffPlug Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/spotless-changelog-lib/src/main/java/com/diffplug/spotless/changelog/GitActions.java b/spotless-changelog-lib/src/main/java/com/diffplug/spotless/changelog/GitActions.java index 0a2dfea..1d7b9d2 100644 --- a/spotless-changelog-lib/src/main/java/com/diffplug/spotless/changelog/GitActions.java +++ b/spotless-changelog-lib/src/main/java/com/diffplug/spotless/changelog/GitActions.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.function.Consumer; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PushCommand; @@ -66,7 +67,7 @@ public void checkCanPush() throws GitAPIException, IOException { if (!ref.getObjectId().equals(remoteRef.getObjectId())) { throw new IllegalStateException("Local branch " + cfg.branch + " is out of sync with " + cfg.remote + ", so we can't safely push it automatically."); } - push(cfg.branch); + push(cfg.branch, RemoteRefUpdate.Status.UP_TO_DATE); } catch (GitAPIException e) { throw new IllegalArgumentException("You can set user/pass with any of these environment variables: " + envVars(), e); } @@ -95,8 +96,8 @@ public void addAndCommit() throws GitAPIException { /** Tags and pushes the tag and the branch. */ public void tagBranchPush() throws GitAPIException { Ref tagRef = git.tag().setName(tagName()).setAnnotated(false).call(); - push(tagRef); - push(cfg.branch); + push(tagRef, RemoteRefUpdate.Status.OK); + push(cfg.branch, RemoteRefUpdate.Status.OK); } private String tagName() { @@ -108,15 +109,15 @@ public void close() { repository.close(); } - private void push(String branch) throws GitAPIException { - push(cmd -> cmd.add(branch)); + private void push(String branch, RemoteRefUpdate.Status expected) throws GitAPIException { + push(cmd -> cmd.add(branch), expected); } - private void push(Ref ref) throws GitAPIException { - push(cmd -> cmd.add(ref)); + private void push(Ref ref, RemoteRefUpdate.Status expected) throws GitAPIException { + push(cmd -> cmd.add(ref), expected); } - private void push(Consumer cmd) throws GitAPIException { + private void push(Consumer cmd, RemoteRefUpdate.Status expected) throws GitAPIException { PushCommand push = git.push().setCredentialsProvider(creds()).setRemote(cfg.remote); cmd.accept(push); @@ -132,8 +133,14 @@ private void push(Consumer cmd) throws GitAPIException { + "..." + (update.getNewObjectId() != null ? update.getNewObjectId().name() : "(null)") + (update.isFastForward() ? " fastForward" : "") - + (update.getMessage() != null ? " " + update.getMessage() : "")); - + + (update.getMessage() != null ? update.getMessage() : "")); + Optional failure = result.getRemoteUpdates().stream() + .map(RemoteRefUpdate::getStatus) + .filter(r -> !expected.equals(r)) + .findAny(); + if (failure.isPresent()) { + throw new IllegalStateException("Error! Expected " + expected + ", got " + failure.get() + "."); + } } // similar to https://github.com/ajoberstar/grgit/blob/5766317fbe67ec39faa4632e2b80c2b056f5c124/grgit-core/src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy