Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : package com.google.gerrit.server.git;
16 :
17 : import static com.google.common.base.Preconditions.checkState;
18 :
19 : import com.google.gerrit.common.UsedAt;
20 : import java.io.File;
21 : import java.io.IOException;
22 : import java.util.List;
23 : import java.util.Map;
24 : import java.util.Set;
25 : import org.eclipse.jgit.attributes.AttributesNodeProvider;
26 : import org.eclipse.jgit.dircache.DirCache;
27 : import org.eclipse.jgit.errors.AmbiguousObjectException;
28 : import org.eclipse.jgit.errors.CorruptObjectException;
29 : import org.eclipse.jgit.errors.IncorrectObjectTypeException;
30 : import org.eclipse.jgit.errors.MissingObjectException;
31 : import org.eclipse.jgit.errors.NoWorkTreeException;
32 : import org.eclipse.jgit.errors.RevisionSyntaxException;
33 : import org.eclipse.jgit.events.ListenerList;
34 : import org.eclipse.jgit.events.RepositoryEvent;
35 : import org.eclipse.jgit.internal.storage.file.FileRepository;
36 : import org.eclipse.jgit.lib.AnyObjectId;
37 : import org.eclipse.jgit.lib.BaseRepositoryBuilder;
38 : import org.eclipse.jgit.lib.ObjectDatabase;
39 : import org.eclipse.jgit.lib.ObjectId;
40 : import org.eclipse.jgit.lib.ObjectInserter;
41 : import org.eclipse.jgit.lib.ObjectLoader;
42 : import org.eclipse.jgit.lib.ObjectReader;
43 : import org.eclipse.jgit.lib.ProgressMonitor;
44 : import org.eclipse.jgit.lib.RebaseTodoLine;
45 : import org.eclipse.jgit.lib.Ref;
46 : import org.eclipse.jgit.lib.RefDatabase;
47 : import org.eclipse.jgit.lib.RefRename;
48 : import org.eclipse.jgit.lib.RefUpdate;
49 : import org.eclipse.jgit.lib.ReflogReader;
50 : import org.eclipse.jgit.lib.Repository;
51 : import org.eclipse.jgit.lib.RepositoryState;
52 : import org.eclipse.jgit.lib.StoredConfig;
53 : import org.eclipse.jgit.revwalk.RevCommit;
54 : import org.eclipse.jgit.util.FS;
55 :
56 : /** Wrapper around {@link Repository} that delegates all calls to the wrapped {@link Repository}. */
57 : @UsedAt(UsedAt.Project.PLUGIN_HIGH_AVAILABILITY)
58 : @UsedAt(UsedAt.Project.PLUGIN_MULTI_SITE)
59 : public class DelegateRepository extends Repository {
60 :
61 : protected final Repository delegate;
62 :
63 : protected DelegateRepository(Repository delegate) {
64 135 : super(toBuilder(delegate));
65 135 : this.delegate = delegate;
66 135 : }
67 :
68 : /** Returns the wrapped {@link Repository} instance. */
69 : public Repository delegate() {
70 1 : return delegate;
71 : }
72 :
73 : @Override
74 : public void create(boolean bare) throws IOException {
75 0 : delegate.create(bare);
76 0 : }
77 :
78 : @Override
79 : public String getIdentifier() {
80 4 : return delegate.getIdentifier();
81 : }
82 :
83 : @Override
84 : public ObjectDatabase getObjectDatabase() {
85 97 : return delegate.getObjectDatabase();
86 : }
87 :
88 : @Override
89 : public RefDatabase getRefDatabase() {
90 0 : return delegate.getRefDatabase();
91 : }
92 :
93 : @Override
94 : public StoredConfig getConfig() {
95 134 : return delegate.getConfig();
96 : }
97 :
98 : @Override
99 : public AttributesNodeProvider createAttributesNodeProvider() {
100 0 : return delegate.createAttributesNodeProvider();
101 : }
102 :
103 : @Override
104 : public void scanForRepoChanges() throws IOException {
105 0 : delegate.scanForRepoChanges();
106 0 : }
107 :
108 : @Override
109 : public void notifyIndexChanged(boolean internal) {
110 0 : delegate.notifyIndexChanged(internal);
111 0 : }
112 :
113 : @Override
114 : public ReflogReader getReflogReader(String refName) throws IOException {
115 0 : return delegate.getReflogReader(refName);
116 : }
117 :
118 : @SuppressWarnings("rawtypes")
119 : private static BaseRepositoryBuilder toBuilder(Repository repo) {
120 135 : if (!repo.isBare()) {
121 0 : throw new IllegalArgumentException(
122 0 : "non-bare repository is not supported: " + repo.getIdentifier());
123 : }
124 :
125 135 : return new BaseRepositoryBuilder<>().setFS(repo.getFS()).setGitDir(repo.getDirectory());
126 : }
127 :
128 : @Override
129 : public ListenerList getListenerList() {
130 0 : return delegate.getListenerList();
131 : }
132 :
133 : @Override
134 : public void fireEvent(RepositoryEvent<?> event) {
135 0 : delegate.fireEvent(event);
136 0 : }
137 :
138 : @Override
139 : public void create() throws IOException {
140 0 : delegate.create();
141 0 : }
142 :
143 : @Override
144 : public File getDirectory() {
145 0 : return delegate.getDirectory();
146 : }
147 :
148 : @Override
149 : public ObjectInserter newObjectInserter() {
150 97 : return delegate.newObjectInserter();
151 : }
152 :
153 : @Override
154 : public ObjectReader newObjectReader() {
155 134 : return delegate.newObjectReader();
156 : }
157 :
158 : @Override
159 : public FS getFS() {
160 0 : return delegate.getFS();
161 : }
162 :
163 : @Override
164 : @Deprecated
165 : public boolean hasObject(AnyObjectId objectId) {
166 0 : return delegate.hasObject(objectId);
167 : }
168 :
169 : @Override
170 : public ObjectLoader open(AnyObjectId objectId, int typeHint)
171 : throws MissingObjectException, IncorrectObjectTypeException, IOException {
172 0 : return delegate.open(objectId, typeHint);
173 : }
174 :
175 : @Override
176 : public void incrementOpen() {
177 0 : delegate.incrementOpen();
178 0 : }
179 :
180 : @Override
181 : public void close() {
182 1 : delegate.close();
183 1 : }
184 :
185 : @Override
186 : public String getFullBranch() throws IOException {
187 0 : return delegate.getFullBranch();
188 : }
189 :
190 : @Override
191 : public String getBranch() throws IOException {
192 0 : return delegate.getBranch();
193 : }
194 :
195 : @Override
196 : @Deprecated
197 : public Map<String, Ref> getAllRefs() {
198 0 : return delegate.getAllRefs();
199 : }
200 :
201 : @Override
202 : @Deprecated
203 : public Map<String, Ref> getTags() {
204 0 : return delegate.getTags();
205 : }
206 :
207 : @Override
208 : public DirCache lockDirCache() throws NoWorkTreeException, CorruptObjectException, IOException {
209 0 : return delegate.lockDirCache();
210 : }
211 :
212 : @Override
213 : public void autoGC(ProgressMonitor monitor) {
214 96 : delegate.autoGC(monitor);
215 96 : }
216 :
217 : @Override
218 : public Set<ObjectId> getAdditionalHaves() throws IOException {
219 0 : return delegate.getAdditionalHaves();
220 : }
221 :
222 : @Override
223 : public Map<AnyObjectId, Set<Ref>> getAllRefsByPeeledObjectId() throws IOException {
224 0 : return delegate.getAllRefsByPeeledObjectId();
225 : }
226 :
227 : @Override
228 : public File getIndexFile() throws NoWorkTreeException {
229 0 : return delegate.getIndexFile();
230 : }
231 :
232 : @Override
233 : public RepositoryState getRepositoryState() {
234 0 : return delegate.getRepositoryState();
235 : }
236 :
237 : @Override
238 : public boolean isBare() {
239 0 : return delegate.isBare();
240 : }
241 :
242 : @Override
243 : public File getWorkTree() throws NoWorkTreeException {
244 0 : return delegate.getWorkTree();
245 : }
246 :
247 : @Override
248 : public String getRemoteName(String refName) {
249 0 : return delegate.getRemoteName(refName);
250 : }
251 :
252 : @Override
253 : public String getGitwebDescription() throws IOException {
254 0 : return delegate.getGitwebDescription();
255 : }
256 :
257 : @Override
258 : public Set<String> getRemoteNames() {
259 0 : return delegate.getRemoteNames();
260 : }
261 :
262 : @Override
263 : public ObjectLoader open(AnyObjectId objectId) throws MissingObjectException, IOException {
264 0 : return delegate.open(objectId);
265 : }
266 :
267 : @Override
268 : public RefUpdate updateRef(String ref) throws IOException {
269 0 : return delegate.updateRef(ref);
270 : }
271 :
272 : @Override
273 : public RefUpdate updateRef(String ref, boolean detach) throws IOException {
274 0 : return delegate.updateRef(ref, detach);
275 : }
276 :
277 : @Override
278 : public RefRename renameRef(String fromRef, String toRef) throws IOException {
279 0 : return delegate.renameRef(fromRef, toRef);
280 : }
281 :
282 : @Override
283 : public ObjectId resolve(String revstr)
284 : throws AmbiguousObjectException, IncorrectObjectTypeException, RevisionSyntaxException,
285 : IOException {
286 0 : return delegate.resolve(revstr);
287 : }
288 :
289 : @Override
290 : public String simplify(String revstr) throws AmbiguousObjectException, IOException {
291 0 : return delegate.simplify(revstr);
292 : }
293 :
294 : @Override
295 : @Deprecated
296 : public Ref peel(Ref ref) {
297 0 : return delegate.peel(ref);
298 : }
299 :
300 : @Override
301 : public RevCommit parseCommit(AnyObjectId id)
302 : throws IncorrectObjectTypeException, IOException, MissingObjectException {
303 0 : return delegate.parseCommit(id);
304 : }
305 :
306 : @Override
307 : public DirCache readDirCache() throws NoWorkTreeException, CorruptObjectException, IOException {
308 0 : return delegate.readDirCache();
309 : }
310 :
311 : @Override
312 : public String shortenRemoteBranchName(String refName) {
313 0 : return delegate.shortenRemoteBranchName(refName);
314 : }
315 :
316 : @Override
317 : public void setGitwebDescription(String description) throws IOException {
318 0 : delegate.setGitwebDescription(description);
319 0 : }
320 :
321 : @Override
322 : public String readMergeCommitMsg() throws IOException, NoWorkTreeException {
323 0 : return delegate.readMergeCommitMsg();
324 : }
325 :
326 : @Override
327 : public void writeMergeCommitMsg(String msg) throws IOException {
328 0 : delegate.writeMergeCommitMsg(msg);
329 0 : }
330 :
331 : @Override
332 : public String readCommitEditMsg() throws IOException, NoWorkTreeException {
333 0 : return delegate.readCommitEditMsg();
334 : }
335 :
336 : @Override
337 : public void writeCommitEditMsg(String msg) throws IOException {
338 0 : delegate.writeCommitEditMsg(msg);
339 0 : }
340 :
341 : @Override
342 : public List<ObjectId> readMergeHeads() throws IOException, NoWorkTreeException {
343 0 : return delegate.readMergeHeads();
344 : }
345 :
346 : @Override
347 : public void writeMergeHeads(List<? extends ObjectId> heads) throws IOException {
348 0 : delegate.writeMergeHeads(heads);
349 0 : }
350 :
351 : @Override
352 : public ObjectId readCherryPickHead() throws IOException, NoWorkTreeException {
353 0 : return delegate.readCherryPickHead();
354 : }
355 :
356 : @Override
357 : public ObjectId readRevertHead() throws IOException, NoWorkTreeException {
358 0 : return delegate.readRevertHead();
359 : }
360 :
361 : @Override
362 : public void writeCherryPickHead(ObjectId head) throws IOException {
363 0 : delegate.writeCherryPickHead(head);
364 0 : }
365 :
366 : @Override
367 : public void writeRevertHead(ObjectId head) throws IOException {
368 0 : delegate.writeRevertHead(head);
369 0 : }
370 :
371 : @Override
372 : public void writeOrigHead(ObjectId head) throws IOException {
373 0 : delegate.writeOrigHead(head);
374 0 : }
375 :
376 : @Override
377 : public ObjectId readOrigHead() throws IOException, NoWorkTreeException {
378 0 : return delegate.readOrigHead();
379 : }
380 :
381 : @Override
382 : public String readSquashCommitMsg() throws IOException {
383 0 : return delegate.readSquashCommitMsg();
384 : }
385 :
386 : @Override
387 : public void writeSquashCommitMsg(String msg) throws IOException {
388 0 : delegate.writeSquashCommitMsg(msg);
389 0 : }
390 :
391 : @Override
392 : public List<RebaseTodoLine> readRebaseTodo(String path, boolean includeComments)
393 : throws IOException {
394 0 : return delegate.readRebaseTodo(path, includeComments);
395 : }
396 :
397 : @Override
398 : public void writeRebaseTodoFile(String path, List<RebaseTodoLine> steps, boolean append)
399 : throws IOException {
400 0 : delegate.writeRebaseTodoFile(path, steps, append);
401 0 : }
402 :
403 : /**
404 : * Converts between ref storage formats.
405 : *
406 : * @param format the format to convert to, either "reftable" or "refdir"
407 : * @param writeLogs whether to write reflogs
408 : * @param backup whether to make a backup of the old data
409 : * @throws IOException on I/O problems.
410 : */
411 : public void convertRefStorage(String format, boolean writeLogs, boolean backup)
412 : throws IOException {
413 0 : checkState(
414 : delegate instanceof FileRepository, "Repository is not an instance of FileRepository!");
415 0 : ((FileRepository) delegate).convertRefStorage(format, writeLogs, backup);
416 0 : }
417 : }
|