diff options
Diffstat (limited to 'org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java')
-rw-r--r-- | org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java | 58 |
1 files changed, 37 insertions, 21 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java index ca61b07104..a9d8719a59 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java @@ -29,15 +29,14 @@ import java.util.StringTokenizer; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.variables.IStringVariableManager; import org.eclipse.core.variables.VariablesPlugin; import org.eclipse.egit.core.internal.CoreText; +import org.eclipse.egit.core.internal.indexdiff.IndexDiffCache; import org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry; import org.eclipse.egit.core.internal.indexdiff.IndexDiffData; import org.eclipse.egit.core.project.RepositoryMapping; @@ -45,8 +44,10 @@ import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.api.GarbageCollectCommand; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.lib.CheckoutEntry; +import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; @@ -64,12 +65,18 @@ import org.eclipse.jgit.treewalk.WorkingTreeIterator; import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FileUtils; +import org.eclipse.jgit.util.SystemReader; import org.osgi.service.prefs.BackingStoreException; /** * Utility class for handling Repositories in the UI. */ -public class RepositoryUtil { +public enum RepositoryUtil { + + /** + * The singleton {@link RepositoryUtil} instance. + */ + INSTANCE; /** * The preferences to store the absolute paths of all repositories shown in @@ -94,22 +101,16 @@ public class RepositoryUtil { private final Map<String, String> repositoryNameCache = new HashMap<>(); private final IEclipsePreferences prefs = InstanceScope.INSTANCE - .getNode(Activator.getPluginId()); + .getNode(Activator.PLUGIN_ID); private final java.nio.file.Path workspacePath; - /** - * Clients should obtain an instance from {@link Activator} - */ - RepositoryUtil() { + private RepositoryUtil() { workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation() .toFile().toPath(); } - /** - * Used by {@link Activator} - */ - void dispose() { + void clear() { commitMappingCache.clear(); repositoryNameCache.clear(); } @@ -124,11 +125,11 @@ public class RepositoryUtil { String key = GitCorePreferences.core_defaultRepositoryDir; String dir = migrateRepoRootPreference(); IEclipsePreferences p = InstanceScope.INSTANCE - .getNode(Activator.getPluginId()); + .getNode(Activator.PLUGIN_ID); if (dir == null) { dir = Platform.getPreferencesService().getString( - Activator.getPluginId(), key, - getDefaultDefaultRepositoryDir(), null); + Activator.PLUGIN_ID, key, getDefaultDefaultRepositoryDir(), + null); } else { p.put(key, dir); } @@ -174,6 +175,24 @@ public class RepositoryUtil { } /** + * Get the configured default branch name configured with git option + * init.defaultBranch. Default is {@code master}. + * + * @return the configured default branch name configured with git option + * init.defaultBranch + * @throws ConfigInvalidException + * if global or system wide configuration is invalid + * @throws IOException + * if an IO error occurred when reading git configurations + */ + public static String getDefaultBranchName() + throws ConfigInvalidException, IOException { + return SystemReader.getInstance().getUserConfig().getString( + ConfigConstants.CONFIG_INIT_SECTION, null, + ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH); + } + + /** * Reads a reflog (in reverse), returning an empty list and logging * exceptions if the reflog cannot be parsed. * @@ -589,9 +608,7 @@ public class RepositoryUtil { try { prefs.flush(); } catch (BackingStoreException e) { - IStatus error = new Status(IStatus.ERROR, Activator.getPluginId(), - e.getMessage(), e); - Activator.getDefault().getLog().log(error); + Activator.logError(e.getMessage(), e); } } @@ -800,8 +817,7 @@ public class RepositoryUtil { * @since 4.1.0 */ public static boolean canBeAutoIgnored(IPath path) throws IOException { - Repository repository = Activator.getDefault().getRepositoryCache() - .getRepository(path); + Repository repository = RepositoryCache.INSTANCE.getRepository(path); if (repository == null || repository.isBare()) { return false; } @@ -865,7 +881,7 @@ public class RepositoryUtil { * otherwise */ public static boolean hasChanges(@NonNull Repository repository) { - IndexDiffCacheEntry entry = Activator.getDefault().getIndexDiffCache() + IndexDiffCacheEntry entry = IndexDiffCache.INSTANCE .getIndexDiffCacheEntry(repository); IndexDiffData data = entry != null ? entry.getIndexDiff() : null; return data != null && data.hasChanges(); |