StagingView wrongly sorted by state initially
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / ConfigurationChecker.java
blobf856a150b6abc65702456531cbdf0ace5f3d94dc
1 /*******************************************************************************
2 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
3 * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
12 package org.eclipse.egit.ui.internal;
14 import java.io.File;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.egit.ui.Activator;
21 import org.eclipse.egit.ui.UIPreferences;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jgit.lib.ConfigConstants;
24 import org.eclipse.jgit.lib.StoredConfig;
25 import org.eclipse.jgit.util.FS;
26 import org.eclipse.jgit.util.LfsFactory;
27 import org.eclipse.jgit.util.LfsFactory.LfsInstallCommand;
28 import org.eclipse.jgit.util.SystemReader;
29 import org.eclipse.osgi.util.NLS;
30 import org.eclipse.ui.PlatformUI;
32 /**
33 * Checks the system configuration
36 public class ConfigurationChecker {
38 /**
39 * Checks the system configuration.
41 public static void checkConfiguration() {
42 // Schedule a job
43 // This avoids that the check is executed too early
44 // because in startup phase the JobManager is suspended
45 // and scheduled Jobs are executed later
46 Job job = new Job(UIText.ConfigurationChecker_checkConfiguration) {
47 @Override
48 protected IStatus run(IProgressMonitor monitor) {
49 if (PlatformUI.isWorkbenchRunning()) {
50 PlatformUI.getWorkbench().getDisplay()
51 .asyncExec(new Runnable() {
52 @Override
53 public void run() {
54 check();
56 });
57 } else {
58 schedule(1000L);
60 return Status.OK_STATUS;
63 job.schedule();
66 private static void check() {
67 checkHome();
68 checkLfs();
71 private static void checkLfs() {
72 IPreferenceStore store = Activator.getDefault().getPreferenceStore();
73 boolean auto = store.getBoolean(UIPreferences.LFS_AUTO_CONFIGURATION);
74 if (auto && !isLfsConfigured()) {
75 try {
76 LfsInstallCommand cmd = LfsFactory.getInstance()
77 .getInstallCommand();
78 if (cmd != null) {
79 cmd.call();
81 } catch (Exception e) {
82 Activator.handleIssue(IStatus.WARNING,
83 UIText.ConfigurationChecker_installLfsCannotInstall, e,
84 true);
89 private static boolean isLfsConfigured() {
90 try {
91 StoredConfig cfg = SystemReader.getInstance().openUserConfig(null,
92 FS.DETECTED);
93 cfg.load();
94 return cfg.getSubsections(ConfigConstants.CONFIG_FILTER_SECTION)
95 .contains("lfs"); //$NON-NLS-1$
96 } catch (Exception e) {
97 Activator.handleIssue(IStatus.WARNING,
98 UIText.ConfigurationChecker_installLfsCannotLoadConfig, e, false);
100 return false;
103 private static void checkHome() {
104 String home = System.getenv("HOME"); //$NON-NLS-1$
105 if (home != null)
106 return; // home is set => ok
107 home = calcHomeDir();
108 String message = NLS.bind(UIText.ConfigurationChecker_homeNotSet, home);
109 IPreferenceStore store = Activator.getDefault().getPreferenceStore();
110 boolean hidden = !store.getBoolean(UIPreferences.SHOW_HOME_DIR_WARNING);
111 if (!hidden)
112 Activator.handleIssue(IStatus.WARNING, message, null, false);
115 private static String calcHomeDir() {
116 if (runsOnWindows()) {
117 String homeDrive = System.getenv("HOMEDRIVE"); //$NON-NLS-1$
118 if (homeDrive != null) {
119 String homePath = SystemReader.getInstance().getenv("HOMEPATH"); //$NON-NLS-1$
120 return new File(homeDrive, homePath).getAbsolutePath();
122 return System.getenv("HOMESHARE"); //$NON-NLS-1$
123 } else {
124 // The user.home property is not compatible with Git for Windows
125 return System.getProperty("user.home"); //$NON-NLS-1$
129 private static boolean runsOnWindows() {
130 String os;
131 try {
132 os = System.getProperty("os.name"); //$NON-NLS-1$
133 } catch (RuntimeException e) {
134 return false;
136 return os.indexOf("Windows") != -1; //$NON-NLS-1$