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 c7ce825
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 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
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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<PushCommand> cmd) throws GitAPIException {
private void push(Consumer<PushCommand> cmd, RemoteRefUpdate.Status expected) throws GitAPIException {
PushCommand push = git.push().setCredentialsProvider(creds()).setRemote(cfg.remote);
cmd.accept(push);

Expand All @@ -132,8 +133,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<RemoteRefUpdate.Status> 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
Expand Down

0 comments on commit c7ce825

Please sign in to comment.