Set GERRIT_SITE in Gerrit Hooks as env. variable.
Allows development of hooks parametrised on Gerrit location.
This can be useful to allow hooks to load Gerrit configuration
when needed (from $GERRIT_SITE) or even store their additional config
files under $GERRIT_SITE/etc and retrieve them at startup.
Change-Id: I642c73ab2a5efd875e17f384a0870893b8bbd3fe
Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
diff --git a/gerrit-server/src/main/java/com/google/gerrit/common/ChangeHookRunner.java b/gerrit-server/src/main/java/com/google/gerrit/common/ChangeHookRunner.java
index 09c33c0..727207f 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/common/ChangeHookRunner.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/common/ChangeHookRunner.java
@@ -30,6 +30,7 @@
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.config.AnonymousCowardName;
import com.google.gerrit.server.config.GerritServerConfig;
+import com.google.gerrit.server.config.SitePath;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.events.ApprovalAttribute;
import com.google.gerrit.server.events.ChangeAbandonedEvent;
@@ -133,6 +134,8 @@
private final EventFactory eventFactory;
+ private final SitePaths sitePaths;
+
/**
* Create a new ChangeHookRunner.
*
@@ -149,7 +152,7 @@
final @AnonymousCowardName String anonymousCowardName,
final SitePaths sitePath, final ProjectCache projectCache,
final AccountCache accountCache, final ApprovalTypes approvalTypes,
- final EventFactory eventFactory) {
+ final EventFactory eventFactory, final SitePaths sitePaths) {
this.anonymousCowardName = anonymousCowardName;
this.repoManager = repoManager;
this.hookQueue = queue.createQueue(1, "hook");
@@ -157,6 +160,7 @@
this.accountCache = accountCache;
this.approvalTypes = approvalTypes;
this.eventFactory = eventFactory;
+ this.sitePaths = sitePath;
final File hooksPath = sitePath.resolve(getValue(config, "hooks", "path", sitePath.hooks_dir.getAbsolutePath()));
@@ -481,10 +485,12 @@
repo = openRepository(project);
}
+ final Map<String, String> env = pb.environment();
+ env.put("GERRIT_SITE", sitePaths.site_path.getAbsolutePath());
+
if (repo != null) {
pb.directory(repo.getDirectory());
- final Map<String, String> env = pb.environment();
env.put("GIT_DIR", repo.getDirectory().getAbsolutePath());
}