Skip to content

Commit

Permalink
Fix #11 by asserting the git result that we expect.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jun 17, 2020
1 parent 107f7d6 commit 5ae734c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@
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;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.TrackingRefUpdate;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

/** API for doing the commit, tag, and push operations. See {@link GitCfg#withChangelog(File, ChangelogAndNext)}. */
Expand Down Expand Up @@ -66,7 +69,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, RefUpdate.Result.NO_CHANGE);
} catch (GitAPIException e) {
throw new IllegalArgumentException("You can set user/pass with any of these environment variables: " + envVars(), e);
}
Expand Down Expand Up @@ -95,8 +98,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, RefUpdate.Result.NEW);
push(cfg.branch, RefUpdate.Result.FAST_FORWARD);
}

private String tagName() {
Expand All @@ -108,15 +111,15 @@ public void close() {
repository.close();
}

private void push(String branch) throws GitAPIException {
push(cmd -> cmd.add(branch));
private void push(String branch, RefUpdate.Result 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, RefUpdate.Result expected) throws GitAPIException {
push(cmd -> cmd.add(ref), expected);
}

private void push(Consumer<PushCommand> cmd) throws GitAPIException {
private void push(Consumer<PushCommand> cmd, RefUpdate.Result expected) throws GitAPIException {
PushCommand push = git.push().setCredentialsProvider(creds()).setRemote(cfg.remote);
cmd.accept(push);

Expand All @@ -132,8 +135,14 @@ private void push(Consumer<PushCommand> cmd) throws GitAPIException {
+ "..."
+ (update.getNewObjectId() != null ? update.getNewObjectId().name() : "(null)")
+ (update.isFastForward() ? " fastForward" : "")
+ (update.getMessage() != null ? " " + update.getMessage() : ""));

+ (update.getMessage() != null ? update.getMessage() : ""));
Optional<RefUpdate.Result> failure = result.getTrackingRefUpdates().stream()
.map(TrackingRefUpdate::getResult)
.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
Expand Down

0 comments on commit 5ae734c

Please sign in to comment.