Line data Source code
1 : // Copyright (C) 2021 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.sshd.commands; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; 19 : 20 : import com.google.gerrit.common.data.GlobalCapability; 21 : import com.google.gerrit.entities.Project; 22 : import com.google.gerrit.extensions.annotations.RequiresCapability; 23 : import com.google.gerrit.server.git.DelegateRepository; 24 : import com.google.gerrit.server.git.GitRepositoryManager; 25 : import com.google.gerrit.server.project.ProjectState; 26 : import com.google.gerrit.sshd.CommandMetaData; 27 : import com.google.gerrit.sshd.SshCommand; 28 : import com.google.inject.Inject; 29 : import java.io.IOException; 30 : import org.eclipse.jgit.errors.RepositoryNotFoundException; 31 : import org.eclipse.jgit.internal.storage.file.FileRepository; 32 : import org.eclipse.jgit.lib.Repository; 33 : import org.kohsuke.args4j.Option; 34 : import org.kohsuke.args4j.spi.ExplicitBooleanOptionHandler; 35 : 36 : @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) 37 : @CommandMetaData( 38 : name = "convert-ref-storage", 39 : description = "Convert ref storage to reftable (experimental)", 40 : runsAt = MASTER_OR_SLAVE) 41 1 : public class ConvertRefStorage extends SshCommand { 42 : @Inject private GitRepositoryManager repoManager; 43 : 44 1 : private enum StorageFormatOption { 45 1 : reftable, 46 1 : refdir, 47 : } 48 : 49 1 : @Option(name = "--format", usage = "storage format to convert to (reftable or refdir)") 50 : private StorageFormatOption storageFormat = StorageFormatOption.reftable; 51 : 52 1 : @Option( 53 : name = "--backup", 54 : aliases = {"-b"}, 55 : usage = "create backup of old ref storage format", 56 : handler = ExplicitBooleanOptionHandler.class) 57 : private boolean backup = true; 58 : 59 1 : @Option( 60 : name = "--reflogs", 61 : aliases = {"-r"}, 62 : usage = "write reflogs to reftable", 63 : handler = ExplicitBooleanOptionHandler.class) 64 : private boolean writeLogs = true; 65 : 66 : @Option( 67 : name = "--project", 68 : aliases = {"-p"}, 69 : metaVar = "PROJECT", 70 : required = true, 71 : usage = "project for which the storage format should be changed") 72 : private ProjectState projectState; 73 : 74 : @Override 75 : public void run() throws Exception { 76 0 : enableGracefulStop(); 77 0 : Project.NameKey projectName = projectState.getNameKey(); 78 0 : try (Repository repo = repoManager.openRepository(projectName)) { 79 0 : if (repo instanceof DelegateRepository) { 80 0 : ((DelegateRepository) repo).convertRefStorage(storageFormat.name(), writeLogs, backup); 81 : } else { 82 0 : checkState( 83 : repo instanceof FileRepository, "Repository is not an instance of FileRepository!"); 84 0 : ((FileRepository) repo).convertRefStorage(storageFormat.name(), writeLogs, backup); 85 : } 86 0 : } catch (RepositoryNotFoundException e) { 87 0 : throw die("'" + projectName + "': not a git archive", e); 88 0 : } catch (IOException e) { 89 0 : throw die("Error converting: '" + projectName + "': " + e.getMessage(), e); 90 0 : } 91 0 : } 92 : }