Merge "ReceiveCommits: Use streams"

This commit is contained in:
David Pursehouse 2016-09-20 15:27:24 +00:00 committed by Gerrit Code Review
commit 4c93a559b6

View File

@ -21,6 +21,9 @@ import static com.google.gerrit.reviewdb.client.RefNames.REFS_CHANGES;
import static com.google.gerrit.server.change.HashtagsUtil.cleanupHashtag; import static com.google.gerrit.server.change.HashtagsUtil.cleanupHashtag;
import static com.google.gerrit.server.git.MultiProgressMonitor.UNKNOWN; import static com.google.gerrit.server.git.MultiProgressMonitor.UNKNOWN;
import static com.google.gerrit.server.mail.MailUtil.getRecipientsFromFooters; import static com.google.gerrit.server.mail.MailUtil.getRecipientsFromFooters;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.eclipse.jgit.lib.Constants.R_HEADS; import static org.eclipse.jgit.lib.Constants.R_HEADS;
import static org.eclipse.jgit.lib.RefDatabase.ALL; import static org.eclipse.jgit.lib.RefDatabase.ALL;
import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED; import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
@ -30,15 +33,11 @@ import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_NONFASTF
import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON; import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap; import com.google.common.collect.BiMap;
import com.google.common.collect.Collections2;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -48,7 +47,6 @@ import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap; import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.SetMultimap; import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.collect.SortedSetMultimap; import com.google.common.collect.SortedSetMultimap;
@ -681,14 +679,9 @@ public class ReceiveCommits {
} }
private void reportMessages() { private void reportMessages() {
Iterable<CreateRequest> created = List<CreateRequest> created =
Iterables.filter(newChanges, new Predicate<CreateRequest>() { newChanges.stream().filter(r -> r.change != null).collect(toList());
@Override if (!created.isEmpty()) {
public boolean apply(CreateRequest input) {
return input.change != null;
}
});
if (!Iterables.isEmpty(created)) {
addMessage(""); addMessage("");
addMessage("New Changes:"); addMessage("New Changes:");
for (CreateRequest c : created) { for (CreateRequest c : created) {
@ -699,21 +692,10 @@ public class ReceiveCommits {
addMessage(""); addMessage("");
} }
List<ReplaceRequest> updated = FluentIterable List<ReplaceRequest> updated = replaceByChange.values().stream()
.from(replaceByChange.values()) .filter(r -> !r.skip && r.inputCommand.getResult() == OK)
.filter(new Predicate<ReplaceRequest>() { .sorted(comparingInt(r -> r.notes.getChangeId().get()))
@Override .collect(toList());
public boolean apply(ReplaceRequest input) {
return !input.skip && input.inputCommand.getResult() == OK;
}
})
.toSortedList(Ordering.natural().onResultOf(
new Function<ReplaceRequest, Integer>() {
@Override
public Integer apply(ReplaceRequest in) {
return in.notes.getChangeId().get();
}
}));
if (!updated.isEmpty()) { if (!updated.isEmpty()) {
addMessage(""); addMessage("");
addMessage("Updated Changes:"); addMessage("Updated Changes:");
@ -831,7 +813,7 @@ public class ReceiveCommits {
// One or more new references failed to create. Assume the // One or more new references failed to create. Assume the
// system isn't working correctly anymore and abort. // system isn't working correctly anymore and abort.
reject(magicBranch.cmd, "Unable to create changes: " reject(magicBranch.cmd, "Unable to create changes: "
+ Joiner.on(' ').join(lastCreateChangeErrors)); + lastCreateChangeErrors.stream().collect(joining(" ")));
logError(String.format( logError(String.format(
"Only %d of %d new change refs created in %s; aborting", "Only %d of %d new change refs created in %s; aborting",
okToInsert, replaceCount + newChanges.size(), project.getName())); okToInsert, replaceCount + newChanges.size(), project.getName()));
@ -1056,11 +1038,12 @@ public class ReceiveCommits {
.getPluginConfig(e.getPluginName()) .getPluginConfig(e.getPluginName())
.getString(e.getExportName()); .getString(e.getExportName());
if (configEntry.getType() == ProjectConfigEntryType.ARRAY) { if (configEntry.getType() == ProjectConfigEntryType.ARRAY) {
List<String> l = oldValue =
Arrays.asList(projectControl.getProjectState() Arrays.stream(
.getConfig().getPluginConfig(e.getPluginName()) projectControl.getProjectState()
.getStringList(e.getExportName())); .getConfig().getPluginConfig(e.getPluginName())
oldValue = Joiner.on("\n").join(l); .getStringList(e.getExportName()))
.collect(joining("\n"));
} }
if ((value == null ? oldValue != null : !value.equals(oldValue)) && if ((value == null ? oldValue != null : !value.equals(oldValue)) &&
@ -1796,14 +1779,10 @@ public class ReceiveCommits {
List<ChangeData> changes = p.destChanges; List<ChangeData> changes = p.destChanges;
if (changes.size() > 1) { if (changes.size() > 1) {
logDebug("Multiple changes in project with Change-Id {}: {}", logDebug("Multiple changes in project with Change-Id {}: {}",
p.changeKey, Lists.transform( p.changeKey,
changes, changes.stream()
new Function<ChangeData, String>() { .map(cd -> cd.getId().toString())
@Override .collect(joining()));
public String apply(ChangeData in) {
return in.getId().toString();
}
}));
// WTF, multiple changes in this project have the same key? // WTF, multiple changes in this project have the same key?
// Since the commit is new, the user should recreate it with // Since the commit is new, the user should recreate it with
// a different Change-Id. In practice, we should never see // a different Change-Id. In practice, we should never see
@ -2188,14 +2167,8 @@ public class ReceiveCommits {
Collection<ChangeNotes> allNotes = Collection<ChangeNotes> allNotes =
notesFactory.create( notesFactory.create(
db, db,
Collections2.transform( replaceByChange.values().stream()
replaceByChange.values(), .map(r -> r.ontoChange).collect(toList()));
new Function<ReplaceRequest, Change.Id>() {
@Override
public Change.Id apply(ReplaceRequest in) {
return in.ontoChange;
}
}));
for (ChangeNotes notes : allNotes) { for (ChangeNotes notes : allNotes) {
replaceByChange.get(notes.getChangeId()).notes = notes; replaceByChange.get(notes.getChangeId()).notes = notes;
} }