mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
serve mercurial repositories with hgweb cgi
This commit is contained in:
@@ -17,6 +17,13 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>servlet-api</artifactId>
|
||||||
|
<version>${servlet.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>sonia.scm</groupId>
|
<groupId>sonia.scm</groupId>
|
||||||
<artifactId>scm-web-api</artifactId>
|
<artifactId>scm-web-api</artifactId>
|
||||||
|
|||||||
@@ -15,9 +15,14 @@ import com.google.inject.Singleton;
|
|||||||
import sonia.scm.repository.HgConfig;
|
import sonia.scm.repository.HgConfig;
|
||||||
import sonia.scm.repository.HgRepositoryHandler;
|
import sonia.scm.repository.HgRepositoryHandler;
|
||||||
import sonia.scm.repository.RepositoryManager;
|
import sonia.scm.repository.RepositoryManager;
|
||||||
|
import sonia.scm.web.HgWebConfigWriter;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
@@ -79,16 +84,23 @@ public class HgConfigResource
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param uriInfo
|
* @param uriInfo
|
||||||
|
* @param servletContext
|
||||||
* @param config
|
* @param config
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@POST
|
@POST
|
||||||
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
|
||||||
public Response setConfig(@Context UriInfo uriInfo, HgConfig config)
|
public Response setConfig(@Context UriInfo uriInfo,
|
||||||
|
@Context ServletContext servletContext,
|
||||||
|
HgConfig config)
|
||||||
|
throws IOException
|
||||||
{
|
{
|
||||||
handler.setConfig(config);
|
handler.setConfig(config);
|
||||||
handler.storeConfig();
|
handler.storeConfig();
|
||||||
|
new HgWebConfigWriter(config).write(servletContext);
|
||||||
|
|
||||||
return Response.created(uriInfo.getRequestUri()).build();
|
return Response.created(uriInfo.getRequestUri()).build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ public class HgConfig extends BasicRepositoryConfig
|
|||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -41,6 +40,17 @@ public class HgConfig extends BasicRepositoryConfig
|
|||||||
return hgBinary;
|
return hgBinary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getPythonBinary()
|
||||||
|
{
|
||||||
|
return pythonBinary;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -65,6 +75,17 @@ public class HgConfig extends BasicRepositoryConfig
|
|||||||
this.hgBinary = hgBinary;
|
this.hgBinary = hgBinary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param pythonBinary
|
||||||
|
*/
|
||||||
|
public void setPythonBinary(String pythonBinary)
|
||||||
|
{
|
||||||
|
this.pythonBinary = pythonBinary;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -78,10 +99,12 @@ public class HgConfig extends BasicRepositoryConfig
|
|||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private String hgBinary = "hg";
|
private String hgBinary = "hg";
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private String pythonBinary = "python";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private File repositoryDirectory;
|
private File repositoryDirectory;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package sonia.scm.web;
|
||||||
|
|
||||||
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import com.google.inject.Scopes;
|
||||||
|
import com.google.inject.servlet.ServletModule;
|
||||||
|
|
||||||
|
import org.apache.catalina.servlets.CGIServlet;
|
||||||
|
|
||||||
|
import sonia.scm.web.filter.BasicAuthenticationFilter;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sebastian Sdorra
|
||||||
|
*/
|
||||||
|
public class HgServletModule extends ServletModule
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void configureServlets()
|
||||||
|
{
|
||||||
|
filter("/hg/*").through(BasicAuthenticationFilter.class);
|
||||||
|
|
||||||
|
Map<String, String> initParams = new HashMap<String, String>();
|
||||||
|
|
||||||
|
initParams.put("cgiPathPrefix", "WEB-INF/cgi/hgweb.cgi");
|
||||||
|
bind(CGIServlet.class).in(Scopes.SINGLETON);
|
||||||
|
serve("/hg/*").with(CGIServlet.class, initParams);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
/*
|
||||||
|
* To change this template, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package sonia.scm.web;
|
||||||
|
|
||||||
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import sonia.scm.io.INIConfiguration;
|
||||||
|
import sonia.scm.io.INIConfigurationWriter;
|
||||||
|
import sonia.scm.io.INISection;
|
||||||
|
import sonia.scm.io.RegexResourceProcessor;
|
||||||
|
import sonia.scm.io.ResourceProcessor;
|
||||||
|
import sonia.scm.repository.HgConfig;
|
||||||
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sebastian Sdorra
|
||||||
|
*/
|
||||||
|
public class HgWebConfigWriter
|
||||||
|
{
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String CGI_PATH = "WEB-INF/cgi/hgweb.cgi";
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String CGI_TEMPLATE = "/sonia/scm/hgweb.cgi";
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String CONFIG_NAME = "hgweb.config";
|
||||||
|
|
||||||
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs ...
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param config
|
||||||
|
*/
|
||||||
|
public HgWebConfigWriter(HgConfig config)
|
||||||
|
{
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param context
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void write(ServletContext context) throws IOException
|
||||||
|
{
|
||||||
|
File webConfigFile = new File(config.getRepositoryDirectory(), CONFIG_NAME);
|
||||||
|
|
||||||
|
writeWebConfigFile(webConfigFile);
|
||||||
|
|
||||||
|
String path = context.getRealPath(CGI_PATH);
|
||||||
|
File cgiFile = new File(path);
|
||||||
|
File cgiDirectory = cgiFile.getParentFile();
|
||||||
|
|
||||||
|
if (!cgiDirectory.exists() &&!cgiDirectory.mkdirs())
|
||||||
|
{
|
||||||
|
throw new IOException(
|
||||||
|
"could not create directory: ".concat(cgiDirectory.getPath()));
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println( cgiFile );
|
||||||
|
|
||||||
|
writeCGIFile(cgiFile, webConfigFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param cgiFile
|
||||||
|
* @param webConfigFile
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private void writeCGIFile(File cgiFile, File webConfigFile) throws IOException
|
||||||
|
{
|
||||||
|
InputStream input = null;
|
||||||
|
OutputStream output = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
input = HgWebConfigWriter.class.getResourceAsStream(CGI_TEMPLATE);
|
||||||
|
output = new FileOutputStream(cgiFile);
|
||||||
|
|
||||||
|
ResourceProcessor rp = new RegexResourceProcessor();
|
||||||
|
|
||||||
|
rp.addVariable("python", config.getPythonBinary());
|
||||||
|
rp.addVariable("config", webConfigFile.getAbsolutePath());
|
||||||
|
rp.process(input, output);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Util.close(input);
|
||||||
|
Util.close(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param webConfigFile
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private void writeWebConfigFile(File webConfigFile) throws IOException
|
||||||
|
{
|
||||||
|
INIConfiguration webConfig = new INIConfiguration();
|
||||||
|
INISection pathsSection = new INISection("paths");
|
||||||
|
String path = config.getRepositoryDirectory().getAbsolutePath();
|
||||||
|
|
||||||
|
if (!path.endsWith(File.separator))
|
||||||
|
{
|
||||||
|
path = path.concat(File.separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
pathsSection.setParameter("/", path.concat("*"));
|
||||||
|
webConfig.addSection(pathsSection);
|
||||||
|
new INIConfigurationWriter().write(webConfig, webConfigFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private HgConfig config;
|
||||||
|
}
|
||||||
@@ -7,10 +7,6 @@
|
|||||||
|
|
||||||
package sonia.scm.web;
|
package sonia.scm.web;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -46,5 +42,6 @@ public class HgWebPlugin implements ScmWebPlugin
|
|||||||
public void contextInitialized(ScmWebPluginContext context)
|
public void contextInitialized(ScmWebPluginContext context)
|
||||||
{
|
{
|
||||||
context.addScriptResource(new ClasspathWebResource(SCRIPT));
|
context.addScriptResource(new ClasspathWebResource(SCRIPT));
|
||||||
|
context.addInjectModule(new HgServletModule());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ registerConfigPanel({
|
|||||||
fieldLabel : 'HG Binary',
|
fieldLabel : 'HG Binary',
|
||||||
name : 'hgBinary',
|
name : 'hgBinary',
|
||||||
allowBlank : false
|
allowBlank : false
|
||||||
|
},{
|
||||||
|
xtype : 'textfield',
|
||||||
|
fieldLabel : 'Python Binary',
|
||||||
|
name : 'pythonBinary',
|
||||||
|
allowBlank : false
|
||||||
},{
|
},{
|
||||||
xtype: 'textfield',
|
xtype: 'textfield',
|
||||||
name: 'repositoryDirectory',
|
name: 'repositoryDirectory',
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env ${python}
|
||||||
|
config = "${config}"
|
||||||
|
from mercurial import demandimport; demandimport.enable()
|
||||||
|
from mercurial.hgweb import hgweb, wsgicgi
|
||||||
|
application = hgweb(config)
|
||||||
|
wsgicgi.launch(application)
|
||||||
Reference in New Issue
Block a user