Line data Source code
1 : // Copyright (C) 2020 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; 16 : 17 : import static com.google.gerrit.pgm.init.api.InitUtil.die; 18 : 19 : import com.google.gerrit.pgm.init.api.ConsoleUI; 20 : import com.google.gerrit.pgm.init.api.InitStep; 21 : import com.google.gerrit.server.config.SitePaths; 22 : import com.google.inject.Inject; 23 : import com.google.inject.Singleton; 24 : import java.io.IOException; 25 : import org.eclipse.jgit.errors.ConfigInvalidException; 26 : import org.eclipse.jgit.lib.ConfigConstants; 27 : import org.eclipse.jgit.storage.file.FileBasedConfig; 28 : import org.eclipse.jgit.transport.TransferConfig; 29 : import org.eclipse.jgit.util.FS; 30 : 31 : /** Initialize the JGit configuration. */ 32 : @Singleton 33 : class InitJGitConfig implements InitStep { 34 : private final ConsoleUI ui; 35 : private final SitePaths sitePaths; 36 : 37 : @Inject 38 15 : InitJGitConfig(ConsoleUI ui, SitePaths sitePaths) { 39 15 : this.ui = ui; 40 15 : this.sitePaths = sitePaths; 41 15 : } 42 : 43 : @Override 44 : public void run() { 45 15 : ui.header("JGit Configuration"); 46 15 : FileBasedConfig jgitConfig = new FileBasedConfig(sitePaths.jgit_config.toFile(), FS.DETECTED); 47 : try { 48 15 : jgitConfig.load(); 49 15 : if (!jgitConfig 50 15 : .getNames(ConfigConstants.CONFIG_RECEIVE_SECTION) 51 15 : .contains(ConfigConstants.CONFIG_KEY_AUTOGC)) { 52 15 : jgitConfig.setBoolean( 53 : ConfigConstants.CONFIG_RECEIVE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOGC, false); 54 15 : jgitConfig.save(); 55 15 : ui.error( 56 : "Auto-configured \"receive.autogc = false\" to disable auto-gc after" 57 : + " git-receive-pack."); 58 1 : } else if (jgitConfig.getBoolean( 59 : ConfigConstants.CONFIG_RECEIVE_SECTION, ConfigConstants.CONFIG_KEY_AUTOGC, true)) { 60 0 : ui.error( 61 : "WARNING: JGit option \"receive.autogc = true\". This is not recommended in Gerrit.\n" 62 : + "git-receive-pack will run auto gc after receiving data from " 63 : + "git-push and updating refs.\n" 64 : + "Disable this behavior to avoid the additional load it creates: " 65 : + "gc should be configured in gc config section or run as a separate process."); 66 : } 67 : 68 15 : if (jgitConfig 69 15 : .getNames(ConfigConstants.CONFIG_PROTOCOL_SECTION) 70 15 : .contains(ConfigConstants.CONFIG_KEY_VERSION)) { 71 0 : String version = 72 0 : jgitConfig.getString( 73 : ConfigConstants.CONFIG_PROTOCOL_SECTION, null, ConfigConstants.CONFIG_KEY_VERSION); 74 0 : if (!TransferConfig.ProtocolVersion.V2.version().equals(version)) { 75 0 : ui.error( 76 : "HINT: JGit option \"%s.%s = %s\". It's recommended to activate git\n" 77 : + "wire protocol version 2 to improve git fetch performance.", 78 : ConfigConstants.CONFIG_PROTOCOL_SECTION, ConfigConstants.CONFIG_KEY_VERSION, version); 79 : } 80 : } 81 0 : } catch (IOException e) { 82 0 : throw die(String.format("Handling JGit configuration %s failed", sitePaths.jgit_config), e); 83 0 : } catch (ConfigInvalidException e) { 84 0 : throw die(String.format("Invalid JGit configuration %s", sitePaths.jgit_config), e); 85 15 : } 86 15 : } 87 : }