-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDefaultAccess.java
More file actions
279 lines (253 loc) · 9.29 KB
/
DefaultAccess.java
File metadata and controls
279 lines (253 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gitiles;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Queues;
import java.io.File;
import java.io.IOException;
import java.text.Collator;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.http.server.ServletUtils;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.resolver.FileResolver;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import org.eclipse.jgit.util.IO;
/**
* Default implementation of {@link GitilesAccess} with local repositories.
*
* <p>Repositories are scanned on-demand under the given path, configured by default from {@code
* gitiles.basePath}. There is no access control beyond what user the JVM is running under.
*/
public class DefaultAccess implements GitilesAccess {
private static final String ANONYMOUS_USER_KEY = "anonymous user";
private static final String DEFAULT_DESCRIPTION =
"Unnamed repository; edit this file 'description' to name the repository.";
private static final Collator US_COLLATOR = Collator.getInstance(Locale.US);
public static class Factory implements GitilesAccess.Factory {
private final File basePath;
private final String canonicalBasePath;
private final String baseGitUrl;
private final Config baseConfig;
private final FileResolver<HttpServletRequest> resolver;
Factory(
File basePath,
String baseGitUrl,
Config baseConfig,
FileResolver<HttpServletRequest> resolver)
throws IOException {
this.basePath = checkNotNull(basePath, "basePath");
this.baseGitUrl = checkNotNull(baseGitUrl, "baseGitUrl");
this.baseConfig = checkNotNull(baseConfig, "baseConfig");
this.resolver = checkNotNull(resolver, "resolver");
this.canonicalBasePath = basePath.getCanonicalPath();
}
@Override
public GitilesAccess forRequest(HttpServletRequest req) {
return newAccess(basePath, canonicalBasePath, baseGitUrl, resolver, req);
}
protected DefaultAccess newAccess(
File basePath,
String canonicalBasePath,
String baseGitUrl,
FileResolver<HttpServletRequest> resolver,
HttpServletRequest req) {
return new DefaultAccess(basePath, canonicalBasePath, baseGitUrl, baseConfig, resolver, req);
}
}
protected final File basePath;
protected final String canonicalBasePath;
protected final String baseGitUrl;
protected final Config baseConfig;
protected final FileResolver<HttpServletRequest> resolver;
protected final HttpServletRequest req;
protected DefaultAccess(
File basePath,
String canonicalBasePath,
String baseGitUrl,
Config baseConfig,
FileResolver<HttpServletRequest> resolver,
HttpServletRequest req) {
this.basePath = checkNotNull(basePath, "basePath");
this.canonicalBasePath = checkNotNull(canonicalBasePath, "canonicalBasePath");
this.baseGitUrl = checkNotNull(baseGitUrl, "baseGitUrl");
this.baseConfig = checkNotNull(baseConfig, "baseConfig");
this.resolver = checkNotNull(resolver, "resolver");
this.req = checkNotNull(req, "req");
}
@Override
public Map<String, RepositoryDescription> listRepositories(String prefix, Set<String> branches)
throws IOException {
Map<String, RepositoryDescription> repos = Maps.newTreeMap(US_COLLATOR);
for (Repository repo : scanRepositories(basePath, prefix, req)) {
repos.put(getRepositoryName(repo), buildDescription(repo, branches));
repo.close();
}
return repos;
}
@Override
public Object getUserKey() {
// Always return the same anonymous user key (effectively running with the
// same user permissions as the JVM). Subclasses may override this behavior.
return ANONYMOUS_USER_KEY;
}
@Override
public String getRepositoryName() {
return getRepositoryName(ServletUtils.getRepository(req));
}
@Override
public RepositoryDescription getRepositoryDescription() throws IOException {
return buildDescription(ServletUtils.getRepository(req), Collections.<String>emptySet());
}
@Override
public Config getConfig() {
return baseConfig;
}
private String getRepositoryName(Repository repo) {
String path = getRelativePath(repo);
if (repo.isBare() && path.endsWith(".git")) {
path = path.substring(0, path.length() - 4);
}
return path;
}
private String getRelativePath(Repository repo) {
String path;
if (repo.isBare()) {
path = repo.getDirectory().getPath();
if (path.endsWith(".git")) {
path = path.substring(0, path.length() - 4);
}
} else {
path = repo.getDirectory().getParent();
}
return getRelativePath(path);
}
private String getRelativePath(String path) {
String base = basePath.getPath();
if (path.equals(base)) {
return "";
}
if (path.startsWith(base)) {
return path.substring(base.length() + 1);
}
if (path.startsWith(canonicalBasePath)) {
return path.substring(canonicalBasePath.length() + 1);
}
throw new IllegalStateException(
String.format("Repository path %s is outside base path %s", path, base));
}
private String loadDescriptionText(Repository repo) throws IOException {
String desc = null;
StoredConfig config = repo.getConfig();
IOException configError = null;
try {
config.load();
desc = config.getString("gitweb", null, "description");
} catch (ConfigInvalidException e) {
configError = new IOException(e);
}
if (desc == null) {
File descFile = new File(repo.getDirectory(), "description");
if (descFile.exists()) {
desc = new String(IO.readFully(descFile), UTF_8);
if (DEFAULT_DESCRIPTION.equals(CharMatcher.whitespace().trimFrom(desc))) {
desc = null;
}
} else if (configError != null) {
throw configError;
}
}
return desc;
}
private RepositoryDescription buildDescription(Repository repo, Set<String> branches)
throws IOException {
RepositoryDescription desc = new RepositoryDescription();
desc.name = getRepositoryName(repo);
desc.cloneUrl = baseGitUrl + getRelativePath(repo);
desc.description = loadDescriptionText(repo);
if (!branches.isEmpty()) {
desc.branches = Maps.newLinkedHashMap();
for (String name : branches) {
Ref ref = repo.exactRef(normalizeRefName(name));
if ((ref != null) && (ref.getObjectId() != null)) {
desc.branches.put(name, ref.getObjectId().name());
}
}
}
return desc;
}
private static String normalizeRefName(String name) {
if (name.startsWith("refs/")) {
return name;
}
return "refs/heads/" + name;
}
private Collection<Repository> scanRepositories(
File basePath, String prefix, HttpServletRequest req) throws IOException {
List<Repository> repos = Lists.newArrayList();
Queue<File> todo = initScan(basePath, prefix);
while (!todo.isEmpty()) {
File file = todo.remove();
try {
repos.add(resolver.open(req, getRelativePath(file.getPath())));
} catch (RepositoryNotFoundException e) {
File[] children = file.listFiles();
if (children != null) {
Collections.addAll(todo, children);
}
} catch (ServiceNotEnabledException e) {
throw new IOException(e);
}
}
return repos;
}
private Queue<File> initScan(File basePath, String prefix) throws IOException {
Queue<File> todo = Queues.newArrayDeque();
File[] entries;
if (isValidPrefix(prefix)) {
entries = new File(basePath, CharMatcher.is('/').trimFrom(prefix)).listFiles();
} else {
entries = basePath.listFiles();
}
if (entries != null) {
Collections.addAll(todo, entries);
} else if (!basePath.isDirectory()) {
throw new IOException("base path is not a directory: " + basePath.getPath());
}
return todo;
}
private static boolean isValidPrefix(String prefix) {
return !Strings.isNullOrEmpty(prefix)
&& !prefix.equals(".")
&& !prefix.equals("..")
&& !prefix.contains("../")
&& !prefix.endsWith("/..");
}
}