| | |
| | | import java.io.OutputStreamWriter;
|
| | | import java.text.MessageFormat;
|
| | | import java.util.Collection;
|
| | | import java.util.Enumeration;
|
| | | import java.util.LinkedHashSet;
|
| | | import java.util.Set;
|
| | |
|
| | | import javax.servlet.ServletConfig;
|
| | | import javax.servlet.ServletContext;
|
| | | import javax.servlet.ServletException;
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | |
| | |
|
| | | import com.gitblit.models.RepositoryModel;
|
| | | import com.gitblit.models.UserModel;
|
| | | import com.gitblit.utils.ClientLogger;
|
| | | import com.gitblit.utils.FileUtils;
|
| | | import com.gitblit.utils.HttpUtils;
|
| | | import com.gitblit.utils.StringUtils;
|
| | |
|
| | |
| | |
|
| | | private File groovyDir;
|
| | |
|
| | | /**
|
| | | * Configure the servlet from Gitblit's configuration.
|
| | | */
|
| | | @Override
|
| | | public String getInitParameter(String name) {
|
| | | if (name.equals("base-path")) {
|
| | | return GitBlit.getRepositoriesFolder().getAbsolutePath();
|
| | | } else if (name.equals("export-all")) {
|
| | | return "1";
|
| | | }
|
| | | return super.getInitParameter(name);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void init(ServletConfig config) throws ServletException {
|
| | | groovyDir = GitBlit.getGroovyScriptsFolder(); |
| | | groovyDir = GitBlit.getGroovyScriptsFolder();
|
| | | try {
|
| | | gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
|
| | | // set Grape root
|
| | | File grapeRoot = new File(GitBlit.getString(Keys.groovy.grapeFolder, "groovy/grape")).getAbsoluteFile();
|
| | | grapeRoot.mkdirs();
|
| | | System.setProperty("grape.root", grapeRoot.getAbsolutePath());
|
| | | |
| | | gse = new GroovyScriptEngine(groovyDir.getAbsolutePath()); |
| | | } catch (IOException e) {
|
| | | throw new ServletException("Failed to instantiate Groovy Script Engine!", e);
|
| | | }
|
| | |
| | | return rp;
|
| | | }
|
| | | });
|
| | | super.init(config);
|
| | | super.init(new GitblitServletConfig(config));
|
| | | }
|
| | |
|
| | | /**
|
| | | * Transitional wrapper class to configure the JGit 1.2 GitFilter. This
|
| | | * GitServlet will probably be replaced by a GitFilter so that Gitblit can
|
| | | * serve Git repositories on the root URL and not a /git sub-url.
|
| | | * |
| | | * @author James Moger
|
| | | * |
| | | */
|
| | | private class GitblitServletConfig implements ServletConfig {
|
| | | final ServletConfig config;
|
| | |
|
| | | GitblitServletConfig(ServletConfig config) {
|
| | | this.config = config;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public String getServletName() {
|
| | | return config.getServletName();
|
| | | }
|
| | |
|
| | | @Override
|
| | | public ServletContext getServletContext() {
|
| | | return config.getServletContext();
|
| | | }
|
| | |
|
| | | @Override
|
| | | public String getInitParameter(String name) {
|
| | | if (name.equals("base-path")) {
|
| | | return GitBlit.getRepositoriesFolder().getAbsolutePath();
|
| | | } else if (name.equals("export-all")) {
|
| | | return "1";
|
| | | }
|
| | | return config.getInitParameter(name);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Enumeration<String> getInitParameterNames() {
|
| | | return config.getInitParameterNames();
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | */
|
| | | @Override
|
| | | public void onPreReceive(ReceivePack rp, Collection<ReceiveCommand> commands) {
|
| | | Set<String> scripts = new LinkedHashSet<String>();
|
| | | scripts.addAll(GitBlit.getStrings(Keys.groovy.preReceiveScripts));
|
| | | RepositoryModel repository = getRepositoryModel(rp);
|
| | | Set<String> scripts = new LinkedHashSet<String>();
|
| | | scripts.addAll(GitBlit.self().getPreReceiveScriptsInherited(repository));
|
| | | scripts.addAll(repository.preReceiveScripts);
|
| | | UserModel user = getUserModel(rp);
|
| | | runGroovy(repository, user, commands, scripts);
|
| | | runGroovy(repository, user, commands, rp, scripts);
|
| | | for (ReceiveCommand cmd : commands) {
|
| | | if (!Result.NOT_ATTEMPTED.equals(cmd.getResult())) {
|
| | | logger.warn(MessageFormat.format("{0} {1} because \"{2}\"", cmd.getNewId()
|
| | |
| | | logger.info("skipping post-receive hooks, no refs created, updated, or removed");
|
| | | return;
|
| | | }
|
| | | Set<String> scripts = new LinkedHashSet<String>();
|
| | | scripts.addAll(GitBlit.getStrings(Keys.groovy.postReceiveScripts));
|
| | | RepositoryModel repository = getRepositoryModel(rp);
|
| | | Set<String> scripts = new LinkedHashSet<String>();
|
| | | scripts.addAll(GitBlit.self().getPostReceiveScriptsInherited(repository));
|
| | | scripts.addAll(repository.postReceiveScripts);
|
| | | UserModel user = getUserModel(rp);
|
| | | runGroovy(repository, user, commands, scripts);
|
| | | runGroovy(repository, user, commands, rp, scripts);
|
| | |
|
| | | // Experimental
|
| | | // runNativeScript(rp, "hooks/post-receive", commands);
|
| | |
| | | */
|
| | | protected RepositoryModel getRepositoryModel(ReceivePack rp) {
|
| | | Repository repository = rp.getRepository();
|
| | | String rootPath = GitBlit.getRepositoriesFolder().getAbsolutePath();
|
| | | String repositoryName = repository.getDirectory().getAbsolutePath();
|
| | | repositoryName = repositoryName.substring(rootPath.length() + 1);
|
| | | String repositoryName = FileUtils.getRelativePath(GitBlit.getRepositoriesFolder(), repository.getDirectory());
|
| | | RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
|
| | | return model;
|
| | | }
|
| | |
| | | * @param scripts
|
| | | */
|
| | | protected void runGroovy(RepositoryModel repository, UserModel user,
|
| | | Collection<ReceiveCommand> commands, Set<String> scripts) {
|
| | | Collection<ReceiveCommand> commands, ReceivePack rp, Set<String> scripts) {
|
| | | if (scripts == null || scripts.size() == 0) {
|
| | | // no Groovy scripts to execute
|
| | | return;
|
| | |
| | | binding.setVariable("commands", commands);
|
| | | binding.setVariable("url", gitblitUrl);
|
| | | binding.setVariable("logger", logger);
|
| | | binding.setVariable("clientLogger", new ClientLogger(rp));
|
| | | for (String script : scripts) {
|
| | | if (StringUtils.isEmpty(script)) {
|
| | | continue;
|