1   /*
2    * Copyright 2023 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server.internal;
17  
18  import static java.util.Objects.requireNonNull;
19  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_COMMIT_SECTION;
20  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
21  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFF_SECTION;
22  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_GC_SECTION;
23  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_ALGORITHM;
24  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_AUTO;
25  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_AUTOGC;
26  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_AUTOPACKLIMIT;
27  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_FILEMODE;
28  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_GPGSIGN;
29  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_HIDEDOTFILES;
30  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES;
31  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PRECOMPOSEUNICODE;
32  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_RENAMES;
33  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION;
34  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_SYMLINKS;
35  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_RECEIVE_SECTION;
36  
37  import java.io.IOException;
38  import java.util.Objects;
39  
40  import org.eclipse.jgit.lib.Config;
41  import org.eclipse.jgit.lib.StoredConfig;
42  
43  public final class JGitUtil {
44  
45      public static final int REPO_FORMAT_VERSION = 1;
46  
47      private static final String REPO_FORMAT_VERSION_STR = String.valueOf(REPO_FORMAT_VERSION);
48  
49      public static void applyDefaultsAndSave(StoredConfig config) throws IOException {
50          if (applyDefaults(config)) {
51              config.save();
52          }
53      }
54  
55      public static boolean applyDefaults(Config config) {
56          boolean updated = false;
57          // Update the repository settings to upgrade to format version 1 and reftree.
58          updated |= set(config, CONFIG_CORE_SECTION, CONFIG_KEY_REPO_FORMAT_VERSION, REPO_FORMAT_VERSION_STR);
59  
60          // Disable hidden files, symlinks and file modes we do not use.
61          updated |= set(config, CONFIG_CORE_SECTION, CONFIG_KEY_HIDEDOTFILES, "false");
62          updated |= set(config, CONFIG_CORE_SECTION, CONFIG_KEY_SYMLINKS, "false");
63          updated |= set(config, CONFIG_CORE_SECTION, CONFIG_KEY_FILEMODE, "false");
64  
65          // Don't log ref updates.
66          updated |= set(config, CONFIG_CORE_SECTION, CONFIG_KEY_LOGALLREFUPDATES, "false");
67  
68          // Do not decompose file names in macOS.
69          updated |= set(config, CONFIG_CORE_SECTION, CONFIG_KEY_PRECOMPOSEUNICODE, "true");
70  
71          // Disable GPG signing.
72          updated |= set(config, CONFIG_COMMIT_SECTION, CONFIG_KEY_GPGSIGN, "false");
73  
74          // Set the diff algorithm.
75          updated |= set(config, CONFIG_DIFF_SECTION, CONFIG_KEY_ALGORITHM, "histogram");
76  
77          // Disable rename detection which we do not use.
78          updated |= set(config, CONFIG_DIFF_SECTION, CONFIG_KEY_RENAMES, "false");
79  
80          // Disable auto-GC by default because of the following reasons:
81          // - GC can take long time affecting performance.
82          // - jGit's GC task has memory leak which adds a JVM shutdown hook for each GC task.
83          //   See https://github.com/eclipse-mirrors/org.eclipse.jgit/blob/2e3f12a0fc63deba80e725bfa985c0c5bd31de99/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java#L1769
84          updated |= set(config, CONFIG_GC_SECTION, CONFIG_KEY_AUTO, "0");
85          updated |= set(config, CONFIG_GC_SECTION, CONFIG_KEY_AUTOPACKLIMIT, "0");
86          updated |= set(config, CONFIG_RECEIVE_SECTION, CONFIG_KEY_AUTOGC, "false");
87  
88          return updated;
89      }
90  
91      private static boolean set(Config config, String section, String name, String value) {
92          requireNonNull(section, "section");
93          requireNonNull(name, "name");
94          requireNonNull(value, "value");
95  
96          final String oldValue = config.getString(section, null, name);
97          if (Objects.equals(oldValue, value)) {
98              return false;
99          }
100 
101         config.setString(section, null, name, value);
102         return true;
103     }
104 
105     private JGitUtil() {}
106 }