Add extension point to register JGit PostReceiveHooks

Plugins may want to get notified when a pack file is received.
For example, the quota plugin may want to cache the repository
size in the file system and update it every time a new pack is
received.

Change-Id: I0212eab6e4867819ebe7ecc7fc5532c26bb82282
This commit is contained in:
Saša Živkov
2014-02-24 17:15:08 +01:00
parent 779ede73cd
commit 626c731633
4 changed files with 27 additions and 1 deletions

View File

@@ -424,6 +424,14 @@ by Gerrit just before a ReceivePack instance will be used. Usually,
plugins will make use of the setXXX methods on the ReceivePack to
set additional properties on it.
[[post-receive-hook]]
== Post Receive-Pack Hooks
Plugins may register PostReceiveHook instances in order to get
notified when JGit successfully receives a pack. This may be useful
for those plugins which would like to monitor changes in Git
repositories.
[[ssh]]
== SSH Commands

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.httpd;
import com.google.common.cache.Cache;
import com.google.common.collect.Lists;
import com.google.gerrit.common.data.Capable;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.reviewdb.client.Project;
@@ -48,6 +49,8 @@ import org.eclipse.jgit.http.server.ServletUtils;
import org.eclipse.jgit.http.server.resolver.AsIsFileService;
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.ReceivePack;
import org.eclipse.jgit.transport.UploadPack;
import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
@@ -247,13 +250,16 @@ public class GitOverHttpServlet extends GitServlet {
private final AsyncReceiveCommits.Factory factory;
private final TransferConfig config;
private DynamicSet<ReceivePackInitializer> receivePackInitializers;
private DynamicSet<PostReceiveHook> postReceiveHooks;
@Inject
ReceiveFactory(AsyncReceiveCommits.Factory factory, TransferConfig config,
DynamicSet<ReceivePackInitializer> receivePackInitializers) {
DynamicSet<ReceivePackInitializer> receivePackInitializers,
DynamicSet<PostReceiveHook> postReceiveHooks) {
this.factory = factory;
this.config = config;
this.receivePackInitializers = receivePackInitializers;
this.postReceiveHooks = postReceiveHooks;
}
@Override
@@ -273,6 +279,8 @@ public class GitOverHttpServlet extends GitServlet {
rp.setTimeout(config.getTimeout());
rp.setMaxObjectSizeLimit(config.getMaxObjectSizeLimit());
init(pc.getProject().getNameKey(), rp);
rp.setPostReceiveHook(PostReceiveHookChain.newChain(
Lists.newArrayList(postReceiveHooks)));
req.setAttribute(ATT_RC, rc);
return rp;
}

View File

@@ -126,6 +126,7 @@ import com.google.inject.TypeLiteral;
import com.google.inject.internal.UniqueAnnotations;
import org.apache.velocity.runtime.RuntimeInstance;
import org.eclipse.jgit.transport.PostReceiveHook;
import java.util.List;
import java.util.Set;
@@ -245,6 +246,7 @@ public class GerritGlobalModule extends FactoryModule {
DynamicMap.mapOf(binder(), CapabilityDefinition.class);
DynamicSet.setOf(binder(), GitReferenceUpdatedListener.class);
DynamicSet.setOf(binder(), ReceivePackInitializer.class);
DynamicSet.setOf(binder(), PostReceiveHook.class);
DynamicSet.setOf(binder(), NewProjectCreatedListener.class);
DynamicSet.setOf(binder(), ProjectDeletedListener.class);
DynamicSet.setOf(binder(), HeadUpdatedListener.class);

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.sshd.commands;
import com.google.common.collect.Lists;
import com.google.gerrit.common.data.Capable;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.reviewdb.client.Account;
@@ -32,6 +33,8 @@ import org.eclipse.jgit.errors.UnpackException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.transport.AdvertiseRefsHook;
import org.eclipse.jgit.transport.PostReceiveHook;
import org.eclipse.jgit.transport.PostReceiveHookChain;
import org.eclipse.jgit.transport.ReceivePack;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
@@ -64,6 +67,9 @@ final class Receive extends AbstractGitCommand {
@Inject
private DynamicSet<ReceivePackInitializer> receivePackInitializers;
@Inject
private DynamicSet<PostReceiveHook> postReceiveHooks;
private final Set<Account.Id> reviewerId = new HashSet<Account.Id>();
private final Set<Account.Id> ccId = new HashSet<Account.Id>();
@@ -103,6 +109,8 @@ final class Receive extends AbstractGitCommand {
rp.setMaxObjectSizeLimit(config.getEffectiveMaxObjectSizeLimit(
projectControl.getProjectState()));
init(rp);
rp.setPostReceiveHook(PostReceiveHookChain.newChain(
Lists.newArrayList(postReceiveHooks)));
try {
rp.receive(in, out, err);
} catch (UnpackException badStream) {