| | |
| | | |
| | | import org.eclipse.jgit.lib.PersonIdent; |
| | | import org.eclipse.jgit.lib.Repository; |
| | | import org.eclipse.jgit.transport.DaemonClient; |
| | | import org.eclipse.jgit.transport.ReceivePack; |
| | | import org.eclipse.jgit.transport.resolver.ReceivePackFactory; |
| | | import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; |
| | |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import com.gitblit.GitBlit; |
| | | import com.gitblit.IStoredSettings; |
| | | import com.gitblit.Keys; |
| | | import com.gitblit.models.RepositoryModel; |
| | | import com.gitblit.models.UserModel; |
| | | import com.gitblit.utils.HttpUtils; |
| | | import com.gitblit.utils.StringUtils; |
| | | |
| | | /** |
| | | * The receive pack factory creates a receive pack which accepts pushes from |
| | | * clients. |
| | | * |
| | | * The receive pack factory creates the receive pack which processes pushes. |
| | | * |
| | | * @author James Moger |
| | | * |
| | | * @param <X> the connection type |
| | |
| | | public class GitblitReceivePackFactory<X> implements ReceivePackFactory<X> { |
| | | |
| | | protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class); |
| | | |
| | | |
| | | private final IStoredSettings settings; |
| | | |
| | | private final GitBlit gitblit; |
| | | |
| | | public GitblitReceivePackFactory(GitBlit gitblit) { |
| | | super(); |
| | | this.settings = gitblit.getSettings(); |
| | | this.gitblit = gitblit; |
| | | } |
| | | |
| | | @Override |
| | | public ReceivePack create(X req, Repository db) |
| | | throws ServiceNotEnabledException, ServiceNotAuthorizedException { |
| | | |
| | | final ReceivePack rp = new ReceivePack(db); |
| | | UserModel user = UserModel.ANONYMOUS; |
| | | String repositoryName = ""; |
| | | String origin = ""; |
| | | String gitblitUrl = ""; |
| | | String repositoryUrl = ""; |
| | | int timeout = 0; |
| | | |
| | | // XXX extract the repository name from the config |
| | | // the name is injected by GitRepositoryResolver |
| | | String repositoryName = db.getConfig().getString("gitblit", null, "repositoryName"); |
| | | |
| | | |
| | | |
| | | if (req instanceof HttpServletRequest) { |
| | | // http/https request may or may not be authenticated |
| | | // http/https request may or may not be authenticated |
| | | HttpServletRequest request = (HttpServletRequest) req; |
| | | repositoryName = request.getAttribute("gitblitRepositoryName").toString(); |
| | | origin = request.getRemoteHost(); |
| | | gitblitUrl = HttpUtils.getGitblitURL(request); |
| | | repositoryUrl = request.getRequestURI(); |
| | | |
| | | // determine pushing user |
| | | String username = request.getRemoteUser(); |
| | | if (username != null && !"".equals(username)) { |
| | | user = GitBlit.self().getUserModel(username); |
| | | if (user == null) { |
| | | // anonymous push, create a temporary usermodel |
| | | user = new UserModel(username); |
| | | if (!StringUtils.isEmpty(username)) { |
| | | UserModel u = gitblit.getUserModel(username); |
| | | if (u != null) { |
| | | user = u; |
| | | } |
| | | } |
| | | } else if (req instanceof DaemonClient) { |
| | | // git daemon request is alway anonymous |
| | | DaemonClient client = (DaemonClient) req; |
| | | } else if (req instanceof GitDaemonClient) { |
| | | // git daemon request is always anonymous |
| | | GitDaemonClient client = (GitDaemonClient) req; |
| | | repositoryName = client.getRepositoryName(); |
| | | origin = client.getRemoteAddress().getHostAddress(); |
| | | |
| | | // set timeout from Git daemon |
| | | timeout = client.getDaemon().getTimeout(); |
| | | } |
| | | |
| | | // set pushing user identity for reflog |
| | | boolean allowAnonymousPushes = settings.getBoolean(Keys.git.allowAnonymousPushes, false); |
| | | if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) { |
| | | // prohibit anonymous pushes |
| | | throw new ServiceNotEnabledException(); |
| | | } |
| | | |
| | | final RepositoryModel repository = gitblit.getRepositoryModel(repositoryName); |
| | | |
| | | final GitblitReceivePack rp = new GitblitReceivePack(gitblit, db, repository, user); |
| | | rp.setGitblitUrl(gitblitUrl); |
| | | rp.setRepositoryUrl(repositoryUrl); |
| | | rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin)); |
| | | rp.setTimeout(timeout); |
| | | |
| | | // set advanced ref permissions |
| | | RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName); |
| | | rp.setAllowCreates(user.canCreateRef(repository)); |
| | | rp.setAllowDeletes(user.canDeleteRef(repository)); |
| | | rp.setAllowNonFastForwards(user.canRewindRef(repository)); |
| | | |
| | | // setup the receive hook |
| | | ReceiveHook hook = new ReceiveHook(); |
| | | hook.user = user; |
| | | hook.repository = repository; |
| | | hook.gitblitUrl = gitblitUrl; |
| | | |
| | | rp.setPreReceiveHook(hook); |
| | | rp.setPostReceiveHook(hook); |
| | | |
| | | return rp; |
| | | } |
| | | } |
| | | } |