diff --git a/scm-core/src/main/java/sonia/scm/io/SimpleCommand.java b/scm-core/src/main/java/sonia/scm/io/SimpleCommand.java index 92943f7788..55d6386ed0 100644 --- a/scm-core/src/main/java/sonia/scm/io/SimpleCommand.java +++ b/scm-core/src/main/java/sonia/scm/io/SimpleCommand.java @@ -47,6 +47,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Map; + /** * * @author Sebastian Sdorra @@ -68,6 +70,20 @@ public class SimpleCommand implements Command */ public SimpleCommand(String... command) { + this(null, command); + } + + /** + * Constructs ... + * + * + * @param environment + * @param command + * @since 1.8 + */ + public SimpleCommand(Map environment, String... command) + { + this.environment = environment; this.command = command; } @@ -91,6 +107,18 @@ public class SimpleCommand implements Command //~--- set methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param environment + * @since 1.8 + */ + public void setEnvironment(Map environment) + { + this.environment = environment; + } + /** * Method description * @@ -134,6 +162,11 @@ public class SimpleCommand implements Command processBuilder = processBuilder.directory(workDirectory); } + if (environment != null) + { + processBuilder.environment().putAll(environment); + } + return processBuilder.redirectErrorStream(true).start(); } @@ -208,6 +241,9 @@ public class SimpleCommand implements Command /** Field description */ private String[] command; + /** Field description */ + private Map environment; + /** Field description */ private File workDirectory; } diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java index b28ba48184..587fd19594 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgChangesetViewer.java @@ -52,7 +52,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.xml.parsers.ParserConfigurationException; @@ -82,6 +84,9 @@ public class HgChangesetViewer implements ChangesetViewer + "\""; //J+ + /** Field description */ + public static final String ENV_PENDING = "HG_PENDING"; + /** Field description */ public static final String TEMPLATE_TOTAL = "{rev}"; @@ -179,12 +184,14 @@ public class HgChangesetViewer implements ChangesetViewer * * @param startRev * @param endRev + * @param pending * * @return * * @throws IOException */ - public List getChangesets(String startRev, String endRev) + public List getChangesets(String startRev, String endRev, + boolean pending) throws IOException { List changesetList = null; @@ -192,10 +199,19 @@ public class HgChangesetViewer implements ChangesetViewer try { - Command command = new SimpleCommand(handler.getConfig().getHgBinary(), - "-R", repositoryPath, "log", "-r", - startRev + ":" + endRev, "--template", - TEMPLATE_CHANGESETS); + SimpleCommand command = + new SimpleCommand(handler.getConfig().getHgBinary(), "-R", + repositoryPath, "log", "-r", startRev + ":" + endRev, + "--template", TEMPLATE_CHANGESETS); + + if (pending) + { + Map env = new HashMap(); + + env.put(ENV_PENDING, repositoryPath); + command.setEnvironment(env); + } + CommandResult result = command.execute(); if (result.isSuccessfull()) @@ -229,6 +245,23 @@ public class HgChangesetViewer implements ChangesetViewer return changesetList; } + /** + * Method description + * + * + * @param startRev + * @param endRev + * + * @return + * + * @throws IOException + */ + public List getChangesets(String startRev, String endRev) + throws IOException + { + return getChangesets(startRev, endRev, false); + } + /** * Method description * diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHookEvent.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHookEvent.java index 3f503c54bc..77d1d721f8 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHookEvent.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHookEvent.java @@ -69,13 +69,16 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent * @param handler * @param repositoryName * @param startRev + * @param type */ public HgRepositoryHookEvent(HgRepositoryHandler handler, - String repositoryName, String startRev) + String repositoryName, String startRev, + RepositoryHookType type) { this.handler = handler; this.repositoryName = repositoryName; this.startRev = startRev; + this.type = type; } //~--- get methods ---------------------------------------------------------- @@ -93,7 +96,8 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent { try { - changesets = createChangesetViewer().getChangesets(startRev, REV_TIP); + changesets = createChangesetViewer().getChangesets(startRev, REV_TIP, + true); } catch (IOException ex) { @@ -113,7 +117,7 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent @Override public RepositoryHookType getType() { - return RepositoryHookType.POST_RECEIVE; + return type; } //~--- methods -------------------------------------------------------------- @@ -146,4 +150,7 @@ public class HgRepositoryHookEvent extends AbstractRepositoryHookEvent /** Field description */ private String startRev; + + /** Field description */ + private RepositoryHookType type; } diff --git a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgHookCallbackServlet.java b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgHookCallbackServlet.java index c7d59f4611..01c69fd8dc 100644 --- a/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgHookCallbackServlet.java +++ b/scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgHookCallbackServlet.java @@ -44,6 +44,7 @@ import org.slf4j.LoggerFactory; import sonia.scm.repository.HgHookManager; import sonia.scm.repository.HgRepositoryHandler; import sonia.scm.repository.HgRepositoryHookEvent; +import sonia.scm.repository.RepositoryHookType; import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.util.HttpUtil; @@ -69,6 +70,12 @@ import javax.servlet.http.HttpServletResponse; public class HgHookCallbackServlet extends HttpServlet { + /** Field description */ + public static final String HGHOOK_POST_RECEIVE = "changegroup"; + + /** Field description */ + public static final String HGHOOK_PRE_RECEIVE = "pretxnchangegroup"; + /** Field description */ private static final String PARAM_CHALLENGE = "challenge"; @@ -164,39 +171,80 @@ public class HgHookCallbackServlet extends HttpServlet * * @param response * @param repositoryName + * @param node * @param type + * + * @throws IOException + */ + private void fireHook(HttpServletResponse response, String repositoryName, + String node, RepositoryHookType type) + throws IOException + { + try + { + repositoryManager.fireHookEvent(HgRepositoryHandler.TYPE_NAME, + repositoryName, + new HgRepositoryHookEvent(handler, + repositoryName, node, type)); + } + catch (RepositoryNotFoundException ex) + { + if (logger.isErrorEnabled()) + { + logger.error("could not find repository {}", repositoryName); + + if (logger.isTraceEnabled()) + { + logger.trace("repository not found", ex); + } + } + + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } + + /** + * Method description + * + * + * @param response + * @param repositoryName + * @param typeName * @param challenge * @param node * * @throws IOException */ private void hookCallback(HttpServletResponse response, - String repositoryName, String type, + String repositoryName, String typeName, String challenge, String node) throws IOException { if (hookManager.isAcceptAble(challenge)) { - try - { - repositoryManager.fireHookEvent(HgRepositoryHandler.TYPE_NAME, - repositoryName, - new HgRepositoryHookEvent(handler, - repositoryName, node)); - } - catch (RepositoryNotFoundException ex) - { - if (logger.isErrorEnabled()) - { - logger.error("could not find repository {}", repositoryName); + RepositoryHookType type = null; - if (logger.isTraceEnabled()) - { - logger.trace("repository not found", ex); - } + if (HGHOOK_PRE_RECEIVE.equals(typeName)) + { + type = RepositoryHookType.PRE_RECEIVE; + } + else if (HGHOOK_POST_RECEIVE.equals(typeName)) + { + type = RepositoryHookType.POST_RECEIVE; + } + + if (type != null) + { + fireHook(response, repositoryName, node, type); + } + else + { + if (logger.isWarnEnabled()) + { + logger.warn("unknown hook type {}", typeName); } - response.sendError(HttpServletResponse.SC_NOT_FOUND); + response.sendError(HttpServletResponse.SC_BAD_REQUEST); } } else