commit | author | age
|
75bca8
|
1 |
/* |
JM |
2 |
* Copyright 2011 gitblit.com. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package com.gitblit.git; |
|
17 |
|
|
18 |
import javax.servlet.http.HttpServletRequest; |
|
19 |
|
|
20 |
import org.eclipse.jgit.lib.PersonIdent; |
|
21 |
import org.eclipse.jgit.lib.Repository; |
|
22 |
import org.eclipse.jgit.transport.ReceivePack; |
|
23 |
import org.eclipse.jgit.transport.resolver.ReceivePackFactory; |
|
24 |
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; |
|
25 |
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; |
|
26 |
import org.slf4j.Logger; |
|
27 |
import org.slf4j.LoggerFactory; |
|
28 |
|
|
29 |
import com.gitblit.GitBlit; |
|
30 |
import com.gitblit.models.RepositoryModel; |
|
31 |
import com.gitblit.models.UserModel; |
|
32 |
import com.gitblit.utils.HttpUtils; |
|
33 |
|
|
34 |
/** |
|
35 |
* The receive pack factory creates a receive pack which accepts pushes from |
|
36 |
* clients. |
|
37 |
* |
|
38 |
* @author James Moger |
|
39 |
* |
|
40 |
* @param <X> the connection type |
|
41 |
*/ |
|
42 |
public class GitblitReceivePackFactory<X> implements ReceivePackFactory<X> { |
|
43 |
|
|
44 |
protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class); |
|
45 |
|
|
46 |
@Override |
|
47 |
public ReceivePack create(X req, Repository db) |
|
48 |
throws ServiceNotEnabledException, ServiceNotAuthorizedException { |
|
49 |
|
|
50 |
final ReceivePack rp = new ReceivePack(db); |
|
51 |
UserModel user = UserModel.ANONYMOUS; |
40aa84
|
52 |
String repositoryName = ""; |
75bca8
|
53 |
String origin = ""; |
JM |
54 |
String gitblitUrl = ""; |
|
55 |
int timeout = 0; |
|
56 |
|
|
57 |
if (req instanceof HttpServletRequest) { |
|
58 |
// http/https request may or may not be authenticated |
|
59 |
HttpServletRequest request = (HttpServletRequest) req; |
40aa84
|
60 |
repositoryName = request.getAttribute("gitblitRepositoryName").toString(); |
75bca8
|
61 |
origin = request.getRemoteHost(); |
JM |
62 |
gitblitUrl = HttpUtils.getGitblitURL(request); |
|
63 |
|
|
64 |
// determine pushing user |
|
65 |
String username = request.getRemoteUser(); |
|
66 |
if (username != null && !"".equals(username)) { |
|
67 |
user = GitBlit.self().getUserModel(username); |
|
68 |
if (user == null) { |
|
69 |
// anonymous push, create a temporary usermodel |
|
70 |
user = new UserModel(username); |
|
71 |
} |
|
72 |
} |
40aa84
|
73 |
} else if (req instanceof GitDaemonClient) { |
75bca8
|
74 |
// git daemon request is alway anonymous |
40aa84
|
75 |
GitDaemonClient client = (GitDaemonClient) req; |
JM |
76 |
repositoryName = client.getRepositoryName(); |
75bca8
|
77 |
origin = client.getRemoteAddress().getHostAddress(); |
JM |
78 |
// set timeout from Git daemon |
|
79 |
timeout = client.getDaemon().getTimeout(); |
|
80 |
} |
|
81 |
|
|
82 |
// set pushing user identity for reflog |
|
83 |
rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin)); |
|
84 |
rp.setTimeout(timeout); |
|
85 |
|
|
86 |
// set advanced ref permissions |
|
87 |
RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName); |
|
88 |
rp.setAllowCreates(user.canCreateRef(repository)); |
|
89 |
rp.setAllowDeletes(user.canDeleteRef(repository)); |
|
90 |
rp.setAllowNonFastForwards(user.canRewindRef(repository)); |
|
91 |
|
|
92 |
// setup the receive hook |
|
93 |
ReceiveHook hook = new ReceiveHook(); |
|
94 |
hook.user = user; |
|
95 |
hook.repository = repository; |
|
96 |
hook.gitblitUrl = gitblitUrl; |
|
97 |
|
|
98 |
rp.setPreReceiveHook(hook); |
|
99 |
rp.setPostReceiveHook(hook); |
|
100 |
|
|
101 |
return rp; |
|
102 |
} |
|
103 |
} |