Line data Source code
1 : // Copyright (C) 2015 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.entities.Project; 20 : import com.google.gerrit.lifecycle.LifecycleModule; 21 : import com.google.gerrit.server.config.GerritServerConfig; 22 : import com.google.gerrit.server.config.RepositoryConfig; 23 : import com.google.gerrit.server.config.SitePaths; 24 : import com.google.inject.Inject; 25 : import com.google.inject.Singleton; 26 : import java.nio.file.Path; 27 : import org.eclipse.jgit.lib.Config; 28 : 29 : /** 30 : * RepositoryManager that looks up repos stored across directories. 31 : * 32 : * <p>Each repository has a path configured in Gerrit server config, repository.NAME.basePath, 33 : * indicating where the repo can be found 34 : */ 35 : @Singleton 36 : public class MultiBaseLocalDiskRepositoryManager extends LocalDiskRepositoryManager { 37 : 38 0 : public static class MultiBaseLocalDiskRepositoryManagerModule extends LifecycleModule { 39 : @Override 40 : protected void configure() { 41 0 : bind(GitRepositoryManager.class).to(MultiBaseLocalDiskRepositoryManager.class); 42 0 : listener().to(MultiBaseLocalDiskRepositoryManager.Lifecycle.class); 43 0 : } 44 : } 45 : 46 : private final RepositoryConfig config; 47 : 48 : @Inject 49 : MultiBaseLocalDiskRepositoryManager( 50 : SitePaths site, @GerritServerConfig Config cfg, RepositoryConfig config) { 51 1 : super(site, cfg); 52 1 : this.config = config; 53 : 54 1 : for (Path alternateBasePath : config.getAllBasePaths()) { 55 1 : checkState( 56 1 : alternateBasePath.isAbsolute(), 57 : "repository.<name>.basePath must be absolute: %s", 58 : alternateBasePath); 59 0 : } 60 1 : } 61 : 62 : @Override 63 : public Path getBasePath(Project.NameKey name) { 64 1 : Path alternateBasePath = config.getBasePath(name); 65 1 : return alternateBasePath != null ? alternateBasePath : super.getBasePath(name); 66 : } 67 : 68 : @Override 69 : protected void scanProjects(ProjectVisitor visitor) { 70 1 : super.scanProjects(visitor); 71 1 : for (Path path : config.getAllBasePaths()) { 72 1 : visitor.setStartFolder(path); 73 1 : super.scanProjects(visitor); 74 1 : } 75 1 : } 76 : }