diff options
author | Thomas Wolf | 2019-09-05 17:15:09 +0000 |
---|---|---|
committer | Michael Keppler | 2019-09-08 12:02:31 +0000 |
commit | 306bc78bfcc7d6e745647d09dc3e8b320ef84f36 (patch) | |
tree | fd3b902efdbdf6d6162acfd2256a90321f7f21da /org.eclipse.egit.mylyn.ui/src/org | |
parent | f71c7eee5428728c4dbbf747cf32d33bc732d10f (diff) | |
download | egit-306bc78bfcc7d6e745647d09dc3e8b320ef84f36.tar.gz egit-306bc78bfcc7d6e745647d09dc3e8b320ef84f36.tar.xz egit-306bc78bfcc7d6e745647d09dc3e8b320ef84f36.zip |
[mylyn] Simplify getting a TaskRepository for a git repo
First, if bugtracker.url is set in the config, use it unconditionally.
Second, simplify the alternate mechanism (find a task repository
matching the git upstream host) by not checking against the resolved
local host name. Calling InetAddress.getLocalHost() here is not such
a good idea since it may be extremely slow over a VPN or especially
if there is no Internet connection at all, and this gets called on the
UI thread.
Bug: 520386
Change-Id: Ia20c33e5e486581c437c5a5cb4aeb490e91dbb65
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.egit.mylyn.ui/src/org')
-rw-r--r-- | org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java | 82 |
1 files changed, 30 insertions, 52 deletions
diff --git a/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java b/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java index 312254ee42..5f1e55e9c2 100644 --- a/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java +++ b/org.eclipse.egit.mylyn.ui/src/org/eclipse/egit/internal/mylyn/ui/commit/TaskReferenceFactory.java @@ -14,10 +14,8 @@ package org.eclipse.egit.internal.mylyn.ui.commit; import java.io.IOException; -import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; -import java.net.UnknownHostException; import java.util.List; import org.eclipse.core.runtime.IAdapterFactory; @@ -50,6 +48,8 @@ public class TaskReferenceFactory implements IAdapterFactory { private static final String BUGTRACK_SECTION = "bugtracker"; //$NON-NLS-1$ private static final String BUGTRACK_URL = "url"; //$NON-NLS-1$ + private static final String LOCALHOST = "localhost"; //$NON-NLS-1$ + @Override public Class<?>[] getAdapterList() { final Class<?>[] c = new Class[ADAPTER_TYPES.length]; @@ -85,11 +85,7 @@ public class TaskReferenceFactory implements IAdapterFactory { */ private AbstractTaskReference adaptFromCommit(IRepositoryCommit commit) { Repository r = commit.getRepository(); - String repoUrl = getRepoUrl(r); - if (repoUrl == null) { - return null; - } - TaskRepository repository = getTaskRepositoryByGitRepoURL(repoUrl); + TaskRepository repository = getTaskRepository(r); if (repository == null) { return null; } @@ -143,16 +139,27 @@ public class TaskReferenceFactory implements IAdapterFactory { } /** - * Finds {@link TaskRepository} by provided Git repository Url - * @param repoUrl Git repository url - * @return {@link TaskRepository} associated with this Git repo or <code>null</code> if nothing found + * Finds a {@link TaskRepository} for the given {@link Repository}. + * + * @param repository + * git repository to find a task repository for + * @return {@link TaskRepository} associated with this git repository or + * {@code null} if none found */ - private TaskRepository getTaskRepositoryByGitRepoURL(final String repoUrl) { - if (repoUrl == null) + private TaskRepository getTaskRepository(Repository repository) { + Config config = repository.getConfig(); + String url = config.getString(BUGTRACK_SECTION, null, BUGTRACK_URL); + if (url != null) { + return TasksUiPlugin.getRepositoryManager().getRepository(url); + } + // Try to find any that uses the same host as the configured origin URL + url = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL); + if (url == null) { return null; - + } try { - return getTaskRepositoryByHost(new URIish(repoUrl).getHost()); + return getTaskRepositoryByHost(new URIish(url).getHost()); } catch (Exception ex) { EGitMylynUI.getDefault().getLog().log( new Status(IStatus.ERROR, EGitMylynUI.PLUGIN_ID, "failed to get repo url", ex)); //$NON-NLS-1$ @@ -160,17 +167,6 @@ public class TaskReferenceFactory implements IAdapterFactory { return null; } - private static String getRepoUrl(Repository repo) { - Config config = repo.getConfig(); - String configuredUrl = config.getString(BUGTRACK_SECTION, null, - BUGTRACK_URL); - if (configuredUrl != null) { - return configuredUrl; - } - return config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL); - } - private TaskRepository getTaskRepositoryByHost(String host) { List<TaskRepository> repositories = TasksUiPlugin.getRepositoryManager().getAllRepositories(); if (repositories == null || repositories.isEmpty()) @@ -194,35 +190,17 @@ public class TaskReferenceFactory implements IAdapterFactory { return null; } - private boolean isSameHosts(final String name1, final String name2) { - String hostname1 = name1 == null ? "" : name1.trim(); //$NON-NLS-1$ - String hostname2 = name2 == null ? "" : name2.trim(); //$NON-NLS-1$ - - if (hostname1.equals(hostname2)) - return true; - - String localHost = "localhost"; //$NON-NLS-1$ - String resolvedHostName; - try { - resolvedHostName = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException ex) { - resolvedHostName = localHost; + private boolean isSameHosts(String name1, String name2) { + String hostname1 = name1 == null ? LOCALHOST : name1.trim(); + String hostname2 = name2 == null ? LOCALHOST : name2.trim(); + if (hostname1.isEmpty()) { + hostname1 = LOCALHOST; + } + if (hostname2.isEmpty()) { + hostname2 = LOCALHOST; } - if (hostname1.length() == 0) - hostname1 = resolvedHostName; - - if (hostname2.length() == 0) - hostname2 = resolvedHostName; - - if (hostname1.equals(hostname2)) - return true; - - if ((hostname1.equals(localHost) && hostname2.equals(resolvedHostName)) - || (hostname1.equals(resolvedHostName) && hostname2.equals(localHost))) - return true; - - return false; + return hostname1.equals(hostname2); } } |