| | |
| | | import groovy.lang.Binding;
|
| | | import groovy.util.GroovyScriptEngine;
|
| | |
|
| | | import java.io.BufferedReader;
|
| | | import java.io.BufferedWriter;
|
| | | import java.io.File;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStreamReader;
|
| | | import java.io.OutputStreamWriter;
|
| | | import java.text.MessageFormat;
|
| | | import java.util.Collection;
|
| | | import java.util.Enumeration;
|
| | | import java.util.LinkedHashSet;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | |
|
| | | import javax.servlet.ServletConfig;
|
| | |
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | | import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory;
|
| | | import org.eclipse.jgit.http.server.resolver.DefaultUploadPackFactory;
|
| | | import org.eclipse.jgit.lib.PersonIdent;
|
| | | import org.eclipse.jgit.lib.Ref;
|
| | | import org.eclipse.jgit.lib.Repository;
|
| | | import org.eclipse.jgit.revwalk.RevCommit;
|
| | | import org.eclipse.jgit.transport.PostReceiveHook;
|
| | |
| | | import org.eclipse.jgit.transport.ReceiveCommand;
|
| | | import org.eclipse.jgit.transport.ReceiveCommand.Result;
|
| | | import org.eclipse.jgit.transport.ReceivePack;
|
| | | import org.eclipse.jgit.transport.RefFilter;
|
| | | import org.eclipse.jgit.transport.UploadPack;
|
| | | import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
|
| | | import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
|
| | | import org.slf4j.Logger;
|
| | |
| | | import com.gitblit.models.UserModel;
|
| | | import com.gitblit.utils.ClientLogger;
|
| | | import com.gitblit.utils.HttpUtils;
|
| | | import com.gitblit.utils.IssueUtils;
|
| | | import com.gitblit.utils.JGitUtils;
|
| | | import com.gitblit.utils.PushLogUtils;
|
| | | import com.gitblit.utils.StringUtils;
|
| | |
|
| | | /**
|
| | |
| | | rp.setAllowDeletes(user.canDeleteRef(repository));
|
| | | rp.setAllowNonFastForwards(user.canRewindRef(repository));
|
| | |
|
| | | if (repository.isFrozen) {
|
| | | throw new ServiceNotEnabledException();
|
| | | }
|
| | | |
| | | return rp;
|
| | | }
|
| | | });
|
| | | |
| | | // override the default upload pack to exclude gitblit refs
|
| | | setUploadPackFactory(new DefaultUploadPackFactory() {
|
| | | @Override
|
| | | public UploadPack create(final HttpServletRequest req, final Repository db)
|
| | | throws ServiceNotEnabledException, ServiceNotAuthorizedException {
|
| | | UploadPack up = super.create(req, db);
|
| | | RefFilter refFilter = new RefFilter() {
|
| | | @Override
|
| | | public Map<String, Ref> filter(Map<String, Ref> refs) {
|
| | | // admin accounts can access all refs |
| | | UserModel user = GitBlit.self().authenticate(req);
|
| | | if (user == null) {
|
| | | user = UserModel.ANONYMOUS;
|
| | | }
|
| | | if (user.canAdmin()) {
|
| | | return refs;
|
| | | }
|
| | |
|
| | | // normal users can not clone gitblit refs
|
| | | refs.remove(IssueUtils.GB_ISSUES);
|
| | | refs.remove(PushLogUtils.GB_PUSHES);
|
| | | return refs;
|
| | | }
|
| | | };
|
| | | up.setRefFilter(refFilter);
|
| | | return up;
|
| | | }
|
| | | });
|
| | | super.init(new GitblitServletConfig(config));
|
| | |
| | | .getName(), cmd.getResult(), cmd.getMessage()));
|
| | | }
|
| | | }
|
| | |
|
| | | // Experimental
|
| | | // runNativeScript(rp, "hooks/pre-receive", commands);
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | logger.info("skipping post-receive hooks, no refs created, updated, or removed");
|
| | | return;
|
| | | }
|
| | | RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName);
|
| | | Set<String> scripts = new LinkedHashSet<String>();
|
| | | scripts.addAll(GitBlit.self().getPostReceiveScriptsInherited(repository));
|
| | | scripts.addAll(repository.postReceiveScripts);
|
| | |
|
| | | UserModel user = getUserModel(rp);
|
| | | runGroovy(repository, user, commands, rp, scripts);
|
| | | RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName);
|
| | |
|
| | | // log ref changes
|
| | | for (ReceiveCommand cmd : commands) {
|
| | | if (Result.OK.equals(cmd.getResult())) {
|
| | | // add some logging for important ref changes
|
| | |
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | // update push log
|
| | | PushLogUtils.updatePushLog(user, rp.getRepository(), commands);
|
| | | logger.info(MessageFormat.format("{0} push log updated", repository.name));
|
| | |
|
| | | // Experimental
|
| | | // runNativeScript(rp, "hooks/post-receive", commands);
|
| | | // run Groovy hook scripts |
| | | Set<String> scripts = new LinkedHashSet<String>();
|
| | | scripts.addAll(GitBlit.self().getPostReceiveScriptsInherited(repository));
|
| | | scripts.addAll(repository.postReceiveScripts);
|
| | | runGroovy(repository, user, commands, rp, scripts);
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | } catch (Exception e) {
|
| | | logger.error(
|
| | | MessageFormat.format("Failed to execute Groovy script {0}", script), e);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * Runs the native push hook script.
|
| | | * |
| | | * http://book.git-scm.com/5_git_hooks.html
|
| | | * http://longair.net/blog/2011/04/09/missing-git-hooks-documentation/
|
| | | * |
| | | * @param rp
|
| | | * @param script
|
| | | * @param commands
|
| | | */
|
| | | @SuppressWarnings("unused")
|
| | | protected void runNativeScript(ReceivePack rp, String script,
|
| | | Collection<ReceiveCommand> commands) {
|
| | |
|
| | | Repository repository = rp.getRepository();
|
| | | File scriptFile = new File(repository.getDirectory(), script);
|
| | |
|
| | | int resultCode = 0;
|
| | | if (scriptFile.exists()) {
|
| | | try {
|
| | | logger.debug("executing " + scriptFile);
|
| | | Process process = Runtime.getRuntime().exec(scriptFile.getAbsolutePath(), null,
|
| | | repository.getDirectory());
|
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(
|
| | | process.getInputStream()));
|
| | | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
|
| | | process.getOutputStream()));
|
| | | for (ReceiveCommand command : commands) {
|
| | | switch (command.getType()) {
|
| | | case UPDATE:
|
| | | // updating a ref
|
| | | writer.append(MessageFormat.format("{0} {1} {2}\n", command.getOldId()
|
| | | .getName(), command.getNewId().getName(), command.getRefName()));
|
| | | break;
|
| | | case CREATE:
|
| | | // new ref
|
| | | // oldrev hard-coded to 40? weird.
|
| | | writer.append(MessageFormat.format("40 {0} {1}\n", command.getNewId()
|
| | | .getName(), command.getRefName()));
|
| | | break;
|
| | | }
|
| | | }
|
| | | resultCode = process.waitFor();
|
| | |
|
| | | // read and buffer stdin
|
| | | // this is supposed to be piped back to the git client.
|
| | | // not sure how to do that right now.
|
| | | StringBuilder sb = new StringBuilder();
|
| | | String line = null;
|
| | | while ((line = reader.readLine()) != null) {
|
| | | sb.append(line).append('\n');
|
| | | }
|
| | | logger.debug(sb.toString());
|
| | | } catch (Throwable e) {
|
| | | resultCode = -1;
|
| | | logger.error(
|
| | | MessageFormat.format("Failed to execute {0}",
|
| | | scriptFile.getAbsolutePath()), e);
|
| | | }
|
| | | }
|
| | |
|
| | | // reject push
|
| | | if (resultCode != 0) {
|
| | | for (ReceiveCommand command : commands) {
|
| | | command.setResult(Result.REJECTED_OTHER_REASON, MessageFormat.format(
|
| | | "Native script {0} rejected push or failed",
|
| | | scriptFile.getAbsolutePath()));
|
| | | }
|
| | | }
|
| | | }
|