Include repo config into CommitReceivedEvent

Some implementors of CommitValidationListener may need the repository
config (e.g. in order to use DiffFormatter). At the moment they need to
reopen the repository for this, but since callers have it already
available they can simply provide it.

This makes the objects that are available to implementors of
CommitValidationListener more consistent with the objects that are
available to implementors of MergeValidationListener, which already
gets the repository and hence its config. We do not want to provide the
Repository instance as it is intentionally write protected when batch
updates are done.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: Ie9439e4196c894ff261f98ba38a9db9d57e209d8
(cherry picked from commit c54963cffb)
This commit is contained in:
Edwin Kempin
2020-10-06 16:24:30 +02:00
committed by Luca Milanesio
parent dc7ad8f299
commit 34f8614cbc
5 changed files with 24 additions and 3 deletions

View File

@@ -546,6 +546,7 @@ public class ChangeInserter implements InsertChangeOp {
cmd,
projectState.getProject(),
change.getDest().branch(),
ctx.getRepoView().getConfig(),
ctx.getRevWalk().getObjectReader(),
commitId,
ctx.getIdentifiedUser())) {

View File

@@ -342,6 +342,7 @@ public class PatchSetInserter implements BatchUpdateOp {
.orElseThrow(illegalState(origNotes.getProjectName()))
.getProject(),
origNotes.getChange().getDest().branch(),
ctx.getRepoView().getConfig(),
ctx.getRevWalk().getObjectReader(),
commitId,
ctx.getIdentifiedUser())) {

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.events;
import com.google.gerrit.entities.Project;
import com.google.gerrit.server.IdentifiedUser;
import java.io.IOException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -28,6 +29,7 @@ public class CommitReceivedEvent extends RefEvent implements AutoCloseable {
public ReceiveCommand command;
public Project project;
public String refName;
public Config repoConfig;
public RevWalk revWalk;
public RevCommit commit;
public IdentifiedUser user;
@@ -40,6 +42,7 @@ public class CommitReceivedEvent extends RefEvent implements AutoCloseable {
ReceiveCommand command,
Project project,
String refName,
Config repoConfig,
ObjectReader reader,
ObjectId commitId,
IdentifiedUser user)
@@ -48,6 +51,7 @@ public class CommitReceivedEvent extends RefEvent implements AutoCloseable {
this.command = command;
this.project = project;
this.refName = refName;
this.repoConfig = repoConfig;
this.revWalk = new RevWalk(reader);
this.commit = revWalk.parseCommit(commitId);
this.user = user;

View File

@@ -37,7 +37,9 @@ import com.google.gerrit.server.ssh.SshInfo;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.io.IOException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.notes.NoteMap;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.ReceiveCommand;
@@ -94,6 +96,7 @@ public class BranchCommitValidator {
/**
* Validates a single commit. If the commit does not validate, the command is rejected.
*
* @param repository the repository
* @param objectReader the object reader to use.
* @param cmd the ReceiveCommand executing the push.
* @param commit the commit being validated.
@@ -102,6 +105,7 @@ public class BranchCommitValidator {
* @return The validation {@link Result}.
*/
Result validateCommit(
Repository repository,
ObjectReader objectReader,
ReceiveCommand cmd,
RevCommit commit,
@@ -109,12 +113,14 @@ public class BranchCommitValidator {
NoteMap rejectCommits,
@Nullable Change change)
throws IOException {
return validateCommit(objectReader, cmd, commit, isMerged, rejectCommits, change, false);
return validateCommit(
repository, objectReader, cmd, commit, isMerged, rejectCommits, change, false);
}
/**
* Validates a single commit. If the commit does not validate, the command is rejected.
*
* @param repository the repository
* @param objectReader the object reader to use.
* @param cmd the ReceiveCommand executing the push.
* @param commit the commit being validated.
@@ -124,6 +130,7 @@ public class BranchCommitValidator {
* @return The validation {@link Result}.
*/
Result validateCommit(
Repository repository,
ObjectReader objectReader,
ReceiveCommand cmd,
RevCommit commit,
@@ -135,7 +142,14 @@ public class BranchCommitValidator {
try (TraceTimer traceTimer = TraceContext.newTimer("BranchCommitValidator#validateCommit")) {
ImmutableList.Builder<CommitValidationMessage> messages = new ImmutableList.Builder<>();
try (CommitReceivedEvent receiveEvent =
new CommitReceivedEvent(cmd, project, branch.branch(), objectReader, commit, user)) {
new CommitReceivedEvent(
cmd,
project,
branch.branch(),
new Config(repository.getConfig()),
objectReader,
commit,
user)) {
CommitValidators validators;
if (isMerged) {
validators =

View File

@@ -2212,6 +2212,7 @@ class ReceiveCommits {
BranchCommitValidator.Result validationResult =
validator.validateCommit(
repo,
receivePack.getRevWalk().getObjectReader(),
magicBranch.cmd,
c,
@@ -3231,7 +3232,7 @@ class ReceiveCommits {
BranchCommitValidator.Result validationResult =
validator.validateCommit(
walk.getObjectReader(), cmd, c, false, rejectCommits, null, skipValidation);
repo, walk.getObjectReader(), cmd, c, false, rejectCommits, null, skipValidation);
messages.addAll(validationResult.messages());
if (!validationResult.isValid()) {
break;