Line data Source code
1 : // Copyright (C) 2017 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.pgm.init.api; 16 : 17 : import com.google.gerrit.entities.Project; 18 : import com.google.gerrit.entities.Project.NameKey; 19 : import com.google.gerrit.server.config.SitePaths; 20 : import com.google.gerrit.server.git.GitRepositoryManager; 21 : import com.google.inject.Inject; 22 : import com.google.inject.Singleton; 23 : import java.io.File; 24 : import java.io.IOException; 25 : import java.nio.file.Path; 26 : import java.util.NavigableSet; 27 : import org.eclipse.jgit.errors.RepositoryNotFoundException; 28 : import org.eclipse.jgit.internal.storage.file.FileRepository; 29 : import org.eclipse.jgit.lib.Repository; 30 : import org.eclipse.jgit.lib.RepositoryCache.FileKey; 31 : import org.eclipse.jgit.util.FS; 32 : 33 : @Singleton 34 : public class GitRepositoryManagerOnInit implements GitRepositoryManager { 35 : private final InitFlags flags; 36 : private final SitePaths site; 37 : 38 : @Inject 39 15 : GitRepositoryManagerOnInit(InitFlags flags, SitePaths site) { 40 15 : this.flags = flags; 41 15 : this.site = site; 42 15 : } 43 : 44 : @Override 45 : public Status getRepositoryStatus(NameKey name) { 46 : try { 47 0 : openRepository(name); 48 0 : } catch (RepositoryNotFoundException e) { 49 0 : return Status.NON_EXISTENT; 50 0 : } catch (IOException e) { 51 0 : return Status.UNAVAILABLE; 52 0 : } 53 0 : return Status.ACTIVE; 54 : } 55 : 56 : @Override 57 : public Repository openRepository(Project.NameKey name) 58 : throws RepositoryNotFoundException, IOException { 59 0 : return new FileRepository(getPath(name)); 60 : } 61 : 62 : @Override 63 : public Repository createRepository(Project.NameKey name) { 64 0 : throw new UnsupportedOperationException("not implemented"); 65 : } 66 : 67 : @Override 68 : public NavigableSet<Project.NameKey> list() { 69 0 : throw new UnsupportedOperationException("not implemented"); 70 : } 71 : 72 : private File getPath(Project.NameKey name) { 73 0 : Path basePath = site.resolve(flags.cfg.getString("gerrit", null, "basePath")); 74 0 : if (basePath == null) { 75 0 : throw new IllegalStateException("gerrit.basePath must be configured"); 76 : } 77 0 : return FileKey.resolve(basePath.resolve(name.get()).toFile(), FS.DETECTED); 78 : } 79 : }