1 /*******************************************************************************
2 * Copyright (c) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * Thomas Wolf -- factored out of Activator
13 *******************************************************************************/
14 package org
.eclipse
.egit
.core
;
16 import org
.eclipse
.core
.resources
.IWorkspace
;
17 import org
.eclipse
.core
.runtime
.Platform
;
18 import org
.eclipse
.core
.runtime
.preferences
.IEclipsePreferences
;
19 import org
.eclipse
.core
.runtime
.preferences
.IEclipsePreferences
.IPreferenceChangeListener
;
20 import org
.eclipse
.core
.runtime
.preferences
.IPreferencesService
;
21 import org
.eclipse
.core
.runtime
.preferences
.InstanceScope
;
22 import org
.eclipse
.egit
.core
.internal
.CoreText
;
23 import org
.eclipse
.egit
.core
.internal
.hosts
.GitHosts
;
24 import org
.eclipse
.egit
.core
.internal
.indexdiff
.IndexDiffCache
;
25 import org
.eclipse
.jgit
.diff
.RawText
;
26 import org
.eclipse
.jgit
.storage
.file
.WindowCacheConfig
;
27 import org
.eclipse
.jgit
.util
.SystemReader
;
28 import org
.osgi
.framework
.FrameworkUtil
;
29 import org
.osgi
.framework
.ServiceRegistration
;
30 import org
.osgi
.service
.component
.annotations
.Activate
;
31 import org
.osgi
.service
.component
.annotations
.Component
;
32 import org
.osgi
.service
.component
.annotations
.Deactivate
;
33 import org
.osgi
.service
.component
.annotations
.Reference
;
36 * An OSGi component triggered when the workspace is ready that sets up the
37 * central EGit caches.
40 public class RepositoryInitializer
{
42 private IPreferencesService preferencesService
;
44 private ServiceRegistration
<RepositoryCache
> registration
;
46 private IEclipsePreferences gitCorePreferences
;
48 private IPreferenceChangeListener prefsListener
;
51 void setPreferencesService(IPreferencesService service
) {
52 this.preferencesService
= service
;
56 void setWorkspace(@SuppressWarnings("unused") IWorkspace workspace
) {
57 // Needed indirectly by the preferences service
63 reconfigureWindowCache(preferencesService
);
64 } catch (RuntimeException
| ExceptionInInitializerError e
) {
65 Activator
.logError(CoreText
.Activator_ReconfigureWindowCacheError
,
68 gitCorePreferences
= InstanceScope
.INSTANCE
69 .getNode(Activator
.PLUGIN_ID
);
70 GitHosts
.loadFromPreferences(gitCorePreferences
);
71 updateTextBufferSize();
72 prefsListener
= event
-> {
73 if (GitCorePreferences
.core_gitServers
.equals(event
.getKey())) {
74 GitHosts
.loadFromPreferences(gitCorePreferences
);
76 if (GitCorePreferences
.core_textBufferSize
.equals(event
.getKey())) {
77 updateTextBufferSize();
80 gitCorePreferences
.addPreferenceChangeListener(prefsListener
);
81 registration
= FrameworkUtil
.getBundle(getClass()).getBundleContext()
82 .registerService(RepositoryCache
.class,
83 RepositoryCache
.INSTANCE
, null);
88 registration
.unregister();
89 if (gitCorePreferences
!= null) {
90 if (prefsListener
!= null) {
92 .removePreferenceChangeListener(prefsListener
);
95 gitCorePreferences
= null;
97 RepositoryUtil
.INSTANCE
.clear();
98 IndexDiffCache
.INSTANCE
.dispose();
99 RepositoryCache
.INSTANCE
.clear();
102 private void updateTextBufferSize() {
103 int bufferSize
= preferencesService
.getInt(Activator
.PLUGIN_ID
,
104 GitCorePreferences
.core_textBufferSize
, -1, null);
105 if (bufferSize
>= 0) {
106 // Guard against broken preferences that give large values. This
107 // buffer size should not be arbitrarily large. JGit ensures a
109 RawText
.setBufferSize(Math
.min(bufferSize
, 128 * 1024));
114 * Update the settings for the global window cache of the workspace.
116 public static void reconfigureWindowCache() {
117 reconfigureWindowCache(Platform
.getPreferencesService());
120 private static void reconfigureWindowCache(IPreferencesService prefs
) {
121 WindowCacheConfig c
= new WindowCacheConfig();
122 c
.setPackedGitLimit(prefs
.getInt(Activator
.PLUGIN_ID
,
123 GitCorePreferences
.core_packedGitLimit
, 0, null));
124 c
.setPackedGitWindowSize(prefs
.getInt(Activator
.PLUGIN_ID
,
125 GitCorePreferences
.core_packedGitWindowSize
, 0, null));
126 if (SystemReader
.getInstance().isWindows()) {
127 c
.setPackedGitMMAP(false);
129 c
.setPackedGitMMAP(prefs
.getBoolean(Activator
.PLUGIN_ID
,
130 GitCorePreferences
.core_packedGitMMAP
, false, null));
132 c
.setDeltaBaseCacheLimit(prefs
.getInt(Activator
.PLUGIN_ID
,
133 GitCorePreferences
.core_deltaBaseCacheLimit
, 0, null));
134 c
.setStreamFileThreshold(prefs
.getInt(Activator
.PLUGIN_ID
,
135 GitCorePreferences
.core_streamFileThreshold
, 0, null));
136 c
.setExposeStatsViaJmx(false);