Add extension point to register JGit PreUploadHooks

Plugins may want to get notified before an upload pack is sent.
For example, a plugin may want to monitor repository usage.

Change-Id: Ie1e82da47dc3906ef3da87dfbd0b583ebeecbef1
This commit is contained in:
Hugo Arès 2014-06-17 14:22:03 -04:00
parent 64031c0d84
commit 572d542b3d
4 changed files with 26 additions and 1 deletions

View File

@ -433,6 +433,14 @@ notified when JGit successfully receives a pack. This may be useful
for those plugins which would like to monitor changes in Git
repositories.
[[pre-upload-hook]]
== Pre Upload-Pack Hooks
Plugins may register PreUploadHook instances in order to get
notified when JGit is about to upload a pack. This may be useful
for those plugins which would like to monitor usage in Git
repositories.
[[ssh]]
== SSH Commands

View File

@ -51,6 +51,8 @@ import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.PostReceiveHook;
import org.eclipse.jgit.transport.PostReceiveHookChain;
import org.eclipse.jgit.transport.PreUploadHook;
import org.eclipse.jgit.transport.PreUploadHookChain;
import org.eclipse.jgit.transport.ReceivePack;
import org.eclipse.jgit.transport.UploadPack;
import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
@ -188,10 +190,12 @@ public class GitOverHttpServlet extends GitServlet {
static class UploadFactory implements UploadPackFactory<HttpServletRequest> {
private final TransferConfig config;
private final DynamicSet<PreUploadHook> preUploadHooks;
@Inject
UploadFactory(TransferConfig tc) {
UploadFactory(TransferConfig tc, DynamicSet<PreUploadHook> preUploadHooks) {
this.config = tc;
this.preUploadHooks = preUploadHooks;
}
@Override
@ -199,6 +203,8 @@ public class GitOverHttpServlet extends GitServlet {
UploadPack up = new UploadPack(repo);
up.setPackConfig(config.getPackConfig());
up.setTimeout(config.getTimeout());
up.setPreUploadHook(PreUploadHookChain.newChain(
Lists.newArrayList(preUploadHooks)));
return up;
}
}

View File

@ -131,6 +131,7 @@ import com.google.inject.internal.UniqueAnnotations;
import org.apache.velocity.runtime.RuntimeInstance;
import org.eclipse.jgit.transport.PostReceiveHook;
import org.eclipse.jgit.transport.PreUploadHook;
import java.util.List;
import java.util.Set;
@ -252,6 +253,7 @@ public class GerritGlobalModule extends FactoryModule {
DynamicSet.setOf(binder(), GitReferenceUpdatedListener.class);
DynamicSet.setOf(binder(), ReceivePackInitializer.class);
DynamicSet.setOf(binder(), PostReceiveHook.class);
DynamicSet.setOf(binder(), PreUploadHook.class);
DynamicSet.setOf(binder(), NewProjectCreatedListener.class);
DynamicSet.setOf(binder(), ProjectDeletedListener.class);
DynamicSet.setOf(binder(), HeadUpdatedListener.class);

View File

@ -14,6 +14,8 @@
package com.google.gerrit.sshd.commands;
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.git.ChangeCache;
import com.google.gerrit.server.git.TagCache;
@ -23,6 +25,8 @@ import com.google.gerrit.sshd.AbstractGitCommand;
import com.google.inject.Inject;
import com.google.inject.Provider;
import org.eclipse.jgit.transport.PreUploadHook;
import org.eclipse.jgit.transport.PreUploadHookChain;
import org.eclipse.jgit.transport.UploadPack;
import java.io.IOException;
@ -41,6 +45,9 @@ final class Upload extends AbstractGitCommand {
@Inject
private ChangeCache changeCache;
@Inject
private DynamicSet<PreUploadHook> preUploadHooks;
@Override
protected void runImpl() throws IOException, Failure {
if (!projectControl.canRunUploadPack()) {
@ -54,6 +61,8 @@ final class Upload extends AbstractGitCommand {
}
up.setPackConfig(config.getPackConfig());
up.setTimeout(config.getTimeout());
up.setPreUploadHook(PreUploadHookChain.newChain(
Lists.newArrayList(preUploadHooks)));
up.upload(in, out, err);
}
}